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.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
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
39 final RealVector actualVector = testSQPOptimizer.lagrangianGradX(expectedVector, null,null, null);
40
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
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
54 testSQPOptimizer.parseOptimizationData(objectiveFunction, expectedOptions);
55
56 Assert.assertEquals(expectedOptions, testSQPOptimizer.getSettings());
57 }
58
59 @Test
60 public void testParseOptimizationDataException() {
61
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
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
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
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 }