Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Constraining over colums of variable array

    Posted 03/04/12 07:41 PM

    Originally posted by: R9UA_Ishan_Mukherjee


    I am very new to using CPLEX and am having trouble figuring out how to sum over columns of a variable array for a constraint.

    This is what I did for rows, which appears to work:

    for(int i=0; i<size; ++i)
    {
    model.add(IloSum(variables[i]) == 1);
    }

    Where variables is a 2 dimensional array. I want to do model.add(variables[0][0] + variables[1][0] + variables[2][0] + ... == 1)
    model.add(variables[0][1] + variables[1][1] + variables[2][1] + ... == 1)

    But I want it to loop so I can have something like the for loop above. How can I do that?
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Constraining over colums of variable array

    Posted 03/04/12 07:55 PM

    Originally posted by: R9UA_Ishan_Mukherjee


    I came up with this but I want a cleaner way:

    IloExpr onePerson(env);
    for(int j=0; j<size; ++j)
    {
    for(int i=0; i<size; ++i)
    {
    onePerson += variables[i][j];
    }
    model.add(onePerson == j+1);
    }

    Specifically I don't like how I constantly add onePerson where and never set it back to 0. When I do onePerson = 0; at the top of the loop I run into problems. Is there another way to clear an expression?
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Constraining over colums of variable array

    Posted 03/05/12 01:47 AM

    Originally posted by: SystemAdmin


    Why not instantiate IloExpr inside the loop:
    for(int j=0; j<size; ++j) {
       IloExpr onePerson(env);
       for(int i=0; i<size; ++i) {
          onePerson += variables[i][j];
       }
       model.add(onePerson == j+1);
    }
    

    Wouldn't that give you what you need?
    I cannot think of a cleaner way to create your sum. Maybe you can do something similar to std::for_each but that sounds like overkill here.
    #CPLEXOptimizers
    #DecisionOptimization