public void SaveAndLoadNetState()
        {
            const int inputsCount = 3;
            var net = new SimpleFuzzyNet(inputsCount, new[] {2},
                                         levelsCount: 3,
                                         littleFuzzyNumberGenerator:
                                             () => DiscreteFuzzyNumber.GenerateLittleNumber(levelsCount: 3));

            var first = new DiscreteFuzzyNumber(new TriangularFuzzyFunction(-1, 0, 1), 3);
            var second = new DiscreteFuzzyNumber(new TriangularFuzzyFunction(-1, -0.5, 0), 3);
            var third = new DiscreteFuzzyNumber(new TriangularFuzzyFunction(0, 0.5, 1), 3);

            var inputs = new List<IFuzzyNumber>
                {
                    first,
                    second,
                    third,
                };

            var expectedOutput = net.Propagate(inputs).First();

            BinaryFileSerializer.SaveNetState(_filename, net);
            var loadedNet = BinaryFileSerializer.LoadNetState(_filename);

            var actualOutput = loadedNet.PropagateLastInput().First();

            expectedOutput.ForeachLevel((alpha, level) => Assert.That(level, Is.EqualTo(actualOutput.GetAlphaLevel(alpha))));
        }
Exemple #2
0
        public void PropagateSignal()
        {
            var levelsRight = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(1.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var weight = new DiscreteFuzzyNumber(levelsRight);
            var input = new Link(weight);
            var output = new Link();
            var f = new Func<double, double>(x => 1.0 / (1.0 + Math.Pow(Math.E, -x)));

            var neuron = new FuzzyNeuron(f, () => DiscreteFuzzyNumber.GenerateLittleNumber(levelsCount: 3));
            neuron.AddInput(input, weight);
            neuron.SetOutput(output);

            neuron.Propagate();

            var expectedLevel00 = new IntervalD(f(1), f(9));
            var expectedLevel05 = new IntervalD(f(2.25), f(6.25));
            var expectedLevel10 = new IntervalD(f(4.0), f(4.0));

            Assert.NotNull(output.Signal);
            Assert.That(output.Signal.GetAlphaLevel(0.0), Is.EqualTo(expectedLevel00));
            Assert.That(output.Signal.GetAlphaLevel(0.5), Is.EqualTo(expectedLevel05));
            Assert.That(output.Signal.GetAlphaLevel(1.0), Is.EqualTo(expectedLevel10));
        }
Exemple #3
0
        public static IFuzzyNumber GenerateLittleNumber(double min = -0.5, double max = 0.5, int levelsCount = StandardAlphaLevelsCount)
        {
            var f = PrepareLittleFuzzyFunction(min, max);

            var result = new DiscreteFuzzyNumber();
            double d = 1.0 / (levelsCount - 1);
            for (int i = 0; i < levelsCount - 1; i++)
            {
                result._alphaLevels.Add(d * i, f.GetAlphaLevel(d * i));
            }
            result._alphaLevels.Add(1.0, f.GetAlphaLevel(1.0));

            return result;
        }
        public void AfterSumLevelsCountIsSameAsInFirstNumber()
        {
            var levelsLeft = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(0.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var x = new DiscreteFuzzyNumber(levelsLeft);
            var levelsRight = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(0.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var y = new DiscreteFuzzyNumber(levelsRight);

            var sum = x.Sum(y);

            Assert.That(sum.LevelsCount, Is.EqualTo(3));
        }
        public void AfterProductionLevelsCountIsSameAsInFirstFactor()
        {
            var levelsLeft = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(0.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var x = new DiscreteFuzzyNumber(levelsLeft);
            var levelsRight = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(0.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var y = new DiscreteFuzzyNumber(levelsRight);

            var product = x.Mul(y);

            Assert.That(product.LevelsCount, Is.EqualTo(3));
        }
        public void CopyNumbers()
        {
            var levels = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(1.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var x = new DiscreteFuzzyNumber(levels);
            var newLevels = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(-2.0, 2.0)},
                    {0.25, new IntervalD(-1.5, 1.5)},
                    {0.5, new IntervalD(-1.0, 1.0)},
                    {1.0, new IntervalD(0.0, 0.0)},
                };
            var y = new DiscreteFuzzyNumber(newLevels);

            x.Set(y);

            Assert.That(x.LevelsCount, Is.EqualTo(4));
            Assert.That(x.GetAlphaLevel(0.0), Is.EqualTo(new IntervalD(-2.0, 2.0)));
            Assert.That(x.GetAlphaLevel(0.25), Is.EqualTo(new IntervalD(-1.5, 1.5)));
            Assert.That(x.GetAlphaLevel(0.5), Is.EqualTo(new IntervalD(-1.0, 1.0)));
            Assert.That(x.GetAlphaLevel(1.0), Is.EqualTo(new IntervalD(0.0, 0.0)));
        }
        public void BeEmptyAfterConstruction()
        {
            var number = new DiscreteFuzzyNumber();
            var result = number.LevelsCount;

            Assert.That(result, Is.EqualTo(0));
        }
        private static void CorretlyDoOperationWithFlatNumbers(Func<double,double, double> flatOp, Func<IFuzzyNumber, double, IFuzzyNumber> fuzzyOp, double value)
        {
            var levels = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(1.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var x = new DiscreteFuzzyNumber(levels);

            var result = fuzzyOp(x, value);

            Assert.That(result.LevelsCount, Is.EqualTo(3));
            Assert.That(result.GetAlphaLevel(0.0), Is.EqualTo(new IntervalD(flatOp(1.0, value), flatOp(3.0, value))));
            Assert.That(result.GetAlphaLevel(0.5), Is.EqualTo(new IntervalD(flatOp(1.5, value), flatOp(2.5, value))));
            Assert.That(result.GetAlphaLevel(1.0), Is.EqualTo(new IntervalD(flatOp(2.0, value), flatOp(2.0, value))));
        }
        public void ThrowExceptionOnDivisionByZeroFlatNumber()
        {
            const double zero = 0.0;
            var levels = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(1.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var x = new DiscreteFuzzyNumber(levels);

            Assert.Throws<DivideByZeroException>(() => { var result = x.Div(zero); });
        }
        public void ReturnActualRightValue()
        {
            var levels = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                };
            var number = new DiscreteFuzzyNumber(levels);
            var result = number.GetRight();

            Assert.That(result, Is.EqualTo(new PointD(3.0, 0.0)));
        }
        public void ReturnActualModValue()
        {
            var levels = new Dictionary<double, IntervalD>
                {
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var number = new DiscreteFuzzyNumber(levels);
            var result = number.GetMod();

            Assert.That(result, Is.EqualTo(new PointD(2.0, 1.0)));
        }
        public void ReturnActualAlphaLevels()
        {
            var levels = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(0.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var number = new DiscreteFuzzyNumber(levels);
            var level0_0 = number.GetAlphaLevel(0.0);
            var level0_5 = number.GetAlphaLevel(0.5);
            var level1_0 = number.GetAlphaLevel(1.0);

            Assert.That(level0_0, Is.EqualTo(new IntervalD(1.0, 3.0)));
            Assert.That(level0_5, Is.EqualTo(new IntervalD(0.5, 2.5)));
            Assert.That(level1_0, Is.EqualTo(new IntervalD(2.0, 2.0)));
        }
        public void CorrectlySumNumbers()
        {
            var levelsLeft = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(0.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var x = new DiscreteFuzzyNumber(levelsLeft);
            var levelsRight = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(0.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var y = new DiscreteFuzzyNumber(levelsRight);

            var sum = x.Sum(y);

            Assert.That(sum.GetAlphaLevel(0.0), Is.EqualTo(new IntervalD(1.0 + 1.0, 3.0 + 3.0)));
            Assert.That(sum.GetAlphaLevel(0.5), Is.EqualTo(new IntervalD(0.5 + 0.5, 2.5 + 2.5)));
            Assert.That(sum.GetAlphaLevel(1.0), Is.EqualTo(new IntervalD(2.0 + 2.0, 2.0 + 2.0)));
        }
        public void CorrectlyMultiplyZeroByZero()
        {
            var levelsLeft = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(-2.0, 2.0)},
                    {0.5, new IntervalD(-1.0, 1.0)},
                    {1.0, new IntervalD(0.0, 0.0)},
                };
            var x = new DiscreteFuzzyNumber(levelsLeft);
            var levelsRight = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(-2.0, 2.0)},
                    {0.5, new IntervalD(-1.0, 1.0)},
                    {1.0, new IntervalD(0.0, 0.0)},
                };
            var y = new DiscreteFuzzyNumber(levelsRight);

            var product = x.Mul(y);

            Assert.That(product.GetAlphaLevel(0.0), Is.EqualTo(new IntervalD(-4.0, 4.0)));
            Assert.That(product.GetAlphaLevel(0.5), Is.EqualTo(new IntervalD(-1.0, 1.0)));
            Assert.That(product.GetAlphaLevel(1.0), Is.EqualTo(new IntervalD(0.0, 0.0)));
        }
        public void CorrectlyMultiplyPositiveNumbers()
        {
            var levelsLeft = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(1.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var x = new DiscreteFuzzyNumber(levelsLeft);
            var levelsRight = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {0.5, new IntervalD(1.5, 2.5)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var y = new DiscreteFuzzyNumber(levelsRight);

            var product = x.Mul(y);

            Assert.That(product.GetAlphaLevel(0.0), Is.EqualTo(new IntervalD(1.0, 9.0)));
            Assert.That(product.GetAlphaLevel(0.5), Is.EqualTo(new IntervalD(2.25, 6.25)));
            Assert.That(product.GetAlphaLevel(1.0), Is.EqualTo(new IntervalD(4.0, 4.0)));
        }
        public void CorrectlyMultiplyPositiveByZero2()
        {
            var x = new DiscreteFuzzyNumber(new TriangularFuzzyFunction(-3.0,-1.0,2.0), 3);
            var y = new DiscreteFuzzyNumber(new TriangularFuzzyFunction(-2.0, 1.0, 4.0), 3);

            var product = x.Mul(y);

            Assert.That(product.GetAlphaLevel(0.0), Is.EqualTo(new IntervalD(-12.0, 8.0)));
            Assert.That(product.GetAlphaLevel(0.5), Is.EqualTo(new IntervalD(-5.0, 1.25)));
            Assert.That(product.GetAlphaLevel(1.0), Is.EqualTo(new IntervalD(-1.0, -1.0)));
        }
        public void ReturnTwoOnLevelsCount()
        {
            var levels = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(1.0, 3.0)},
                    {1.0, new IntervalD(2.0, 2.0)},
                };
            var number = new DiscreteFuzzyNumber(levels);
            var result = number.LevelsCount;

            Assert.That(result, Is.EqualTo(2));
        }
        public void CorrectlyApplyActivationFunction()
        {
            var f = new Func<double, double>(value => 1.0/(1.0 + Math.Pow(Math.E, -value)));
            var levelsLeft = new Dictionary<double, IntervalD>
                {
                    {0.0, new IntervalD(-2.0, 2.0)},
                    {0.5, new IntervalD(-1.0, 1.0)},
                    {1.0, new IntervalD(0.0, 0.0)},
                };
            var x = new DiscreteFuzzyNumber(levelsLeft);

            var activation = x.Apply(f);

            var expectedValueOnLevel00 = new IntervalD(f(-2.0), f(2.0));
            var expectedValueOnLevel05 = new IntervalD(f(-1.0), f(1.0));
            var expectedValueOnLevel10 = new IntervalD(f(0.0), f(0.0));

            Assert.That(activation.GetAlphaLevel(0.0), Is.EqualTo(expectedValueOnLevel00));
            Assert.That(activation.GetAlphaLevel(0.5), Is.EqualTo(expectedValueOnLevel05));
            Assert.That(activation.GetAlphaLevel(1.0), Is.EqualTo(expectedValueOnLevel10));
        }