commons-math (ver. 2.2) had an LP solver.
Here I found the follow example code:
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.math.optimization.GoalType;
import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.RealPointValuePair;
import org.apache.commons.math.optimization.linear.LinearConstraint;
import org.apache.commons.math.optimization.linear.LinearObjectiveFunction;
import org.apache.commons.math.optimization.linear.Relationship;
import org.apache.commons.math.optimization.linear.SimplexSolver;
@SuppressWarnings("deprecation")
public class Main {
@SuppressWarnings({ "rawtypes", "unchecked"})
public static void main(String[] args) {
//describe the optimization problem
LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 3, 5}, 0);
Collection constraints = new ArrayList();
constraints.add(new LinearConstraint(new double[] { 2, 8}, Relationship.LEQ, 13));
constraints.add(new LinearConstraint(new double[] { 5, -1}, Relationship.LEQ, 11));
constraints.add(new LinearConstraint(new double[] { 1, 0}, Relationship.GEQ, 0));
constraints.add(new LinearConstraint(new double[] { 0, 1}, Relationship.GEQ, 0));
//create and run solver
RealPointValuePair solution = null;
try {
solution = new SimplexSolver().optimize(f, constraints, GoalType.MAXIMIZE, false);
}
catch (OptimizationException e) {
e.printStackTrace();
}
if (solution != null) {
//get solution
double max = solution.getValue();
System.out.println("Opt: " + max);
//print decision variables
for (int i = 0; i < 2; i++) {
System.out.print(solution.getPoint()[i] + "\t");
}
}
}
}
However, when adding the maven dependency of the latest math version (3.6.1)
I see most related classes are deprecated, and I haven't found any code examples for the updated versions.
Would be glad to use 3.6.1 for my LP problems - can someone assist here please?
The example from the link is using commons-math version 2.
The main package from commons math seems to change from version 2 org.apache.commons.math
to org.apache.commons.math3
in version 3.
The classes used in the example are from org.apache.commons.math.optimization
package, in this concret case the package in the new version is org.apache.commons.math3.optim
.
The sample code from version 2, to version 3 looks like:
package commons.math;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.math3.optim.PointValuePair;
import org.apache.commons.math3.optim.linear.LinearConstraint;
import org.apache.commons.math3.optim.linear.LinearConstraintSet;
import org.apache.commons.math3.optim.linear.LinearObjectiveFunction;
import org.apache.commons.math3.optim.linear.Relationship;
import org.apache.commons.math3.optim.linear.SimplexSolver;
import org.apache.commons.math3.optim.nonlinear.scalar.GoalType;
public class MathTest {
public static void main(String[] args) {
//describe the optimization problem
LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 3, 5}, 0);
Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
constraints.add(new LinearConstraint(new double[] { 2, 8}, Relationship.LEQ, 13));
constraints.add(new LinearConstraint(new double[] { 5, -1}, Relationship.LEQ, 11));
constraints.add(new LinearConstraint(new double[] { 1, 0}, Relationship.GEQ, 0));
constraints.add(new LinearConstraint(new double[] { 0, 1}, Relationship.GEQ, 0));
//create and run solver
PointValuePair solution = null;
solution = new SimplexSolver().optimize(f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE);
if (solution != null) {
//get solution
double max = solution.getValue();
System.out.println("Opt: " + max);
//print decision variables
for (int i = 0; i < 2; i++) {
System.out.print(solution.getPoint()[i] + "\t");
}
}
}
}
Hope it helps,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With