commons-math(ver.2.2)有一个LP求解器。
Here我找到了以下示例代码:
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.math.optimization.GoalType;
import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.RealPointValuePair;
import org.apache.commons.math.optimization.linear.LinearConstraint;
import org.apache.commons.math.optimization.linear.LinearObjectiveFunction;
import org.apache.commons.math.optimization.linear.Relationship;
import org.apache.commons.math.optimization.linear.SimplexSolver;
@SuppressWarnings("deprecation")
public class Main {
@SuppressWarnings({ "rawtypes", "unchecked"})
public static void main(String[] args) {
//describe the optimization problem
LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 3, 5}, 0);
Collection 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));
constraints.add(new LinearConstraint(new double[] { 1, 0}, Relationship.GEQ, 0));
constraints.add(new LinearConstraint(new double[] { 0, 1}, Relationship.GEQ, 0));
//create and run solver
RealPointValuePair solution = null;
try {
solution = new SimplexSolver().optimize(f, constraints, GoalType.MAXIMIZE, false);
}
catch (OptimizationException e) {
e.printStackTrace();
}
if (solution != null) {
//get solution
double max = solution.getValue();
System.out.println("Opt: " + max);
//print decision variables
for (int i = 0; i < 2; i++) {
System.out.print(solution.getPoint()[i] + "\t");
}
}
}
}
但是,在添加最新数学版本(3.6.1)
的maven依赖项时我看到大多数相关的类都已弃用,我还没有找到更新版本的代码示例。
很高兴使用3.6.1解决我的LP问题 - 有人可以在这里协助吗?
答案 0 :(得分:4)
该链接的示例是使用 commons-math 版本2.
来自公共数学的主程序包似乎在版本3中从版本2 org.apache.commons.math
更改为org.apache.commons.math3
。
示例中使用的类来自org.apache.commons.math.optimization
包,在这个具体案例中,新版本中的包是org.apache.commons.math3.optim
。
从版本2到版本3的示例代码如下所示:
package commons.math;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.math3.optim.PointValuePair;
import org.apache.commons.math3.optim.linear.LinearConstraint;
import org.apache.commons.math3.optim.linear.LinearConstraintSet;
import org.apache.commons.math3.optim.linear.LinearObjectiveFunction;
import org.apache.commons.math3.optim.linear.Relationship;
import org.apache.commons.math3.optim.linear.SimplexSolver;
import org.apache.commons.math3.optim.nonlinear.scalar.GoalType;
public class MathTest {
public static void main(String[] args) {
//describe the optimization problem
LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 3, 5}, 0);
Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
constraints.add(new LinearConstraint(new double[] { 2, 8}, Relationship.LEQ, 13));
constraints.add(new LinearConstraint(new double[] { 5, -1}, Relationship.LEQ, 11));
constraints.add(new LinearConstraint(new double[] { 1, 0}, Relationship.GEQ, 0));
constraints.add(new LinearConstraint(new double[] { 0, 1}, Relationship.GEQ, 0));
//create and run solver
PointValuePair solution = null;
solution = new SimplexSolver().optimize(f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE);
if (solution != null) {
//get solution
double max = solution.getValue();
System.out.println("Opt: " + max);
//print decision variables
for (int i = 0; i < 2; i++) {
System.out.print(solution.getPoint()[i] + "\t");
}
}
}
}
希望它有所帮助,