Пример #1
0
        public void FeedForwardNeuralNetwork_LoadModelSparseMatrix()
        {
            var target = new FeedForwardNeuralNetwork <double>(
                new[] { 2L, 3L, 2L });

            var parser = new DoubleParser <string>();
            var matrix = TestsHelper.ReadMatrix(
                5,
                5,
                "[[-1.0, 1.0, 0.5, 0, 0], [1.0, -1.0, 0.5, 0, 0], [0, 0, 0, -1.0, 2.0], [0, 0, 0, 0.5, -1.5], [0, 0, 0, 1.0, -0.5]]",
                (i, j) => new SparseDictionaryMatrix <double>(i, j, 0),
                parser,
                true);
            var vector = TestsHelper.ReadVector(
                5,
                "[-1.0, 0.0, 1.0, -0.5, 0.5]",
                new SparseDictionaryMathVectorFactory <double>(),
                parser,
                true);
            var model = new NeuralNetworkModel <double, SparseDictionaryMatrix <double>, IMathVector <double> >(
                matrix,
                vector);

            target.LoadModelSparse <SparseDictionaryMatrix <double>, ILongSparseMatrixLine <double>, IMathVector <double> >(
                model);

            this.AssertTargetFromMatrix(
                model,
                target);
        }
Пример #2
0
        public void RunTest_OneEquality()
        {
            var inputConstraintsMatrixText = "[[1,0,3],[0,2,2]]";
            var inputConstraintsVectorText = "[4,12,18]";
            var cost = new SimplexMaximumNumberField <double>(0, -18);
            var nonBasicVariables = new[] { 0, 1 };
            var basicVariables    = new[] { 2, 3, 4 };

            // Leitura da matriz das retrições.
            var doubleElementsParser   = new DoubleParser <string>();
            var inputConstraintsMatrix = TestsHelper.ReadMatrix <double>(
                3,
                2,
                inputConstraintsMatrixText,
                (i, j) => new ArrayMathMatrix <double>(i, j),
                doubleElementsParser,
                true);

            // Leitura do vector de restrições.
            var vectorFactory          = new ArrayVectorFactory <double>();
            var inputConstraintsVector = TestsHelper.ReadVector <double>(
                3,
                inputConstraintsVectorText,
                vectorFactory,
                doubleElementsParser,
                true);

            // Introdução da função objectivo.
            var inputObjectiveFunction = new ArrayMathVector <SimplexMaximumNumberField <double> >(2);

            inputObjectiveFunction[0] = new SimplexMaximumNumberField <double>(
                -3,
                -3);
            inputObjectiveFunction[1] = new SimplexMaximumNumberField <double>(
                -5,
                -2);

            // Objecto de entrada para o algoritmo do simplex.
            var simplexInput = new SimplexInput <double, SimplexMaximumNumberField <double> >(
                basicVariables,
                nonBasicVariables,
                inputObjectiveFunction,
                cost,
                inputConstraintsMatrix,
                inputConstraintsVector);

            var doubleField = new DoubleField();
            var target      = new SimplexAlgorithm <double>(Comparer <double> .Default, doubleField);
            var actual      = target.Run(simplexInput);

            // Verifica o custo
            Assert.AreEqual(36, actual.Cost);

            // Verifica a solução
            Assert.AreEqual(2, actual.Solution[0]);
            Assert.AreEqual(6, actual.Solution[1]);
        }
Пример #3
0
        public void RunSimplexTest_NoFeasibleSolution()
        {
            var inputConstraintsMatrixText = "[[0.3,0.5,0.6],[0.1,0.5,0.4],[0,0,-1]]";
            var inputConstraintsVectorText = "[1.8,6,6]";
            var cost = new SimplexMaximumNumberField <double>(0, -12);
            var nonBasicVariables = new[] { 0, 1, 2 };
            var basicVariables    = new[] { 3, 4, 5 };

            // Leitura da matriz das retrições.
            var doubleElementsParser   = new DoubleParser <string>();
            var inputConstraintsMatrix = TestsHelper.ReadMatrix <double>(
                3,
                3,
                inputConstraintsMatrixText,
                (i, j) => new ArrayMathMatrix <double>(i, j),
                doubleElementsParser,
                true);

            // Leitura do vector de restrições.
            var vectorFactory          = new ArrayVectorFactory <double>();
            var inputConstraintsVector = TestsHelper.ReadVector <double>(
                3,
                inputConstraintsVectorText,
                vectorFactory,
                doubleElementsParser,
                true);

            // Introdução da função objectivo.
            var inputObjectiveFunction = new ArrayMathVector <SimplexMaximumNumberField <double> >(3);

            inputObjectiveFunction[0] = new SimplexMaximumNumberField <double>(
                0.4,
                -1.1);
            inputObjectiveFunction[1] = new SimplexMaximumNumberField <double>(
                0.5,
                -0.9);
            inputObjectiveFunction[2] = new SimplexMaximumNumberField <double>(
                0,
                1);

            // Objecto de entrada para o algoritmo do simplex.
            var simplexInput = new SimplexInput <double, SimplexMaximumNumberField <double> >(
                basicVariables,
                nonBasicVariables,
                inputObjectiveFunction,
                cost,
                inputConstraintsMatrix,
                inputConstraintsVector);

            var doubleField = new DoubleField();
            var target      = new SimplexAlgorithm <double>(Comparer <double> .Default, doubleField);
            var actual      = target.Run(simplexInput);

            // O problema tem uma solução cujo custo é inifito.
            Assert.IsFalse(actual.HasSolution);
        }
Пример #4
0
        public void ReplaceTest_ReplaceByMatrixWithMatrixAlgebra()
        {
            // Representação dos polinómios.
            var polynomText  = "x^2 + 2*x + 1";
            var variableName = "x";

            var integerDomain        = new IntegerDomain();
            var integerToIntegerConv = new ElementToElementConversion <int>();
            var integerParser        = new IntegerParser <string>();
            var fractionField        = new FractionField <int>(integerDomain);
            var fractionFieldParser  = new FieldDrivenExpressionParser <Fraction <int> >(
                new SimpleElementFractionParser <int>(integerParser, integerDomain),
                fractionField);

            var polynomial = TestsHelper.ReadUnivarPolynomial <Fraction <int> >(
                polynomText,
                fractionField,
                fractionFieldParser,
                new ElementFractionConversion <int>(integerDomain),
                variableName);

            // Leitura da matriz.
            var matrix = TestsHelper.ReadMatrix <Fraction <int> >(
                2,
                2,
                "[[1/2+1/3,1/2-1/3],[1/5+1/4,1/5-1/4]]",
                (i, j) => new ArrayMathMatrix <Fraction <int> >(i, j),
                fractionFieldParser);

            var matrixAlgebra = new GeneralMatrixAlgebra <Fraction <int> >(
                2,
                new ArrayMathMatrixFactory <Fraction <int> >(),
                fractionField);
            var actual   = polynomial.Replace(matrix, matrixAlgebra);
            var expected = TestsHelper.ReadMatrix <Fraction <int> >(
                2,
                2,
                "[[1237/360,167/360],[501/400,391/400]]",
                (i, j) => new ArrayMathMatrix <Fraction <int> >(i, j),
                fractionFieldParser);

            for (int i = 0; i < 2; ++i)
            {
                for (int j = 0; j < 2; ++j)
                {
                    Assert.AreEqual(expected[i, j], actual[i, j]);
                }
            }
        }
        public void RunTest()
        {
            var inputMatrix  = "[[1,2,1],[2,-1,-1],[1,-1,3]]";
            var inputVector  = "[[1,2,3]]";
            var expectedText = "[[21/19,-6/19,10/19]]";

            var integerDomain       = new BigIntegerDomain();
            var integerParser       = new BigIntegerParser <string>();
            var fractionField       = new FractionField <BigInteger>(integerDomain);
            var fractionFieldParser = new FieldDrivenExpressionParser <Fraction <BigInteger> >(
                new SimpleElementFractionParser <BigInteger>(integerParser, integerDomain),
                fractionField);
            var matrixFactory = new ArrayMathMatrixFactory <Fraction <BigInteger> >();

            // Leitura da matriz que representa o sistema de equações.
            var coeffsMatrix = TestsHelper.ReadMatrix <Fraction <BigInteger> >(
                3,
                3,
                inputMatrix,
                (i, j) => new ArrayMathMatrix <Fraction <BigInteger> >(i, j),
                fractionFieldParser);

            // Leitura do vector de termos independente.
            var vectorMatrix = TestsHelper.ReadMatrix <Fraction <BigInteger> >(
                3,
                1,
                inputVector,
                (i, j) => new ArrayMathMatrix <Fraction <BigInteger> >(i, j),
                fractionFieldParser);

            var expectedMatrix = TestsHelper.ReadMatrix <Fraction <BigInteger> >(
                3,
                1,
                expectedText,
                (i, j) => new ArrayMathMatrix <Fraction <BigInteger> >(i, j),
                fractionFieldParser);

            var systemSolver = new SequentialLanczosAlgorithm <Fraction <BigInteger>, FractionField <BigInteger> >(
                matrixFactory,
                fractionField);
            var squareMatrix = (coeffsMatrix as ArrayMathMatrix <Fraction <BigInteger> >).AsSquare();
            var actual       = systemSolver.Run(squareMatrix, vectorMatrix);

            for (int i = 0; i < 3; ++i)
            {
                Assert.AreEqual(expectedMatrix[i, 0], actual[i, 0]);
            }
        }
        public void RunTest()
        {
            var coefficientsMatrixText = "[[1,0,0],[0,0,0],[0,3,3],[2,0,1]]";
            var independentVectorText  = "[[1,3,3]]";
            var expectedText           = "[[1,0,1,0]]";

            var integerDomain       = new IntegerDomain();
            var integerParser       = new IntegerParser <string>();
            var fractionField       = new FractionField <int>(integerDomain);
            var fractionFieldParser = new FieldDrivenExpressionParser <Fraction <int> >(
                new SimpleElementFractionParser <int>(integerParser, integerDomain),
                fractionField);
            var matrixFactory = new ArrayMatrixFactory <Fraction <int> >();

            // Leitura da matriz que representa o sistema de equações.
            var coeffsMatrix = TestsHelper.ReadMatrix <Fraction <int> >(
                3,
                4,
                coefficientsMatrixText,
                matrixFactory,
                fractionFieldParser);

            // Leitura do vector de termos independente.
            var vectorMatrix = TestsHelper.ReadMatrix <Fraction <int> >(
                3,
                1,
                independentVectorText,
                new ArrayMatrixFactory <Fraction <int> >(),
                fractionFieldParser);

            var expectedMatrixVector = TestsHelper.ReadMatrix <Fraction <int> >(
                4,
                1,
                expectedText,
                matrixFactory,
                fractionFieldParser);

            var algorithm = new DenseCondensationLinSysAlgorithm <Fraction <int> >(fractionField);
            var actual    = algorithm.Run(coeffsMatrix, vectorMatrix);

            Assert.AreEqual(expectedMatrixVector.GetLength(0), actual.Vector.Length);
            for (int i = 0; i < actual.Vector.Length; ++i)
            {
                Assert.AreEqual(expectedMatrixVector[i, 0], actual.Vector[i]);
            }
        }
Пример #7
0
        public void RunTest_BigIntegerMatrix()
        {
            // A leitura é realizada por colunas.
            var matrixText    = "[[100000,1001,20005], [32534,4245341,56134513451], [21346136,1134613,1136135613]]";
            var integerDomain = new BigIntegerDomain();
            var variableName  = "x";
            var integerParser = new BigIntegerParser <string>();
            var conversion    = new BigIntegerToIntegerConversion();
            var matrix        = TestsHelper.ReadMatrix <BigInteger>(
                3,
                3,
                matrixText,
                (i, j) => new ArraySquareMathMatrix <BigInteger>(i),
                integerParser);
            var fastDivFreeCharacPolAlg = new FastDivisionFreeCharPolynomCalculator <BigInteger>(variableName, integerDomain);
            var expected = TestsHelper.ReadUnivarPolynomial("1*x^3+-1140480954*x^2-58754054577367644*x+4689162494877443109176", integerDomain, integerParser, conversion, variableName);
            var actual   = fastDivFreeCharacPolAlg.Run(matrix as ISquareMathMatrix <BigInteger>);

            Assert.AreEqual(expected, actual);
        }
Пример #8
0
        public void RunTest_IntegerMatrix()
        {
            // A leitura é realizada por colunas.
            var matrixText    = "[[1,-1,2], [3,4,5], [2,1,1]]";
            var integerDomain = new IntegerDomain();
            var variableName  = "x";
            var integerParser = new IntegerParser <string>();
            var conversion    = new ElementToElementConversion <int>();
            var matrix        = TestsHelper.ReadMatrix <int>(
                3,
                3,
                matrixText,
                (i, j) => new ArraySquareMathMatrix <int>(i),
                integerParser,
                true);
            var fastDivFreeCharacPolAlg = new FastDivisionFreeCharPolynomCalculator <int>(variableName, integerDomain);
            var expected = TestsHelper.ReadUnivarPolynomial("x^3-6*x^2+3*x+18", integerDomain, integerParser, conversion, variableName);
            var actual   = fastDivFreeCharacPolAlg.Run(matrix as ISquareMathMatrix <int>);

            Assert.AreEqual(expected, actual);
        }
Пример #9
0
        public void FeedFrowardNeuralNetwork_InternalComputeOutputs()
        {
            var target = new FeedForwardNeuralNetwork <double>(
                new[] { 2L, 3L, 2L });

            var parser = new DoubleParser <string>();
            var matrix = TestsHelper.ReadMatrix(
                5,
                5,
                "[[-1.0, 1.0, 0.5, 0, 0], [1.0, -1.0, 0.5, 0, 0], [0, 0, 0, -1.0, 2.0], [0, 0, 0, 0.5, -1.5], [0, 0, 0, 1.0, -0.5]]",
                (i, j) => new SparseDictionaryMatrix <double>(i, j, 0),
                parser,
                true);
            var vector = TestsHelper.ReadVector(
                5,
                "[0.5, 0.5, 0.5, 0.5, 0.5]",
                new SparseDictionaryMathVectorFactory <double>(),
                parser,
                true);
            var model = new NeuralNetworkModel <double, SparseDictionaryMatrix <double>, IMathVector <double> >(
                matrix,
                vector);

            target.LoadModel(model);

            var outputMatrix = target.InternalReserveOutput();

            target.InternalComputeLayerOutputs(
                new ArrayMathVector <double>(new[] { 1.0, -1.0 }),
                outputMatrix,
                (d1, d2) =>
            {
                if (d2 > d1)
                {
                    return(1.0);
                }
                else
                {
                    return(0.0);
                }
            },
                (u, v, l) =>
            {
                var result = 0.0;
                for (var i = 0L; i < l; ++i)
                {
                    result += u[i] * v[i];
                }

                return(result);
            });

            Assert.AreEqual(target.Schema.LongCount() - 1L, outputMatrix.LongLength);

            var currOut = outputMatrix[0];

            Assert.AreEqual(0.0, currOut[0]);
            Assert.AreEqual(1.0, currOut[1]);
            Assert.AreEqual(0.0, currOut[2]);

            currOut = outputMatrix[1];
            Assert.AreEqual(0.0, currOut[0]);
            Assert.AreEqual(0.0, currOut[1]);
        }
Пример #10
0
        public void FeedFrowardNeuralNetwork_RunSimpleMatrixTest()
        {
            var target = new FeedForwardNeuralNetwork <double>(
                new[] { 2L, 3L, 2L });

            var parser = new DoubleParser <string>();
            var matrix = TestsHelper.ReadMatrix(
                5,
                5,
                "[[-1.0, 1.0, 0.5, 0, 0], [1.0, -1.0, 0.5, 0, 0], [0, 0, 0, -1.0, 2.0], [0, 0, 0, 0.5, -1.5], [0, 0, 0, 1.0, -0.5]]",
                (i, j) => new SparseDictionaryMatrix <double>(i, j, 0),
                parser,
                true);
            var vector = TestsHelper.ReadVector(
                5,
                "[0.5, 0.5, 0.5, 0.5, 0.5]",
                new SparseDictionaryMathVectorFactory <double>(),
                parser,
                true);
            var model = new NeuralNetworkModel <double, SparseDictionaryMatrix <double>, IMathVector <double> >(
                matrix,
                vector);

            target.LoadModel(model);

            var actual = target.Run(
                new[] { 1.0, 0.0 },
                (u, v, l) =>
            {
                var result = 0.0;
                for (var i = 0L; i < l; ++i)
                {
                    result += u[i] * v[i];
                }

                return(result);
            },
                (d1, d2) =>
            {
                if (d2 > d1)
                {
                    return(1.0);
                }
                else
                {
                    return(0.0);
                }
            });
            var expected = new[] { 0.0, 0.0 };

            CollectionAssert.AreEqual(expected, actual);

            actual = target.Run(
                new[] { 0.0, 1.0 },
                (u, v, l) =>
            {
                var result = 0.0;
                for (var i = 0L; i < l; ++i)
                {
                    result += u[i] * v[i];
                }

                return(result);
            },
                (d1, d2) =>
            {
                if (d2 > d1)
                {
                    return(1.0);
                }
                else
                {
                    return(0.0);
                }
            });
            expected = new[] { 0.0, 1.0 };

            CollectionAssert.AreEqual(expected, actual);

            actual = target.Run(
                new[] { 1.0, -1.0 },
                (u, v, l) =>
            {
                var result = 0.0;
                for (var i = 0L; i < l; ++i)
                {
                    result += u[i] * v[i];
                }

                return(result);
            },
                (d1, d2) =>
            {
                if (d2 > d1)
                {
                    return(1.0);
                }
                else
                {
                    return(0.0);
                }
            });
            expected = new[] { 0.0, 0.0 };

            CollectionAssert.AreEqual(expected, actual);
        }
        public void RunTest_OnlyInequalitiesDouble()
        {
            var inputConstraintsMatrixText = "[[1,0,3],[0,2,2]]";
            var inputConstraintsVectorText = "[4,12,18]";
            var inputObjectiveFuncText     = "[-3,-5]";
            var inverseMatrixText          = "[[1,0,0],[0,1,0],[0,0,1]]";
            var cost = 0.0;
            var nonBasicVariables = new[] { 0, 1 };
            var basicVariables    = new[] { 2, 3, 4 };

            // Leitura da matriz das retrições.
            var doubleElementsParser   = new DoubleParser <string>();
            var inputConstraintsMatrix = TestsHelper.ReadMatrix <double>(
                3,
                2,
                inputConstraintsMatrixText,
                (i, j) => new ArrayMathMatrix <double>(i, j),
                doubleElementsParser,
                true);

            // Leitura da matriz inversa.
            var inverseMatrix = TestsHelper.ReadMatrix <double>(
                3,
                3,
                inverseMatrixText,
                (i, j) => new ArraySquareMathMatrix <double>(i),
                doubleElementsParser,
                true) as ISquareMathMatrix <double>;

            // Leitura do vector de restrições.
            var vectorFactory          = new ArrayVectorFactory <double>();
            var inputConstraintsVector = TestsHelper.ReadVector <double>(
                3,
                inputConstraintsVectorText,
                vectorFactory,
                doubleElementsParser,
                true);

            // Leitura da função objectivo.
            var inputObjectiveFunction = TestsHelper.ReadVector <double>(
                2,
                inputObjectiveFuncText,
                vectorFactory,
                doubleElementsParser,
                true);

            // Objecto de entrada para o algoritmo do simplex.
            var simplexInput = new RevisedSimplexInput <double, double>(
                basicVariables,
                nonBasicVariables,
                inputObjectiveFunction,
                cost,
                inputConstraintsMatrix,
                inputConstraintsVector,
                inverseMatrix);

            // Executa o algoritmo do simplex.
            var doubleField = new DoubleField();
            var target      = new RevisedSimplexAlgorithm <double>(Comparer <double> .Default, doubleField);
            var actual      = target.Run(simplexInput);

            // Verifica o custo
            Assert.AreEqual(36, actual.Cost);

            // Verifica a solução
            Assert.AreEqual(2, actual.Solution[0]);
            Assert.AreEqual(6, actual.Solution[1]);
        }