![](/img/trans.png)
[英]Saving results of CPLEX with flow control (change data solve with regenerate)
[英]Change some data and resolve in CPLEX with flow control
我未能在每次迭代中更改 due_time 的值。 如何更改我的代碼?
ct9: // due time
forall(k in orders, j in orders_nodes[k]:j in k.dest)
arrival_time[k][j]<=due_time;
main {
thisOplModel.generate();
var new_due = 25;
while( new_due <50)
{ ...
new_due ++;
thisOplModel.ct9.UB = new_due;
}
}
CPLEX 告訴我 ct9 的 UB 未定義。 ct9右側的due_time是UB嗎?
我也試過thisOplModel.due_time ++;
但失敗了。 你有什么主意嗎?
改寫
// due time
forall(k in orders, j in orders_nodes[k])
ct9: if (j in k.dest) arrival_time[k][j]<=due_time;
加上而不是
thisOplModel.ct9.UB
你應該索引 ct9
thisOplModel.ct9[k][j].UB = new_due;
int nbKids=300;
float costBus40=500;
float costBus30=400;
dvar int+ nbBus40;
dvar int+ nbBus30;
minimize
costBus40*nbBus40 +nbBus30*costBus30;
subject to
{
ctKids:40*nbBus40+nbBus30*30>=nbKids;
}
execute
{
writeln("nbBus40 = ",nbBus40);
writeln("nbBus30 = ",nbBus30);
}
main
{
thisOplModel.generate();
cplex.solve();
thisOplModel.postProcess();
//now 350 kids instead of 300
writeln("now 350 kids instead of 300");
thisOplModel.ctKids.LB=350;
cplex.solve();
thisOplModel.postProcess();
// no more than 4 buses 40 seats
writeln("no more than 4 buses 40 seats");
thisOplModel.nbBus40.UB=4;
cplex.solve();
thisOplModel.postProcess();
// change the objective so that cost for 40 seats is 450
// and remove the limit on the number of buses 40 seats
writeln("change the objective so that cost for 40 seats is 450");
writeln("and remove the limit on the number of buses 40 seats");
thisOplModel.nbBus40.UB=1000;
cplex.setObjCoef(thisOplModel.nbBus40,450);
cplex.setObjCoef(thisOplModel.nbBus30,400);
cplex.solve();
thisOplModel.postProcess();
}
這使
nbBus40 = 6
nbBus30 = 2
now 350 kids instead of 300
nbBus40 = 8
nbBus30 = 1
no more than 4 buses 40 seats
nbBus40 = 2
nbBus30 = 9
change the objective so that cost for 40 seats is 450
and remove the limit on the number of buses 40 seats
nbBus40 = 8
nbBus30 = 1
對於您的循環,請參見
int nbKids=300;
float costBus40=500;
float costBus30=400;
range r=1..4;
dvar int+ nbBus40[i in r][j in r];
dvar int+ nbBus30[i in r][j in r];
minimize
sum(i,j in r)(costBus40*nbBus40[i][j] +nbBus30[i][j]*costBus30);
subject to
{
forall(i,j in r)ctKids: if (i!=j) 40*nbBus40[i][j]+nbBus30[i][j]*30>=nbKids;
}
execute
{
writeln("nbBus40 = ",nbBus40);
writeln("nbBus30 = ",nbBus30);
}
main
{
thisOplModel.generate();
cplex.solve();
thisOplModel.postProcess();
//now 350 kids instead of 300
writeln("now 350 kids instead of 300");
for(i in thisOplModel.r) for(j in thisOplModel.r) if (i!=j) thisOplModel.ctKids[i][j].LB=350;
cplex.solve();
thisOplModel.postProcess();
}
謝謝你的分享。 我仍然無法更改 ct9 的上行,CPLEX 告訴我索引是可變的。 索引 k 和 j 在數據文檔中給出。 你知道為什么嗎?
或者我認為我們可以更改每個訂單的到期時間,而不是更改 ct9 的上行。 然后我寫了這些代碼。 我習慣了 output 第一單的到期時間。 是的。 它改變了,但目標沒有改變。 增加到期時間時,目標應該更小:當我將 ct9 的騎行側更改為等於和小於 45 時,目標應該更小。比如:
ct9:// due time
forall(k in orders, j in k.dest)
arrival_time[k][j]<=45;
你知道如何更正這些代碼嗎?
主要的 {
var ofile = new IloOplOutputFile("due time and threshold.txt");
var new_due = 40;
var due_max = 45;
writeln("due time changes from "+ new_due+" to "+due_max);
while( new_due <= due_max )
{
ofile.writeln("For due time = "+new_due);
ofile.writeln("**************************************");
thisOplModel.generate();
cplex.solve()
thisOplModel.postProcess();
if (cplex.getCplexStatus())
{
for (var k in thisOplModel.orders)
{
ofile.writeln(" * for order "+ k.no+" "+k.volume);
for( var s in thisOplModel.services)
{
for( var a in thisOplModel.orders_arcs[k])
{ if (thisOplModel.x[k][a][s]>0 && a==s.a)
{
ofile.writeln(" via arc "+ a +" by "+ s.keynumb);
}
}
}
ofile.writeln(" ");
ofile.writeln(" ");
}
writeln("objective is "+thisOplModel.WH_income+" ");
writeln(" ");
}
else
{
writeln("No solution");
writeln(cplex.getCplexStatus());
break;
}
new_due ++;
for (var k=1; k<=thisOplModel.N_order;k++)
{
temp_order = Opl.item(thisOplModel.orders,k-1);
temp_order.due_time = new_due;
if (k==1)
{writeln("new due time for next iteration is "+temp_order.due_time);
writeln(" "); }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.