作者:刘兴禄,清华大学,清华大学深圳国际研究生院,清华-伯克利深圳学院,博士在读
用到的函数
addMIPStart(IloNumVar[] vars, double[] values)
具体案例
考虑下面一个非常简单的整数规划模型
max ∑ i = 1 10 x i s . t . x i ⩽ 1 , ∀ i = 1 , ⋯ , 10 , 0 ⩽ x i ⩽ 100 , ∀ i = 1 , ⋯ , 10 , x i ∈ Z , ∀ i = 1 , ⋯ , 10. \begin{aligned} \max \quad &\sum_{i=1}^{10} x_i \\ s.t. \quad& x_i \leqslant 1, && \forall i = 1, \cdots, 10, \\ &0 \leqslant x_{i} \leqslant 100,&& \forall i = 1, \cdots, 10, \\ &x_{i} \in \mathbb{Z}, && \forall i = 1, \cdots, 10. \end{aligned} maxs.t.i=1∑10xixi⩽1,0⩽xi⩽100,xi∈Z,∀i=1,⋯,10,∀i=1,⋯,10,∀i=1,⋯,10.
下面是具体的代码中,我们设置 x i = 1 , ∀ i = 1 , ⋯ , 10 x_i = 1, \forall i = 1, \cdots, 10 xi=1,∀i=1,⋯,10.
/*
author: Liu Xinglu
institute: Tsinghua University
date: 2023-3-16
*/
package Test;
import ilog.concert.*;
import ilog.cplex.*;
public class Main {
public static void main(String[] args) throws IloException {
int num = 10;
IloCplex cplex = new IloCplex();
IloNumVar[] x = new IloNumVar[num];
double[] initSol = new double[num];
for(int i = 0; i < num; i++) {
// x[i] = cplex.numVar(0, 10000);
x[i] = cplex.intVar(0, 10000);
initSol[i] = 1.0;
}
IloNumExpr obj = cplex.numExpr();
for(int i = 0; i < num; i++) {
obj = cplex.sum(obj, x[i]);
}
cplex.addMaximize(obj);
for(int i = 0; i < num; i++) {
IloNumExpr expr = cplex.numExpr();
expr = cplex.sum(expr, x[i]);
cplex.addLe(expr, 1);
}
cplex.addMIPStart(x, initSol);
cplex.exportModel("MyTestModel.lp");
cplex.solve();
System.out.println(cplex.getObjValue());
}
}
运行结果如下
1 of 1 MIP starts provided solutions.
MIP start 'm1' defined initial solution with objective 10.0000.
Tried aggregator 1 time.
MIP Presolve eliminated 10 rows and 10 columns.
All rows and columns eliminated.
Presolve time = 0.00 sec. (0.00 ticks)
Root node processing (before b&c):
Real time = 0.00 sec. (0.02 ticks)
Parallel b&c, 16 threads:
Real time = 0.00 sec. (0.00 ticks)
Sync time (average) = 0.00 sec.
Wait time (average) = 0.00 sec.
------------
Total (root+branch&cut) = 0.00 sec. (0.02 ticks)
10.0
可以看到,设置成功了。日志中出现了下面的信息:
1 of 1 MIP starts provided solutions.
MIP start 'm1' defined initial solution with objective 10.0000.
这就说明设置初始解成功。