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.linear.RealVector;
21 import org.hipparchus.util.FastMath;
22 import org.hipparchus.util.MathUtils;
23
24
25
26
27 public abstract class BoundedConstraint implements Constraint {
28
29
30 private final RealVector lower;
31
32
33 private final RealVector upper;
34
35
36
37
38
39
40
41
42
43 public BoundedConstraint(final RealVector lower, final RealVector upper) {
44
45
46 if (lower == null) {
47 MathUtils.checkNotNull(upper);
48 this.lower = MatrixUtils.createRealVector(upper.getDimension());
49 this.lower.set(Double.NEGATIVE_INFINITY);
50 } else {
51 this.lower = lower;
52 }
53
54
55 if (upper == null) {
56 this.upper = MatrixUtils.createRealVector(lower.getDimension());
57 this.upper.set(Double.POSITIVE_INFINITY);
58 } else {
59 this.upper = upper;
60 }
61
62
63 MathUtils.checkDimension(this.lower.getDimension(), this.upper.getDimension());
64
65 }
66
67
68 @Override
69 public int dimY() {
70 return lower.getDimension();
71 }
72
73
74 @Override
75 public RealVector getLowerBound() {
76 return lower;
77 }
78
79
80 @Override
81 public RealVector getUpperBound() {
82 return upper;
83 }
84
85
86 @Override
87 public double overshoot(final RealVector y) {
88
89 double overshoot = 0;
90 for (int i = 0; i < y.getDimension(); ++i) {
91 overshoot += FastMath.max(0, lower.getEntry(i) - y.getEntry(i));
92 overshoot += FastMath.max(0, y.getEntry(i) - upper.getEntry(i));
93 }
94
95 return overshoot;
96
97 }
98
99 }