Skip to content

Commit

Permalink
Fixed PMD and Spotbugs warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
maisonobe committed Mar 26, 2024
1 parent 8a5c6bd commit 5badc42
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 192 deletions.
1 change: 1 addition & 0 deletions hipparchus-core/src/conf/spotbugs-exclude-filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@
<Class name="org.hipparchus.random.SynchronizedRandomGenerator"/>
<Class name="org.hipparchus.random.UniformRandomGenerator"/>
<Class name="org.hipparchus.random.UnitSphereRandomVectorGenerator"/>
<Class name="org.hipparchus.random.GaussMarkovGenerator"/>
<Class name="org.hipparchus.special.elliptic.jacobi.FieldJacobiTheta"/>
</Or>
<Method name="&lt;init>"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ public FieldGradient<T> hypot(final FieldGradient<T> y) {
* @param g1 first derivative of the function at the current point (i.e. at {@code g'(getValue())})
* @return g(this)
*/
@Override
public FieldGradient<T> compose(final T g0, final T g1) {
final FieldGradient<T> result = newInstance(g0);
for (int i = 0; i < grad.length; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ public FieldUnivariateDerivative1<T> hypot(final FieldUnivariateDerivative1<T> y
* @param g1 first derivative of the function at the current point (i.e. at {@code g'(getValue())})
* @return g(this)
*/
@Override
public FieldUnivariateDerivative1<T> compose(final T g0, final T g1) {
return new FieldUnivariateDerivative1<>(g0, g1.multiply(f1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public SparseGradient withValue(final double v) {
* @return a new instance
*/
public static SparseGradient createConstant(final double value) {
return new SparseGradient(value, Collections.<Integer, Double> emptyMap());
return new SparseGradient(value, Collections.emptyMap());
}

/** Factory method creating an independent variable.
Expand Down Expand Up @@ -152,6 +152,7 @@ public double getDerivative(final int index) {
* Get the value of the function.
* @return value of the function.
*/
@Override
public double getValue() {
return value;
}
Expand Down Expand Up @@ -218,7 +219,7 @@ public SparseGradient subtract(final SparseGradient a) {
@Override
public SparseGradient multiply(final SparseGradient a) {
final SparseGradient out =
new SparseGradient(value * a.value, Collections.<Integer, Double> emptyMap());
new SparseGradient(value * a.value, Collections.emptyMap());

// Derivatives.
for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
Expand Down Expand Up @@ -280,7 +281,7 @@ public SparseGradient multiply(final int n) {
/** {@inheritDoc} */
@Override
public SparseGradient divide(final SparseGradient a) {
final SparseGradient out = new SparseGradient(value / a.value, Collections.<Integer, Double> emptyMap());
final SparseGradient out = new SparseGradient(value / a.value, Collections.emptyMap());

// Derivatives.
for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
Expand Down Expand Up @@ -435,7 +436,7 @@ public SparseGradient copySign(final double sign) {
/** {@inheritDoc} */
@Override
public SparseGradient scalb(final int n) {
final SparseGradient out = new SparseGradient(FastMath.scalb(value, n), Collections.<Integer, Double> emptyMap());
final SparseGradient out = new SparseGradient(FastMath.scalb(value, n), Collections.emptyMap());
for (Map.Entry<Integer, Double> entry : derivatives.entrySet()) {
out.derivatives.put(entry.getKey(), FastMath.scalb(entry.getValue(), n));
}
Expand Down Expand Up @@ -611,6 +612,7 @@ public double taylor(final double ... delta) {
* @exception MathIllegalArgumentException if the number of elements
* in the array is not equal to 2 (i.e. value and first derivative)
*/
@Override
public SparseGradient compose(final double... f) {
MathUtils.checkDimension(f.length, 2);
return compose(f[0], f[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public <T extends CalculusFieldElement<T>> T value(final T x, final T y)
*/
public boolean isValidPoint(double x,
double y) {
return !(x < xval[0]) && !(x > xval[xval.length - 1]) && !(y < yval[0]) && !(y > yval[yval.length - 1]);
return x >= xval[0] && x <= xval[xval.length - 1] && y >= yval[0] && y <= yval[yval.length - 1];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,6 @@ public double[] getKnots() {
* @return {@code true} if {@code x} is a valid point.
*/
public boolean isValidPoint(double x) {
return !(x < knots[0]) && !(x > knots[n]);
return x >= knots[0] && x <= knots[n];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,7 @@ private static BigInteger lcm(final BigInteger i0, final BigInteger i1) {
BigInteger a = i0.abs();
BigInteger b = i1.abs();
BigInteger gcd = i0.gcd(b);
BigInteger lcm = (a.multiply(b)).divide(gcd);
return lcm;
return (a.multiply(b)).divide(gcd);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Licensed to the Hipparchus project under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The Hipparchus project licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

0.05 1019.28514031432 4.19892612568951 89.9849853443891 1026.57855317649 100.258471325986 4.99992376451156 -0.372790425959169 100.009996198111 -0.0185911842189891 9.09938047660586
0.10 1003.64456445668 8.11769294928251 89.9053600341848 1015.61489076285 101.006331345204 9.99875593985759 -0.603337707126964 100.019767447146 -0.0500880581381564 4.77255476316153
0.15 1004.37512895945 12.1026224146043 89.8007116058457 1011.9454825604 102.236446261076 14.9936315467677 -0.820181035401379 100.028513982531 -0.0940397295724976 3.24142675739822
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ public static RotationOrder getRotationOrder(final String value) {
return RotationOrder.valueOf(value);
} catch (IllegalArgumentException iae) {
// Invalid value. An exception is thrown
throw new MathIllegalStateException(LocalizedGeometryFormats.INVALID_ROTATION_ORDER_NAME, value);
throw new MathIllegalStateException(iae,
LocalizedGeometryFormats.INVALID_ROTATION_ORDER_NAME,
value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.hipparchus.Field;
import org.hipparchus.ode.FieldExpandableODE;
import org.hipparchus.ode.FieldODEIntegrator;
import org.hipparchus.ode.FieldODEState;
import org.hipparchus.ode.FieldOrdinaryDifferentialEquation;
import org.hipparchus.util.MathArrays;

Expand Down Expand Up @@ -118,7 +117,7 @@ default int getNumberOfStages() {
* so it can be embedded in outer loops.</p>
* <p>
* This method is <em>not</em> used at all by the {@link #integrate(FieldExpandableODE,
* FieldODEState, CalculusFieldElement)} method. It also completely ignores the step set at
* org.hipparchus.ode.FieldODEState, CalculusFieldElement)} method. It also completely ignores the step set at
* construction time, and uses only a single step to go from {@code t0} to {@code t}.
* </p>
* <p>
Expand Down
55 changes: 55 additions & 0 deletions hipparchus-optim/src/conf/spotbugs-exclude-filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,66 @@
<Class name="org.hipparchus.optim.linear.LinearObjectiveFunction"/>
<Class name="org.hipparchus.optim.nonlinear.scalar.noderiv.CMAESOptimizer"/>
<Class name="org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresBuilder"/>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.ADMMQPKKT"/>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.ADMMQPModifiedRuizEquilibrium"/>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.ADMMQPSolution"/>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.BoundedConstraint"/>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.LagrangeSolution"/>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.LinearBoundedConstraint"/>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.LinearEqualityConstraint"/>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.LinearInequalityConstraint"/>
<Class name="org.hipparchus.optim.univariate.MultiStartUnivariateOptimizer"/>
</Or>
<Method name="&lt;init>"/>
<Bug pattern="EI_EXPOSE_REP2" />
</Match>
<Match>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.ADMMQPKKT"/>
<Method name="initialize"/>
<Bug pattern="EI_EXPOSE_REP2" />
</Match>
<Match>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.ADMMQPSolution"/>
<Or>
<Method name="getV"/>
<Method name="getZ"/>
</Or>
<Bug pattern="EI_EXPOSE_REP" />
</Match>
<Match>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.AbstractSQPOptimizer"/>
<Method name="getSettings"/>
<Bug pattern="EI_EXPOSE_REP" />
</Match>
<Match>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.BoundedConstraint"/>
<Or>
<Method name="getLowerBound"/>
<Method name="getUpperBound"/>
</Or>
<Bug pattern="EI_EXPOSE_REP" />
</Match>
<Match>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.LagrangeSolution"/>
<Or>
<Method name="getLambda"/>
<Method name="getX"/>
</Or>
<Bug pattern="EI_EXPOSE_REP" />
</Match>
<Match>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.LinearEqualityConstraint"/>
<Method name="getA"/>
<Bug pattern="EI_EXPOSE_REP" />
</Match>
<Match>
<Class name="org.hipparchus.optim.nonlinear.vector.constrained.QuadraticFunction"/>
<Or>
<Method name="getP"/>
<Method name="getQ"/>
</Or>
<Bug pattern="EI_EXPOSE_REP" />
</Match>

<!-- the following equality tests are part of the reference algorithms -->
<!-- which already know about limited precision of the double numbers -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public void normalize(double epsilon, int maxIteration) {
RealVector q1 = q.copy();
RealMatrix H1 = H.copy();
RealMatrix A1 = A.copy();
RealMatrix diagD = null;
RealMatrix diagE = null;
RealMatrix diagD;
RealMatrix diagE;
RealMatrix SD = MatrixUtils.createRealIdentityMatrix(H.getRowDimension());
RealMatrix SE = MatrixUtils.createRealIdentityMatrix(A.getRowDimension());
RealVector H1norm = new ArrayRealVector(H1.getColumnDimension());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class ADMMQPOptimizer extends QPOptimizer {
private QuadraticFunction function;

/** Problem solver. */
private ADMMQPKKT solver;
private final ADMMQPKKT solver;

/** Problem convergence checker. */
private ADMMQPConvergenceChecker checker;
Expand Down Expand Up @@ -124,7 +124,6 @@ protected void parseOptimizationData(OptimizationData... optData) {

if (data instanceof ADMMQPOption) {
settings = (ADMMQPOption) data;
continue;
}

}
Expand Down Expand Up @@ -205,7 +204,7 @@ public LagrangeSolution doOptimize() {
RealVector qw = q.copy();
RealVector ubw = ub.copy();
RealVector lbw = lb.copy();
RealVector x =null;
RealVector x;
if (getStartPoint() != null) {
x = new ArrayRealVector(getStartPoint());
} else {
Expand All @@ -214,7 +213,7 @@ public LagrangeSolution doOptimize() {

ADMMQPModifiedRuizEquilibrium dec = new ADMMQPModifiedRuizEquilibrium(H, A,q);

if (settings.getScaling()) {
if (settings.isScaling()) {
//
dec.normalize(settings.getEps(), settings.getScaleMaxIteration());
Hw = dec.getScaledH();
Expand All @@ -236,7 +235,7 @@ public LagrangeSolution doOptimize() {
solver.initialize(Hw, Aw, qw, me, lbw, ubw, rho, settings.getSigma(), settings.getAlpha());
RealVector xstar = null;
RealVector ystar = null;
RealVector zstar = null;
RealVector zstar;

while (iterations.getCount() <= iterations.getMaximalCount()) {
ADMMQPSolution sol = solver.iterate(x, y, z);
Expand All @@ -257,7 +256,7 @@ public LagrangeSolution doOptimize() {
}


if (settings.getScaling()) {
if (settings.isScaling()) {

xstar = dec.unscaleX(x);
ystar = dec.unscaleY(y);
Expand All @@ -284,28 +283,22 @@ public LagrangeSolution doOptimize() {

}



//SOLUTION POLISHING

ADMMQPSolution finalSol = null;
if (settings.getPolishing()) {
finalSol = polish(Hw, Aw, qw, lbw, ubw, x, y, z);
if (settings.getScaling()) {
if (settings.isPolishing()) {
ADMMQPSolution finalSol = polish(Hw, Aw, qw, lbw, ubw, x, y, z);
if (settings.isScaling()) {
xstar = dec.unscaleX(finalSol.getX());
ystar = dec.unscaleY(finalSol.getLambda());
zstar = dec.unscaleZ(finalSol.getZ());
} else {
xstar = finalSol.getX();
ystar = finalSol.getLambda();
zstar = finalSol.getZ();
}
}
for (int i = 0; i < me + mi; i++) {
ystar.setEntry(i,-ystar.getEntry(i));
ystar.setEntry(i, -ystar.getEntry(i));
}

return new LagrangeSolution(xstar,ystar,function.value(xstar));
return new LagrangeSolution(xstar, ystar, function.value(xstar));

}

Expand Down Expand Up @@ -355,17 +348,17 @@ private ADMMQPSolution polish(RealMatrix H, RealMatrix A, RealVector q, RealVect
}

}
RealMatrix Aactive = null;
RealVector lub = null;
RealMatrix Aactive;
RealVector lub;

RealVector ystar;
RealVector xstar = x.copy();
//!Aentry.isEmpty()
if (!Aentry.isEmpty()) {

Aactive = new Array2DRowRealMatrix(Aentry.toArray(new double[Aentry.size()][]));
lub = new ArrayRealVector(lubEntry.toArray(new Double[lubEntry.size()]));
ystar = new ArrayRealVector(yEntry.toArray(new Double[yEntry.size()]));
Aactive = new Array2DRowRealMatrix(Aentry.toArray(new double[0][]));
lub = new ArrayRealVector(lubEntry.toArray(new Double[0]));
ystar = new ArrayRealVector(yEntry.toArray(new Double[0]));
solver.initialize(H, Aactive, q, 0, lub, lub,
settings.getSigma(), settings.getSigma(), settings.getAlpha());

Expand Down Expand Up @@ -396,7 +389,7 @@ private ADMMQPSolution polish(RealMatrix H, RealMatrix A, RealVector q, RealVect
*/
private boolean manageRho(int me, double rp, double rd, double maxPrimal, double maxDual) {
boolean updated = false;
if (settings.getRhoUpdate()) {
if (settings.updateRho()) {

// estimate new step size
double rhonew = FastMath.min(FastMath.max(rho * FastMath.sqrt((rp * maxDual) / (rd * maxPrimal)),
Expand Down
Loading

0 comments on commit 5badc42

Please sign in to comment.