gpt4 book ai didi

java - apache.commons.math3 - 如何使用线性规划?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:40:44 26 4
gpt4 key购买 nike

commons-math(版本 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");
}
}
}
}

但是,添加最新math版本(3.6.1)的maven依赖时

我看到大多数相关类都已弃用,而且我还没有找到更新版本的任何代码示例。

很乐意使用 3.6.1 来解决我的 LP 问题 - 有人可以帮忙吗?

最佳答案

链接中的示例使用的是 commons-math 版本 2。

commons math 的主要包似乎从版本 2 org.apache.commons.math 更改为版本 3 中的 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");
}
}
}
}

希望对你有帮助

关于java - apache.commons.math3 - 如何使用线性规划?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39309486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com