【问题标题】:apache commons LinearObjectiveFunction Algorithmapache commons LinearObjectiveFunction 算法
【发布时间】:2018-03-24 08:38:31
【问题描述】:

apache commons 数学库如何计算给定不等式约束的目标函数的最小值和最大值。

例如

maximize 3x1+5x2 
given - 2x1+8x2<=13
5x1-x2<=11
x1>=0,x2>=0

apache commons 库对此应用什么算法。

【问题讨论】:

  • 您能否就我对您问题的回答给我一些反馈?非常欢迎负面或正面反馈,总比没有反馈好。

标签: java math apache-commons


【解决方案1】:

在 Apache 公共数学中有一个 Simplex 求解器。

你可以使用这个库来解决你的问题:

@Test
public void testMathStackOverflow() {
    //      maximize 3x1+5x2
    //      subject to
    //          2x1+8x2<=13
    //          5x1-x2<=11
    //              x1>=0
    // x2>=0

    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 3, 5}, 0);

    ArrayList<LinearConstraint> constraints = new ArrayList<>();

    constraints.add(new LinearConstraint(new double[] {2, 8}, Relationship.LEQ, 13));
    constraints.add(new LinearConstraint(new double[] {5, -1}, Relationship.LEQ, 11));

    double epsilon = 1e-6;
    SimplexSolver solver = new SimplexSolver();
    PointValuePair solution = solver.optimize(f, new LinearConstraintSet(constraints),
            GoalType.MAXIMIZE,
            new NonNegativeConstraint(true),
            PivotSelectionRule.BLAND);
    System.out.printf("x1: %f; x2: %f; max: %f", solution.getPoint()[0], solution.getPoint()[1], solution.getValue());
}

结果是:

x1: 2.404762; x2: 1.023810; max: 12.333333

我已经使用了这些依赖项:

<groupId>org.apache.commons</groupId>
<artifactId>commons-math4</artifactId>
<version>4.0-SNAPSHOT</version>

有关此库中 Simplex 求解器的更多使用示例,请下载源代码并检查包中的单元测试:

org.apache.commons.math4.optim.linear

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 2023-03-30
    • 2018-04-04
    • 2012-11-05
    • 1970-01-01
    • 2018-08-27
    • 2013-03-28
    相关资源
    最近更新 更多