Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Problem about IloExprArray::add

    Posted 05/13/12 09:17 AM

    Originally posted by: jefflee2011


    I am using Cplex 12.3 with vs2008.
    The pseudo-code is as follows.

    IloExprArray exprA;
    ...
    function1
    {
      IloExpr expr(env);
      ...
      exprA.add(expr);
    }
    ...
    function2
    {
      for(i=1;i<n;i++)
      {
        ...
        function1;
      }
    }
    


    In the loop, each time function1 is executed, the element added into IloExprArray last time disappears. In other words, the element is added to the first position of exprA every time in the loop. I guess expr is deleted after function1 is executed, causing elements not really added into IloExprArray. I don't know how to solve the problem.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Problem about IloExprArray::add

    Posted 05/13/12 09:46 AM

    Originally posted by: SystemAdmin


    If exprA is indeed a global variable that is properly initialized then I don't see why your code should not work. Do you call exprA.clear() anywhere? Or do you initialize the array more than once? Maybe print out exprA.getImpl() every time you enter function1() and make sure that the same value is printed each time.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Problem about IloExprArray::add

    Posted 05/13/12 10:01 AM

    Originally posted by: jefflee2011


    exprA is a data member of a class, and function1 and function2 are member functions of the same class.
    I don't call exprA.clear() anywhere, but I write the code as follows in function1.
    function1
    {
      exprA = IloExprArray(env);
      ...
    }
    

    Maybe that is the problem?
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Problem about IloExprArray::add

    Posted 05/13/12 10:10 AM

    Originally posted by: SystemAdmin


    Yes, of course that is the problem!
    Every time you call function1() you initialize exprA again. Right after the line
    exprA = IloExprArray(env)
    

    exprA is a new and empty array.
    You probably should initialize exprA in the class's constructor (and call exprA.end() in the class's destructor)?
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Problem about IloExprArray::add

    Posted 05/13/12 10:16 AM

    Originally posted by: jefflee2011


    Thank you so much. I will try that.
    Another question, when should I call expr.end()?
    If I write
    exprA.add(expr);
      expr.end();
      env.out << exprA <<endl;
    

    it seems that expr is not added into exprA.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Problem about IloExprArray::add

    Posted 05/13/12 10:26 AM

    Originally posted by: SystemAdmin


    I think IloExprArray::add(IloExpr) does not create a deep copy of the added expression. So you should only call expr.end() when you no longer need that expression. Or just call exprA.endElements() right before calling exprA.end().
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Problem about IloExprArray::add

    Posted 05/14/12 03:52 AM

    Originally posted by: jefflee2011


    Thanks a lot. I have solved the problem.
    By the way, I think it is best to initialize exprA in the class's constructor. However, before the model is builded the class is instantiated when reading data from files, so IloEnv can not be passed to the constructor. In that case, I call another function to initialize exprA before function2 is executed. I was wondering if you could suggest a better way?
    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Problem about IloExprArray::add

    Posted 05/16/12 04:32 AM

    Originally posted by: SystemAdmin


    The only way I could think is to move exprA out of the class. I have no idea whether this makes sense in your design or not.
    On the other hand, I don't think that having a function to set the IloEnv for your instance is a bad thing. So I would keep your code as is :-)
    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Problem about IloExprArray::add

    Posted 05/17/12 04:33 AM

    Originally posted by: jefflee2011


    Many thanks :-)
    #CPLEXOptimizers
    #DecisionOptimization