Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Adding the same variable twice in a User Cut Callback

    Posted 07/05/17 09:46 AM

    Originally posted by: vera_deschamps


    Dear Forum

    I am using the following (senseless, but for the purpose of the question slimmed-down) user cut callback:

     

    ILOUSERCUTCALLBACK1 (myUserCallback_fractional_cuts, IloBoolVarArray, variables) {

        IloEnv env = getEnv();

        

        // extract solution

        IloNumArray values (env);

        getValues (values, variables);

        

        //env.setNormalizer (IloFalse);

        

        IloExpr HM_cut_lhs (env);

        

        HM_cut_lhs += variables[0];

        HM_cut_lhs += variables[0];

        

        add (HM_cut_lhs >= 0);

     

        values.end();

        

        return;

    }

    This code appears to "freeze" on my computer: it waits indefinitely in __psunch_mutexwait when adding the constraint. This seems to be caused since I add the same variable twice to the constraint (otherwise it works as expected). Everything works if I uncomment the setNormalizer constraint.

    Could you please let me know if this behaviour is expected? I get the same behaviour when I use "IloTrue" in the setNormalizer constraint. In that case, however, I would have expected CPLEX to correctly deal with the fact that the same variable is added twice? Also, shouldn't an exception be raised, rather than the code freezing? (I am running in debug mode, that is, an assert (false); terminates the program.)

     

    Many thanks!


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Adding the same variable twice in a User Cut Callback

    Posted 07/05/17 10:41 AM

    Unfortunately, you have hit on a bug in CPLEX. There is indeed a deadlock here.

    The only workaround is to call env.setNormalizer(false) (potentially even before calling cplex.solve()) and make sure your expressions do not contain duplicates. If you cannot ensure that your expressions do not contain duplicates then you can use this small code to explicitly normalize them:

    #include <map>
    struct CompareNumVar {
       bool operator()(IloNumVar const &v1, IloNumVar const &v2) const {
          return v1.getId() < v2.getId();
       }
    };
    IloExpr normalize(IloExpr const &expr)
    {
       typedef std::map<IloNumVar, IloNum, CompareNumVar> CoefMap;
       CoefMap coefs;
       for (IloExpr::LinearIterator it(expr.getLinearIterator()); it.ok(); ++it) {
          CoefMap::iterator c = coefs.find(it.getVar());
          if ( c == coefs.end() )
             coefs.insert(CoefMap::value_type(it.getVar(), it.getCoef()));
          else
             c->second += it.getCoef();
       }
       IloExpr e(expr.getEnv());
       for (CoefMap::const_iterator it = coefs.begin(); it != coefs.end(); ++it)
          e += it->first * it->second;
       return e;
    }
    

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Adding the same variable twice in a User Cut Callback

    Posted 03/12/18 08:23 PM

    Originally posted by: Dinochek


    Hello Daniel, 

     

    I think I am experiencing the problem you have been talking about above with the deadlock.

    I was wondering how I could normalise my expression in java.

     

    Thank you.


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Adding the same variable twice in a User Cut Callback

    Posted 03/13/18 01:29 AM

    The problem described here was exclusive to C++. The deadlock did not exist in Java. Do you experience a deadlock in Java? Do you have some code to reproduce that?


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Adding the same variable twice in a User Cut Callback

    Posted 03/13/18 06:59 AM

    Originally posted by: Dinochek


    Hi Daniel,

     

    Ok, that's interesting because the solutions you described above seemed to have helped me (code attached). 

     

    I made the following changes to constraints:

    from defining minimum and maximum constraint separately like so

     

    for(int i=0; i<24; i++){

         cplex.addLe(demandEleHP[i],maxHPower);
         cplex.addLe(0,demandEleHP[i]);
    }

     

    to defining constraints as a range

    for(int i=0; i<24; i++){

         cplex.addRange(0, demandEleHP[i],maxHPower);
    }

     

    and I also set the thread count to 1:

    cplex.setParam(IloCplex.Param.Threads,1); 

     

    This seems to have solved the problem.

     

    I have cplex 12.6.3.

     

    Thank you.

     

     

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Adding the same variable twice in a User Cut Callback

    Posted 03/13/18 09:21 AM

    As far as I can tell, in your code you are not even using a callback? And you say the program still freezes? Can you tell at which statement it freezes?

    Normalization in Java and C++ uses a completely different codebase, so these are really different things.
     


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Adding the same variable twice in a User Cut Callback

    Posted 03/13/18 09:43 AM

    Originally posted by: Dinochek


    I see. It freezes when it implements 

    if (cplex.solve()) {

    ...

    }

    and doesn't give out an error, the console just stops outputting new information.

    It always occurs at a different point.

     

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Adding the same variable twice in a User Cut Callback

    Posted 05/10/18 01:18 PM

    Sorry, I just realized that I never got back to you. From your code snippet I could see an obvious problem. You will have to send a complete example if you want us to debug this.


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Adding the same variable twice in a User Cut Callback

    Posted 07/05/17 11:31 AM

    Originally posted by: vera_deschamps


    Thanks for the prompt response -- the normalization routine you suggested works fine!

    Best wishes


    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: Adding the same variable twice in a User Cut Callback

    Posted 01/22/18 03:23 AM

    The deadlock should be fixed in CPLEX version 12.8.


    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: Adding the same variable twice in a User Cut Callback

    Posted 01/22/18 04:03 AM

    Originally posted by: vera_deschamps


    Thanks!


    #CPLEXOptimizers
    #DecisionOptimization