Modeling by Column
按列建模的概念来源于数学规划问题的矩阵观。从一个只有行但没有列的(退化的)约束矩阵开始,通过向其添加列来填充它。约束矩阵的列对应于变量。
在ILOG CPLEX中,按列建模不仅限于IloLPMatrix,还可以通过ilooobjective和IloRange对象来实现。简而言之,对于ILOG CPLEX,按列建模通常可以理解为:通过使用列来为在建模对象中安装的新变量保留一个位置。
Procedure for Modeling by Column
1、创建行对象
IloColumn col = cplex.column(obj, 1.0).and(cplex.column(rng, 2.0));
在目标函数加入决策变量系数为1.0,约束中加入决策变量系数为2.0
2、将行对象插入到现有模型
IloNumVar var = cplex.numVar(col, 0.0, 1.0);
该决策变量的下届是0.0,上届是1.0;在目标函数中的系数为1.0,约束中的系数为2.0。
Example
package cplexdemo;
import ilog.concert.*;
import ilog.cplex.IloCplex;
import static net.mindview.util.Print.*;
// The following methods all populate the problem with data for the following
// linear program:
//
// Maximize
// x1 + 2 x2 + 3 x3
// Subject To
// - x1 + x2 + x3 <= 20
// x1 - 3 x2 + x3 <= 30
// Bounds
// 0 <= x1 <= 40
// End
//
// using the IloMPModeler API
public class LPex1Test {
static void populateByColumn(IloMPModeler model, IloNumVar[][] var, IloRange[][] rng)
throws IloException {
IloObjective obj = model.addMaximize();
rng[0] = new IloRange[2];
rng[0][0] = model.addRange(-Double.MAX_VALUE, 20, "c1");
rng[0][1] = model.addRange(-Double.MAX_VALUE, 30, "c2");
IloRange r0 = rng[0][0];
IloRange r1 = rng[0][1];
var[0] = new IloNumVar[3];
IloColumn col1 = model.column(obj, 1).and(model.column(r0, -1).and(model.column(r1, 1)));
IloColumn col2 = model.column(obj, 2).and(model.column(r0, 1).and(model.column(r1, -3)));
IloColumn col3 = model.column(obj, 3).and(model.column(r0, 1).and(model.column(r1, 1)));
var[0][0] = model.numVar(col1, 0, 40, "x1");
var[0][1] = model.numVar(col2, 0, Double.MAX_VALUE, "x2");
var[0][2] = model.numVar(col3, 0, Double.MAX_VALUE, "x3");
}
public static void main(String[] args) {
try {
IloCplex cplex = new IloCplex();
//定义决策变量
IloNumVar[][] var = new IloNumVar[1][];
//约束
IloRange[][] rng = new IloRange[1][];
populateByColumn(cplex, var, rng);
if (cplex.solve()) {
double[] x = cplex.getValues(var[0]);
double[] dj = cplex.getReducedCosts(var[0]);
double[] pi = cplex.getDuals(rng[0]);
double[] slack = cplex.getSlacks(rng[0]);
cplex.output().println("Soluton status = " + cplex.getStatus());
cplex.output().println("Solution value = " + cplex.getObjValue());
for (int i = 0; i < x.length; i++) {
cplex.output().println(var[0][i].getName() + "=" + x[i] +
" reduced cost = " + dj[i]);
}
for (int i = 0; i < slack.length; i++) {
cplex.output().println("slack = " + slack[i] +
" Pi = " + pi[i]);
}
}
} catch (IloException e) {
print(e.getMessage());
}
}
}