Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
6 replies Latest Post - ‏2013-08-02T14:35:25Z by EBKN_Eduardo_Aime
EBKN_Eduardo_Aime
EBKN_Eduardo_Aime
7 Posts
ACCEPTED ANSWER

Pinned topic Search strategy by Index value.

‏2013-07-30T16:07:15Z |

Are there any way to use phases, in Opl Script,  to order all variables in a model by index value?
First all varibles by index value 1, next all variables by index value 2, and so on.
Thanks for your help

Updated on 2013-07-31T01:55:33Z at 2013-07-31T01:55:33Z by EBKN_Eduardo_Aime
  • Philippe_Refalo
    Philippe_Refalo
    48 Posts
    ACCEPTED ANSWER

    Re: Search strategy by Index value.

    ‏2013-07-31T12:30:05Z  in response to EBKN_Eduardo_Aime

    Using search phases, you can branch on variables following the index (i.e. the position) they have in a given array. For this purpose you need to use cp.factory.varIndex(dvar int[]).

    You can also use cp.factory.explicitVarEval(dvar int[], int[]) to define explicitely the evaluation of some variables. In your case, you can define the evaluation as the index value of the variables (I assume they are in several different arrays). Then choosing with cp.factory.selectSmallest will branch on the variables having the smallest index first.

    Regards

    Philippe

    • EBKN_Eduardo_Aime
      EBKN_Eduardo_Aime
      7 Posts
      ACCEPTED ANSWER

      Re: Search strategy by Index value.

      ‏2013-08-01T00:44:05Z  in response to Philippe_Refalo

      Philippe 

      Thank you for your advice

      I will thank you a lot if you tell me the sintax for the following case:

      Several (3 in this example) variables  with the following indexes (2 each)

      Var1[n][t]  ,   Var2[n][t] ,   Var3[k][t]

      n=1..3,    t = 1…4,    k = 1..2

      I need to order the variables on index t,  all varibles with t=1 first, next all variables with t=2, and so on:

       

      All t= 1

      Var1[1]][1], Var2[1][1] , Var3[1][1] ,

      Var1[2][1] , Var2[2][1] , Var3[2][1] ,

      Var1[3]][1], Var2[3][1] ,

      Next all t=2

      Var1[1]][2], Var2[1][2] , Var3[1][2] ,

      Var1[2][2] , Var2[2][2] , Var3[2][2] ,

      Var1[3]][2], Var2[3][2] ,

      And so on…

       

      Thank you in advance,

      Eduardo.

      • Philippe_Refalo
        Philippe_Refalo
        48 Posts
        ACCEPTED ANSWER

        Re: Search strategy by Index value.

        ‏2013-08-01T07:14:52Z  in response to EBKN_Eduardo_Aime

        For using search phases, you need to pass flat arrays, so there are two possibilities:

        - either you create a flat array and a search phase for each group of variables t=1, t=2 and so on...

        - either you flatten all these variables into one array and use the explicitVarEval evaluator to give the evaluation 1 for variables in t = 1, 2 for variables in t = 2 and so on...

        Note that you can still use another criteria for selecting variables one grouped, for instance you can further filter variables with cp.factory.selectLargest(cp.factory.varImpact()) this branch first on the variables having the largest impact on search among those that have been selected at the previous stage using their index. 

        Regards 

        Philippe

        • EBKN_Eduardo_Aime
          EBKN_Eduardo_Aime
          7 Posts
          ACCEPTED ANSWER

          Re: Search strategy by Index value.

          ‏2013-08-01T16:30:17Z  in response to Philippe_Refalo

           Philippe:

          Trying to flattening the bidimensional decision variables, the function "searchPhase" give me the following error: (using the example "car")
          Execution of "SearchPhases" failed: "C:...Application Data\IBM\ILOG\CPLEX_Studio125\workspace\car\car.mod", line 67: Wrong argument type. The first argument contains an unsupported element. Expected: "dvar", found "object".: null.

           I'm doing something wrong?

          /***************************************************/

          using CP;

           

          int nbCars= ...; // # of cars

          int nbOptions= ...;// # of options

          int nbSlots= ...;// # of slots

           

          range Cars= 1..nbCars;

          range Options= 1..nbOptions;

          range Slots= 1..nbSlots;

           

          int demand[Cars] = ...;

          int option[Options,Cars] = ...; 

          tuple Tcapacity{

          int l;

          int u;

          };

          Tcapacity capacity[Options] = ...;

          int optionDemand[i in Options] = sum(j in Cars) demand[j] * option[i,j];

           

          dvar int slot[Slots] in Cars;

          dvar int setup[Options,Slots] in 0..1;

           

          /* ::::::::::: Phase def for bi-dim dvars ::::::::::::::::::::*/

          execute SearchPhases{

          var vPhase= cp.factory;

          var elements= (nbOptions* nbSlots) - 1

           

          var phaseArray= new Array(elements);

          var j= 0;

          for(var iOpt= 1; iOpt==nbOptions; iOpt++)

          for(var iSlot= 1; iSlot==nbSlots; iSlot++)

          phaseArray[j]=setup[iOpt][iSlot]; j++;

          /* This approach give me the error: Wrong argument type. Expected "dvar", found "object", "null" */

          cp.setSearchPhases(vPhase.searchPhase(phaseArray));

          }

          /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/

           

          subject to{

          // # of cars = demand

          forall(c in Cars)

          sum(s in Slots) (slot[s] == c) == demand[c];

           

          forall(o in Options, s in 1..(nbSlots- capacity[o].u+ 1) )

          sum(j in s..(s+ capacity[o].u- 1)) setup[o,j] <= capacity[o].l;

          forall(o in Options, s in Slots)

          setup[o,s] == option[o][slot[s]];

          forall(o in Options, i in 1..optionDemand[o])

          sum(s in 1.. (nbSlots- i* capacity[o].u)) setup[o,s] >=

          optionDemand[o] - i* capacity[o].l;

          };

          • Philippe_Refalo
            Philippe_Refalo
            48 Posts
            ACCEPTED ANSWER

            Re: Search strategy by Index value.

            ‏2013-08-02T07:52:10Z  in response to EBKN_Eduardo_Aime

            There are several mistakes in the loops you wrote, the continuation test must be  iOpt <nbOptions and j++ is not inside the loop. If you rewrite it to:

              for(var iOpt = 1; iOpt <= nbOptions; iOpt++){
                for(var iSlot = 1; iSlot <= nbSlots; iSlot++){
                  phaseArray[j]=setup[iOpt][iSlot]; 
                  j++;
                }
              }          
             

            then you have you want.

            Please use displays (write, writeln) to check the structures that you compute in order to debug your code before posting.

            Regards

            Philippe