MATLAB API - 解决模型#
- gurobi(model)#
- gurobi(model, params)#
此函数优化给定的模型。用于优化的算法取决于模型类型(连续模型的单纯形法或障碍法;MIP模型的分支定界法)。成功完成后,它将返回一个包含解决方案信息的
struct
变量。请参考 变量和约束 部分 在参考手册中,讨论了一些与使用有限精度浮点算术解决精确定义的数学模型相关的实际问题。
- Parameters:
- Example:
result = gurobi(model, params); if strcmp(result.status, 'OPTIMAL'); fprintf('Optimal objective: %e\n', result.objval); disp(result.x) else fprintf('Optimization returned status: %s\n', result.status); end
- Returns:
The optimization result
The
gurobi
function returns astruct
, with the various results of the optimization stored in its fields. The specific results that are available depend on the type of model that was solved, the parameters used, and the status of the optimization. The following is a list of fields that might be available in the returned result. We will discuss the circumstances under which each will be available after presenting the list.Model fields
- status
The status of the optimization, returned as a string. The desired result is OPTIMAL, which indicates that an optimal solution to the model was found. Other status are possible, for example if the model has no feasible solution or if you set a Gurobi parameter that leads to early solver termination. See the 状态码 section for further information on the Gurobi status codes.
- objval
The objective value of the computed solution. Note that for multi-objective models
result.objval
will be a vector, whereresult.objval(i)
stores the value formodel.multiobj(i)
.- objbound
Best available bound on solution (lower bound for minimization, upper bound for maximization).
- objboundc
The best unrounded bound on the optimal objective. In contrast to
objbound
, this attribute does not take advantage of objective integrality information to round to a tighter bound. For example, if the objective is known to take an integral value and the current best bound is 1.5,objbound
will return 2.0 whileobjboundc
will return 1.5.- mipgap
Current relative MIP optimality gap; computed as \(\vert ObjBound-ObjVal\vert/\vert ObjVal\vert\) (where \(ObjBound\) and \(ObjVal\) are the MIP objective bound and incumbent solution objective, respectively). Returns
GRB_INFINITY
when an incumbent solution has not yet been found, when no objective bound is available, or when the current incumbent objective is 0. This is only available for mixed-integer problems.- runtime
The elapsed wall-clock time (in seconds) for the optimization.
- work
The work (in work units) spent on the optimization. As opposed to the runtime in seconds, the work is deterministic. This means that on the same hardware and with the same parameter and attribute settings, solving the same model twice will lead to exactly the same amount of work in each of the two solves. One work unit corresponds very roughly to one second, but this greatly depends on the hardware on which Gurobi is running and on the model that has been solved.
- itercount
Number of simplex iterations performed.
- baritercount
Number of barrier iterations performed.
- nodecount
Number of branch-and-cut nodes explored.
- maxvio
Value of the maximal (unscaled) violation of the returned solution.
- farkasproof
Magnitude of infeasibility violation in Farkas infeasibility proof. Only available if the model was found to be infeasible. Please refer to 属性 section in the reference manual for details.
Variable fields
- x
The computed solution. This vector contains one entry for each column of
A
.- rc
Variable reduced costs for the computed solution. This vector contains one entry for each column of
A
.- vbasis
Variable basis status values for the computed optimal basis. You generally should not concern yourself with the contents of this vector. If you wish to use an advanced start later, you would simply copy the
vbasis
andcbasis
fields into the corresponding fields for the next model. This vector contains one entry for each column ofA
.- unbdray
Unbounded ray. Provides a vector that, when added to any feasible solution, yields a new solution that is also feasible but improves the objective. Only available if the model is found to be unbounded. This vector contains one entry for each column of
A
.
Linear constraint fields
- slack
The constraint slack for the computed solution. This vector contains one entry for each row of
A
.- pi
Dual values for the computed solution (also known as shadow prices). This vector contains one entry for each row of
A
.- cbasis
Constraint basis status values for the computed optimal basis. This vector contains one entry for each row of
A
.- farkasdual
Farkas infeasibility proof. Only available if the model was found to be infeasible. Please refer to 属性 section in the reference manual for details.
Quadratic constraint fields
- qcslack
The quadratic constraint slack in the current solution. This vector contains one entry for each quadratic constraint.
- qcpi
The dual values associated with the quadratic constraints. This vector contains one entry for each quadratic constraint.
Solution Pool fields
- pool
When multiple solutions are found during the optimization call, these solutions are returned in this field. A struct array. When present, each struct has the following fields:
- objval
Stores the objective value of the \(i\)-th solution in
result.pool(i).objval
. Note that when the model is a multi-objective model, instead of a single value,result.pool(i).objval(j)
stores the value of the \(j\)-th objective function for the \(i\)-th solution.- xn
Stores the \(i\)-th solution in
result.pool(i).xn
. This vector contains one entry for each column ofA
.
Note that to query the number of solutions stored, you can query the length of
result.pool
.- poolobjbound
For single-objective MIP optimization problems, this value gives a bound on the best possible objective of an undiscovered solution. The difference between this value and
objbound
is that the former gives an objective bound for undiscovered solutions, while the latter gives a bound for any solution.
What is Available When
The
status
field will be present in all cases. It indicates whether Gurobi was able to find a proven optimal solution to the model. In cases where a solution to the model was found, optimal or otherwise, theobjval
andx
fields will be present.For linear and quadratic programs, if a solution is available, then the
pi
andrc
fields will also be present. For models with quadratic constraints, if the parameterqcpdual
is set to 1, the fieldqcpi
will be present. If the final solution is a basic solution (computed by simplex), thenvbasis
andcbasis
will be present. If the model is an unbounded linear program and the InfUnbdInfo parameter is set to 1, the fieldunbdray
will be present. Finally, if the model is an infeasible linear program and the InfUnbdInfo parameter is set to 1, the fieldsfarkasdual
andfarkasproof
will be set.For mixed integer problems, no dual information (i.e.
pi
,slack
,rc
,vbasis
,cbasis
,qcslack
,qcpi
,ubdray
orfarkasdual
) is ever available. When multiple solutions are found, thepool
andpoolobjbound
fields will be present. Depending on thestatus
field value, the fieldsnodecount
,objbound
,objbundc
andmipgap
will be available.For continuous and mixed-integer models, under normal execution, the fields
runtime
,work
,itercount
andbaritercount
will be available.
- gurobi_iis(model)#
- gurobi_iis(model, params)#
计算一个不可约不一致子系统(IIS)。
IIS 是具有以下属性的约束和变量边界的子集:
这仍然是不可行的,并且
如果移除单个约束或边界,子系统将变得可行。
请注意,一个不可行的模型可能有多个IIS。Gurobi返回的不一定是最小的一个;可能存在其他具有更少约束或边界的IIS。
您可以从返回的IIS结果(如下所述)中获取有关IIS计算结果的信息。请注意,此方法可用于计算连续模型和MIP模型的IIS。
- Parameters:
- Example:
model = gurobi_read('examples/data/klein1.mps'); iis = gurobi_iis(model);
- Returns:
gurobi_iis()
函数返回一个struct
,其中存储了各种结果。可用的具体结果取决于模型的类型。返回的
struct
将始终包含以下fields
:- minimal
一个逻辑标量,指示计算的 IIS 是否最小。通常为 true,但如果 IIS 计算提前停止(由于时间限制或用户中断),则可能为 false。
- Arows
一个逻辑向量,指示线性约束是否出现在计算的 IIS 中。
- lb
一个逻辑向量,指示下界是否出现在计算的 IIS 中。
- ub
一个逻辑向量,指示上界是否出现在计算的 IIS 中。
如果您的模型包含一般约束,返回的
struct
还将包含以下fields
:- genconmax
一个逻辑向量,指示一般 MAX 约束是否出现在计算的 IIS 中。
- genconmin
一个逻辑向量,指示一般 MIN 约束是否出现在计算的 IIS 中。
- genconand
一个逻辑向量,指示一般 AND 约束是否出现在计算的 IIS 中。
- genconor
一个逻辑向量,指示一般 OR 约束是否出现在计算的 IIS 中。
- genconabs
一个逻辑向量,指示一般 ABS 约束是否出现在计算的 IIS 中。
- genconind
一个逻辑向量,指示一般 INDICATOR 约束是否出现在计算的 IIS 中。
- genconpwl
一个逻辑向量,指示一般分段线性函数约束是否出现在计算的 IIS 中。
- genconpoly
一个逻辑向量,指示多项式函数约束是否出现在计算的 IIS 中。
- genconexp
一个逻辑向量,指示自然指数函数约束是否出现在计算的 IIS 中。
- genconexpa
一个逻辑向量,指示指数函数约束是否出现在计算的 IIS 中。
- genconlog
一个逻辑向量,指示自然对数函数约束是否出现在计算的 IIS 中。
- genconloga
一个逻辑向量,指示对数函数约束是否出现在计算的 IIS 中。
- genconlogistic
一个逻辑向量,指示逻辑函数约束是否出现在计算的 IIS 中。
- genconpow
一个逻辑向量,指示幂函数约束是否出现在计算的 IIS 中。
- genconsin
一个逻辑向量,指示 SIN 函数约束是否出现在计算的 IIS 中。
- genconcos
一个逻辑向量,指示 COS 函数约束是否出现在计算的 IIS 中。
- gencontan
一个逻辑向量,指示 TAN 函数约束是否出现在计算的 IIS 中。
如果您的模型包含 SOS 约束,返回的
struct
还将包含以下field
:- sos
一个逻辑向量,指示 SOS 约束是否出现在计算的 IIS 中。
如果您的模型包含二次约束,返回的
struct
还将包含以下field
:- quadcon
一个逻辑向量,指示二次约束是否出现在计算的 IIS 中。
要将IIS计算的结果写入.ilp文件格式,请在调用
gurobi
函数之前设置结果文件参数。- Example:
params.resultfile = 'infeas_submodel.ilp' result = gurobi(model, params)
- gurobi_feasrelax(model, relaxobjtype, minrelax, penalties)#
- gurobi_feasrelax(model, relaxobjtype, minrelax, penalties, params)#
此函数计算输入
model
参数的可行性松弛。可行性松弛是一个模型,当求解时,最小化解违反原始模型的边界和线性约束的程度。您必须提供一个惩罚来关联每个单独的边界或约束的松弛(通过penalties
参数)。这些惩罚根据relaxobjtype
参数的值以不同的方式解释。有关此函数如何转换模型的示例,以及有关创建的变量和约束的更多详细信息,请参阅本节。
- Parameters:
model – 模型
struct
必须包含一个有效的 Gurobi 模型。有关更多信息,请参阅 model 参数部分。relaxobjtype – 用于对违规施加惩罚的方法。 如果指定
relaxobjtype=0
,可行性松弛的目标是最小化边界和约束违规的加权幅度之和。 如果指定relaxobjtype=1
,可行性松弛的目标是最小化边界和约束违规的加权平方和。 如果指定relaxobjtype=2
,可行性松弛的目标是最小化边界和约束违规的加权计数。 在所有情况下,权重取自penalties.lb
、penalties.ub
和penalties.rhs
。您可以提供特殊的惩罚值Inf
来表示相应的边界或约束不能被松弛。minrelax –
minrelax
参数是一个布尔值,用于控制创建的可行性松弛类型。如果minrelax=false
,优化返回的模型会得到一个最小化违规成本的解。如果minrelax=true
,优化返回的模型会找到一个最小化原始目标的解,但仅从那些最小化违规成本的解中选择。请注意,当minrelax=true
时,gurobi_feasrelax
必须解决一个优化问题以找到最小可能的松弛,这可能会非常耗时。penalties –
penalties
参数是一个结构体数组,包含以下可选字段(默认值:全部为Inf
):lb
违反每个下界的惩罚。应与模型中的变量数量相同。请注意,Gurobi 可能已自动为范围约束创建了人工变量。ub
违反每个上界的惩罚。应与模型中的变量数量相同。请注意,Gurobi 可能已自动为范围约束创建了人工变量。rhs
违反每个约束的惩罚。应与模型中的约束数量相同。
举个例子,如果具有
penalties.rhs
值p
的约束被违反了 2.0,它将为relaxobjtype=0
的可行性松弛目标贡献2*p
,为relaxobjtype=1
贡献2*2*p
,为relaxobjtype=2
贡献p
。params – 当提供时,params
struct
包含一系列修改过的 Gurobi 参数。更多信息请参见 params 参数部分。
- Returns:
一个包含两个字段的
struct
:result.model
,一个结构体变量,如model参数部分所述。result.feasobj
,一个标量。如果minrelax==true
,这是松弛问题的目标值,否则为0.0。- Example:
model = gurobi_read('stein9.mps'); penalties.lb = ones(length(model.lb),1); penalties.ub = ones(length(model.ub),1); penalties.rhs = ones(length(model.rhs),1); feasrelaxresult = gurobi_feasrelax(model, 0, false, penalties);