Originally posted by: Laser.Cho
Hi. I made a source file to solve a problem with my latest CPLEX with Visual C++, and I did it.
but my original code is too ugly to let it be. so, I'm trying to make it better code. But I cannot accumulate variable array value with loop sentence.
Funny thing is I can add all elements by hard coding.
here is the summarized code.
===================original code(it works. just ugly)==================================
typedef IloArray<IloIntVarArray> intvar2array;
intvar2array route(env, 9);
for (int i = 0; i < 9; i++)
route[i] = IloIntVarArray(env, 10, 0, 1);
// bla bla ~~~
// constraints
IloExpr tmp(env,0), tmp2(env,0);
for (int x = 1; x <= 8; x++) {
tmp = route[x][0] + route[x][1] + route[x][2] + route[x][3] + route[x][4] + route[x][5] + route[x][6] + route[x][7] + route[x][8] + route[x][9];
tmp2 = route[0][x] + route[1][x] + route[2][x] + route[3][x] + route[4][x] + route[5][x] + route[6][x] + route[7][x] + route[8][x];
model.add(tmp==tmp2);
}
==========================================
================but it doesnt work.===================
for (int x = 1; x <= 8; x++) {
for (int j = 0; j < 10; j++)
tmp += route[x][j];
// tmp = route[x][0] + route[x][1] + route[x][2] + route[x][3] + route[x][4] + route[x][5] + route[x][6] + route[x][7] + route[x][8] + route[x][9];
tmp2 = route[0][x] + route[1][x] + route[2][x] + route[3][x] + route[4][x] + route[5][x] + route[6][x] + route[7][x] + route[8][x];
model.add(tmp == tmp2);
}
==================================================
sometimes it show the message "+= operand does not type INT and ILOINTVARARRAY blabla~" sorry i forgot the message.
but curious thing is why the "hard coding part" code works? and then why it doesnt work in "loop coding" code?
#CPLEXOptimizers#DecisionOptimization