4

Apache Commons Math でバイナリint、、などの決定変数タイプを設定する方法は? 以下のプログラムの出力は次のとおりです。doubleSimplexSolver

332.6666666666667
1.0
8331.666666666668

決定変数の型intを notにしたいdouble333, 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());
}
4

1 に答える 1

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

補遺:このアプローチは、シンプレックスアルゴリズムが実数を使用して適用され、結果が整数に丸められることを前提としています。、、を含むパッケージはSimplexSolverorg.apache.commons.math.optimization.linear他の実装を提供しません。別の方法として、で利用可能な別のアプローチまたは を検討してください。Maxtrix<Rational>JScience

于 2011-10-29T20:29:27.710 に答える