Originally posted by: rdumeur
Dear Hossein,
First, doing ((S[i]+D[W[i]]>j)&&(S[i]<j))* W[i]).end() doesn't actually delete the initial expression. It immediately deletes the newly ((S[i]+D[W[i]]>j)&&(S[i]<j))* W[i]) created expression.
If you actually need to delete the expression it would be safer to record each created ((S[i]+D[W[i]]>j)&&(S[i]<j))* W[i]) expression element into an array and then delete each array element in reverse order after solve. For that, you can't use += on the objective expression.
IloExtractableArray record(env);
// create expr recording array
record.add(obj);
for(i = 0; i < n; i++){
for (j = 0; j < m; j++){
IloNumExpr eltdw(D[W[i]]);
IloNumExpr sum1(S[i]+eltdw);
IloNumExpr cmp1(sum1 > j);
IloNumExpr cmp2(S[i] < j);
IloNumExpr conj(cmp1 && cmp2);
IloNumExpr weight(conj * W[i]);
IloNumExpr newobj(OBJECTIVE + weight);
record.add(eltw);
record.add(sum1);
record.add(cmp1);
record.add(cmp2);
record.add(conj);
record.add(weight);
record.add(newobj);
OBJECTIVE = newobj;
}
then after solve you can do :
for(int i(record.getSize()-1); i >= 0; --i)
record[i].end();
record.end(); // array no longer needed.
Note that if you are doing very complex expressions that need to be later deleted, then it would be better to create your own expression factory object (MyExprFactory) that would record each created expression:
MyExprFactory f(env); // create my factory object that owns a recording array
OBJECTIVE = f.constant(0);
for(i = 0; i < n; i++)
for (j = 0; j < m; j++)
OBJECTIVE = f.plus
(OBJECTIVE, f.times
(f.conjunction
(f.gt
(f.plus
(S[i],f.element(D,W[j])), j),
f.lt(S[i], j)), W[i]));
MySolve(OBJECTIVE);
f.endCreatedExprs(); // destroy all the recorded exprs.
I hope this helps.
Cheers,
#CPOptimizer#DecisionOptimization