I am coding a decomposition algorithm including scenario subproblems. I used model pointer to create subproblem optimization models. Then, it is needed to modify the objective function coefficient of each subproblem as the algorithm proceeds. I used pointers to avoid creating the subproblem models every time from scratch. The code is shown partially as follows; the subproblem model pointers (for 2 scenarios) are generated and then objective function coefficients for one scenario subproblem must change using IloObjective::setLinearCoefficients. The modified subproblem must be solved again:

int main (int argc, char **argv)
{
   int ScenearioNum=2;//Number of scenarios in small example 
   IloEnv env;
   try {

       //Generate the subproblem model pointers:

      IloModel* MaxProblemPtr= new(env) IloModel[ScenearioNum];
      IloObjective* MaxObjPtr= new(env) IloObjective[ScenearioNum];
      for (int s=0;s<ScenearioNum;s++){

         IloModel MaxProblem(env);
         *(MaxProblemPtr+s)=MaxProblem;

          IloObjective MaxObj= IloAdd(MaxProblem, IloMaximize(env));

         *(MaxObjPtr+s)=MaxObj;

          IloRangeArray ConstMax;

          if (s==0){
              ConstMax=IloAdd(MaxProblem,
                               IloRangeArray(env, - IloInfinity,RHS_sub1));
          }else{
              ConstMax=IloAdd(MaxProblem,
                               IloRangeArray(env, - IloInfinity,RHS_sub2));
          }

          for (int j=0;j<2;j++){
               X.add(IloNumVar(MaxObj(1)+ConstMax[0](1)));
               Y.add(IloNumVar(MaxObj(2)+ConstMax[1](1)));
          }

           IloCplex maxcplex(MaxProblem);

           maxcplex.solve();

        if (maxcplex.solve()) {
            double currentObj=maxcplex.getObjValue();
            cout<<"**max Objective function for s"<<s+1<<" :"<<                                                   currentObj<<endl;
        }
      }

   //CHANGING OBJECTIVE FUNCTION OF FIRST SCENARIO

    IloNumArray XCoeff(env);
     IloObjective tempMaxObj=*MaxObjPtr;
     tempMaxObj.setLinearCoefs(X,XCoeff);
     IloModel Maxproblem=*MaxProblemPtr;    
     IloCplex maxcplex(Maxproblem);

  }
}

  catch (IloException& ex) {
     cerr << "Error: " << ex << endl;
  }
  catch (...) {
   cerr << "Error" << endl;
  }

   env.end();

 return 0;
}

But I get the Unhandled exception error in line of "tempMaxObj.setLinearCoefs". I have no idea how to fix this since the model pointer seems to be working correctly. I will appreciate if anyone helps me with that. Thanks.

asked 08 Oct '15, 15:26

ORSE's gravatar image

ORSE
52
accept rate: 0%

edited 08 Oct '15, 20:50

It's hard to read code when the formatting is slightly off, but it looks to me as if there might be an extra closing brace between the try and catch.

If I'm wrong, then the exception should be caught, in which case you should know what the exception is.

(09 Oct '15, 16:55) Paul Rubin ♦♦
Be the first one to answer this question!
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
×9
×1

Asked: 08 Oct '15, 15:26

Seen: 520 times

Last updated: 09 Oct '15, 16:55

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