我正在尝试使用Apache Commons解决优化问题。我为Commons Math 2找到了一个“Hello World”示例here。但是,我想使用Commons Math 3.2,我找不到任何关于如何使用这部分代码的示例:
PointValuePair solution = null;
SimplexSolver solver = new SimplexSolver();
solution = solver.optimize(optData);
具体来说,我不知道什么是optData以及我放置约束的地方。如果有人给我一个关于如何使用org.apache.commons.math3.optim库的“Hello World”示例,我将不胜感激。
祝福!
答案 0 :(得分:10)
这对我有用:
我的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();