public void LdlDecompLinearSystemAlgorithm_RunTest() { var domain = new IntegerDomain(); var fractionField = new FractionField <int>(domain); var decompositionAlg = new TriangDiagSymmMatrixDecomposition <Fraction <int> >( fractionField); var symmDecompSolver = new SymmetricLdlDecompLinearSystemAlgorithm <Fraction <int> >( decompositionAlg); var target = new LdlDecompLinearSystemAlgorithm <Fraction <int> >( symmDecompSolver, fractionField); var matrix = this.GetGeneralMatrix(domain); var vector = this.GetIndVectorForGenMatrix(domain); var actual = target.Run(matrix, vector); var lines = matrix.GetLength(0); var columns = matrix.GetLength(1); var nullVector = new ZeroVector <Fraction <int> >(lines, fractionField); var expectedVector = new ArrayMathVector <Fraction <int> >(lines); for (var i = 0; i < lines; ++i) { expectedVector[i] = vector[i, 0]; } actual = target.Run(matrix, vector); this.AssertVector(expectedVector, matrix, actual.Vector, fractionField); for (var i = 0; i < actual.VectorSpaceBasis.Count; ++i) { var vec = actual.VectorSpaceBasis[i]; this.AssertVector(nullVector, matrix, vec, fractionField); } }
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]); }
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); }
/// <summary> /// Obtém um modelo para rede complexo associado ao esquema /// [5, 3, 4, 2, 5] /// </summary> /// <returns> /// O modelo complexo. /// </returns> private NeuralNetworkModel <double, CoordinateSparseMathMatrix <double>, ArrayMathVector <double> > GetComplexTestModel() { var result = new CoordinateSparseMathMatrix <double>(14, 14, 0.0); var schema = new[] { 5, 3, 4, 2, 5 }; var cycleCount = schema.Length - 1; // Introdução do primeiro bloco var pointer = 0; var prevCol = 0; var currCol = schema[pointer++]; var prevLine = 0; var currLine = schema[pointer]; var value = 1.0; while (pointer < cycleCount) { for (var i = prevLine; i < currLine; ++i) { for (var j = prevCol; j < currCol; ++j) { result[i, j] = value; value += 0.25; } } prevCol = currCol; currCol += schema[pointer++]; prevLine = currLine; currLine += schema[pointer]; } // Última coluna for (var i = prevLine; i < currLine; ++i) { for (var j = prevCol; j < currCol; ++j) { result[i, j] = value; value += 0.25; } } var tresholds = new ArrayMathVector <double>(14); value = 0.0; for (var i = 0; i < 14; ++i) { tresholds[i] = value; value += 0.25; } return(new NeuralNetworkModel <double, CoordinateSparseMathMatrix <double>, ArrayMathVector <double> >( result, tresholds)); }
public void SymmetricLdlDecompLinearSystemAlgorithm_RunTest() { var domain = new IntegerDomain(); var fractionField = new FractionField <int>(domain); var decompositionAlg = new TriangDiagSymmMatrixDecomposition <Fraction <int> >( fractionField); var target = new SymmetricLdlDecompLinearSystemAlgorithm <Fraction <int> >( decompositionAlg); var matrix = this.GetSingularMatrix(domain); var vector = this.GetZeroFilledVector(domain); var actual = target.Run(matrix, vector); // Teste à característica da matriz Assert.AreEqual(2, actual.VectorSpaceBasis.Count); // Teste ao vector independente var size = matrix.GetLength(0); var nullVector = new ZeroVector <Fraction <int> >(size, fractionField); this.AssertVector(nullVector, matrix, actual.Vector, fractionField); // Teste aos vectores da base for (var i = 0; i < actual.VectorSpaceBasis.Count; ++i) { var vec = actual.VectorSpaceBasis[i]; this.AssertVector(nullVector, matrix, vec, fractionField); } // Teste à matriz com vector independente vector = this.GetIndtVectorForSingularMatrix(domain); var expectedVector = new ArrayMathVector <Fraction <int> >(size); for (var i = 0; i < size; ++i) { expectedVector[i] = vector[i, 0]; } actual = target.Run(matrix, vector); this.AssertVector(expectedVector, matrix, actual.Vector, fractionField); for (var i = 0; i < actual.VectorSpaceBasis.Count; ++i) { var vec = actual.VectorSpaceBasis[i]; this.AssertVector(nullVector, matrix, vec, fractionField); } }
/// <summary> /// Obtém um modelo para rede disconexo e complexo associado ao esquema /// [5, 3, 4, 2, 5] /// </summary> /// <returns> /// O modelo disconexo e complexo. /// </returns> private NeuralNetworkModel <double, CoordinateSparseMathMatrix <double>, ArrayMathVector <double> > GetComplexUnconnectedTestModel() { var result = new CoordinateSparseMathMatrix <double>(14, 14, 0.0); var schema = new[] { 5, 3, 4, 2, 5 }; var tresholds = new ArrayMathVector <double>(14); var value = 0.0; for (var i = 0; i < 14; ++i) { tresholds[i] = value; value += 0.25; } return(new NeuralNetworkModel <double, CoordinateSparseMathMatrix <double>, ArrayMathVector <double> >( result, tresholds)); }
public void RunTest2() { var integerDomain = new IntegerDomain(); var decimalField = new DecimalField(); var vectorFactory = new ArrayVectorFactory <decimal>(); var decimalComparer = Comparer <decimal> .Default; var nearest = new DecimalNearestInteger(); var scalarProd = new OrthoVectorScalarProduct <decimal>( decimalComparer, decimalField); var integerDecimalConverter = new IntegerDecimalConverter(); var dim = 4; var vectorSet = new IMathVector <decimal> [dim]; var lllReductionAlg = new LLLBasisReductionAlgorithm <IMathVector <decimal>, decimal, int>( new VectorSpace <decimal>(dim, vectorFactory, decimalField), scalarProd, nearest, Comparer <decimal> .Default); vectorSet[0] = new ArrayMathVector <decimal>(new decimal[] { 1, 1, 7, 2 }); vectorSet[1] = new ArrayMathVector <decimal>(new decimal[] { 9, 8, 4, 6 }); vectorSet[2] = new ArrayMathVector <decimal>(new decimal[] { 1, 8, 5, 7 }); vectorSet[3] = new ArrayMathVector <decimal>(new decimal[] { 2, 3, 1, 1 }); var reduced = lllReductionAlg.Run(vectorSet, 3M / 4); var expected = new IMathVector <decimal> [dim]; expected[0] = new ArrayMathVector <decimal>(new decimal[] { 2, 3, 1, 1 }); expected[1] = new ArrayMathVector <decimal>(new decimal[] { 3, -1, 1, 3 }); expected[2] = new ArrayMathVector <decimal>(new decimal[] { -2, 2, 6, -1 }); expected[3] = new ArrayMathVector <decimal>(new decimal[] { -4, 1, -4, 3 }); this.AsserVectorLists(expected, reduced); }
public void RunTest1() { var integerDomain = new IntegerDomain(); var decimalField = new DecimalField(); var vectorFactory = new ArrayVectorFactory <decimal>(); var decimalComparer = Comparer <decimal> .Default; var nearest = new DecimalNearestInteger(); var scalarProd = new OrthoVectorScalarProduct <decimal>( decimalComparer, decimalField); var integerDecimalConverter = new IntegerDecimalConverter(); var lllReductionAlg = new LLLBasisReductionAlgorithm <IMathVector <decimal>, decimal, int>( new VectorSpace <decimal>(3, vectorFactory, decimalField), scalarProd, nearest, Comparer <decimal> .Default); var vectorSet = new IMathVector <decimal> [3]; vectorSet[0] = new ArrayMathVector <decimal>(new decimal[] { 1, 1, 1 }); vectorSet[1] = new ArrayMathVector <decimal>(new decimal[] { -1, 0, 2 }); vectorSet[2] = new ArrayMathVector <decimal>(new decimal[] { 3, 5, 6 }); var reduced = lllReductionAlg.Run(vectorSet, 3M / 4); // O resultado esperado. var expected = new IMathVector <decimal> [3]; expected[0] = new ArrayMathVector <decimal>(new decimal[] { 0, 1, 0 }); expected[1] = new ArrayMathVector <decimal>(new decimal[] { 1, 0, 1 }); expected[2] = new ArrayMathVector <decimal>(new decimal[] { -1, 0, 2 }); this.AsserVectorLists(expected, reduced); }
/// <summary> /// Aplica o algoritmo sobre a matriz dos custos. /// </summary> /// <param name="costs">A matriz dos custos.</param> /// <param name="numberOfMedians">O número de medianas a serem escolhidas.</param> /// <returns>O resultado da relaxação.</returns> public CoeffType[] Run(SparseDictionaryMathMatrix <CoeffType> costs, int numberOfMedians) { if (costs == null) { throw new ArgumentNullException("costs"); } else { var numberOfVertices = costs.GetLength(1); var objectiveFunction = new List <SimplexMaximumNumberField <CoeffType> >(); // A mediana que cobre todas as outras é ligeiramente diferente objectiveFunction.Add(new SimplexMaximumNumberField <CoeffType>( this.coeffsField.AdditiveUnity, this.coeffsField.AdditiveInverse(this.coeffsField.MultiplicativeUnity))); for (int i = 1; i < numberOfVertices; ++i) { objectiveFunction.Add(new SimplexMaximumNumberField <CoeffType>( this.coeffsField.AdditiveUnity, this.coeffsField.AddRepeated( this.coeffsField.AdditiveInverse(this.coeffsField.MultiplicativeUnity), 2))); } // Adiciona as variáveis x à função objectivo. var numberXVars = 0; foreach (var line in costs.GetLines()) { foreach (var column in line.Value.GetColumns()) { if (line.Key != column.Key) { ++numberXVars; var valueToAdd = new SimplexMaximumNumberField <CoeffType>( column.Value, this.coeffsField.AdditiveInverse(this.coeffsField.MultiplicativeUnity)); objectiveFunction.Add(valueToAdd); } } } // Cria a matriz das restrições e preenche-a com os valores correctos var constraintsNumber = numberXVars + numberOfVertices; var nonBasicVariables = new int[objectiveFunction.Count]; var basicVariables = new int[constraintsNumber]; var linesLength = nonBasicVariables.Length + basicVariables.Length; // Preence os vectores que permitem seleccionar as variáveis. this.FillVariablesSelectors(nonBasicVariables, basicVariables, linesLength); // Preencimento da matriz das restrições var constraintsMatrix = new ArrayMathMatrix <CoeffType>( constraintsNumber, constraintsNumber, this.coeffsField.AdditiveUnity); var constraintLineNumber = 0; var constraintXYLineNumber = 0; var unity = this.coeffsField.MultiplicativeUnity; var inverseAdditive = this.coeffsField.AdditiveInverse(unity); foreach (var line in costs.GetLines()) { foreach (var column in line.Value.GetColumns()) { if (line.Key != column.Key) { constraintsMatrix[constraintXYLineNumber, constraintLineNumber] = inverseAdditive; constraintsMatrix[constraintXYLineNumber, constraintXYLineNumber + numberOfVertices] = unity; ++constraintXYLineNumber; } } ++constraintLineNumber; } var lastLine = constraintsNumber - 1; constraintsMatrix[lastLine, 0] = unity; constraintLineNumber = numberXVars; for (int i = 1; i < numberOfVertices; ++i) { constraintsMatrix[constraintLineNumber, i] = unity; constraintsMatrix[lastLine, i] = unity; constraintXYLineNumber = numberOfVertices; foreach (var lines in costs.GetLines()) { foreach (var column in lines.Value.GetColumns()) { if (lines.Key != column.Key) { if (column.Key == i) { constraintsMatrix[constraintLineNumber, constraintXYLineNumber] = unity; } ++constraintXYLineNumber; } } } ++constraintLineNumber; } // Preenchimento do vector independente das restrições var vector = new ArrayMathVector <CoeffType>(constraintsNumber, this.coeffsField.AdditiveUnity); lastLine = constraintsNumber - 1; for (int i = numberXVars; i < lastLine; ++i) { vector[i] = unity; } vector[lastLine] = this.conversion.InverseConversion(numberOfMedians); var sumVector = this.coeffsField.AdditiveUnity; for (int i = 0; i < vector.Length; ++i) { sumVector = this.coeffsField.Add(sumVector, vector[i]); } sumVector = this.coeffsField.AdditiveInverse(sumVector); var simplexInput = new SimplexInput <CoeffType, SimplexMaximumNumberField <CoeffType> >( basicVariables, nonBasicVariables, new ArrayMathVector <SimplexMaximumNumberField <CoeffType> >(objectiveFunction.ToArray()), new SimplexMaximumNumberField <CoeffType>(this.coeffsField.AdditiveUnity, sumVector), constraintsMatrix, vector); var resultSimplexSol = this.simplexAlg.Run(simplexInput); var result = new CoeffType[numberOfVertices]; Array.Copy(resultSimplexSol.Solution, result, numberOfVertices); return(result); } }