1

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.optimizeis 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 :-)

Andy
  • 169
  • 4
  • 15

2 Answers2

2

You configured NonNegativeConstraint wrongly, you should pass "true" to its constructor if you want x,y be both be positive

Alexei Kovalev
  • 416
  • 4
  • 6
1

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.

Community
  • 1
  • 1
sascha
  • 32,238
  • 6
  • 68
  • 110
  • Ah that solved the issue, thank you! So in my solution before, i have said: "every decision variable can be negative and positive" instead of "every decision variable has to be 0 or positive". So it makes sense, that there are unbounded solution values. I am looking for a good optimization library. Would be great if it is open source. In my study i have worked only with the commercial tool ibm ilog cplex, which was great. Choco CP Solver was a great alternative, but it do not support double solution (only integers and binary). Do you have a great alternative? – Andy May 28 '17 at 20:14
  • Technically you said: ```add no nonnegativity constraints```. Together with the assumption variables are free by default (which is probably hidden somewhere in the docs) it's working as expected. But keep in mind, that this assumption often is different. Example: nearly all solvers based on the Interior-point method (not your library) go with the theoretical design, which only allows nonnegative variables (natural form and free variables need special care;). – sascha May 28 '17 at 20:17
  • There is a huge gap between free and non-free (M)IP-solvers (especially if binaries/integers are involved; in the pure LP-case, that's most of the time easy to solve). Ignoring Choco and co. (which is a completely different approach; mostly tackling different problems), my rating of free solvers is: CBC > GLPK > LPsolve, with GLPK and LPsolve having the better docs by far! Remark: when i played with Choco, i saw there is rational-support (or call it floats), but it's troublesome to install. But keep in mind that CP is very different from MIP. Especially optimizing objectives is hard. – sascha May 28 '17 at 20:20
  • Thank you for that detailed information! I have also seen the SCIP-Solver. Is there a special reason, why all these solvers are written in C(++)? The wish would be, to use it as a java api. Often it works fine with the native library etc. But it would be great to use it without these dependencies from core java. – Andy May 28 '17 at 20:28
  • (1) C(++) is the leading language in high-performance computing (and some Fortran; but newer projects in general won't use F). This has many reasons, but manual memory-management is probably one of the more important (2) SCIP is open-source, but not free! I would say it's maybe even better than CBC, but well, not for commerical purposes. (3) Implementation-language and APIs are two different things. The commercial solvers are all C(++)(maybe some F),but there are interfaces for maybe 10 languages incl. Java, Python... There are probably Java-bases APIs for one of these open-source solvers. – sascha May 28 '17 at 20:31
  • Has CBC such an API for java? This is the website right? https://projects.coin-or.org/Cbc Thank you for all the stuff :-) – Andy May 28 '17 at 20:48
  • Consider googling a bit. I'm not sure as i'm not a huge java developer. But [this links might be a start (if ortools is still offering this)](https://www.or-exchange.org/questions/3443/or-solvers-with-java-api-especially-for-mips). As i said: CBC is powerful, and there are some generic wrappers, but in terms of docs and supporting-libraries glpk for example is much more interesting. – sascha May 28 '17 at 20:50