Decision Optimization

 View Only
Expand all | Collapse all

Reach a constraint based on a random number

  • 1.  Reach a constraint based on a random number

    Posted Mon August 31, 2020 03:29 AM
    Hello Everyone,

    I am looking for an efficient way to reach a specific constraint based on a random number.
    For instance, I have a random number=3, and I have the following constraint. 

    for (int i=0; i<5; i++){
     for (int j=0; j<7; j++){
      IloLinearNumExpr exp = cplex.linearNumExpr();
       exp.addTerm(1, Variable[i][j]);
       cplex.addLe(exp, 0);
     }
    }
    What is an efficient way to reach the 3rd constraint?
    I mainly need to know what are i and j in the 3rd constraint and use them for some operations.
    It is not effiecent to go over all constraints to match the random number with the constraint number (or any counter) and reach the constraint. 
    I know this case may be simple, but I have several IF-conditions after Foor-loops.

    Thank you so much for your help and time.


    ------------------------------
    Mehrzad Mehrabipour
    Ph.D. Student
    Graduate Research Assistant
    Civil, Construction, & Environmental Engineering Department
    North Carolina State University
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Reach a constraint based on a random number

    Posted Mon August 31, 2020 04:55 AM
    The most simple way to me seems to just store the information you want to have. You can create a class that holds the information for i and j
    class Info { public final int i; public final int j; public final IloRange r; public Info(int i, int j, IloRange r) { this.i = i; this,j = j; this.r = r; } }​

    With this, you can create your constraints like so

    Vector<Info> info = new Vector<Info>(); for (int i=0; i<5; i++){ for (int j=0; j<7; j++){ IloLinearNumExpr exp = cplex.linearNumExpr(); exp.addTerm(1, Variable[i][j]); info.add(new Info(i, j, cplex.addLe(exp, 0))); } }

    Then you can so something like info.elementAt(2) to get information about the 3rd constraint.



    ------------------------------
    Daniel Junglas
    ------------------------------