如何在Apache Commons Math SimplexSolver中设置二进制,整数,双精度等决策变量类型?

时间:2011-10-29 19:33:34

标签: java apache solver linear-programming simplex

如何在Apache Commons Math SimplexSolver中设置二进制,intdouble等决策变量类型?以下程序的输出是:

332.6666666666667
1.0
8331.666666666668

我希望决策变量的类型为int,而不是double;如果作为整数决策变量求解,则输出应为333, 0, 8325

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 个答案:

答案 0 :(得分:3)

NumberFormat方便:

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()));

控制台:

333
1
8,332

附录:此方法假定使用实数应用simplex algorithm,并将结果四舍五入为整数。包含SimplexSolverorg.apache.commons.math.optimization.linear的包不提供其他实现。作为替代方案,请考虑different approach中提供的JScienceMaxtrix<Rational>