I'm new using cplex and I try to find some information on internet but didn't find clear stuff to help me in my problem.
I have P[k] k will be equal to 1 to 4
and I have a decision variable x[i][k] must be equal to 0 or 1 (also p[k])
the i is between 1 to 5
For now I do like this
IloEnv env;
IloModel model(env);
IloNumVarArray p(env);
p.add(IloNumVar(env, 0, 1));
p.add(IloNumVar(env, 0, 1));
p.add(IloNumVar(env, 0, 1));
IloIntVar x(env, 0, 1);
model.add(IloMaximize(env, 1000 * p[1] + 2000 * p[2] + 500 * p[3] + 1500 * p[4]));
for(int k = 1; k <= 4; k++){
for(int i = 1; i <= 5; i++){
model.add(x[i][k] + x[i][k] + x[i][k] + x[i][k] + x[i][k] => 2 * p[k]; );
}}
The loop should do something like this:
x[1][1] + x[2][1] + x[3][1] + x[4][1] + x[5][1] => 2 * p[1];
x[1][2] + x[2][2] + x[3][2] + x[4][2] + x[5][2] => 2 * p[2];
x[1][3] + x[2][3] + x[3][3] + x[4][3] + x[5][3] => 2 * p[3];
x[1][4] + x[2][4] + x[3][4] + x[4][4] + x[5][4] => 3 * p[4];
but I'm far away from this result.
Does anyone have an idea?
Thanks
You probably want to use an IloNumExpr
You also need to declare x as a 2-dimensional array.
Also, in c++, array indices are from 0 to size-1, not 1 to size. Your objective should be written
Usertfwr already gave a good answer, but I would like to give another version of solution which might help you to code CPLEX applications in a more generic way. First, I would suggest you to use a text file to hold all the data (objective function coefficients) which will be fed into the program. In your case, you only have to copy literally the following matrix like data to notepad and name it as “coef.dat”:
[1000, 2000, 500, 1500]
Now comes the full code, let me know if have difficulties understanding any statement: