Topic
5 replies Latest Post - ‏2013-12-08T03:31:36Z by MarekGrzes
MarekGrzes
MarekGrzes
10 Posts
ACCEPTED ANSWER

Pinned topic arg max in OPL?

‏2013-12-06T20:02:45Z |

Hello,

Is there someghing like arg max in OPL? For example, this line

maxCap  = max(i in 1..nbCap) capacities[i];

retruns max value. Is there an equivalend operator that can return the index of the max value?

Thanks,

Marek

  • AlexFleischer
    AlexFleischer
    453 Posts
    ACCEPTED ANSWER

    Re: arg max in OPL?

    ‏2013-12-07T08:43:25Z  in response to MarekGrzes

    Hi,

     

    let me give you an example:

    int nbCap=5;
    int capacities[i in 1..nbCap]=i mod 3;

    int maxCap  = max(i in 1..nbCap) capacities[i];
    int argMax= first({i | i in 1..nbCap : capacities[i]==maxCap});

    execute
    {
      writeln(argMax);
    }

     

    regards

    • s.st1led
      s.st1led
      2 Posts
      ACCEPTED ANSWER

      Re: arg max in OPL?

      ‏2013-12-07T16:44:14Z  in response to AlexFleischer

      I'm afraid this only works if the target array (capacities in this case) is constant. If it is variable, you could try something like

      dexpr int argMax = sum(i in 1..nbCap) (capacities[i]==maxCap)*i;

      but if there is more than one maximal element in capacities this doesn't work as well. I'm not sure what you could do if there are multiple elements with the maximum value and you want to retrieve all their indices.

       

      Regards,

      Stefano

      • MarekGrzes
        MarekGrzes
        10 Posts
        ACCEPTED ANSWER

        Re: arg max in OPL?

        ‏2013-12-07T19:33:05Z  in response to s.st1led

        Thank you guys for your replies.

        In my case, capabilities would be a decision variable infortunately and there are multiple elements with the maximum value because the array is binary with values 0 and 1. What I need in fact is to compute the highest index with value 1. For example, if the array is

        [ 1, 0, 0, 1, 1] then I would like to get 5, because 5 is the highest index with the highest value in the array (the array is a decision variable).

        If anybody knows how to code it efficiently, please let me/us know.

         

        Cheers,

        Marek

         

        • s.st1led
          s.st1led
          2 Posts
          ACCEPTED ANSWER

          Re: arg max in OPL?

          ‏2013-12-07T19:45:46Z  in response to MarekGrzes

          I was just thinking that if you only the biggest index that has the maximal value, you can replace sum by max and write:

          dexpr int argMax = max(i in 1..nbCap) (capacities[i]==maxCap)*i;

          This also works if, in general, you want only one index out of many, and you don't care that it's the maximum, or the minimum if in the above definition you replace max by min. I still have no idea in the general case when one wants to retrieve all the argmax/argmin indices.

          Regards,

          Stefano

          • MarekGrzes
            MarekGrzes
            10 Posts
            ACCEPTED ANSWER

            Re: arg max in OPL?

            ‏2013-12-08T03:31:36Z  in response to s.st1led

            Thank you. It works. In my case my array is actually 4 dimiensional but it looks that this approach can be extended to more dimensions:

            int nbCap=5;
            int capacities[1..2][1..nbCap] = [[1, 0, 1, 1, 1],[0, 1, 0, 0, 0]];

            int maxCap  = max(i in 1..nbCap, j in 1..2) capacities[j][i];
            int argMax = max(i in 1..nbCap, j in 1..2) (capacities[j][i]==maxCap)*i;

            execute
            {
              writeln(argMax);
            }

            I am not sure though what happens after cplex will make max linear. The number of constraints will probably blow up. But, let's see.

            Thanks,

            Marek