Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Sort based on constraints

    Posted 12/03/18 05:09 AM

    Originally posted by: SN2683


    I want to output data with input data like the following, and changing the record order.

    <Input>

    ID  thickness  Width  
    --- ---------- ------ 
    1   170        50     
    2   160        30     
    3   200        40     
    4   150        40     
    5   180        50     

     

    <Output>

    ID  thickness  Width  
    --- ---------- ------ 
    3   200        40     
    5   180        50     
    1   170        50     
    4   150        40     
    2   160        30     

     

     

    The data after sorting is subject to the following constraints.

    · The thickness must differ by 20 or less between the front and back records.
    · The width must be less than or equal to 10 between the preceding and succeeding records.

    Is it possible to implement such processing with CPLEX?
    I would like to realize it with MP if possible, but if it is impossible I would like to realize with CP.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Sort based on constraints

    Posted 12/03/18 05:27 AM

    Hi

    reversed could help

    tuple t
    {
    int ID;
    key int thickness;
    int Widths;
    }

    {t} s=
    {
    <1,   170  ,      50   >,  
    <2 ,  160  ,      30     >,
    <3 ,  200  ,      40     >,
    <4  , 150  ,      40   >,  
    <5  , 180  ,      50   >
    };

    reversed {t} s2=s;

    execute
    {
    writeln(s2);
    }

     

    gives

     

    {<3 200 40> <5 180 50> <1 170 50> <2 160 30> <4 150 40>}

    regards

     

    https://www.linkedin.com/pulse/how-opl-alex-fleischer/


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Sort based on constraints

    Posted 12/04/18 03:00 AM

    Originally posted by: SN2683


    Thank you very much for your prompt response. I appreciate it.
    I'm very sorry. The data mentioned as the previous example was too simple.

    In actual data, there are multiple pieces of data with exactly the same thickness.
    In addition, when an output result that satisfies the plurality of constraints listed above can not be obtained,
    I would like to output it as a relaxation solution.

    If there is input data like the following, will you be able to output data as relaxation solution?

     

    >· The thickness must differ by 20 or less between the front and back records.
    >· The width must be less than or equal to 10 between the preceding and succeeding records.

    <Input>

    ID  thickness  Width  
    --- ---------- ------ 
    1   170        50     
    2   160        30     
    3   200        40     
    4   150        40     
    5   180        30     
    6   230        50     
    7   140        70     
    8   200        50     


    <Output>
    ID  thickness  Width  
    --- ---------- ------ 
    6   230        50     
    8   200        50     ←relaxing the 1st constraint
    3   200        40     
    5   180        30     
    2   160        30     
    4   150        40     
    1   170        50     
    7   140        70     ←relaxing the both constraints

    The above relaxation solution is an example, and other relaxation solutions may be used.

     

    best regards.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Sort based on constraints

    Posted 12/04/18 04:23 AM

    Hi,

    then you should write some smarter code like

    using CP;

    tuple t
    {
    int ID;
    int thickness;
    int Widths;
    }

     

    {t} s=
    {
    <1,   170  ,      50   >,  
    <2 ,  160  ,      30     >,
    <3 ,  200  ,      40     >,
    <4  , 150  ,      40   >,  
    <5  , 180  ,      30   >,
    <6 ,  230  ,      50  >,   
    <7  , 140  ,      70    >,
    <8 ,  200  ,      50   >
    };

    int thickness[i in 1..card(s)]=item(s,i-1).thickness;
    int Widths[i in 1..card(s)]=item(s,i-1).Widths;

    dvar int which[1..card(s)] in 1..card(s); // which slab is at position i ?

    dvar int nbOk; // number of constraints that are ok

    maximize nbOk;
    subject to
    {
    allDifferent(which);

    nbOk==
    (abs(thickness[which[1]]-thickness[which[card(s)]])<=20)
    +sum(i in 1..card(s)-1) (abs(Widths[which[i]]-Widths[which[i+1]])<=10);

    }

    {t} result={item(s,which[i]-1) | i in 1..card(s)};

    execute
    {
    writeln(result);
    }

     

    that gives

     

    {<2 160 30> <5 180 30> <3 200 40> <8 200 50> <4 150 40>
         <6 230 50> <1 170 50> <7 140 70>}

    regards

     

    https://www.linkedin.com/pulse/%E5%8B%95%E7%89%A9%E5%9C%92%E3%81%A8%E3%83%90%E3%82%B9%E3%81%A8%E5%AD%90%E4%BE%9B%E3%81%9F%E3%81%A1%E3%81%A8%E6%9C%80%E9%81%A9%E5%8C%96-ferenc-katai/


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Sort based on constraints

    Posted 12/10/18 07:34 AM

    Originally posted by: SN2683


    Thank you very much for your prompt response. I appreciate it.
    We edited the source code a bit and we were able to achieve the object below.


    using CP; 

    tuple t 
     { 
     int ID; 
     int thickness; 
     int Widths; 
     } 

    {t} s= 

    <1,   170  ,      50   >,   
    <2 ,  160  ,      30     >, 
    <3 ,  200  ,      40     >, 
    <4  , 150  ,      40   >,   
    <5  , 180  ,      30   >, 
    <6 ,  230  ,      50  >,    
    <7  , 140  ,      70    >, 
    <8 ,  200  ,      50   >
    // ,<9 ,  215  ,      60   > 
    }; 

    int thickness[i in 1..card(s)]=item(s,i-1).thickness; //card(s)で配列数を取得
    int Widths[i in 1..card(s)]=item(s,i-1).Widths; 

    dvar int which[1..card(s)] in 1..card(s); // which slab is at position i ? 

    dvar int nbOk; // number of constraints that are ok 

    maximize nbOk;


    subject to 

    allDifferent(which); 

    //nbOk== 
    //(abs(thickness[which[1]]-thickness[which[card(s)]])<=20) 
    //+sum(i in 1..card(s)-1) (abs(Widths[which[i]]-Widths[which[i+1]])<=10); 

    //Edited constraint by me is here.
    nbOk== 
    sum(i in 1..card(s)-1)
      (abs(thickness[which[i]]-thickness[which[i+1]])<=20) *
      (abs(Widths[which[i]]-Widths[which[i+1]])<=10)

    {t} result={item(s,which[i]-1) | i in 1..card(s)}; 

    execute 
     { 
    writeln(result); 
     } 
     

    ****************
    If possible, I would like to realize another purpose at the same time.
    I would like to arrange the values multiplied by thickness and width so that they are as large as possible.

    For example, it is as follows.

    <A>
    ID  thickness  Width  thickness*Width
    --- ---------- ------ -----------------
    6   230        50     11500
    7   140        70     9800
    8   200        50     10000
    3   200        40     8000
    5   180        30     5400
    2   160        30     4800
    4   150        40     6000
    1   170        50     8500

    <B>
    ID  thickness  Width  thickness*Width
    --- ---------- ------ ------------------
    1   170        50     8500
    4   150        40     6000
    2   160        30     4800
    5   180        30     5400
    3   200        40     8000
    8   200        50     10000
    7   140        70     9800
    6   230        50     11500


    Both of the above satisfy the original rules and are sorted in reverse order.

    <A>:6→7→8→3→5→2→4→1
    <B>:6←7←8←3←5←2←4←1

    But A is lined up in order of larger value multiplied by thickness and width.
    Is it possible with CPLEX to prioritize output results from A over B?

    Thank you very much.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Sort based on constraints

    Posted 12/10/18 11:58 AM

    Hi

    then you could turn

    maximize nbOk;

    into

    maximize staticLex(nbOk,thickness[which[1]]*Widths[which[1]]);

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Sort based on constraints

    Posted 12/17/18 12:49 AM

    Originally posted by: SN2683


    Thank you very much for your prompt response. I appreciate it.
    It was confirmed that the first data was arranged from the one with the larger value multiplied by the thickness and the width.
    The things that I really want to do are to sort the second and later ones in order from bigger to bigger.

    When I implemented this by myself, I got an error and I could not do it.
    I do not know how to solve the error.
    I'm sorry, could you tell me the feasible way?

    ※ In the following code, line 31 becomes an error and it can not be executed.

    using CP; 

    tuple t 
     { 
     int ID; 
     int thickness; 
     int Widths; 
     } 

    {t} s= 

    <1,   170  ,      50   >,   
    <2 ,  160  ,      30     >, 
    <3 ,  200  ,      40     >, 
    <4  , 150  ,      40   >,   
    <5  , 180  ,      30   >, 
    <6 ,  230  ,      50  >,    
    <7  , 140  ,      70    >, 
    <8 ,  200  ,      50   >
    // ,<9 ,  215  ,      60   > 
    }; 

    int thickness[i in 1..card(s)]=item(s,i-1).thickness; //card(s)で配列数を取得
    int Widths[i in 1..card(s)]=item(s,i-1).Widths; 

    dvar int which[1..card(s)] in 1..card(s); // which slab is at position i ? 

    dvar int nbOk; // number of constraints that are ok 

    //dvar int sumArea[1..card(s)]; //multiplied by thickness and width
    dexpr float sumArea[i1 in 1..card(s)]=sum(i2 in 1..card(s):which[i1]>=which[i2])Widths[i2]*thickness[i2];  //Error on this line

    maximize staticLex(nbOk,sum(i in 1..card(s))sumArea[i]);

    subject to

    allDifferent(which); 

    //nbOk== 
    //(abs(thickness[which[1]]-thickness[which[card(s)]])<=20) 
    //+sum(i in 1..card(s)-1) (abs(Widths[which[i]]-Widths[which[i+1]])<=10); 

    //Edited constraint by me is here.
    nbOk== 
    sum(i in 1..card(s)-1)
      (abs(thickness[which[i]]-thickness[which[i+1]])<=20) *
      (abs(Widths[which[i]]-Widths[which[i+1]])<=10)
    ;
    }

    {t} result={item(s,which[i]-1) | i in 1..card(s)}; 

    execute 
     { 
    writeln(result); 
     } 


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: Sort based on constraints

    Posted 12/17/18 02:52 AM

    Hi,

    you cannot have decision variable in the slicing part.

    You could rewrite

    dexpr float sumArea[i1 in 1..card(s)]=sum(i2 in 1..card(s):which[i1]>=which[i2])Widths[i2]*thickness[i2]; 

    into

    dexpr float sumArea[i1 in 1..card(s)]=sum(i2 in 1..card(s))(which[i1]>=which[i2])*(Widths[i2]*thickness[i2]); 

     

    regards

     

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


    #DecisionOptimization
    #OPLusingCPLEXOptimizer