Decision Optimization

Expand all | Collapse all

How to iterate in 2-D array in CPLEX any alternate

Jump to Best Answer
  • 1.  How to iterate in 2-D array in CPLEX any alternate

    Posted 14 days ago
    Hi,
    I want to model thesein figure in CPLEX OPL, but CPLEX does not give opportunity to iterate in the multidimensional arrays. Cst and Tst are binary matrices, and the idea is to only sum for non zero elements.
    How can I do this please.
    Cst = [0 0 1]
             [1 0 1]
             [0 1 0]



    ------------------------------
    Muhammad Usama
    ------------------------------


  • 2.  RE: How to iterate in 2-D array in CPLEX any alternate
    Best Answer

    Posted 14 days ago
    Hi

    range S=1..3;
    range T=1..3;
    
    int Cst[S][T] = [[0, 0 ,1],
             [1 ,0, 1],
             [0 ,1 ,0]];
             
    dvar boolean x[S][T];
    
    dvar int su; // sum all x such as Cst is 1
    
    maximize su;
    subject to
    {
      su==sum(s in S,t in T:Cst[s][t]==1) x[s][t];
    }   ​


    works fine and gives su=4



    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------



  • 3.  RE: How to iterate in 2-D array in CPLEX any alternate

    Posted 12 days ago
    Thank you, I have another question
    how can extract certain element from string set?
    basically I want to change index of an array to string set like
    v_1[{"a" "b"}][{"c" "d"}] = v[1..2][1..2]

    ------------------------------
    Muhammad Usama
    ------------------------------



  • 4.  RE: How to iterate in 2-D array in CPLEX any alternate

    Posted 12 days ago
    Hi

    int v[1..2][1..2]=[[1,2],[3,4]];
    
    {string} s1={"a", "b"};
    {string} s2={"c", "d"};
    
    int v1[i in s1][j in s2]=v[1+ord(s1,i)][1+ord(s2,j)];
    
    execute
    {
      writeln(v1);
    }​

    gives

     [[1 2]
             [3 4]]


    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------



  • 5.  RE: How to iterate in 2-D array in CPLEX any alternate

    Posted 12 days ago
    Thank you very much I has used loops to fill all arrays but this is so simple.
    another question is how to take intersection or union of two sets in constraints?
    like set Jp and and set Js
    Jp
    J1 J2 J3 J4
    P1     1  
    P2     1  
    P3     1  
    P4     1  
    P5     1  

    Js (Procesing Units)
    J1 J2 J3 J4
    S1 1 1 1 1
    S2 1 1 1  

    how can I implement ?

    ------------------------------
    Muhammad Usama
    ------------------------------



  • 6.  RE: How to iterate in 2-D array in CPLEX any alternate

    Posted 12 days ago
    Edited by ALEX FLEISCHER 12 days ago
    Hi,

    you can use inter

    In the documentation you have a small example:

    //This code line initializes rInter to {2}.
    
    
    {int} i[1..3] = [ {1, 2}, {2, 3}, {2, 5} ]; 
    {int} rInter = inter( x in 1..3 ) i[x];
    
    ​


    and with your values you could start with

    {int} P=asSet(1..5);
    {int} J=asSet(1..4);
    {int} Jp[p in P]={3};
    
    assert forall(p in P) forall(x in Jp[p]) x in J;
    
    {int} S={1,2};
    
    {int} Js[S]=[{1,2,3,4},{1,2,3}];
    
    int v[s in S][p in P]=sum(j in (Js[s] inter Jp[p])) i;
    
    execute
    {
      writeln(v);
    }


    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------