Question

I am using CPLEX to solve a mip. My objective is to minimize the sum of the decision values which are positive. So I need to judge the sign of the decision value when I define the objective function. for example:

    IloNumVarArray pp(env);
    for (int i=0;i<N;i++)
    {
      pp.add(IloNumVar(env, -50.0, 50.0));
    }
    IloExpr obj(env);
    for (int in=0;in<N;in++)
    {     
       if (pp[in]>0)   ///error:this expression is wrong, how to judge the value? ///
       obj+=pp[in];
    }
    model.add(IloMinimize(env, obj));
    obj.end();

how can I properly express it? Can I get the value of "pp" when I define the objective function?

Was it helpful?

Solution

By definition, the value of a decision variable is not known, until after you do a solve, so (pp[i] > 0) returns an IloRange object not a boolean so you get a compile-time error.

To minimize the positive parts of a variable pp[i] you need to define a second variable that is nonzero, but greater than pp[i].

IloNumVarArray pp(env, N, -50, 50, ILOFLOAT);
IloNumVarArray pp_positive(env, N, 0, IloInfinity, ILOFLOAT);
for (int i=0; i<N; ++i)
{
  model.add(pp[i] - pp_positive[i] <= 0);
}
model.add(IloMinimize(env, IloSum(pp_postiive));

Because pp_positive appears only in the objective function, it will take on the value of pp[i] if pp[i] is positive and the value 0 if pp[i] is negative.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top