Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

sum of a tuple

ALEX FLEISCHER

ALEX FLEISCHER09/01/16 12:34 PM

  • 1.  sum of a tuple

    Posted 08/25/16 08:22 AM

    Originally posted by: Treee


    hello, i have another question, which i can not solve.. i looked at every example, but i did not find my problem.

    when i have in .dat : 

    Container =

    {<"Container1" 1 1000 5>  <"Container3" 3 1100 2> <"Container2" 2 500 1> <"Container4" 1 900 2> <"Container5" 4 550 4>};

     

    and in mod: 

    tuple ContainerDat {
    key string Containername; 
    int type;
    int weight;
    int duration;
    }

    {ContainerDaten} Container = ...;

    how can get the sum, where the type is 1? (would be container 1 and 4, so the sum would be 2)

    i tried a couple of things, but CPLEX does not give me right sum..

     

    kind regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: sum of a tuple

    Posted 08/25/16 09:27 AM

    Hi,

    tuple ContainerDaten {
    key string Containername;
    int type;
    int weight;
    int duration;
    }

    {ContainerDaten} Container = ...;


    int s1=sum(i in Container : i.type==1) 1;

    execute
    {
    writeln(s1);
    }

    gives

    2

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: sum of a tuple

    Posted 08/25/16 10:14 AM

    Originally posted by: Treee


    ah thanks!

    i forgot the last 1...


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: sum of a tuple

    Posted 08/29/16 02:28 AM

    Originally posted by: Treee


    hey,

    in my model the containers just "spawn" simultaneosly. But actually there is a container list and the first container should be placed first, the second container second and so on.And This Containers should be placed in a tuple like this :

    tuple ContainerData
        {

       key  int ContainerNumber
        int value;
        int y;
        int x;
        int t;

       int block
        
        }

    How can i do that?

    my.dat:

    Container =

    {<"Container1" 1 1000 5>  <"Container3" 3 1100 2>

    <"Container2" 2 500 1> <"Container4" 1 900 2>

    <"Container5" 4 550 4>};

    Rows = 4;
    Columns = 5;
    Tiers = 3;
    Blocks =2;

    nbTypesOfContainers = 2; //normalContainers, CoolingContainers

    TimeNeededBlock =

                      [[[[ 1 7 13 19 25] //
                         [ 3 9 2 21 27] //
                         [5 11 17 23 29] // 
                         [7 13 19 25 31]]
                       [[2 8 14 20 26]
                         [4 10 16 22 28]
                         [6 12 18 24 30]
                         [8 14 20 26 32]]
                       [[3 9 15 21 27]
                         [5 11 17 23 29]
                         [7 13 19 25 31]
                         [9 15 21 27 33]]] 
                         
                       [[[37 43 49 55 61]
                         [39 45 51 57 63]
                         [41 47 53 59 65]
                         [44 50 55 61 67]]
                       [[38 44 50 56 62]
                         [40 46 52 58 64]
                         [42 48 54 60 66]
                         [44 50 56 62 68]]
                       [[39 45 51 57 63]
                         [41 47 53 59 65]
                         [43 49 55 61 67]
                         [45 51 57 63 69]]]];

     

    and my mod:

    int nbTypesOfContainers = ...;

    range ContainerTypes = 1..nbTypesOfContainers;

    int Rows = ...;
    range Row = 1..Rows;
    int Columns = ...;
    range CoolingColumn = 4..Columns;
    range NormalColumn = 1..Columns-2;
    range Column = 1..Columns;
    int Tiers = ...;
    range Tier = 1..Tiers;
    int Blocks = ...;
    range Block = 1..Blocks;
    int TimeNeededBlock[Block][Tier][Row][Column] = ...; 
    dvar boolean placedTypes[ContainerTypes][Block][Tier][Row][Column];

     

    tuple ContainerDaten {
    key int ContainerNumber; 
    int type;
    int weight;
    int duration;
    }

    {ContainerDaten} Container = ...;

     

    minimize
          sum( t in Tier,r in Row, c in Column, a in ContainerTypes, b in Block)
              placedTypes[a][b][t][r][c]*TimeNeededBlock[b][t][r][c];
                   

    int totalType1 = sum(con in Container : con.type ==1)1;
    int totalType2 = sum(con in Container : con.type ==2)1;

     subject to {

          ctType1:
                 sum(t in Tier,r in Row, c in NormalColumn, b in Block)
                  placedTypes[1][b][t][r][c] == totalType1;
          ctType2:
                 sum(t in Tier,r in Row, c in NormalColumn,b in Block)
                  placedTypes[2][b][t][r][c] == totalType2;

    } // some more constraints

     

    kind regards

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: sum of a tuple

    Posted 09/01/16 06:52 AM

    Originally posted by: Treee


    It would be kind, if someone could answer my question.Without the solution to my problem, i can not work further on my project...

     

    kind regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: sum of a tuple

    Posted 09/01/16 12:34 PM

    Hi,

    you could try

    tuple ContainerData
        {

       string ContainerNumber;
        int value;
        int y;
        int x;
        int t;

       int block;
        
        }
        
        
    {ContainerData} result={  <c.ContainerNumber,ty,y,x,t,b> | c in Container,ty in ContainerTypes,y in Row,x in Column, b in Block,t in Tier:
     placedTypes[ty][b][t][y][x]==1};

    execute
    {
    writeln(result);
    }

    PS:

    Is your project a business project or a research project ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: sum of a tuple

    Posted 09/02/16 03:32 AM

    Originally posted by: Treee


    thank you for your effort mr. fleischer. 

    it is a research project.

    when i execute writeln(result), he gives me :

     {<1 1 1 1 1 1> <1 1 1 1 2 1>
         <1 1 1 1 3 1> <1 2 5 1 1 1>
         <1 2 5 1 2 1> <2 1 1 1 1 1>
         <2 1 1 1 2 1> <2 1 1 1 3 1>
         <2 2 5 1 1 1> <2 2 5 1 2 1>
         <3 1 1 1 1 1> <3 1 1 1 2 1>
         <3 1 1 1 3 1> <3 2 5 1 1 1>
         <3 2 5 1 2 1> <4 1 1 1 1 1>
         <4 1 1 1 2 1> <4 1 1 1 3 1>
         <4 2 5 1 1 1> <4 2 5 1 2 1>
         <5 1 1 1 1 1> <5 1 1 1 2 1>
         <5 1 1 1 3 1> <5 2 5 1 1 1>
         <5 2 5 1 2 1>}

     

    Maybe you did not get me right. I want to know, where exactly every container is. For example for this container list cplex should give me :

     {<1 1 1 1 1 1> <2 2 1 1 2 1>  //<ContainerNumber, Value, y, x, t, block
         <3 3 1 1 3 1> <4 3 2 1 1 1>
         <5 4 2  2 1>}  

    my placedTypes matrix gives me this list, but not with the containernumber.

    Maybe i have to declare in my constraints, that cplex should go through the container list?

     

    kind regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: sum of a tuple

    Posted 09/02/16 06:12 AM

    Hi,

    that is because you miss the non ubiquity contraint

    // No ubiquity
          
          forall(c in Container)
            sum(ty in ContainerTypes,y in Row,x in Column, b in Block,t in Tier)
                placedTypes[ty][b][t][y][x]<=1;

    Then you would get something like

    {<"Container1" 2 1 1 1 1> <"Container3" 2 1 1 1 1>
         <"Container2" 2 1 1 1 1>
         <"Container4" 2 1 1 1 1>
         <"Container5" 2 1 1 1 1>}

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: sum of a tuple

    Posted 09/02/16 06:31 AM

    Originally posted by: Treee


    hi,

     

    ah ok, i had such a constraint, but not with "c in Container".

    That means, that every Container is at place <1,1,1,1>. But it should just be one Container at one place and not all on the same place.

     

    kind regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 10.  Re: sum of a tuple

    Posted 09/02/16 06:50 AM

    Ok then why do not you use anything like

    dvar boolean where[Container][Row][Column];

    in order to know where each container is ?

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 11.  Re: sum of a tuple

    Posted 09/02/16 08:15 AM

    Originally posted by: Treee


    I tried a couple of things, but that did not work.

    Maybe you could give me more information how you mean your suggestion? I do not know exactly where to implement this dvar and how to implement this in my project. 

    How should it look in my .mod?

     

    Kind regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 12.  Re: sum of a tuple

    Posted 09/02/16 01:44 PM

    Hi,

    you use the decision variable

    dvar boolean placedTypes[ContainerTypes][Block][Tier][Row][Column];

    which lets you which type of container is where but then you lose the info about which specific customer is where.

    If you need that, I would recommend to turn

    dvar boolean placedTypes[ContainerTypes][Block][Tier][Row][Column];

    into

    dvar boolean placed[Container][Block][Tier][Row][Column];

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 13.  Re: sum of a tuple

    Posted 09/05/16 07:24 AM

    Originally posted by: Treee


    hi,

    argh, i dont know why i did not the idea by myself with dvar boolean placed[Container][Block][Tier][Row][Column];

    I am sorry to bother you again with that problem, but it still does not work the way i want.

    I have now several placed matrix, but cplex does not take the values from "Container" in their order.

    The first 2 containers are type 1 for example, but in my second placed matrix he places a type 2 container in the matrix.

    i would like to get this displayed like:

     {<1 1 1 1 1 1> <2 2 1 1 2 1>  //<ContainerNumber, Value, y, x, t, block
         <3 3 1 1 3 1> <4 3 2 1 1 1>
         <5 4 2  2 1>}  

     

    with :

    sorted {ContainerData} valuesType1={<TimeNeededBlock[Block,Tier,Row,Column],Block,Tier,Row,Column> | ContainerTypes in 1..nbTypesOfContainers,Row in 1..Rows-2, Column in 1..Columns, Tier in 1..Tiers, Block in 1..Blocks : placedTypes[ContainerTypes,Block,Tier,Row,Column]==1 };

    i got the right display, but without the containernumber.

    I tried to adjust it with my tuple "container" but cplex did not want that.

    And how do i just write the placed matrix for the first container?

    i should be something like :

    writeln(placedTypes[1][b][t][r][c]); but that does not work since Container is not a range

     

     

    kind regards

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 14.  Re: sum of a tuple

    Posted 09/05/16 11:20 AM

    Hi,

    can you attach current .mod and .dat ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 15.  Re: sum of a tuple

    Posted 09/05/16 12:11 PM

    Originally posted by: Treee


    hi,

    sure.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 16.  Re: sum of a tuple

    Posted 09/06/16 02:55 AM

    Hi,

    what about

    tuple ContainerResult {
    key int ContainerNummer;
    int y;
    int x;
    int t;
    int b;
    }


    int totalTime = sum(tp in Container, t in Tier, r in Row, c in Column, b in Block : placedTypes[tp][b][t][r][c] == 1) TimeNeededBlock[b][t][r][c];


    execute DISPLAY_RESULTS {
    writeln("totalTime ",totalTime);
    }

     

    {ContainerResult} result={  <c.ContainerNummer,y,x,t,b> | c in Container,y in Row,x in Column, b in Block,t in Tier:
     placedTypes[c][b][t][y][x]==1};

    execute
    {
    writeln(result);
    }

    which gives

     

    totalTime 21
     {<1 1 1 1 1> <2 1 1 1 1> <3 5 1 1 1>
         <4 1 1 1 1> <5 5 1 1 1>}

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 17.  Re: sum of a tuple

    Posted 09/06/16 04:51 AM

    Originally posted by: Treee


    hi,

    thanks for your effort, but i have still the same problem.

     {<1 1 1 1 1> <2 1 1 1 1> <3 5 1 1 1>
         <4 1 1 1 1> <5 5 1 1 1>}

    Means that i have Container 1, 2 and 4 on the same Position, all at y=1,x=1,t=1,b=1 (and container 3 and 5 also on the same position)

    And that is not allowed. Container 1 should be at that position, but the other containers not.

    This should be the right places for every container :

     {<1 1 1 1 1 1> <2 9 5 1 1 1>  //<ContainerNumber, Value, y, x, t, block
         <3 2 1 1 2 1> <4 3 2 1 3 1>
         <5 10 5 1 2 1>}  

     

    kind regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 18.  Re: sum of a tuple

    Posted 09/06/16 06:31 AM

    Hi,

    you should add a constraint about not 2 containers at the same spot:

    // No 2 containers at the same spot
          
          forall(y in Row,x in Column)
            sum(c in Container, b in Block,t in Tier)
                placedTypes[c][b][t][y][x]<=1;

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 19.  Re: sum of a tuple

    Posted 09/06/16 07:40 AM

    Originally posted by: Treee


    Hi,

    i already tried that constraint,  cplex gives me then :

    {<1 5 1 1 1> <2 4 1 1 1> <3 3 1 1 1>
         <4 2 1 1 1> <5 6 1 1 1>}

     

    This is still not the right solution. Cplex gives neither  the right order nor the lowest values. Tier 2, and x=1,y=1  (Value =2) f.e. would be lower, but cplex does not give that place.

     

    kind regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 20.  Re: sum of a tuple

    Posted 09/06/16 07:58 AM

    Hi,

    well it is difficult to guess what you have in mind.

    For a given x and y, how many containers maximum ? 1 ?

    What I suggest, try to write your constraints in English and then you ll translate that into constraints

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 21.  Re: sum of a tuple

    Posted 09/06/16 09:23 AM

    Originally posted by: Treee


    hi,

    maybe, because i wrote my model, i took a few things for granted you could not know. Sorry for that and the resulting confusion.

    My goal is to place the containers from a container list , minimizing the time-effort. In this example the 5 containers from my list should be placed at minimum time-effort with respect to my constraints. Then i want to know, where exactly the 5 containers are stored ( x, y , tier, block)

    When i place a container at a certain position, there can be no other container on exactly that position.

    For a given x and y, there can be max. 6 containers, because i have 3 tiers and 2 blocks.

    so the possible positions for x=1 and y=1 are [x, y, tier, block] = [1,1,1,1] [ 1,1,2,1]  [1,1,3,1]  [1,1,1,2] [1,1,2,2] [ 1,1,3,2]

    Maybe this helps you to understand my problem.

    Kind regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 22.  Re: sum of a tuple

    Posted 09/06/16 09:38 AM

    Hi,

    then

    // No 2 containers at the same spot

     

    has to be rewritten into

     


          
          forall(y in Row,x in Column, b in Block,t in Tier)
            sum(c in Container)
                placedTypes[c][b][t][y][x]<=1;

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 23.  Re: sum of a tuple

    Posted 09/06/16 10:01 AM

    Originally posted by: Treee


    hi,

    thank you for your patience and help.

    i already tried that, but then cplex places all type 1 at once and all type 2 at once and i can not differentiate between them.

    And then :

    {ContainerResult} result={  <c.ContainerNummer,y,x,t,b> | c in Container,y in Row,x in Column, b in Block,t in Tier:
     placedTypes[c][b][t][y][x]==1}; ,

     

    does not work anymore.

    Kind regards

     

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 24.  Re: sum of a tuple

    Posted 09/06/16 10:09 AM

    Hi,

    if you need the type in the result, you simply can add type to the tuple result:

    tuple ContainerResult {
    key int ContainerNummer;
    int y;
    int x;
    int t;
    int b;
    int type;
    }


    int totalTime = sum(tp in Container, t in Tier, r in Row, c in Column, b in Block : placedTypes[tp][b][t][r][c] == 1) TimeNeededBlock[b][t][r][c];


    execute DISPLAY_RESULTS {
    writeln("totalTime ",totalTime);
    }

    {ContainerResult} result={  <c.ContainerNummer,y,x,t,b,c.type> | c in Container,y in Row,x in Column, b in Block,t in Tier:
     placedTypes[c][b][t][y][x]==1};

    execute
    {
    writeln(result);
    }

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 25.  Re: sum of a tuple

    Posted 09/06/16 10:33 AM

    Originally posted by: Treee


    thanks, this looks nearly perfect!

    But there is still a better solution. Your result is 29, but the best is 25. It could be achieved when the containers would be stacked on each other. the three lowest values in my timeNeeded-matrix are for all three tier in x=1, y=1, block =1 (the values are 1,2,3).  Instead cplex calculates x=1 and y= 1,2,3 with higher values. Why does cplex dont take the lowest values for minimizing?

     

    when i delete the constraints :

    //     ctPositionInTierOneIsNotFree:
    //         forall(r in Row, c in Column, a in Container,b in Block)     
    //            placedTypes[a][b][2][r][c] <=  placedTypes[a][b][1][r][c];
    //                 
    //    ctPositionInTierTwoIsNotFree:
    //         forall(r in Row, c in Column, a in Container,b in Block)     
    //            placedTypes[a][b][3][r][c] <=  placedTypes[a][b][2][r][c];

    he calculates the right value, but "result" is wrong.

    kind regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 26.  Re: sum of a tuple

    Posted 09/06/16 11:44 AM

    Hi,

    if you think you have a better solution, you may add constraints describing your solution such as

    placedTypes[item(Container,0)][1][1][1][1]==1;

    and then you ll see which constraint will be relaxed

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 27.  Re: sum of a tuple

    Posted 09/07/16 02:34 PM

    Originally posted by: Treee


    hi,

    i want to add the following constraint :

    For placing a container in tier 2, there has to be a container at tier 1 (for the same x, y , block)

    I did : 

     ctPositionInTierOneIsNotFree:
             forall(r in Row, c in Column, a in Container,b in Block)     
                placedTypes[a][b][2][r][c] <=  placedTypes[a][b][1][r][c];

    It works, but somehow cplex then does not consider other constraints. Is something wrong with that constraint?

     

    kind regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 28.  Re: sum of a tuple

    Posted 09/08/16 03:36 AM

    Hi,

    indeed

    ctPositionInTierOneIsNotFree:
             forall(r in Row, c in Column, a in Container,b in Block)    
                placedTypes[a][b][2][r][c] <=  placedTypes[a][b][1][r][c];
                    
        ctPositionInTierTwoIsNotFree:
             forall(r in Row, c in Column, a in Container,b in Block)    
                placedTypes[a][b][3][r][c] <=  placedTypes[a][b][2][r][c];

    is wrong, because you state here that if a container is tier 2 then this same container should be tier 1 which is not possible so this will turn into a solution where all containers are tier 1 which is suboptimal.

    I d rather write

    ctPositionInTierOneIsNotFree:
             forall(r in Row, c in Column, a in Container,b in Block)    
                placedTypes[a][b][2][r][c] <=  sum(a2 in Container)placedTypes[a2][b][1][r][c];
                    
        ctPositionInTierTwoIsNotFree:
             forall(r in Row, c in Column, a in Container,b in Block)    
                placedTypes[a][b][3][r][c] <=  sum(a2 in Container)placedTypes[a2][b][2][r][c];

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer