Ortools MPSolver方法用法示例实战源码详解
文章目录
概述
MPSolver 是ortools中一个用于解决线性规划和混合整数规划问题的工具箱。具体包含Glop、SCIP、CPLEX、CLP、 GLPK 库。
Ortools MPSolver 方法总结
构造函数
MPSolver(String solverType)
- 描述:构造一个新的 MPSolver 对象。
- 参数:
solverType
:求解器类型,可以是以下值之一:MPSolver.GLOP_LINEAR_PROGRAMMING
:线性规划求解器。MPSolver.CBC_MIXED_INTEGER_PROGRAMMING
:混合整数规划求解器。MPSolver.CLP_LINEAR_PROGRAMMING
:线性规划求解器(使用 COIN-OR CLP 库)。MPSolver.CPLEX_LINEAR_PROGRAMMING
:线性规划求解器(使用 IBM CPLEX 库)。MPSolver.GLPK_LINEAR_PROGRAMMING
:线性规划求解器(使用 GLPK 库)。
- 返回值:无。
设置问题参数
void setTimeLimit(double timeLimitInSeconds)
- 描述:设置求解时间限制。
- 参数:
timeLimitInSeconds
:时间限制(以秒为单位)。
- 返回值:无。
定义变量
MPVariable makeIntVar(double lb, double ub, String name)
- 描述:创建一个整数变量。
- 参数:
lb
:变量的下界。ub
:变量的上界。name
:变量的名称。
- 返回值:一个表示整数变量的 MPVariable 对象。
MPVariable makeNumVar(double lb, double ub, String name)
- 描述:创建一个实数变量。
- 参数:
lb
:变量的下界。ub
:变量的上界。name
:变量的名称。
- 返回值:一个表示实数变量的 MPVariable 对象。
定义约束
void addConstraint(MPConstraint constraint)
- 描述:添加一个约束。
- 参数:
constraint
:要添加的约束。
- 返回值:无。
MPConstraint makeConstraint(double lb, double ub, String name)
- 描述:创建一个约束。
- 参数:
lb
:约束的下界。ub
:约束的上界。name
:约束的名称。
- 返回值:一个表示约束的 MPConstraint 对象。
设置目标函数
void setObjective(MPObjective objective)
- 描述:设置目标函数。
- 参数:
objective
:要设置的目标函数。
- 返回值:无。
MPObjective objective()
- 描述:获取当前的目标函数。
- 参数:无。
- 返回值:表示目标函数的 MPObjective 对象。
求解问题
MPSolver.ResultStatus solve()
- 描述:求解问题。
- 参数:无。
- 返回值:求解结果的状态,可以是以下值之一:
MPSolver.OPTIMAL
:找到最优解。MPSolver.FEASIBLE
:找到可行解。MPSolver.INFEASIBLE
:问题不可行。MPSolver.UNBOUNDED
:问题无界。MPSolver.ABNORMAL
:求解过程异常。MPSolver.NOT_SOLVED
:未找到解。
获取解
double objectiveValue()
- 描述:获取最优解的目标函数值。
- 参数:无。
- 返回值:最优解的目标函数值。
double value(MPVariable var)
- 描述:获取变量的取值。
- 参数:
var
:要获取取值的变量。
- 返回值:变量的取值。
这些方法是 MPSolver 类中的一部分。可以使用它们来构建数学规划模型,并求解该模型以获得最优解或可行解。
其他常用方法:
void clear()
- 描述:清空当前模型,删除所有变量、约束和目标函数。
- 参数:无。
- 返回值:无。
void setMinimization()
- 描述:设置问题为最小化问题。
- 参数:无。
- 返回值:无。
void setMaximization()
- 描述:设置问题为最大化问题。
- 参数:无。
- 返回值:无。
MPConstraint makeRowConstraint(double lb, double ub)
- 描述:创建一个行约束。
- 参数:
lb
:约束的下界。ub
:约束的上界。
- 返回值:表示约束的 MPConstraint 对象。
MPObjective objective(int index)
- 描述:获取指定索引位置的目标函数。
- 参数:
index
:目标函数的索引。
- 返回值:表示目标函数的 MPObjective 对象。
int numVariables()
- 描述:获取当前模型中的变量数。
- 参数:无。
- 返回值:当前模型中的变量数。
int numConstraints()
- 描述:获取当前模型中的约束数。
- 参数:无。
- 返回值:当前模型中的约束数。
以上是 MPSolver 类的一些常用方法。可以根据实际需求使用这些方法来构建和求解数学规划问题。
以下是对这些方法的使用总结,并给出了相应的 Java 示例代码:
- constraint(int index):通过索引获取约束条件。
MPConstraint constraint = solver.constraint(index);
- lookupConstraintOrNull(String constraint_name):通过名称查找约束条件,如果找不到则返回 null。
MPConstraint constraint = solver.lookupConstraintOrNull(constraint_name);
- makeConstraint():创建一个无名称的约束条件。
MPConstraint constraint = solver.makeConstraint();
- makeConstraint(String name):创建一个具有指定名称的约束条件。
MPConstraint constraint = solver.makeConstraint(name);
- makeConstraint(double lb, double ub):创建一个取值范围为 [lb, ub] 的约束条件。
MPConstraint constraint = solver.makeConstraint(lb, ub);
- makeConstraint(double lb, double ub, String name):创建一个具有指定名称和取值范围的约束条件。
MPConstraint constraint = solver.makeConstraint(lb, ub, name);
- constraints():获取所有的约束条件。
MPConstraint[] allConstraints = solver.constraints();
- objective():获取目标函数。
MPObjective objective = solver.objective();
- MPSolver(String name, MPSolver.OptimizationProblemType problem_type):创建一个求解器对象。
MPSolver solver = new MPSolver(name, MPSolver.OptimizationProblemType.valueOf(problem_type));
- problemType():获取问题类型。
MPSolver.OptimizationProblemType type = solver.problemType();
- solve():求解问题。
MPSolver.ResultStatus resultStatus = solver.solve();
- solve(MPSolverParameters param):使用指定的参数求解问题。
MPSolver.Parameters parameters = new MPSolver.Parameters();
// 设置参数...
MPSolver.ResultStatus resultStatus = solver.solve(parameters);
- lookupVariableOrNull(String var_name):通过名称查找变量,如果找不到则返回 null。
MPVariable variable = solver.lookupVariableOrNull(var_name);
- makeBoolVar(String name):创建一个布尔变量。
MPVariable variable = solver.makeBoolVar(name);
- makeIntVar(double lb, double ub, String name):创建一个整数变量。
MPVariable variable = solver.makeIntVar(lb, ub, name);
- makeNumVar(double lb, double ub, String name):创建一个数值变量。
MPVariable variable = solver.makeNumVar(lb, ub, name);
- makeVar(double lb, double ub, boolean integer, String name):根据给定的类型创建一个变量。
MPVariable variable = solver.makeVar(lb, ub, integer, name);
- variable(int index):通过索引获取变量。
MPVariable variable = solver.variable(index);
- makeBoolVarArray(int count):创建一个包含指定数量布尔变量的数组。
MPVariable[] variables = solver.makeBoolVarArray(count);
- makeBoolVarArray(int count, String var_name):创建一个具有指定名称的包含指定数量布尔变量的数组。
MPVariable[] variables = solver.makeBoolVarArray(count, var_name);
- makeIntVarArray(int count, double lb, double ub):创建一个包含指定数量整数变量的数组。
MPVariable[] variables = solver.makeIntVarArray(count, lb, ub);
- makeIntVarArray(int count, double lb, double ub, String var_name):创建一个具有指定名称的包含指定数量整数变量的数组。
MPVariable[] variables = solver.makeIntVarArray(count, lb, ub, var_name);
- makeNumVarArray(int count, double lb, double ub):创建一个包含指定数量数值变量的数组。
MPVariable[] variables = solver.makeNumVarArray(count, lb, ub);
- makeNumVarArray(int count, double lb, double ub, String var_name):创建一个具有指定名称的包含指定数量数值变量的数组。
MPVariable[] variables = solver.makeNumVarArray(count, lb, ub, var_name);
- makeVarArray(int count, double lb, double ub, boolean integer):根据给定的类型创建一个包含指定数量变量的数组。
MPVariable[] variables = solver.makeVarArray(count, lb, ub, integer);
- makeVarArray(int count, double lb, double ub, boolean integer, String var_name):创建一个具有指定名称的根据给定类型创建的包含指定数量变量的数组。
MPVariable[] variables = solver.makeVarArray(count, lb, ub, integer, var_name);
- variables():获取所有的变量。
MPVariable[] allVariables = solver.variables();
这些方法可以帮助您在使用 MPSolver 进行数学优化时更加方便地操作约束条件和变量。
示例
import com.google.ortools.Loader;
import com.google.ortools.linearsolver.MPConstraint;
import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
/** Minimal Mixed Integer Programming example to showcase calling the solver. */
public final class SimpleMipProgram {
public static void main(String[] args) {
Loader.loadNativeLibraries();
// Create the linear solver with the SCIP backend.
MPSolver solver = MPSolver.createSolver("SCIP");
if (solver == null) {
System.out.println("Could not create solver SCIP");
return;
}
double infinity = java.lang.Double.POSITIVE_INFINITY;
// x and y are integer non-negative variables.
MPVariable x1 = solver.makeIntVar(0.0, infinity, "x");
MPVariable x2 = solver.makeIntVar(0.0, infinity, "y");
System.out.println("Number of variables = " + solver.numVariables());
// x1 + x2 = 10.
MPConstraint c0 = solver.makeConstraint(-infinity, 10, "c0");
c0.setCoefficient(x1, 1);
c0.setCoefficient(x2, 1);
// x1 = 2.
MPConstraint c1 = solver.makeConstraint(2, 2, "c1");
c1.setCoefficient(x1, 1);
System.out.println("Number of constraints = " + solver.numConstraints());
// Maximize x2.
MPObjective objective = solver.objective();
objective.setCoefficient(x2, 1);
objective.setMaximization();
final MPSolver.ResultStatus resultStatus = solver.solve();
if (resultStatus == MPSolver.ResultStatus.OPTIMAL) {
System.out.println("Solution:");
System.out.println("Objective value = " + objective.value());
System.out.println("x1 = " + x1.solutionValue());
System.out.println("x2 = " + x2.solutionValue());
} else {
System.err.println("The problem does not have an optimal solution!");
}
System.out.println("\nAdvanced usage:");
System.out.println("Problem solved in " + solver.wallTime() + " milliseconds");
System.out.println("Problem solved in " + solver.iterations() + " iterations");
System.out.println("Problem solved in " + solver.nodes() + " branch-and-bound nodes");
}
private SimpleMipProgram() {
}
}
源码
MPSolver源码
import java.lang.reflect.*;
/**
* 这个数学规划(MP)求解器类是主要的类<br>
* 用户通过它构建和解决问题。
*/
public class MPSolver {
private transient long swigCPtr;
protected transient boolean swigCMemOwn;
protected MPSolver(long cPtr, boolean cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = cPtr;
}
protected static long getCPtr(MPSolver obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}
protected static long swigRelease(MPSolver obj) {
long ptr = 0;
if (obj != null) {
if (!obj.swigCMemOwn)
throw new RuntimeException("无法释放所有权,因为内存不是自己拥有的");
ptr = obj.swigCPtr;
obj.swigCMemOwn = false;
obj.delete(