C++ cplex variable problem in loop for and random number

Hi,

I'm new using Cplex callable library in a C++ environment
I got a problem in my program. I know where but I don't know why.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main() {
	IloEnv env; 
	IloInt i, j, k;
	IloModel model(env); 	
	IloInt pro= 4;
	IloInt time = 5;
	IloNumArray p(env, pro, 1000, 2000, 500, 1500);
 
	IloNumVarArray p(env, pro, 0, 1);
	NumVarMatrix y(env, pro);
 
	for(j = 0; j < time; j++) {
		y[j] = IloNumVarArray(env, time+1, 0, 1); 
	}
 
	for(j = 0; j < time; j++){
		IloExpr sum_over_k(env);			
		for(k = 0; k < pro; k++)
			sum_over_k += (y[j][k] * p[k]);
		model.add(sum_over_k <= 1);
		sum_over_k.end();
	}
 
	IloExpr sum_obj(env); 
	for(int k = 0; k < pro; k++)		
		sum_obj += p[k] * p[k]; 
	model.add(IloMaximize(env, sum_obj));
	
	IloCplex cplex(env);
	cplex.extract(model);
	cplex.solve();
 
       env.out() << cplex.getValue(sum_obj) ;
       env.end();
	return 0;
}



The problem appear at this line sum_over_k += (y[j][k] * p[k]) if I put a value p[k] it will work so I don't know why it can't work like this. Does anyone have an idea?

I also wanted to try to do random value like IloInt dt= (rand() % time) + 1 but it doesn't work with cplex (it will in c++). so how can do in cplex?

Thanks
Topic archived. No new replies allowed.