View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.Assert;
24  import org.junit.Test;
25  
26  public class SQPOptimizerSTest extends AbstractTestAbstractSQPOptimizerTest {
27  
28      protected ConstraintOptimizer buildOptimizer() {
29          return new SQPOptimizerS();
30      }
31  
32      @Test
33      public void test2() {
34          QuadraticFunction q = new QuadraticFunction(new double[][] { { 6.0, 2.0 }, { 2.0, 8.0 } },
35                                                      new double[] { 5.0, 1.0 },
36                                                      0.0);
37  
38          // constraint x + 2y = 1
39          LinearEqualityConstraint eqc = new LinearEqualityConstraint(new double[][] { { 1.0, 2.0 } },
40                                                                      new double[] { 1.0 });
41  
42          // x > 0, y > 0
43          LinearInequalityConstraint ineqc = new LinearInequalityConstraint(new double[][] { { 1.0, 0.0 }, { 0.0, 1.0 } },
44                                                                            new double[] { 0.0, 0.0 });
45  
46  
47          doTestProblem(new double[] {  0, 0.5 },     1.9e-15,
48                        new double[] { 2.5, 3.5, 0 }, 7.8e-5,
49                        1.5, 4.0e-15,
50                        new ObjectiveFunction(q),
51                        new double[] { 10.5, 10.5 },
52                        eqc, ineqc);
53  
54      }
55  
56      @Test
57      public void testHockShittkowski71() {
58          doTestProblem(new double[] { 1, 4.74293167, 3.82123882, 1.37939596 }, 1.0e-8,
59                        new double[] { -0.16145839, 0.55229016, 1.08782965, 0, 0, 0, 0, 0, 0, 0 }, 1.1e-8,
60                        17.01401698, 1.0e-8,
61                        new ObjectiveFunction(new HockSchittkowskiFunction71()),
62                        new double[] { 1, 5, 5, 1 },
63                        new HockSchittkowskiConstraintInequality71(),
64                        new HockSchittkowskiConstraintEquality71());
65      }
66  
67      @Test
68      public void testHockShittkowski72() {
69          doTestProblem(new double[] { 193.12529425, 180.14766487, 184.58883790, 168.82104861 }, 1.1e-8,
70                        new double[] { 7693.73706410, 41453.54250351 }, 1.1e-8,
71                        727.68284564, 1.0e-8,
72                        new ObjectiveFunction(new HockSchittkowskiFunction72()),
73                        new double[] { 1, 5, 5, 1 },
74                        new HockSchittkowskiConstraintInequality72());
75      }
76  
77      @Test
78      public void testHockShittkowski77() {
79          doTestProblem(new double[] { 1.16617194, 1.18211086, 1.38025671, 1.50603641, 0.61092012 }, 1.4e-8,
80                        new double[] { 0.08553981, 0.03187858 }, 1.0e-8,
81                        0.24150486, 1.0e-8,
82                        new ObjectiveFunction(new HockSchittkowskiFunction77()),
83                        new double[] { 2, 2, 2, 3, 3 },
84                        new HockSchittkowskiConstraintEquality77());
85      }
86  
87      @Test
88      public void testHockShittkowski78() {
89          doTestProblem(new double[] { -1.71714365, 1.59570987, 1.82724583, 0.76364341, 0.76364341 }, 1.4e-8,
90                        new double[] { -0.74445225, 0.70358075, -0.09680628 }, 1.0e-8,
91                        -2.91970350, 1.0e-8,
92                        new ObjectiveFunction(new HockSchittkowskiFunction78()),
93                        new double[] { -2, 1.5, 2, 1, 1 },
94                        new HockSchittkowskiConstraintEquality78());
95      }
96  
97      @Test
98      public void testRosenbrock() {
99          doTestProblem(new double[] { 1, 1 }, 1.5e-7,
100                       new double[] { 0, 0, 0, 0, 0}, 1.0e-15,
101                       0.0, 3.4e-15,
102                       new ObjectiveFunction(new RosenbrockFunction()),
103                       new double[] { 2, 2 },
104                       new RosenbrookConstraint(MatrixUtils.createRealMatrix(5, 2),
105                                                MatrixUtils.createRealVector(new double[]{ -2, -1.5, -1.5, -1.5, -1.5 })));
106     }
107 
108     @Test
109     public void testLowMaxLineSearchAndConvergenceCriterion0() {
110         // GIVEN
111         final OptimizationData[] data = createOptimizationData();
112         final SQPOption option = new SQPOption();
113         option.setMaxLineSearchIteration(2);
114         option.setConvCriteria(0);
115         data[data.length - 1] = option;
116 
117         // WHEN
118         final SQPOptimizerS optimizer = new SQPOptimizerS();
119         optimizer.parseOptimizationData(data);
120         final LagrangeSolution    solution  = optimizer.optimize(data);
121 
122         // THEN
123         final double[] expectedSolution = new double[] { 1, 1 };
124         Assert.assertEquals(0.0,
125                 MatrixUtils.createRealVector(expectedSolution).subtract(solution.getX()).getL1Norm(), 2.5e-5);
126         Assert.assertEquals(8., solution.getValue(), 2e-4);
127     }
128 
129     private OptimizationData[] createOptimizationData() {
130         final QuadraticFunction q = new QuadraticFunction(new double[][] { { 4.0, -2.0 }, { -2.0, 4.0 } },
131                 new double[] { 6.0, 0.0 },
132                 0.0);
133         // y = 1
134         final LinearEqualityConstraint eqc = new LinearEqualityConstraint(new double[][] { { 0.0, 1.0 } },
135                 new double[] { 1.0 });
136         // x > 0, y > 0, x + y > 2
137         final LinearInequalityConstraint ineqc = new LinearInequalityConstraint(new double[][] { { 1.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 1.0 } },
138                 new double[] { 0.0, 0.0, 2.0 });
139 
140         final OptimizationData[] constraints = new OptimizationData[] { eqc, ineqc };
141         final OptimizationData[] data = new OptimizationData[constraints.length + 3];
142         final ObjectiveFunction objectiveFunction =  new ObjectiveFunction(q);
143         data[0] = objectiveFunction;
144         System.arraycopy(constraints, 0, data, 1, constraints.length);
145         final double[] initialGuess = new double[] { -3.5, 3.5 };
146         data[data.length - 2] = new InitialGuess(initialGuess);
147         return data;
148     }
149 
150 }