0

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.

  • You do know that the C++ classes like IloModel are just handle classes, and the instances of these handle classes just contain a pointer to the implementation of each instance. You can get these with the getImpl() member function. You would be better keeping pointers to the implementation instances. – TimChippingtonDerrick Oct 09 '15 at 12:42
  • Thanks for the suggestion. I tried getImpl() member function, did not fix the problem. What I am confused about is I tried only for one scenario and it is successful to get the instance implementation from pointer. However, when I add more scenarios, the error occurs. Any ideas will be highly appreciated. – Shakiba Enayati Oct 09 '15 at 15:36

0 Answers0