How to set types of solution variables, such as binary, int, double in Apache Commons Math SimplexSolver?

How to set types of solution variables such as binary, int , double in Apache Commons Math SimplexSolver ? Following is the following program:

 332.6666666666667 1.0 8331.666666666668 

I want the decision variables to be of type int not double ; the output should be 333, 0, 8325 if resolved 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()); } 
+4
source share
1 answer

NumberFormat 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

Appendix: This approach assumes that the simplex algorithm is applied using real numbers, and the result is rounded to the nearest integer. A package containing SimplexSolver , org.apache.commons.math.optimization.linear , does not offer any other implementation. Alternatively, consider a different approach, or Maxtrix<Rational> , available in JScience .

+3
source

All Articles