Decision Optimization

 View Only
Expand all | Collapse all

Counting last "1"s in int Array

  • 1.  Counting last "1"s in int Array

    Posted Wed April 03, 2019 05:34 AM

    Originally posted by: trxw


    Hi,

    I have an int array Work[Instants 1 to 47] which values are 1 (person working) or 0 (person not working). I want to count the consecutive numbers of 1s on the last rotation, i.e. when the last value of the array is 1.

     

    Example:  

    Work[Instants ]: 1 0 0 0 0 0 1...... 1 1 1 1 1 0 0 0 0 1 1 1   Count should be 3

    Work[Instants ]  1 0 0 0 0 0 1 1.......1 1 1 1 0 0 0 0 1 1 0   Count should be 0

     

    Thanks again


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Counting last "1"s in int Array

    Posted Wed April 03, 2019 10:07 AM

    hi

    int N=20;
    range instant = 1..N;
    dvar boolean x[instant];

    dexpr int OrdenDeSalida = max(t in instant) (x[t]==1)*t;  // This is the exit order which works fine
    dexpr int OrdenDeEntrada = //N+1-max(t in instant) ((x[t]==1)*(N+1-t));   
    max(t in instant) (x[t]==1 && x[t-1]==0)*t;


    subject to
    {
    forall(i in instant) (x[i]==1) == ((3<=i<=7) || (12<=i<=18));
    }

    execute
    {
    writeln(OrdenDeSalida);
    writeln(OrdenDeEntrada);
    }

    gives

     

    18
    12

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Counting last "1"s in int Array

    Posted Thu April 04, 2019 11:41 AM

    Originally posted by: trxw


    Great help again! Slight change most probably because I din't fully explain the problem but the proposed idea was the key


    #DecisionOptimization
    #OPLusingCPLEXOptimizer