Decision Optimization

Expand all | Collapse all

If else for binary decision variable

  • 1.  If else for binary decision variable

    Posted Sat April 18, 2020 04:55 AM

    Originally posted by: Blestoy


    Dear CPLEX community,

     

    I am writing a mTSP model in which every caregiver have multiple service types.

    acs: caregiver c that is able to perform service type s

    ris: service requirement s that is required for patient i in p

     

     Currently, the model works fine if every patient requires 1 service type and every caregiver can provide 1 service type I used the following constraint for this:

      forall(i in 2..p, s in S)
        sum(j in 1..p, c in C) a[c][s] * x[j][i][c][s] == r[i][s];

    this will look like this in the dat file:

    a = [
    [1 0]
    [0 1]];

    r = [
    [ 0 1 ]
    [ 0 1 ]
    [ 1 0 ]
    [ 1 0 ]];

    I now want to add syncrhonization to the model and thus some patient are required to need multple service types of different caregivers. so the dat file will look something like this:

    a = [
    [1 1 0 0]
    [0 0 1 1]];

    r = [
    [ 0 0 1 0 ]
    [ 0 1 1 0 ]
    [ 1 1 0 0 ]
    [ 1 0 0 0 ]];

    To solve this I thought I have to implement the following constraint:

    In cplex i wrote the following:

      forall(i,j in 1..p, c in C, s in S)
        (x[i][j][c][s] == 1) => (a[c][s] * r[j][s] == 1);

     

    It is still not working and I have been trying for the past days to make it work. Maybe I am doing something wrong or is the constraint not the one I should use. I am not using CP and I am very new to using cplex.

     

     

     

     

     

     



  • 2.  Re: If else for binary decision variable

    Posted Sat April 18, 2020 07:42 AM

    Hi,

    your issue looks like an out of range.

    If you change

    forall(i,j in 1..p, c in C, s in S)
        (x[i][j][c][s] == 1) => (a[c][s] * r[j][s] == 1);

    into

    forall(i,j in 1..p, c in C, s in S:j!=1)
        (x[i][j][c][s] == 1) => (a[c][s] * r[j][s] == 1);

    This error will go away

    regards



  • 3.  Re: If else for binary decision variable

    Posted Sat April 18, 2020 07:50 AM

    Originally posted by: Blestoy


    Thank you for your reply!

     

    When I now try to run the model having 

    a = [
    [1 1 0 0]
    [0 0 1 1]
    ];

    r = [
    [ 0 0 1 1 ]
    [ 0 0 1 1 ]
    [ 1 1 0 0 ]
    [ 1 1 0 0 ]];

     

    it gives me with no solution, however caregiver 1 should go to patient 4 and 5 and caregiver 2 to patient 2 and 3. When i change this back to 1 service type per caregiver and 1 service type requirement per patient it works again? Do you have any idea how to fix this?



  • 4.  Re: If else for binary decision variable



  • 5.  Re: If else for binary decision variable

    Posted Sat April 18, 2020 08:17 AM

    Originally posted by: Blestoy


    Thank you so much! I think this will definitely help me.



  • 6.  Re: If else for binary decision variable

    Posted Sat April 18, 2020 01:55 PM

    Originally posted by: Blestoy


    I have taken a close look at the tutorial and understand what they are doing however in my case it is a little bit different. I was wondering if the following constraint is the reason why the following works:

      forall(i in 2..p, s in S)
        sum(j in 1..p, c in C) a[c][s] * x[j][i][c][s] == r[i][s];

     

    a = [
    [1 1 0 0]
    [0 0 1 1]
    ];

    r = [
    [ 0 0 0 1 ]
    [ 0 0 0 1 ]
    [ 1 0 0 0 ]
    [ 1 0 0 0 ]
    ];

    with this constraint and this data i get a solution x variable is a binary. But if i change the data to the following I do not get a solution:

    a = [
    [1 1 0 0]
    [0 0 1 1]
    ];

    r = [
    [ 0 0 1 1 ]
    [ 0 0 0 1 ]
    [ 1 0 0 0 ]
    [ 1 0 0 0 ]
    ];