Reputation: 315
i want to solve the following model with commons math 3 from apache:
maximize: 30x + 40y
s.t. x+y <= 240; 2x+y <= 320; x,y>=0;
My code, related to the docs should be:
// objective f = 30x + 40y + 0
LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 30, 40},0);
List<LinearConstraint> constraints = new ArrayList();
// x + y <= 240
constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.LEQ, 240));
// x + y <= 320
constraints.add(new LinearConstraint(new double[] {2, 1}, Relationship.LEQ, 320));
// x,y >=0
NonNegativeConstraint nonNegativeConstraint = new NonNegativeConstraint(false);
LinearConstraintSet constraintSet = new LinearConstraintSet(constraints);
SimplexSolver linearOptimizer = new SimplexSolver();
// put everything together in order to get a maximization problem
// in the next line i receive org.apache.commons.math3.optim.linear.UnboundedSolutionException: unbounded solution
PointValuePair solution = linearOptimizer.optimize(f, constraintSet, GoalType.MAXIMIZE, nonNegativeConstraint);
if (solution != null) {
//get solution
double max = solution.getValue();
System.out.println("Opt: " + max);
}
But everytime, when linearOptimizer.optimize
is called, i get: org.apache.commons.math3.optim.linear.UnboundedSolutionException
. The docs say:
public class UnboundedSolutionException extends MathIllegalStateException This class represents exceptions thrown by optimizers when a solution escapes to infinity.
But i have solved this optimization problem with the GUI of LPSolve and it gives me the solution
x=0; y=240; f(x,y)=9600
. So i assume, i do something wrong.
1) Any idea, what i am doing wrong?
2) I have read this post, which is 4 years ago and was written with the commons math library (not , math3). Is there now a possibility to say, that some decision variables should be integer, binary etc.? Otherwise i would programm the Branch and Bound -appoach manually to archieve that.
I would be really glad for your help and any ideas.
Thanks a lot :-)
Upvotes: 1
Views: 820
Reputation: 426
You configured NonNegativeConstraint wrongly, you should pass "true" to its constructor if you want x,y be both be positive
Upvotes: 2
Reputation: 33542
Never used that lib, but the docs tell you this:
public NonNegativeConstraint(boolean restricted)
Parameters:
restricted - If true, all the variables must be positive.
And you are doing exactly the opposite:
NonNegativeConstraint nonNegativeConstraint = new NegativeConstraint(false);
Reading the docs, i'm strongly leaning to Integer programming is not supported.
Upvotes: 1