1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.optim.nonlinear.vector.constrained;
18
19 import org.hipparchus.linear.MatrixUtils;
20 import org.hipparchus.optim.InitialGuess;
21 import org.hipparchus.optim.OptimizationData;
22 import org.hipparchus.optim.nonlinear.scalar.ObjectiveFunction;
23 import org.junit.jupiter.api.Test;
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26
27 class SQPOptimizerSTest extends AbstractTestAbstractSQPOptimizerTest {
28
29 protected ConstraintOptimizer buildOptimizer() {
30 return new SQPOptimizerS();
31 }
32
33 @Test
34 void test2() {
35 QuadraticFunction q = new QuadraticFunction(new double[][] { { 6.0, 2.0 }, { 2.0, 8.0 } },
36 new double[] { 5.0, 1.0 },
37 0.0);
38
39
40 LinearEqualityConstraint eqc = new LinearEqualityConstraint(new double[][] { { 1.0, 2.0 } },
41 new double[] { 1.0 });
42
43
44 LinearInequalityConstraint ineqc = new LinearInequalityConstraint(new double[][] { { 1.0, 0.0 }, { 0.0, 1.0 } },
45 new double[] { 0.0, 0.0 });
46
47
48 doTestProblem(new double[] { 0, 0.5 }, 1.9e-15,
49 new double[] { 2.5, 3.5, 0 }, 7.8e-5,
50 1.5, 4.0e-15,
51 new ObjectiveFunction(q),
52 new double[] { 10.5, 10.5 },
53 eqc, ineqc);
54
55 }
56
57 @Test
58 void testHockShittkowski71() {
59 doTestProblem(new double[] { 1, 4.74293167, 3.82123882, 1.37939596 }, 1.0e-8,
60 new double[] { -0.16145839, 0.55229016, 1.08782965, 0, 0, 0, 0, 0, 0, 0 }, 1.1e-8,
61 17.01401698, 1.0e-8,
62 new ObjectiveFunction(new HockSchittkowskiFunction71()),
63 new double[] { 1, 5, 5, 1 },
64 new HockSchittkowskiConstraintInequality71(),
65 new HockSchittkowskiConstraintEquality71());
66 }
67
68 @Test
69 void testHockShittkowski72() {
70 doTestProblem(new double[] { 193.12529425, 180.14766487, 184.58883790, 168.82104861 }, 1.1e-8,
71 new double[] { 7693.73706410, 41453.54250351 }, 1.1e-8,
72 727.68284564, 1.0e-8,
73 new ObjectiveFunction(new HockSchittkowskiFunction72()),
74 new double[] { 1, 5, 5, 1 },
75 new HockSchittkowskiConstraintInequality72());
76 }
77
78 @Test
79 void testHockShittkowski77() {
80 doTestProblem(new double[] { 1.16617194, 1.18211086, 1.38025671, 1.50603641, 0.61092012 }, 1.4e-8,
81 new double[] { 0.08553981, 0.03187858 }, 1.0e-8,
82 0.24150486, 1.0e-8,
83 new ObjectiveFunction(new HockSchittkowskiFunction77()),
84 new double[] { 2, 2, 2, 3, 3 },
85 new HockSchittkowskiConstraintEquality77());
86 }
87
88 @Test
89 void testHockShittkowski78() {
90 doTestProblem(new double[] { -1.71714365, 1.59570987, 1.82724583, 0.76364341, 0.76364341 }, 1.4e-8,
91 new double[] { -0.74445225, 0.70358075, -0.09680628 }, 1.0e-8,
92 -2.91970350, 1.0e-8,
93 new ObjectiveFunction(new HockSchittkowskiFunction78()),
94 new double[] { -2, 1.5, 2, 1, 1 },
95 new HockSchittkowskiConstraintEquality78());
96 }
97
98 @Test
99 void testRosenbrock() {
100 doTestProblem(new double[] { 1, 1 }, 1.5e-7,
101 new double[] { 0, 0, 0, 0, 0}, 1.0e-15,
102 0.0, 3.4e-15,
103 new ObjectiveFunction(new RosenbrockFunction()),
104 new double[] { 2, 2 },
105 new RosenbrookConstraint(MatrixUtils.createRealMatrix(5, 2),
106 MatrixUtils.createRealVector(new double[]{ -2, -1.5, -1.5, -1.5, -1.5 })));
107 }
108
109 @Test
110 void testLowMaxLineSearchAndConvergenceCriterion0() {
111
112 final OptimizationData[] data = createOptimizationData();
113 final SQPOption option = new SQPOption();
114 option.setMaxLineSearchIteration(2);
115 option.setConvCriteria(0);
116 data[data.length - 1] = option;
117
118
119 final SQPOptimizerS optimizer = new SQPOptimizerS();
120 optimizer.parseOptimizationData(data);
121 final LagrangeSolution solution = optimizer.optimize(data);
122
123
124 final double[] expectedSolution = new double[] { 1, 1 };
125 assertEquals(0.0,
126 MatrixUtils.createRealVector(expectedSolution).subtract(solution.getX()).getL1Norm(), 2.5e-5);
127 assertEquals(8., solution.getValue(), 2e-4);
128 }
129
130 private OptimizationData[] createOptimizationData() {
131 final QuadraticFunction q = new QuadraticFunction(new double[][] { { 4.0, -2.0 }, { -2.0, 4.0 } },
132 new double[] { 6.0, 0.0 },
133 0.0);
134
135 final LinearEqualityConstraint eqc = new LinearEqualityConstraint(new double[][] { { 0.0, 1.0 } },
136 new double[] { 1.0 });
137
138 final LinearInequalityConstraint ineqc = new LinearInequalityConstraint(new double[][] { { 1.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 1.0 } },
139 new double[] { 0.0, 0.0, 2.0 });
140
141 final OptimizationData[] constraints = new OptimizationData[] { eqc, ineqc };
142 final OptimizationData[] data = new OptimizationData[constraints.length + 3];
143 final ObjectiveFunction objectiveFunction = new ObjectiveFunction(q);
144 data[0] = objectiveFunction;
145 System.arraycopy(constraints, 0, data, 1, constraints.length);
146 final double[] initialGuess = new double[] { -3.5, 3.5 };
147 data[data.length - 2] = new InitialGuess(initialGuess);
148 return data;
149 }
150
151 }