Hi

I am working on a model that should be able to solve knapsack problems with different objective functions and right hand sides. The left hand side coefficients and the dimension of the knapsack problems will stay the same.

My idea is to make a class called KnSolver where in the constructor the IloEnv, the IloModel, the Cplex environment, the LHS coefficients and the variables (called x) are initialized as these should be used repeatedly. Further an IloExpr is created as lhs = IloScalProd(LHS , x).

The idea was to create a member function called KpSolve(double* objCoefs, int RHS) that takes as argument the obj.coefficients and the RHS. It could in pseudo code look like

void KpSolve(double* objCoefs, int RHS){
  IloNumArray CostCoefs(env);
  IloExpr Obj(env);
  for(j<m) CostCoefs.add( objCoefs[j] );

  Obj = IloScalProd( CostCoefs , x );
  IloMaximize( env , Obj );
  IloModel.add ( lhs <= RHS );
  Cplex.Solve()
}

This is generally not a problem. But in my model I have to repeatedly solve these knapsack problems with different obj.coefs and lhs's. Is there a way to alter the objective function and the constraint without having to destroy the hole model, and rebuild it every time I need to solve it?

Somethind like:

void KpSolve(double* objCoefs, int RHS){
  IloNumArray CostCoefs(env);
  IloExpr Obj(env);
  remove_old_obj;
  remove_old_rhs;
  for(j<m) CostCoefs.add( objCoefs[j] );

  Obj = IloScalProd( CostCoefs , x );
  IloMaximize( env , Obj );
  IloModel.add ( lhs <= RHS );
  Cplex.Solve()
}

(Actually I am maximizing the Lagrangean dual of the single-sourced CFLP when the demand constrains are relaxed using supergradient optimization, so a lot of knapsack problems need to be solved)

asked 23 Oct '12, 08:36

Sune's gravatar image

Sune
958414
accept rate: 20%

edited 23 Oct '12, 08:58

fbahr's gravatar image

fbahr ♦
4.6k716


You can modify the objective function using IloObjective::setLinearCoefs See the cutstock.cpp for an example of use.

More generally, if in CPLEX 12.4 documentation you search for "changing right hand side" you get to the "Modifying problems" page that lists all relevant APIs. For modification of rhs you have these :

IloRange::setLB, IloRange::setUB, IloRange::setBounds

link

answered 23 Oct '12, 09:49

jfpuget's gravatar image

jfpuget
2.5k310
accept rate: 8%

Thank you very much for the fast reply.

(24 Oct '12, 03:20) Sune

My pleasure. Can you formally accept the answer if it helped you?

(24 Oct '12, 04:17) jfpuget

That I can. However, I'm getting an Empty handle message when I implement it, and I don't know how to resolve it. In the constructor I do

IloObjective OBJ = IloMaximize(env);
for(int j=0; j<m; j++) obj.setlinearcoef(x[j],0);
model.add(obj);

just to initialize the objective. In the member function I do

for(int j=0; j<m; j++){
  OBJ.setLinearCoef(x[j],CostCoefs[j]);
}

where CostCoefs is an IloNumArray. It will compile, but when I run the code I get an Empty handle message. What am I doing wrong here?

link

answered 24 Oct '12, 05:59

Sune's gravatar image

Sune
958414
accept rate: 20%

I need to see the whole code as the issue could come from many places.

Note that you do not need to initialize the linear coefficients in the constructor, you can simply do:

IloObjective OBJ = IloAdd(model, IloMaximize(env, 0));

(24 Oct '12, 06:25) jfpuget

I'm using a class containing the kp-solver with a separate header file and then I make an instance of the kp-solver in my main function. I think I took on more than I could manage there, and have therefore just included it in the main function, with a messy but working code as a result...

(24 Oct '12, 07:25) Sune
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×191
×30
×21

Asked: 23 Oct '12, 08:36

Seen: 4,025 times

Last updated: 24 Oct '12, 07:25

OR-Exchange! Your site for questions, answers, and announcements about operations research.