Exemplo n.º 1
0
        public void CorrectlyMultiplyByAnotherVector()
        {
            var valuesX = new[] { new RealNumber(1.0), new RealNumber(2.0), new RealNumber(-1.0) };
            IVector vectorX = new Vector(valuesX);
            var valuesY = new[] { new RealNumber(4.0), new RealNumber(-2.0), new RealNumber(0.0) };
            IVector vectorY = new Vector(valuesY);

            var result = vectorX.Mul(vectorY);

            Assert.That(result.GetMod().X, Is.EqualTo(new RealNumber(0.0).GetMod().X));
        }
Exemplo n.º 2
0
        public void CorrectlyAccessElements()
        {
            var values = new[] {new RealNumber(1.0), new RealNumber(2.0), new RealNumber(-1.0)};
            var vector = new Vector(values);

            Assert.That(vector.Length, Is.EqualTo(3));
            for (int i = 0; i < vector.Length; i++)
            {
                Assert.That(vector[i].GetMod().X, Is.EqualTo(values[i].GetMod().X));
            }
        }
Exemplo n.º 3
0
        public void CorrectlyMultiplyByFuzzyNumber()
        {
            var values = new[] { new RealNumber(1.0), new RealNumber(2.0), new RealNumber(-1.0) };
            IVector vector = new Vector(values);
            var multiplier = new RealNumber(3.0);

            vector = vector.Mul(multiplier);

            Assert.That(vector.Length, Is.EqualTo(3));
            Assert.That(vector[0].GetMod().X, Is.EqualTo(new RealNumber(3.0).GetMod().X));
            Assert.That(vector[1].GetMod().X, Is.EqualTo(new RealNumber(6.0).GetMod().X));
            Assert.That(vector[2].GetMod().X, Is.EqualTo(new RealNumber(-3.0).GetMod().X));
        }
Exemplo n.º 4
0
        public void CorrectlyMemberviseMulWithAnotherVector()
        {
            var valuesX = new[] { new RealNumber(1.0), new RealNumber(2.0), new RealNumber(-1.0) };
            IVector vectorX = new Vector(valuesX);
            var valuesY = new[] { new RealNumber(4.0), new RealNumber(-2.0), new RealNumber(0.0) };
            IVector vectorY = new Vector(valuesY);

            var result = vectorX.MemberviseMul(vectorY);
            var expected = new[]
                {
                    new RealNumber(4.0),
                    new RealNumber(-4.0),
                    new RealNumber(0.0),
                };

            Assert.That(result.Length, Is.EqualTo(valuesX.Length));
            for (int i = 0; i < valuesX.Length; i++)
                Assert.That(result[i].GetMod().X, Is.EqualTo(expected[i].GetMod().X));
        }
Exemplo n.º 5
0
        public void CorrectlyCalculateOuterProduct()
        {
            var valuesX = new[] { new RealNumber(1.0), new RealNumber(2.0), new RealNumber(3.0) };
            IVector vectorX = new Vector(valuesX);
            var valuesY = new[] { new RealNumber(1.0), new RealNumber(2.0)};
            IVector vectorY = new Vector(valuesY);
            var expected = new[]
                {
                    new[] {new RealNumber(1.0), new RealNumber(2.0)},
                    new[] {new RealNumber(2.0), new RealNumber(4.0)},
                    new[] {new RealNumber(3.0), new RealNumber(6.0)},
                };

            var result = vectorX.OuterMul(vectorY);

            for (int i = 0; i < valuesX.Length; i++)
                for (int j = 0; j < valuesY.Length; j++)
                    Assert.That(result[i, j].GetMod().X, Is.EqualTo(expected[i][j].GetMod().X));
        }
Exemplo n.º 6
0
        public void CorrectlyMultiplyByVector()
        {
            var values = new[]
                {
                    new[] {new RealNumber(1.0), new RealNumber(2.0), new RealNumber(3.0)},
                    new[] {new RealNumber(1.0), new RealNumber(2.0), new RealNumber(3.0)},
                    new[] {new RealNumber(0.0), new RealNumber(0.0), new RealNumber(4.0)},
                    new[] {new RealNumber(0.0), new RealNumber(1.0), new RealNumber(0.0)},
                };
            var matrix = new Matrix(values);
            var vector =
                new Vector(new[]
                    {
                        new RealNumber(1.0), new RealNumber(2.0), new RealNumber(1.0)
                    });

            var expected = new[]
                {
                    new RealNumber(8.0), new RealNumber(8.0), new RealNumber(4.0), new RealNumber(2.0)
                };

            var result = matrix.Mul(vector);

            Assert.That(result.Length, Is.EqualTo(matrix.Rows));
            for (int i = 0; i < result.Length; i++)
                Assert.That(result[i].GetMod().X, Is.EqualTo(expected[i].GetMod().X));
        }
Exemplo n.º 7
0
        public void PropagateSignalAfterWeightsChanged()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            Func<double, double> activation = x => x;
            var net = new SimpleFuzzyNet(inputs, hidden, () => new RealNumber(0.0), activation, outputs);

            var input = new List<IFuzzyNumber>
                {
                    new RealNumber(1.0),
                    new RealNumber(2.0),
                };
            var weights = new Vector(new IFuzzyNumber[]
                {
                    //output layer
                    //first neuron
                    new RealNumber(2.0),
                    new RealNumber(2.0),
                    //second neuron
                    new RealNumber(4.0),
                    new RealNumber(2.0),

                    //hidden 1
                    new RealNumber(1.0),
                    new RealNumber(-1.5),
                    new RealNumber(2.0),
                    new RealNumber(1.0),

                    //hidden 2
                    new RealNumber(-1.0),
                    new RealNumber(-1.0),
                    new RealNumber(2.0),
                    new RealNumber(-1.0),
                });

            net.SetWeights(weights);
            var output = net.Propagate(input);

            var expected = new List<IFuzzyNumber>
                {
                    new RealNumber(-20.0),
                    new RealNumber(-24.0),
                };

            int i = 0;
            foreach (var expectedNumber in expected)
            {
                Assert.That(output.ElementAt(i).GetMod().X, Is.EqualTo(expectedNumber.GetMod().X));
                i++;
            }
        }
Exemplo n.º 8
0
        public void CorrectlySetWeightsVector()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            var net = new SimpleFuzzyNet(inputs, hidden, ()=>new RealNumber(0.0), outputNeuronsCount: outputs);
            var weights = new Vector(new IFuzzyNumber[]
                {
                    //output layer
                    //first neuron
                    new RealNumber(2.0),
                    new RealNumber(2.0),
                    //second neuron
                    new RealNumber(4.0),
                    new RealNumber(2.0),

                    //hidden 1
                    new RealNumber(1.0),
                    new RealNumber(-1.5),
                    new RealNumber(2.0),
                    new RealNumber(1.0),

                    //hidden 2
                    new RealNumber(-1.0),
                    new RealNumber(-1.0),
                    new RealNumber(2.0),
                    new RealNumber(-1.0),
                });

            //tuple: (layer, neuron, weight)
            var expectedWeights = new Dictionary<Tuple<int, int, int>, IFuzzyNumber>();
            for (int i = 0; i < weights.Length; i++)
            {
                expectedWeights.Add(new Tuple<int, int, int>(ToLayerIndex(i), ToNeuronIndex(i), ToWeightIndex(i)),
                                    weights[i]);
            }

            net.SetWeights(weights);

            int l = 0;
            foreach (var layer in net.Layers)
            {
                layer.ForeachNeuron((i, neuron) =>
                {
                    neuron.ForeachWeight((j, weight) =>
                        {
                            Assert.That(weight.Signal.GetMod().X,
                                        Is.EqualTo(expectedWeights[new Tuple<int, int, int>(l, i, j)].GetMod().X));
                        });
                });
                l++;
            }
        }
Exemplo n.º 9
0
        public void CorrectlyGetWeightsOfNextLayer()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            var net = new SimpleFuzzyNet(inputs, hidden, GenerateNumbers, outputNeuronsCount: outputs);

            var weights = new Vector(new IFuzzyNumber[]
                {
                    //output layer
                    //first neuron
                    new RealNumber(2.0),
                    new RealNumber(2.0),
                    //second neuron
                    new RealNumber(4.0),
                    new RealNumber(2.0),

                    //hidden 1
                    new RealNumber(1.0),
                    new RealNumber(-1.5),
                    new RealNumber(2.0),
                    new RealNumber(1.0),

                    //hidden 2
                    new RealNumber(-1.0),
                    new RealNumber(-1.0),
                    new RealNumber(2.0),
                    new RealNumber(-1.0),
                });

            //tuple: (layer, neuron, weight)
            var expectedWeights = new Dictionary<Tuple<int, int, int>, IFuzzyNumber>();
            for (int i = 0; i < weights.Length; i++)
            {
                expectedWeights.Add(new Tuple<int, int, int>(ToLayerIndex(i), ToNeuronIndex(i), ToWeightIndex(i)),
                                    weights[i]);
            }

            for (int i = net.Layers.Count - 2; i >= 0; i--)
            {
                var layer = net.Layers.ElementAt(i);
                var nextLayer = net.Layers.ElementAt(i + 1);

                layer.ForeachNeuron((neuronIndex, neuron) =>
                {
                    for (int j = 0; j < nextLayer.NeuronsCount; j++)
                    {
                        var weight = nextLayer.GetNeuron(j).GetWeight(neuronIndex).Signal;
                        Assert.That(weight.GetMod().X,
                                    Is.EqualTo(expectedWeights[new Tuple<int, int, int>(i + 1, j, neuronIndex)].GetMod().X));
                    }
                });
            }
        }
Exemplo n.º 10
0
        public void CorrectlyCreateWeightsVector()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            var net = new SimpleFuzzyNet(inputs, hidden, GenerateNumbers, outputNeuronsCount: outputs);

            var weights = net.GetWeights();
            var expectedWeights = new Vector(new IFuzzyNumber[]
                {
                    //output layer
                    //first neuron
                    new RealNumber(2.0),
                    new RealNumber(2.0),
                    //second neuron
                    new RealNumber(4.0),
                    new RealNumber(2.0),

                    //hidden 1
                    new RealNumber(1.0),
                    new RealNumber(-1.5),
                    new RealNumber(2.0),
                    new RealNumber(1.0),

                    //hidden 2
                    new RealNumber(-1.0),
                    new RealNumber(-1.0),
                    new RealNumber(2.0),
                    new RealNumber(-1.0),
                });

            for (int i = 0; i < expectedWeights.Length; i++)
            {
                Assert.That(weights[i].GetMod().X, Is.EqualTo(expectedWeights[i].GetMod().X));
            }
        }
Exemplo n.º 11
0
        public void CorrectlyNegateVector()
        {
            var valuesX = new[] { new RealNumber(1.0), new RealNumber(0.0), new RealNumber(-1.0) };
            IVector vectorX = new Vector(valuesX);

            var result = vectorX.Negate();
            var expected = new[]
                {
                    new RealNumber(-1.0),
                    new RealNumber(0.0),
                    new RealNumber(1.0),
                };

            Assert.That(result.Length, Is.EqualTo(valuesX.Length));
            for (int i = 0; i < valuesX.Length; i++)
                Assert.That(result[i].GetMod().X, Is.EqualTo(expected[i].GetMod().X));
        }
Exemplo n.º 12
0
        public void ProvideCommutativeMultiplication()
        {
            var valuesX = new[] { new RealNumber(1.0), new RealNumber(2.0), new RealNumber(-1.0) };
            IVector vectorX = new Vector(valuesX);
            var valuesY = new[] { new RealNumber(4.0), new RealNumber(-2.0), new RealNumber(0.0) };
            IVector vectorY = new Vector(valuesY);

            var resultXY = vectorX.Mul(vectorY);
            var resultYX = vectorY.Mul(vectorX);

            Assert.That(resultXY.GetMod().X, Is.EqualTo(resultYX.GetMod().X));
        }
Exemplo n.º 13
0
        public IVector Mul(IVector x)
        {
            if(x.Length != Columns)
                throw new ArgumentException("Vector and matrix dimensions are different");

            var result = new Vector(Rows);

            for (int i = 0; i < result.Length; i++)
                result[i] = x.Mul(new Vector(_values[i]));

            return result;
        }