Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Modeling equal values of variables !! z[i][j]==(x[i]==x[j])

  • 1.  Modeling equal values of variables !! z[i][j]==(x[i]==x[j])

    Posted Wed July 24, 2019 06:40 AM

    Originally posted by: zab.a


    Hi,

    I have the following scenario: variable x[i] is an integer variable. Assume that i =5 , x[i] may have values of [1 1 1 2 3] // x[i] represent arrival time of customers . I need to strict the number of customers they will arrive at the same time. for example x[i]=1  shall occur  only 2 times. so that x[i] will be [ 1 1 2 2 3] instead of [ 1 1 1 2 3] . another example same case in x=[ 1 5 5 5 5 3], the four "5" shall be only thee "fives" for example. means only three customers shall arrive on time 5.

    I need to formulate a constraint the counts the number of equal values in x array and put a limit on it e.g z <= cap. //maybe z will be the number of equal values in the variable array x[i], or another way.

    this is my trial,

    int n=...; // number of customers

    range costm=1..n; 

    int P[costm]=...; // preferable customer arrival time

    int cap=...;  // limit on how may customer shall arrive each time

    dvar int+ x[costm]; // customer arrival time --> integer 
    dvar boolean z[costm][costm];    // if customer i will arrive at the same time of cutomer j 

    minimize sum (i in costm) abs(x[i]-P[i]);

    subject to {

    C3:forall ( i,j in costm:i!=j) z[i][j]==(x[i]==x[j]); // defining z:  z=1 if customer i will arrive at the same time of cutomer j 
    C4: sum (i,j in costm:i) z[i][j] <=cap; // to limit the number of customers arriving at the same time 

    but the above code did not work properly. since sum (i,j in costm:i) z[i][j] is not the number of customers arrive at the same time.

    Thanks In Advance;

     

     


    #DecisionOptimization
    #MathematicalProgramming-General


  • 2.  Re: Modeling equal values of variables !! z[i][j]==(x[i]==x[j])

    Posted Wed July 24, 2019 03:54 PM

    Hi,

    let me share a small example to help you do the count:

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

    dvar int x[r] in r;
    dvar boolean xx[i in r][j in r]; // true if x[i]==j
    dvar int count[v in r]; // number of value v in x

    subject to
    {

    // example

    x[1]==2;
    x[3]==2;
    x[2]==4;
    x[4]==1;

    // end of example

    forall(i in r,j in r) xx[i][j]==(x[i]==j);
    forall(i in r) count[i]==sum(j in r) xx[j][i];

    }

    execute
    {
    writeln(count);
    }

     

    gives

     

    [1 2 0 1]

    NB:

    if you were using CPO then you could use count:

    using CP;

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

    dvar int x[r] in r;

    dvar int countv[v in r]; // number of value v in x

    subject to
    {

    // example

    x[1]==2;
    x[3]==2;
    x[2]==4;
    x[4]==1;

    // end of example


    forall(i in r) countv[i]==count(x,i);
    }

    execute
    {
    writeln(countv);
    }

    regards


    #DecisionOptimization
    #MathematicalProgramming-General


  • 3.  Re: Modeling equal values of variables !! z[i][j]==(x[i]==x[j])

    Posted Wed July 24, 2019 11:21 PM

    Originally posted by: zab.a


    Thank you Alex for your reply,

    it is very helpful since I now know about count function in cplex, btw, I am using CPLEX Studio IDE 12.9.0 .

    however, I would like to count any value in the array of variable x regardless of the value of index j in x. 

    e.g. x[i] = [15 20 20 20 14] , here i=1..5 . so that this line of code you proposed: forall(i in r,j in r) xx[i][j]==(x[i]==j);  unfortunately , will not work with this case .

    May be my explanation was not that clear, but now I want to count and constrain the number of times a variable x[i] has the same value. in other words:

     scenario 1 (simpler): if variable x[i] will have similar values in its array, I want to constrain the number of those equal values. e,.g limiting the number of having equal values in array x[i] to be only two times. 

      scenario 2 (more detailed level): to constrain how many times a certain value ex. 20 shall be obtained in array x[i] . e.g. the value of 20 obtained only one time, the value of 3 obtained only 2 times, value x if 7 obtained only 2 times and so on.

    Thank you for your support,

     


    #DecisionOptimization
    #MathematicalProgramming-General


  • 4.  Re: Modeling equal values of variables !! z[i][j]==(x[i]==x[j])

    Posted Thu July 25, 2019 02:41 AM

    Hi,

    Let me adapt my example to your values.

    With your values

    int n=5;
        range r=1..n;
        range r2=1..20;

        dvar int x[r] in r2;
        dvar boolean xx[i in r][j in r2]; // true if x[i]==j
        dvar int count[v in r2]; // number of value v in x

        subject to
        {

        // example

        x[1]==15;
        x[2]==20;
        x[3]==20;
        x[4]==20;
        x[5]==14;

        // end of example

        forall(i in r,j in r2) xx[i][j]==(x[i]==j);
        forall(i in r2) count[i]==sum(j in r) xx[j][i];

        }

        execute
        {
        writeln(count);
        for(var i in r2) if (count[i]!=0) writeln(count[i]," times ",i);
        }

     

    gives

     

     [0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 3]
    1 times 14
    1 times 15
    3 times 20

    with regards to scenario 2 once you have the count decision variables you may use them in a constraint.

    regards

     


    #DecisionOptimization
    #MathematicalProgramming-General


  • 5.  Re: Modeling equal values of variables !! z[i][j]==(x[i]==x[j])

    Posted Thu July 25, 2019 05:06 AM

    Originally posted by: minayekta


    hi; 

    i'm a  student from iran. i need cplex academic version But unfortunately they do not accept my email. can you help me please to download it??


    #DecisionOptimization
    #MathematicalProgramming-General


  • 6.  Re: Modeling equal values of variables !! z[i][j]==(x[i]==x[j])

    Posted Thu July 25, 2019 05:35 AM

    Please do not hijack threads. Create a new thread with your problem.


    #DecisionOptimization
    #MathematicalProgramming-General