现在,我正在尝试通过在cplex中“使用CP”来制作迭代代码。 当我使用CP来获取有关'q1'凸面的解决方案时,就会出现问题。
我该怎么办,我需要在下面更改我的代码。
main {
var source = new IloOplModelSource("Ver.1.0.mod");
var def = new IloOplModelDefinition(source);
var opl = new IloOplModel(def,cplex);
var data = new IloOplDataSource("Ver.1.0.dat");
var nInstances = 1;
opl.addDataSource(data);
opl.generate();
for (var i=0; i<nInstances; i++){
var def = opl.modelDefinition;
var data= opl.dataElements;
cplex.tilim=60;
for (var u=1; u<=data.UNum; u++){data.nU = data.Many[u];
for (var t=1; t<=data.TNum; t++){data.nT = data.Many[t];
for (var g=1; g<=data.GNum; g++){data.nG = data.Many[g];
for (var e=1; e<=data.ENum; e++){data.nE = data.Many[e];
for (var c=5; c<=30; c++){data.T = data.Many[c];
if(opl!=thisOplModel){opl.end(); }
opl=new IloOplModel(def,cplex);
opl.addDataSource(data);
opl.generate();
if (cplex.solve()) {
writeln(data.nU,":",data.nT,":",data.nG,":",data.nE,":",data.T,":",cplex.getObjValue(),":",cplex.getBestObjValue(),":",cplex.getDetTime(),":",cplex.getCplexTime(),":",cplex.status);
}
else {
writeln(data.nU,":",data.nT,":",data.nG,":",data.nE,":",data.T,":","Error",":",cplex.getDetTime(),":",cplex.status);
}
}}}}}} //}}}}
writeln();
writeln();
opl.end();
data.end();
def.end();
source.end();
}
我将单词“ cplex”更改为“ CP”,但没有用。
答案 0 :(得分:1)
让我分享一个小例子:
sub.mod
using CP;
int maxOfx = ...;
dvar int x;
maximize x;
subject to {
x<=maxOfx;
}
execute
{
writeln("x= ",x);
}
然后是主模型
main {
var source = new IloOplModelSource("sub.mod");
var cp = new IloCP();
var def = new IloOplModelDefinition(source);
for(var k=1;k<=10;k++)
{
var opl = new IloOplModel(def,cp);
var data2= new IloOplDataElements();
data2.maxOfx=k;
opl.addDataSource(data2);
opl.generate();
if (cp.solve()) {
opl.postProcess();
writeln("OBJ = " + cp.getObjValue());
} else {
writeln("No solution");
}
opl.end();
}
}
给出
x= 1
OBJ = 1
x= 2
OBJ = 2
x= 3
OBJ = 3
x= 4
OBJ = 4
x= 5
OBJ = 5
x= 6
OBJ = 6
x= 7
OBJ = 7
x= 8
OBJ = 8
x= 9
OBJ = 9
x= 10
OBJ = 10