Search code examples
cplexopl

how to initialize a derived set that has fewer elements than the original set im OPL, CPLEX


tuple Combination_L {
  int labrooms;
  int instructors;
  int studentgroups;
  int labcourses;
};

{Combination_L} L = ...; 
                        
tuple Combination_T {
  int days;
  int slots;
};

{Combination_T} T = ...; 
                      
tuple Combination_L_T{
  int labrooms;
  int days;
  int slots;
  int instructors;
  int studentgroups;
  int labcourses;
};

{Combination_L_T} V_l; 
execute {
 for (var p in L){
   for (var q in T){
     V_l.add(p.labrooms, 
             q.days,
             q.slots, 
             p.instructors, 
             p.studentgroups,
             p.labcourses);
   };
 }; 
};

I want to initialize a derived set Q_l, which represents all possible assignments of room, lecturer, student group and course for the laboratory planning level. These assignments fulfill the condition that the value of the fourth element of the quadruple is l, which means that the lecturer assigned to the quadruple has the value l. These assignments are derived from the variable set V_l Hier ist my code:

{Combination_L} Q_l;

execute {
    for (var l in instructors) {
        Q_l += {<i, l, m, n> |
         <i, _, _, l, m, n> in V_l};
    };
};

I get a scripting parser error: missing expression. Please tell me where I made a mistake. Thanks so much!


Solution

  • You mix OPL modeling syntax and OPL scripting syntax. You could use slicing:

    tuple Combination_L {
      int labrooms;
      int instructors;
      int studentgroups;
      int labcourses;
    };
    
    
    {Combination_L} L = {<1,2,3,4>,<2,3,4,5>};
    
    tuple Combination_T {
      int days;
      int slots;
    };
    
    {Combination_T} T = {<1,2>,<2,3>};; 
                          
    tuple Combination_L_T{
      int labrooms;
      int days;
      int slots;
      int instructors;
      int studentgroups;
      int labcourses;
    };
    
    {Combination_L_T} V_l; 
    execute {
     for (var p in L){
       for (var q in T) {
         V_l.add(p.labrooms, 
                 q.days,
                 q.slots, 
                 p.instructors, 
                 p.studentgroups,
                 p.labcourses);
       };
     }; 
    };
    
    {int} instructors={i.instructors | i in L};
    
    {Combination_L_T} Q_l[i in instructors]={l |l in V_l :l.instructors==i};
    

    {Combination_L} Q_i[i in instructors]={ <j.labrooms,j.days,j.slots,j.instructors> | j in Q_l[i]};

    execute
    {
      writeln(Q_l);
      writeln(Q_i);
    };
    

    gives

    [{<1 1 2 2 3 4> <1 2 3 2 3 4>} {
            <2 1 2 3 4 5> <2 2 3 3 4 5>}]
     [{<1 1 2 2> <1 2 3 2>} {<2 1 2 3> <2 2 3 3>}]