Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

Sum or count function

  • 1.  Sum or count function

    Posted Thu September 26, 2019 10:08 AM

    Originally posted by: Frank99


    Hey everybody, I am looking for an sum or count function which solve my following problem:

    I have an array which starts at 1 and ends at 10. The value of this arrays are 1 or 0. Now I would like to make a sum of the related values. If there is a 0 I want to start with another sum.
    It should look like this for example.

     

    In the beginning it should look like this:

    Number      Value
         1                1
         2                1
         3                0
         4                1
         5                1

         6                1

         7                1

         8                0

         9                1

         10              1

     

     

    After the function it should look like this:

    Number      Value       Sum value
         1                1                   2
         2                1                   2
         3                0                   0
         4                1                   4
         5                1                   4

         6                1                   4

         7                1                   4

         8                0                   0

         9                1                   2

         10              1                   2

     

    I hope you understand my problem and could help me.

    Thanks.

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Sum or count function

    Posted Thu September 26, 2019 03:50 PM

    Hi,

    int n=10;
    range r=1..n;

    dvar boolean x[r];
    dvar boolean start[r];
    dvar int su[r];
    dvar int res[r];

    subject to
    {

    // example values

    forall(i in r: i!=3 && i!=8) x[i]==1;
    x[3]==0;
    x[8]==0;

    // end of example values

    start[1]==x[1];

    forall(i in r:i!=1) start[i] ==  ((x[i]==1) && (x[i-1]==0));

    forall(i in r) x[i]==0 => su[i]==0;
    su[1]==x[1];
    forall(i in r:i!=1) x[i]==1 => su[i]==su[i-1]+1;

    res[n]==su[n];
    forall(i in r:i!=n) (x[i]==0) => res[i]==0;
    forall(i in r:i!=n) ((x[i]==1) && (x[i+1]==0)) => res[i]==su[i];
    forall(i in r:i!=n) ((x[i]==1) && (x[i+1]==1)) => res[i]==res[i+1];
    }

    execute
    {
    writeln(res);
    }

    gives

     

    [2 2 0 4 4 4 4 0 2 2]

    regards

     

    PS: Many how to at https://www.linkedin.com/pulse/how-opl-alex-fleischer/


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Sum or count function

    Posted Fri September 27, 2019 07:00 AM

    Originally posted by: Frank99


    Thanks a lot Mr. Fleischer.

     

    That's exactly what I wanted.

     


    #CPLEXOptimizers
    #DecisionOptimization