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.exception.LocalizedCoreFormats;
20  import org.hipparchus.exception.MathIllegalArgumentException;
21  import org.hipparchus.linear.MatrixUtils;
22  import org.hipparchus.linear.RealMatrix;
23  import org.hipparchus.linear.RealVector;
24  import org.hipparchus.optim.nonlinear.scalar.ObjectiveFunction;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  public class AbstractSQPOptimizerTest {
29  
30      @Test
31      public void testLagrangianGradX() {
32          // GIVEN
33          final TestSQPOptimizer testSQPOptimizer = new TestSQPOptimizer();
34          final TwiceDifferentiableFunction multivariateFunction = new RosenbrockFunction();
35          final ObjectiveFunction objectiveFunction = new ObjectiveFunction(multivariateFunction);
36          testSQPOptimizer.parseOptimizationData(objectiveFunction);
37          final RealVector expectedVector = MatrixUtils.createRealVector(new double[] { 1 });
38          // WHEN
39          final RealVector actualVector = testSQPOptimizer.lagrangianGradX(expectedVector, null,null, null);
40          // THEN
41          for (int i = 0; i < expectedVector.getDimension(); i++) {
42              Assert.assertEquals(expectedVector.getEntry(i), actualVector.getEntry(i), 0);
43          }
44      }
45  
46      @Test
47      public void testParseOptimizationData() {
48          // GIVEN
49          final TestSQPOptimizer testSQPOptimizer = new TestSQPOptimizer();
50          final SQPOption expectedOptions = new SQPOption();
51          final TwiceDifferentiableFunction multivariateFunction = new RosenbrockFunction();
52          final ObjectiveFunction objectiveFunction = new ObjectiveFunction(multivariateFunction);
53          // WHEN
54          testSQPOptimizer.parseOptimizationData(objectiveFunction, expectedOptions);
55          // THEN
56          Assert.assertEquals(expectedOptions, testSQPOptimizer.getSettings());
57      }
58  
59      @Test
60      public void testParseOptimizationDataException() {
61          // GIVEN
62          final TestSQPOptimizer testSQPOptimizer = new TestSQPOptimizer();
63          final EqualityConstraint equalityConstraint = new TestEqualityConstraints(100000);
64          final TwiceDifferentiableFunction multivariateFunction = new RosenbrockFunction();
65          final ObjectiveFunction objectiveFunction = new ObjectiveFunction(multivariateFunction);
66          // WHEN
67          try {
68              testSQPOptimizer.parseOptimizationData(objectiveFunction, equalityConstraint);
69              Assert.fail();
70          } catch (final MathIllegalArgumentException exception) {
71              Assert.assertEquals("rank of constraints must be lesser than domain dimension, but 100,000 >= 2",
72                      exception.getMessage());
73          }
74      }
75  
76      @Test
77      public void testParseOptimizationDataException2() {
78          // GIVEN
79          final TestSQPOptimizer testSQPOptimizer = new TestSQPOptimizer();
80          final EqualityConstraint equalityConstraint = new TestEqualityConstraints(0);
81          final TwiceDifferentiableFunction multivariateFunction = new RosenbrockFunction();
82          final ObjectiveFunction objectiveFunction = new ObjectiveFunction(multivariateFunction);
83          // WHEN
84          try {
85              testSQPOptimizer.parseOptimizationData(objectiveFunction, equalityConstraint);
86              Assert.fail();
87          } catch (final MathIllegalArgumentException exception) {
88              Assert.assertEquals(LocalizedCoreFormats.ZERO_NOT_ALLOWED.getSourceString(),
89                      exception.getMessage());
90          }
91      }
92  
93      private static class TestSQPOptimizer extends AbstractSQPOptimizer {
94  
95          TestSQPOptimizer() {
96              super();
97          }
98  
99          @Override
100         protected LagrangeSolution doOptimize() {
101             return null;
102         }
103     }
104 
105     private static class TestEqualityConstraints extends EqualityConstraint {
106 
107         private final int dimension;
108 
109         TestEqualityConstraints(final int dimension) {
110             super(MatrixUtils.createRealVector(new double[dimension]));
111             this.dimension = dimension;
112         }
113 
114         @Override
115         public int dim() {
116             return dimension;
117         }
118 
119         @Override
120         public RealVector value(RealVector x) {
121             return null;
122         }
123 
124         @Override
125         public RealMatrix jacobian(RealVector x) {
126             return null;
127         }
128     }
129 
130 }