4

How to set decision variables types like binary, int, double in Apache Commons Math SimplexSolver? The output of the program below is this:

332.6666666666667
1.0
8331.666666666668

I want decision variables to be of type int not double; output should be 333, 0, 8325 if solved as integer decision variables.

public static void testSample() throws OptimizationException {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[]{25, 15}, 0);
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[]{5, 8}, Relationship.LEQ, 5000));
    constraints.add(new LinearConstraint(new double[]{1, 4}, Relationship.LEQ, 1500));
    constraints.add(new LinearConstraint(new double[]{3, 2}, Relationship.LEQ, 1000));
    constraints.add(new LinearConstraint(new double[]{1, 0}, Relationship.GEQ, 1));
    constraints.add(new LinearConstraint(new double[]{0, 1}, Relationship.GEQ, 1));

    SimplexSolver solver = new SimplexSolver();
    RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, true);

    System.out.println(solution.getPoint()[0]);
    System.out.println(solution.getPoint()[1]);
    System.out.println(solution.getValue());
}

1 Answers1

3

NumberFormat is convenient for this:

NumberFormat nf = NumberFormat.getIntegerInstance();
System.out.println(nf.format(solution.getPoint()[0]));
System.out.println(nf.format(solution.getPoint()[1]));
System.out.println(nf.format(solution.getValue()));

Console:

333
1
8,332

Addendum: This approach assumes that the simplex algorithm is applied using real numbers and the result(s) rounded to integer. The package containing SimplexSolver, org.apache.commons.math.optimization.linear, offers no other implementation. As an alternative, consider a different approach or Maxtrix<Rational>, available in JScience.

trashgod
  • 196,350
  • 25
  • 213
  • 918
  • Hi thanks for the answer. In your answer you are just formatting the number it has nothing to do with the optimization. 333 1 8332 is not the optimized answer the optimized solution is 333 0 8325. By setting the decision variable type we can get the real optimization. Is there any body know how to do that? – user1020082 Oct 30 '11 at 17:40
  • 1
    Ah, I misunderstood; more above. Sorry I edited your question in error. – trashgod Oct 30 '11 at 19:14