public void RunTest_OnlyInequalitiesDouble() { var inputConstraintsMatrixText = "[[1,0,3],[0,2,2]]"; var inputConstraintsVectorText = "[4,12,18]"; var inputObjectiveFuncText = "[-3,-5]"; 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 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 SimplexInput <double, double>( basicVariables, nonBasicVariables, inputObjectiveFunction, cost, inputConstraintsMatrix, inputConstraintsVector); // Executa o algoritmo do simplex. 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]); }
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); }
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]); }
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_OneEquality() { var inputConstraintsMatrixText = "[[1,0,3],[0,2,2]]"; var inputConstraintsVectorText = "[4,12,18]"; var inverseMatrixText = "[[1,0,0],[0,1,0],[0,0,1]]"; 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 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); // 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 RevisedSimplexInput <double, SimplexMaximumNumberField <double> >( basicVariables, nonBasicVariables, inputObjectiveFunction, cost, inputConstraintsMatrix, inputConstraintsVector, inverseMatrix); 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]); }