我正在尝试使用 Apache Commons 解决优化问题。我找到了一个“Hello World”示例 here对于 Commons Math 2。但是,我想使用 Commons Math 3.2,但我找不到任何关于如何使用这部分代码的示例:
PointValuePair solution = null;
SimplexSolver solver = new SimplexSolver();
solution = solver.optimize(optData);
具体来说,我不知道什么是 optData 以及我将约束放在哪里。如果有人指出一个如何使用 org.apache.commons.math3.optim 库的“Hello World”示例,我将不胜感激。
最佳答案
这对我有用:
我的 max cx 版本:Ax <= b, x >= 0。也许不是“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();
关于java - SimplexSolver 的 Hello World 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17954235/