Skip to content

Commit 6a8635a

Browse files
committed
Improved test coverage.
1 parent 18c1787 commit 6a8635a

File tree

6 files changed

+109
-66
lines changed

6 files changed

+109
-66
lines changed

hipparchus-optim/src/main/java/org/hipparchus/optim/nonlinear/vector/constrained/Constraint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface Constraint extends VectorDifferentiableFunction, OptimizationDa
3636

3737
/** Check how much a point overshoots the constraint.
3838
* <p>
39-
* The overshoots is zero if the point fulfills the constraint, and
39+
* The overshoot is zero if the point fulfills the constraint, and
4040
* positive if the {@link #value(RealVector) value} of the constraint
4141
* is on the wrong side of {@link #getLowerBound() lower} or {@link
4242
* #getUpperBound() upper} boundaries.

hipparchus-optim/src/main/java/org/hipparchus/optim/nonlinear/vector/constrained/LineSearch.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ public LineSearch(final double eps, final int maxHistory, final double mu, final
100100
this.maxBadSteps = maxBadSteps;
101101
this.history = new LinkedList<>();
102102
this.nonMonotoneEnabled = false;
103-
this.monotoneFailures = 0;
104-
this.badStepCount = 0;
105-
this.badStepDetected = false;
106-
this.badStepFailed = false;
103+
resetBadStepCount();
107104
}
108105

109106
/** Check if bad step has been detected.
@@ -113,8 +110,8 @@ public boolean isBadStepDetected() {
113110
return badStepDetected;
114111
}
115112

116-
/** Check if too many conscutive bad step have been detected.
117-
* @return true if too many conscutive bad step have been detected
113+
/** Check if too many consecutive bad step have been detected.
114+
* @return true if too many consecutive bad step have been detected
118115
*/
119116
public boolean isBadStepFailed() {
120117
return badStepFailed;
@@ -132,7 +129,6 @@ public int getIteration() {
132129
public void resetBadStepCount() {
133130
badStepCount = 0;
134131
monotoneFailures = 0;
135-
136132
badStepDetected = false;
137133
badStepFailed = false;
138134
}
@@ -149,12 +145,12 @@ public void updateHistory(final double fx) {
149145
}
150146
}
151147

152-
/** Verify Armjo condition for accept step.
148+
/** Verify Armijo condition for accept step.
153149
* @param fxNew penalty at candidate point x+dx*alpha
154150
* @param fxCurrent penalty at the current point
155151
* @param alpha step length
156152
* @param directionalDeriv penalty gradient
157-
* @return true o false
153+
* @return true or false
158154
*/
159155
public boolean acceptStep(double fxNew, double fxCurrent, double alpha, double directionalDeriv) {
160156
double ref = fxCurrent;
@@ -163,7 +159,7 @@ public boolean acceptStep(double fxNew, double fxCurrent, double alpha, double d
163159
ref = FastMath.max(ref, v);
164160
}
165161
}
166-
// alfaPenalty - currentPenalty) > getSettings().getMu() * alpha * currentPenaltyGrad
162+
// alfaPenalty - currentPenalty > getSettings().getMu() * alpha * currentPenaltyGrad
167163
return fxNew < ref + sigma * alpha * directionalDeriv;
168164
}
169165

@@ -250,7 +246,7 @@ public double search(final MeritFunctionL2 f) {
250246

251247
}
252248

253-
/** Iterativ search (either monotone or non-monotone).
249+
/** Iterative search (either monotone or non-monotone).
254250
* @param f penalty function
255251
* @param fxCurrent penalty function at current point
256252
* @param directionalDeriv directional derivative

hipparchus-optim/src/main/java/org/hipparchus/optim/nonlinear/vector/constrained/MeritFunctionL2.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ public double value(double alpha) {
223223
RealVector yi = yAlpha.getSubVector(me, mi);
224224

225225
RealVector yk = yAlpha.getSubVector(me, mi);
226-
//RealVector yk = y.getSubVector(me, mi);
227226

228227
iqEval = iqConstraint.value(xAlpha);
229228
RealVector gk = iqEval.subtract(iqConstraint.getLowerBound());
@@ -305,7 +304,7 @@ public RealVector gradY() {
305304
RealVector partial = new ArrayRealVector(y.getDimension());
306305
if (eqConstraint != null) {
307306
me = eqConstraint.dimY();
308-
RealVector g = this.eqEval.subtract(eqConstraint.getLowerBound());
307+
RealVector g = this.eqEval.subtract(eqConstraint.getLowerBound());
309308
partial.setSubVector(0, g.mapMultiply(-1.0));
310309
}
311310

@@ -330,7 +329,7 @@ public RealVector gradY() {
330329
}
331330

332331
/**
333-
* Update Weight Vector Rj.
332+
* Update weight vector Rj.
334333
* called after QP solution before update the penalty function
335334
* @param H hessina Matrix (updated after line search)
336335
* @param newY last estimate of multiplier (updated after line search)
@@ -340,7 +339,7 @@ public RealVector gradY() {
340339
* @param iterations current iteration
341340
*/
342341
public void updateRj(RealMatrix H, RealVector newY, RealVector newDx, RealVector newU, double sigmaValue, int iterations) {
343-
//calculate sigma vector that depends on iterations
342+
// calculate sigma vector that depends on iterations
344343
if (newY.getDimension() == 0) {
345344
return;
346345
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
20+
import org.hipparchus.linear.ArrayRealVector;
21+
import org.hipparchus.linear.RealVector;
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.Test;
24+
25+
class LinearSearchTest {
26+
27+
@Test
28+
public void testMonotoneSuccess() {
29+
final LineSearch ls = new LineSearch(1.0e-7, 5, 1.0e-4, 0.5, 50, 2);
30+
double d = ls.search(buildMeritFunction(0.99, 0.8));
31+
Assertions.assertEquals(0.125, d, 1.0e-3);
32+
Assertions.assertFalse(ls.isBadStepDetected());
33+
Assertions.assertFalse(ls.isBadStepFailed());
34+
}
35+
36+
@Test
37+
public void testBadStep() {
38+
final LineSearch ls = new LineSearch(1.0e-7, 5, 1.0e-4, 0.5, 50, 2);
39+
double d = ls.search(buildMeritFunction(-2.0, -0.5));
40+
Assertions.assertEquals(7.071e-7, d, 1.0e-10);
41+
Assertions.assertTrue(ls.isBadStepDetected());
42+
Assertions.assertFalse(ls.isBadStepFailed());
43+
}
44+
45+
private MeritFunctionL2 buildMeritFunction(final double startX, final double startY) {
46+
final TwiceDifferentiableFunction objective = new RosenbrockFunction();
47+
final InequalityConstraint iqConstraint =
48+
new LinearInequalityConstraint(new double[][] {
49+
{ 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 }
50+
}, new double[] {
51+
-1.5, -1.5, -1.5, -1.5
52+
});
53+
final RealVector x = new ArrayRealVector(new double[] { startX, startY });
54+
final MeritFunctionL2 f = new MeritFunctionL2(objective, null, iqConstraint, x);
55+
f.update(objective.gradient(x),
56+
null,
57+
iqConstraint.gradient(x.toArray()),
58+
x,
59+
new ArrayRealVector(iqConstraint.dimY()),
60+
new ArrayRealVector(new double[] { -1.0, 1.0 }),
61+
new ArrayRealVector(new double[4]));
62+
return f;
63+
}
64+
65+
}

hipparchus-optim/src/test/java/org/hipparchus/optim/nonlinear/vector/constrained/RosenbrookConstraint.java

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,65 +18,49 @@
1818

1919
import org.hipparchus.linear.Array2DRowRealMatrix;
2020
import org.hipparchus.linear.ArrayRealVector;
21+
import org.hipparchus.linear.MatrixUtils;
2122
import org.hipparchus.linear.RealMatrix;
2223
import org.hipparchus.linear.RealVector;
2324

24-
public class RosenbrookConstraint extends LinearInequalityConstraint {
25+
public class RosenbrookConstraint extends InequalityConstraint {
2526

26-
public RosenbrookConstraint(RealMatrix m, RealVector b) {
27-
super(m, b);
27+
public RosenbrookConstraint(final double[] b) {
28+
super(MatrixUtils.createRealVector(b));
2829
}
2930

3031
@Override
31-
public RealVector value(RealVector x) {
32-
RealVector a=new ArrayRealVector(5);
33-
a.setEntry(0,-x.getEntry(0)*x.getEntry(0)-x.getEntry(1)*x.getEntry(1));
34-
a.setEntry(1,x.getEntry(0));
35-
a.setEntry(2,-x.getEntry(0));
36-
a.setEntry(3,x.getEntry(1));
37-
a.setEntry(4,-x.getEntry(1));
38-
39-
return a;
32+
public RealVector value(final RealVector x) {
33+
final double x0 = x.getEntry(0);
34+
final double x1 = x.getEntry(1);
35+
final RealVector a = new ArrayRealVector(5);
36+
a.setEntry(0, -x0 * x0 - x1 * x1);
37+
a.setEntry(1, x0);
38+
a.setEntry(2, -x0);
39+
a.setEntry(3, x1);
40+
a.setEntry(4, -x1);
41+
return a;
4042
}
4143

4244
@Override
43-
public RealMatrix jacobian(RealVector x) {
44-
RealMatrix a= new Array2DRowRealMatrix(5,2);
45-
a.setEntry(0, 0,-2*x.getEntry(0));
46-
a.setEntry(0, 1,-2*x.getEntry(1));
47-
a.setEntry(1, 0,1);
48-
a.setEntry(2, 0,-1);
49-
a.setEntry(3, 1,1);
50-
a.setEntry(4, 1,-1);
51-
return a;
45+
public RealMatrix jacobian(final RealVector x) {
46+
final RealMatrix a = new Array2DRowRealMatrix(5, 2);
47+
a.setEntry(0, 0, -2 * x.getEntry(0));
48+
a.setEntry(0, 1, -2 * x.getEntry(1));
49+
a.setEntry(1, 0, 1);
50+
a.setEntry(2, 0, -1);
51+
a.setEntry(3, 1, 1);
52+
a.setEntry(4, 1, -1);
53+
return a;
5254
}
53-
//
54-
// @Override
55-
// public RealVector getLowerBound()
56-
// { RealVector lb=new ArrayRealVector(9);
57-
// lb.setEntry(0,25.0);
58-
// lb.setEntry(1,1.0);
59-
// lb.setEntry(2,1.0);
60-
// lb.setEntry(3,1.0);
61-
// lb.setEntry(4,1.0);
62-
// lb.setEntry(5,-5.0);
63-
// lb.setEntry(6,-5.0);
64-
// lb.setEntry(7,-5.0);
65-
// lb.setEntry(8,-5.0);
66-
// return lb;
67-
// }
68-
//
69-
// /**
70-
// * Return Upper Bound .
71-
// * @return Upper Bound Vector
72-
// */
73-
// @Override
74-
// public RealVector getUpperBound()
75-
// {
76-
// return new ArrayRealVector(9,Double.POSITIVE_INFINITY);
77-
// }
78-
// @Override
79-
public int dimY(){
55+
56+
@Override
57+
public int dim(){
58+
return 2;
59+
}
60+
61+
@Override
62+
public int dimY(){
8063
return 5;
8164
}
65+
8266
}

hipparchus-optim/src/test/java/org/hipparchus/optim/nonlinear/vector/constrained/SQPOptimizerSTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ void testRosenbrock() {
106106
0.0, 1.6e-13,
107107
new ObjectiveFunction(new RosenbrockFunction()),
108108
new double[] { 2, 2 },
109-
new RosenbrookConstraint(MatrixUtils.createRealMatrix(5, 2),
110-
MatrixUtils.createRealVector(new double[]{ -2, -1.5, -1.5, -1.5, -1.5 })));
109+
new RosenbrookConstraint(new double[]{ -2, -1.5, -1.5, -1.5, -1.5 }));
111110
}
112111

113112
@Test

0 commit comments

Comments
 (0)