public void FindMinimum_TestFunction1WithConstraints_AnalyticResult() { /* Constraints: * x_0 + x_1 = 3, * x_0 >= 0, * x_1 >= 0, * x_0 + x_2 >= 2 */ var optimizer = new GoldfarbIdanaQuadraticProgram(); var boxConstraint = optimizer.Constraint.Create(MultiDimRegion.Interval.Create(2, new[] { 0.0, 0.0 }, new[] { Double.NaN, Double.NaN })); var equalityConstraint = optimizer.Constraint.Create(new MultiDimRegion.LinearEquality(new DenseMatrix(2, 1, new[] { 1.0, 1.0 }), new[] { 3.0 })); var inequalityConstraint = optimizer.Constraint.Create(new MultiDimRegion.LinearInequality(new DenseMatrix(2, 1, new[] { 1.0, 1.0 }), new[] { 2.0 })); var algorithm = optimizer.Create(boxConstraint, equalityConstraint, inequalityConstraint); var A = new DenseMatrix(2, 2, new[] { 4.0, -2.0, -2.0, 4 - 0 }); // = (4 & -2 \\ -2 & 4) var b = new[] { 6.0, 0.0 }; algorithm.Function = optimizer.Function.Create(A, b); var actualArgMin = new double[2]; double actualMinimum; var state = algorithm.FindMinimum(actualArgMin, out actualMinimum); var expectedArgMin = new[] { 1.0, 2.0 }; var expectedMinimum = 12.0; Assert.That(actualMinimum, Is.EqualTo(expectedMinimum).Within(1E-7), String.Format("State: {0}; actual Minimum: {1}; expected Minimum: {2}; actual argMin: [{3}; {4}]; expected argMin: [{5}; {6}].", state, actualMinimum, expectedMinimum, actualArgMin[0], actualArgMin[1], expectedArgMin[0], expectedArgMin[1])); Assert.That(actualArgMin, Is.EqualTo(expectedArgMin).AsCollection.Within(1E-7), String.Format("State: {0}; actual Minimum: {1}; expected Minimum: {2}; actual argMin: [{3}; {4}]; expected argMin: [{5}; {6}].", state, actualMinimum, expectedMinimum, actualArgMin[0], actualArgMin[1], expectedArgMin[0], expectedArgMin[1])); }
public void FindMinimum_TestFunction3WithConstraints_AnalyticResult() { /* minimize: * f(x) = - 8 * x_0 - 16 * x_1 + x_0^2 + 4 * x_1^2 * * subject to x_0 + x_1 <= 5, x_0 <? 3, x_0 >= 0, x_1 >= 0 */ var optimizer = new GoldfarbIdanaQuadraticProgram(); var boxConstraint = optimizer.Constraint.Create(MultiDimRegion.Interval.Create(2, new[] { 0.0, 0.0 }, new[] { 3.0, Double.NaN })); var inequalityConstraint = optimizer.Constraint.Create(new MultiDimRegion.LinearInequality(new DenseMatrix(2, 1, new[] { -1.0, -1.0 }), new[] { -5.0 })); var algorithm = optimizer.Create(boxConstraint, inequalityConstraint); var A = new DenseMatrix(2, 2, new[] { 2.0, 0.0, 0.0, 8.0 }); var b = new[] { -8.0, -16.0 }; algorithm.Function = optimizer.Function.Create(A, b); var actualArgMin = new double[2]; double actualMinimum; var state = algorithm.FindMinimum(actualArgMin, out actualMinimum); var expectedArgMin = new[] { 3.0, 2.0 }; var expectedMinimum = -31.0; Assert.That(actualMinimum, Is.EqualTo(expectedMinimum).Within(1E-7), String.Format("State: {0}; actual Minimum: {1}; expected Minimum: {2}; actual argMin: [{3}; {4}]; expected argMin: [{5}; {6}].", state, actualMinimum, expectedMinimum, actualArgMin[0], actualArgMin[1], expectedArgMin[0], expectedArgMin[1])); Assert.That(actualArgMin, Is.EqualTo(expectedArgMin).AsCollection.Within(1E-7), String.Format("State: {0}; actual Minimum: {1}; expected Minimum: {2}; actual argMin: [{3}; {4}]; expected argMin: [{5}; {6}].", state, actualMinimum, expectedMinimum, actualArgMin[0], actualArgMin[1], expectedArgMin[0], expectedArgMin[1])); }
public void FindMinimum_TestFunction1NoConstraints_AnalyticResult() { var optimizer = new GoldfarbIdanaQuadraticProgram(); var algorithm = optimizer.Create(2); var A = new DenseMatrix(2, 2, new[] { 4.0, -2.0, -2.0, 4 - 0 }); // = (4 & -2 \\ -2 & 4) var b = new[] { 6.0, 0.0 }; algorithm.Function = optimizer.Function.Create(A, b); var actualArgMin = new double[2]; double actualMinimum; var state = algorithm.FindMinimum(actualArgMin, out actualMinimum); var expectedArgMin = new[] { -2.0, -1.0 }; var expectedMinimum = -6.0; Assert.That(actualMinimum, Is.EqualTo(expectedMinimum).Within(1E-7), String.Format("State: {0}; actual Minimum: {1}; expected Minimum: {2}; actual argMin: [{3}; {4}]; expected argMin: [{5}; {6}].", state, actualMinimum, expectedMinimum, actualArgMin[0], actualArgMin[1], expectedArgMin[0], expectedArgMin[1])); Assert.That(actualArgMin, Is.EqualTo(expectedArgMin).AsCollection.Within(1E-7), String.Format("State: {0}; actual Minimum: {1}; expected Minimum: {2}; actual argMin: [{3}; {4}]; expected argMin: [{5}; {6}].", state, actualMinimum, expectedMinimum, actualArgMin[0], actualArgMin[1], expectedArgMin[0], expectedArgMin[1])); }
/// <summary>Initializes a new instance of the <see cref="Algorithm"/> class. /// </summary> /// <param name="optimizer">The <see cref="GoldfarbIdanaQuadraticProgram"/> object that serves as factory of the current object.</param> /// <param name="dimension">The dimension of the feasible region.</param> /// <param name="inequalityMatrix">The representation of the inequality constraint matrix C with C' * x >= c, provided column-by-column, where the number of rows corresponds to the dimension of the feasible region.</param> /// <param name="inequalityVector">The inequality constraint vector c with C' * x >= c, with k relevant entries, where k is the numer of columns of matrix C, i.e. the number of inequality constraints.</param> /// <param name="equalityMatrix">The equality constraint matrix D with D' * x = d, provided column-by-column, where the number of rows corresponds to the dimension of the feasible region.</param> /// <param name="equalityVector">The equality constraint vector d with D' * x = d, with k relevant entries, where k is the numer of columns of matrix D, i.e. the number of equality constraints; perhaps <c>null</c>.</param> internal Algorithm(GoldfarbIdanaQuadraticProgram optimizer, int dimension, List <double> inequalityMatrix, List <double> inequalityVector, List <double> equalityMatrix, List <double> equalityVector) : this(optimizer, dimension) { if ((inequalityMatrix != null) && (inequalityMatrix.Count > 0)) { if ((inequalityMatrix.Count % dimension) != 0) { throw new ArgumentException(nameof(inequalityMatrix)); } m_ConstraintInequalityMatrix = new DenseMatrix(dimension, inequalityMatrix.Count / dimension, inequalityMatrix.ToArray()); m_ConstraintInequalityVector = inequalityVector.ToArray(); } if ((equalityMatrix != null) && (equalityMatrix.Count > 0)) { if ((equalityMatrix.Count % dimension) != 0) { throw new ArgumentException(nameof(equalityMatrix)); } m_ConstraintEqualityMatrix = new DenseMatrix(dimension, equalityMatrix.Count / dimension, equalityMatrix.ToArray()); m_ConstraintEqualityVector = equalityVector.ToArray(); } }
/// <summary>Initializes a new instance of the <see cref="Algorithm"/> class. /// </summary> /// <param name="optimizer">The <see cref="GoldfarbIdanaQuadraticProgram"/> object that serves as factory of the current object.</param> /// <param name="dimension">The dimension of the feasible region.</param> internal Algorithm(GoldfarbIdanaQuadraticProgram optimizer, int dimension) { m_Optimizer = optimizer ?? throw new ArgumentNullException(nameof(optimizer)); if (dimension <= 0) { throw new ArgumentOutOfRangeException(nameof(dimension)); } Dimension = dimension; m_dVector = new double[dimension]; m_zVector = new double[dimension]; m_MatrixR = new double[dimension * dimension]; m_MatrixJ = new double[dimension][]; m_nPlusVector = new double[dimension]; m_TempArgMin = new double[dimension]; for (int j = 0; j < dimension; j++) { m_MatrixJ[j] = new double[dimension]; } m_CopyOfMatrixA = new double[dimension * dimension]; }