ojAlgo解决LP问题
ojAlgo 是一个开源的 Java 类库,用于解决一些常用的数学问题以及线性代数和最优化问题,所以也可以用来解决LP问题
1 2 3 4 5
| <dependency> <groupId>org.ojalgo</groupId> <artifactId>ojalgo</artifactId> <version>51.4.1</version> </dependency>
|
求解 5x1 + 6x2 + 23x3 + 5x4 + 24x5 + 6x6 + 23x7 + 5x8
的最小值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| ExpressionsBasedModel model = new ExpressionsBasedModel(); model.addVariable(Variable.makeInteger("x"+1).lower(0).weight(5)); model.addVariable(Variable.makeInteger("x"+2).lower(0).weight(6)); model.addVariable(Variable.makeInteger("x"+3).lower(0).weight(23)); model.addVariable(Variable.makeInteger("x"+4).lower(0).weight(5)); model.addVariable(Variable.makeInteger("x"+5).lower(0).weight(24)); model.addVariable(Variable.makeInteger("x"+6).lower(0).weight(6)); model.addVariable(Variable.makeInteger("x"+7).lower(0).weight(23)); model.addVariable(Variable.makeInteger("x"+8).lower(0).weight(5));
Expression expression = model.addExpression("e" + 1); expression.set(0,2); expression.set(1,1); expression.set(2,1); expression.set(3,1); expression.lower(100);
Expression expression2 = model.addExpression("e" + 2); expression2.set(1,2); expression2.set(2,1); expression2.set(4,3); expression2.set(5,2); expression2.set(6,1); expression2.lower(150);
Expression expression3 = model.addExpression("e" + 3); expression3.set(0,1); expression3.set(2,1); expression3.set(3,3); expression3.set(5,2); expression3.set(6,3); expression3.set(7,5); expression3.lower(100);
Optimisation.Result minimise = model.minimise(); System.out.println(minimise);
|
结果为
1
| 30.0, 40.0, 0.0, 0.0, 0.0, 35.0, 0.0, 0.0, 1.0
|
使用起来比JOptimizer感觉好多了,至少可读性强