apache commons线性目标函数算法
问题描述:
apache commons math library如何计算给定不等式约束条件下目标函数的最小值和最大值。apache commons线性目标函数算法
e.g
maximize 3x1+5x2
given - 2x1+8x2<=13
5x1-x2<=11
x1>=0,x2>=0
不阿帕奇公共图书馆适用哪种算法这一点。
答
在Apache commons数学中有一个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
我已经使用了这些依赖关系:
对于更多的例子在这个库中使用Simplex求解器下载源代码并检查单元测试包:
org.apache.commons.math4.optim.linear
你能给我一些关于我对你问题的回答的反馈吗?消极或积极的反馈非常受欢迎,比没有反馈更好。 –