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());
}
NumberFormat
is convenient for this:Console:
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 orMaxtrix<Rational>
, available inJScience
.