SimplexSolver的Hello World示例

11

我正在尝试使用Apache Commons解决一个优化问题。我在这里找到了Commons Math 2的“Hello World”示例(链接),但我想使用Commons Math 3.2,而我无法找到如何使用代码中的这一部分的任何示例:

PointValuePair solution = null;
SimplexSolver solver = new SimplexSolver();
solution = solver.optimize(optData);

具体来说,我不知道optData是什么,也不知道在哪里放置约束条件。如果有人能给我一个使用org.apache.commons.math3.optim库的“Hello World”示例,我将不胜感激。

1个回答

12

这对我有用:

http://mail-archives.apache.org/mod_mbox/commons-user/201307.mbox/%3CCAJSjvws+9uC=jMP_A_Mbc4szbJL2VXAwp=Q2A+zZ=51mLeRw6g@mail.gmail.com%3E

我使用的版本是:Ax <= b, x >= 0 的最大化 cx,也许不是“hello world”,但我希望它能有所帮助。

        LinearObjectiveFunction f = new LinearObjectiveFunction(c, 0);
        Collection<LinearConstraint> constraints = new
                ArrayList<LinearConstraint>();
        for(int i=0; i<A.length; i++) {
            double[] Av = new double[A[i].length];
            for(int j=0; j<A[i].length; j++) {
                Av[j] = A[i][j];
            }
            constraints.add(new LinearConstraint(Av, Relationship.LEQ, b[i]));
        }

        SimplexSolver solver = new SimplexSolver();
        PointValuePair optSolution = solver.optimize(new MaxIter(100), f, new
                LinearConstraintSet(constraints),
                GoalType.MAXIMIZE, new
                NonNegativeConstraint(true));


        double[] solution;
        solution = optSolution.getPoint();

你能否说一下这个方法的实现是使用了两阶段提交还是其他方式? - R. Karlus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接