Decision Optimization

Decision Optimization

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

 View Only
  • 1.  AllDifferent ID in constraint

    Posted Thu August 02, 2018 05:29 AM

    Originally posted by: Eliosa


    Hello,

    I have a tuple that I call ID and an boolean dvar called Assignement such as:

    dvar boolean Assignment[ID];
    
    dexpr float objective = 
     sum(t in ID)
        (Assignment[t]*Match_Issues[t]);
    
    minimize objective;
    

    My ID tuple look like:

    ID = {<1,A,2,B>
        <1,A,3,C>
        <1,A,4<D>
        <5,E,6,F>
        <5,E,7,G>
    };
    

    I would like to write a constraint such as 

    The assignement variable can only use the ID "1" and "5" once

    I was trying to write that using All different, but it does not seem to work.

    subject to {
     forall(test in ID)
        Unique: allDifferent(Assignment[test] * ID[test].LongID);
    
    }
    

    Thanks in advance.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: AllDifferent ID in constraint

    Posted Thu August 02, 2018 05:34 AM

    Hi,

    in OPL documentation you may read the example:

    using CP;

    int n = 5;

    range R = 1..n;
    dvar int x[R] in R;


    subject to {
      allDifferent(all (i in R : i%2 == 0) x[i] );
      allDifferent(all (i in R : i%2 == 1) x[i] );
    }

    regards

     

    https://www.linkedin.com/pulse/making-decision-optimization-simple-alex-fleischer/


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: AllDifferent ID in constraint

    Posted Thu August 02, 2018 05:40 AM

    Originally posted by: Eliosa


    Yes I tried something like that but clearly I am writing it wrong.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: AllDifferent ID in constraint

    Posted Thu August 02, 2018 06:07 AM

    Hi,

    because allDifferent only works if you use CP but with CPLEX (Linear Programming) I would rewrite the tiny example into

    int n = 5;

        range R = 1..n;
        dvar int x[R] in R;


        subject to {
          
          
          forall(ordered i,j in R : i%2 == 0 && j%2==0 ) x[i]!=x[j];
          forall(ordered i,j in R : i%2 == 1 && j%2==1 ) x[i]!=x[j];
        }

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: AllDifferent ID in constraint

    Posted Thu August 02, 2018 10:18 AM

    Originally posted by: Eliosa


    Thank you

    I struggle with this part : 

    i%2 == 0 && j%2==0


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: AllDifferent ID in constraint

    Posted Thu August 02, 2018 10:29 AM

    Hi,

    let me decompose a bit:

    int n = 5;

    range R = 1..n;


    {int} Reven={i | i in R:i%2 == 0};
    {int} Rodd={i | i in R:i%2 == 1};

    execute
    {
    writeln("Reven= ",Reven);
    writeln("Rodd= ",Rodd);
    }

    dvar int x[R] in R;


    subject to {
              
      forall(ordered i,j in Reven  ) x[i]!=x[j];
      forall(ordered i,j in Rodd  ) x[i]!=x[j];
    }

    gives

    Reven=  {2 4}
    Rodd=  {1 3 5}

    regards

     

    PS: See slicing https://www.ibm.com/support/knowledgecenter/en/SSSA5P_12.8.0/ilog.odms.ide.help/OPL_Studio/opllangref/topics/opl_langref_formalParams_filtering.html


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: AllDifferent ID in constraint

    Posted Thu August 02, 2018 10:52 AM

    Originally posted by: Eliosa


    Thanks Alex, although this is useful, I don't think that will help achieving what I want to do.

    In my ID tuple I have several id, I want to have a constraint that it can only use the specific id only once. Such as my assignement * id number result in an array of 0 and unique id number.

    tuple IDType {
    int id;
    string source;
    int match;
    string dest; }
    
    {IDType} ID = {<1,A,2,B>
        <1,A,3,C>
        <1,A,4<D>
        <5,E,6,F>
        <5,E,7,G>
    };
    
    Assignment can take value [1,0,0,1,0]
    but can't take value [1,1,0,1,1]
    

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: AllDifferent ID in constraint

    Posted Thu August 02, 2018 11:40 AM

    Ok then can you try

    .mod

    tuple IDType {
    int id;
    string source;
    int match;
    string dest; }

    {IDType} ID = ...;


    {int} ids={i.id | i in ID};

    execute
    {
    writeln("ids = ",ids);
    }
    dvar boolean x[ID];

    maximize sum(i in ID) x[i];
    subject to
    {
    forall(idOption in ids) sum(i in ID:i.id==idOption) x[i]<=1;
    }

     

    .dat

    ID={<1,A,2,B>
        <1,A,3,C>
        <1,A,4,D>
        <5,E,6,F>
        <5,E,7,G>
    };

    ?

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer