How to set decision variables types like binary, int, double in Apache Commons Math SimplexSolver?

1.1k views Asked by At

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

There are 1 answers

2
trashgod On BEST ANSWER

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.