4

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?

Bhargav Rao
52.5k29 gold badges129 silver badges142 bronze badges
asked Sep 3, 2016 at 17:20
2
  • Just because something is deprecated doesn't mean it can't be used. But there's probably a reason stated somewhere why they're deprecated. Keep reading and you'll learn the secrets of that library. Commented Sep 3, 2016 at 17:23
  • Perhaps if you post an example of something you are trying to do (with the deprecated API), someone may help you find the non-deprecated way. Commented Sep 3, 2016 at 17:27

1 Answer 1

10

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,

answered Sep 5, 2016 at 5:16

2 Comments

Does this solver use all CPU cores?
@StepanYakovenko actually I don't know it, however it's an interesting question, maybe you can post a new one asking for this.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.