Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

How to change coefficient in constraint with aggregate expression from IloScript?

  • 1.  How to change coefficient in constraint with aggregate expression from IloScript?

    Posted 09/20/13 10:48 AM

    Hi there,

    I have this OPL model:

    range I = 1..3;
    dvar boolean x[I];

    maximize sum(i in I) x[i];
    subject to {
      c1: x[1] + x[2] + x[3] <= 3;
      c2: sum(i in I) x[i] <= 3;
    }

    main {
      thisOplModel.generate();
      cplex.solve();
      writeln(thisOplModel.x[1], " " , thisOplModel.x[2], " " ,thisOplModel.x[3]);
      // Changing c1 works without a problem
      thisOplModel.c1.UB = 2;
      thisOplModel.c1.setCoef(thisOplModel.x[1], 2);
      thisOplModel.c1.setCoef(thisOplModel.x[2], 2);
      thisOplModel.c1.setCoef(thisOplModel.x[3], 2);
      // Changing c2 throws "You can not change the coefficient of an aggregate expression"  
      thisOplModel.c2.UB = 3;
      for (var i in thisOplModel.I) {
        thisOplModel.c2.setCoef(thisOplModel.x[i], 3); // this line throws
      }    
      cplex.solve();
      writeln(thisOplModel.x[1], " " , thisOplModel.x[2], " " ,thisOplModel.x[3]);
    }

    As you can see, I am trying to dynamically change coefficients and right-hand sides of constraints c1 and c2. This works well for c1 (seemingly because I just explicitly list all terms of the sum). For c2 I get a "You can not change the coefficient of an aggregate expression"  error. So my question is: how can I avoid this error and change the coefficients of c2 in the script? I looked at the reference documentation of setCoef() of class IloConstraint but that did not even mention any limitation. According to this section in the manual, "The method IloCplex.setCoef is available for all CPLEX linear constraints." Clearly c2 is a linear constraint, so I am wondering what is the right way to use setCoef() here?

    Thank you for any help.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: How to change coefficient in constraint with aggregate expression from IloScript?

    Posted 09/20/13 12:13 PM

    Hi,

    in the doc for setcoef you may read

    Changes the coefficient of a decision variable in the invoking constraint. This method is limited to constraints that use simple real linear expressions with no aggregation. In other cases, use the method from the IloCplex class.

     

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: How to change coefficient in constraint with aggregate expression from IloScript?

    Posted 09/20/13 12:20 PM

    So the right way is to turn

     

    thisOplModel.c2.setCoef(thisOplModel.x[i], 3);

     

    into


        cplex.setCoef(thisOplModel.c2,thisOplModel.x[i], 3);

     

    I hope this helps

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: How to change coefficient in constraint with aggregate expression from IloScript?

    Posted 09/20/13 03:37 PM

    How could have overlooked that. Thanks a lot. Using cplex.setCoef() did indeed solve the problem.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer