Topic
7 replies Latest Post - ‏2013-03-11T08:16:30Z by SystemAdmin
SystemAdmin
SystemAdmin
754 Posts
ACCEPTED ANSWER

Pinned topic Variable bounds do not work together with objective function in C++ CPLEX

‏2013-03-07T17:05:12Z |
I am writing a linear programming model in C++ unsing the concert technology from cplex.

I declare the variables and the objective function of my model like this:

IloModel genInst (env);

IloNumVarArray Sizes(env,N,0.25+EPS,1.0-EPS);

IloObjective Weight = IloAdd(genInst, IloMaximize(env,IloSum(Sizes));

EPS is a small constant, because I want the variables to be strictly larger than 0.25 and strictly smaller than 1.0.

The objective function should just maximize the sum of all variables, but whenever I try this, the variables in the solution will have values outside my restriction (e.g. smaller than 0.25 or too large) and I don't see any possibilities to keep the variable values in their range.

Can someone please help me? I probably made some mistake because it shouldn't be that complicated. Thanks you!
Updated on 2013-03-11T08:16:30Z at 2013-03-11T08:16:30Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    754 Posts
    ACCEPTED ANSWER

    Re: Variable bounds do not work together with objective function in C++ CPLEX

    ‏2013-03-07T18:25:34Z  in response to SystemAdmin
    What exactly is your EPS and what are the invalid solution values you get? I just tried the following here and it worked without problem:
    
    #include <iostream> #include <ilcplex/ilocplex.h>   #define N 5 #define EPS 1e-6   
    
    int main(
    
    void) 
    { IloEnv env; IloModel genInst (env);   IloNumVarArray Sizes(env,N,0.25+EPS,1.0-EPS);   IloObjective Weight = IloAdd(genInst, IloMaximize(env,IloSum(Sizes)));   IloCplex cplex(genInst); cplex.exportModel(
    "bounds.lp"); cplex.solve(); IloNumArray vals(env); cplex.getValues(Sizes, vals); 
    
    for (IloInt i = 0; i < vals.getSize(); ++i) std::cout << vals[i] << std::endl;   
    
    return 0; 
    }
    

    How does the bounds.lp file look like when you plug in your EPS and your N? And what solution values do you get?
    • SystemAdmin
      SystemAdmin
      754 Posts
      ACCEPTED ANSWER

      Re: Variable bounds do not work together with objective function in C++ CPLEX

      ‏2013-03-07T21:45:44Z  in response to SystemAdmin
      Thanks a lot for your reply. I still have the problem, and I don't understand it.

      This is how I define the model (sorry, I don't know how to format the post nicely):

      #define EPS 1.0e-3

      IloModel genInst (env);
      IloNumVarArray Sizes(env,N,0.25+EPS,1.0-EPS);
      IloObjective Weight = IloAdd(genInst, IloMaximize(env,IloSum(Sizes)));

      Then I have a loop where I add constraints like this (i1,.. are indices):

      genInst.add(Sizesi1 + Sizesi2 + Sizesi3<=1);
      genInst.add(Sizesi1 + Sizesi2 + Sizesi3 - Sizes[j] >= 1+EPS);

      Whenever I don't have any expression for the objectiv function (e.g. just leave it to be 1), I get values in the range ]0.25,1.0[ after I solve the LP, e.g. like this:

      Report 5: Best sum of sizes 1
      Size values
      0.251 0.251 0.251 0.251 0.251 0.251 0.251 0.251 0.251 0.251 0.75 0.75 0.75 0.75 0.75

      But when I set the objectiv function to be the sum of the variables, the variables of the solution are outside the range, e.g.

      Report 5: Best sum of sizes 2.219
      Size values
      -0.001 1.517 -1.516 1.262 0.251 -1.516 -1.516 -1.012 -0.76 0.251 2.265 0.243 1.254 0.999 0.498

      I tried different possibilities like adding the range restrictions as further constraints, which does not work either so I guess something with the way I add the constraints is wrong. There I tried to follow the populate-by-row solution as in http://www.tu-chemnitz.de/mathematik/discrete/manuals/cplex/doc/getstart/html/ilocplex13.html.
      • SystemAdmin
        SystemAdmin
        754 Posts
        ACCEPTED ANSWER

        Re: Variable bounds do not work together with objective function in C++ CPLEX

        ‏2013-03-07T21:55:52Z  in response to SystemAdmin
        To format code nicely you should wrap it into '{code}' tags.
        One thing you should do is to add cplex.exportModel("model.lp") right before you call cplex.solve(). This writes the model that CPLEX will solve into model.lp which is a human readable format. Then look at the created model.lp file. Do the bounds in the file look correct? Maybe post the file here.
        • SystemAdmin
          SystemAdmin
          754 Posts
          ACCEPTED ANSWER

          Re: Variable bounds do not work together with objective function in C++ CPLEX

          ‏2013-03-07T22:16:11Z  in response to SystemAdmin
          Yes, I just tried this, and I saw that there was an error in the model (the - should always be a +). So now, it seems to work. Thanks a lot for your advise with the exportModel.
  • SystemAdmin
    SystemAdmin
    754 Posts
    ACCEPTED ANSWER

    Re: Variable bounds do not work together with objective function in C++ CPLEX

    ‏2013-03-10T09:23:24Z  in response to SystemAdmin
    I still have one further question, because the problem occurred again.

    I have a lot of constraints and the optimal solution returned by cplex does not satisfy the variable bounds.

    Because of my previous experience, I think it might be because some constraints are incompatible (but this time, this is not easily checked), but why does cplex return an optimal solution, which does not satisfy the variable bounds (e.g. some variables are negative), and it does not say that my program is infeasible?

    Thank you
    • SystemAdmin
      SystemAdmin
      754 Posts
      ACCEPTED ANSWER

      Re: Variable bounds do not work together with objective function in C++ CPLEX

      ‏2013-03-11T08:16:30Z  in response to SystemAdmin
      By how much does the solution violate the bounds?
      This may just be a matter of numerical roundoff. For example, a negative value like -1e-9 for a non-negative variable is within numerical tolerances, while a negative value of -0.1 is usually not.
      If the bound violation is not within numerical tolerances, could you attach your problem as SAV file and the offending solution as SOL file here?
  • SystemAdmin
    SystemAdmin
    754 Posts
    ACCEPTED ANSWER

    Re: Variable bounds do not work together with objective function in C++ CPLEX

    ‏2013-03-10T09:24:07Z  in response to SystemAdmin
    problem occurred again