Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Solving my model iteratively with different variables

    Posted 01/06/14 05:48 PM

    Originally posted by: optimiz


    Hi all,

    I have a cplex concert c++ code. I have a question about it and posted that on another thread here https://www.ibm.com/developerworks/community/forums/html/topic?id=cad78d12-a020-4bc2-90b6-a9d90ecfef66. I could not see any replies, so I thought maybe I posted on a less relevant place, and wanted to ask here. I am sorry if this is an inappropriate type of copy and paste, but I can edit to get it into a better form. 

    Thanks


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Solving my model iteratively with different variables

    Posted 01/07/14 03:23 AM

    The link you gave is into the CP Optimizer Forum. The CP Optimizer forum is for questions about the CP optimizer engine, not for questions about the CPLEX engine. Please re-post the full question here (it is easier to answer it if we can see the full question) and delete it from the CP Optimizer Forum.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Solving my model iteratively with different variables

    Posted 01/07/14 04:25 PM

    Originally posted by: optimiz


    Hi again,

     

    Thanks for the above help Mr. DanielJunglas. I am new to this forum, so sorry about my confusion. My original question is:

    I am working on an optimization model which is an MIP. I am changing one of the variables "y" that is a binary variable and solve the model again. I keep doing this for a predetermined number of iterations. The sample code is:

    int main()
    
    {
    
         //randomly generate y arrays. for example, y can be y={1,1,1,1,1,1,0,0,0}. This will be updated throughout the iterations.
    
    for(int j=0; j<100; j++)
    {
        problem(y);
    }
    
    }
    
    int problem(int y[])
    
    {
    
       IloEnv env;
    
       try{
    
    IloModel model(env);
    
    // definition of variables and constraints here. One of the constraints is below:
    
    IloRangeArray C1(env, 20, 0, 0);
    
    //Objective:
    
    IloObjective objective;
    
    IloObjective objective = IloMinimize(env);
    
    model.add(objective);
    
    for(IloInt i = 0; i < 20; i ++){
                objective.setLinearCoef(f[i], p[i]*y[i]);
            }
    
      for(IloInt i = 0; i < 20; i++){
            C1[i].setLinearCoef(x[i],1);
            C1[i].setLinearCoef(y[i],-1);
            model.add(C1[i]);
          }
    
          C1.end();
    
          IloCplex cplex(model);
          cplex.setOut(env.getNullStream());
          cplex.solve();
          
           return cplex.getObjValue();
    
        env.end();
     }
       catch (IloException& ex) {
          cerr << "Error: " << ex << endl;
       }
       catch (...) {
          cerr << "Error" << endl;
       }
    }
    

    There exists a huge memory leak with this implementation. How would I be able to eliminate this leak? I have tried removing the constraints but it did not seem to help. I appreciate any suggestions. Thanks


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Solving my model iteratively with different variables

    Posted 01/10/14 01:44 AM

    There are a number of C++ programming errors in your code (all unrelated to CPLEX). The leak is caused by this:

    return cplex.getObjValue();
    env.end();

    env.end() is never called, so memory is never released. This should be rewritten to

    IloNum result = cplex.getObjValue();
    env.end();
    return result;

    Moreover, your code will leak again if an exception occurs since in that case env.end() is not called either. In case of an exception there is also no 'return' statement, so the function will return random values. Doesn't your compiler complain about this? Finally, the function returns 'int' while cplex.getObjValue() returns a 'double'. You should at least round(), floor() or ceil() the result or you will likely get unexpected truncations.


    #CPLEXOptimizers
    #DecisionOptimization