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.0EPS);
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!
Topic
NOTICE: developerWorks Community will be offline May 2930, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
This topic has been locked.
7 replies
Latest Post
 20130311T08:16:30Z by SystemAdmin
ACCEPTED ANSWER
Pinned topic Variable bounds do not work together with objective function in C++ CPLEX
20130307T17:05:12Z

Answered question
This question has been answered.
Unanswered question
This question has not been answered yet.
Updated on 20130311T08:16:30Z at 20130311T08:16:30Z by SystemAdmin

ACCEPTED ANSWER
Re: Variable bounds do not work together with objective function in C++ CPLEX
20130307T18:25:34Z in response to SystemAdminWhat 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 1e6 int main( void) { IloEnv env; IloModel genInst (env); IloNumVarArray Sizes(env,N,0.25+EPS,1.0EPS); 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?
ACCEPTED ANSWER
Re: Variable bounds do not work together with objective function in C++ CPLEX
20130307T21:45:44Z in response to SystemAdminThanks 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.0e3
IloModel genInst (env);
IloNumVarArray Sizes(env,N,0.25+EPS,1.0EPS);
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 populatebyrow solution as in http://www.tuchemnitz.de/mathematik/discrete/manuals/cplex/doc/getstart/html/ilocplex13.html.
ACCEPTED ANSWER
Re: Variable bounds do not work together with objective function in C++ CPLEX
20130307T21:55:52Z in response to SystemAdminTo 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.
ACCEPTED ANSWER
Re: Variable bounds do not work together with objective function in C++ CPLEX
20130307T22:16:11Z in response to SystemAdminYes, 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.




ACCEPTED ANSWER
Re: Variable bounds do not work together with objective function in C++ CPLEX
20130310T09:23:24Z in response to SystemAdminI 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
ACCEPTED ANSWER
Re: Variable bounds do not work together with objective function in C++ CPLEX
20130311T08:16:30Z in response to SystemAdminBy how much does the solution violate the bounds?
This may just be a matter of numerical roundoff. For example, a negative value like 1e9 for a nonnegative 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?


ACCEPTED ANSWER
Re: Variable bounds do not work together with objective function in C++ CPLEX
20130310T09:24:07Z in response to SystemAdminproblem occurred again