+ Reply to Thread
Results 1 to 28 of 28

Generating all possible combinations per macro

  1. #1
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Post Generating all possible combinations per macro

    Dear community, good morning.

    I am new to this forum and hope you can help me.

    Here is my problem.

    Short version:

    1. I need VBA to generate all possible combinations following a certain logic
    2. Once a new combination is found, this combination needs to be evaluated with a certain function (that I already have)

    Detailed version:

    - I need to optimize the allocation of products to a shelf
    - I need only two integer variables, number of products N and shelf-space (capacity) S
    - I want VBA to generate the full list of all possible combinations of allocating products to the shelf and evaluate each combination with a certain function (that I already have)
    - Assumption 1: I always need to fill the shelf completely
    - Assumption 2: the order of the products does not matter (means only the total number per product counts, not where the product is placed on the shelf)
    - here comes an example for N=3 product and S=3 shelf-spaces (which means that I can put a total of 3 products onto the shelf) - which gives in total 10 possible combinations

    Quantity of product 1 Quantity of product 2 Quantity of product 3

    0 0 3
    0 1 2
    0 2 1
    0 3 0
    1 0 2
    1 1 1
    1 2 0
    2 0 1
    2 1 0
    3 0 0

    I assume you need to build a kind of "recursive" loop for this. I have managed to write a non-flexible macro which can deal with a flexible number for S but I always need to build in a new loop whenever I want to add a product.

    What I am looking for is a fully flexible macro such that I can just put S and N into two excel cells and the macro evaluates all combinations.

    You do not need to worry about the evaluation function for each combination (to simplify, assume this is just the sum of the number of all products, which then of course would always equal S).

    Please do let me know if you need more information - I hope the above is clear.

    Thanks a lot in advance for your help.
    Best regards

    Kai

  2. #2
    Registered User
    Join Date
    09-27-2011
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    86

    Re: Generating all possible combinations per macro

    Hi Kani2015,

    Please use the below code to generate your permutations
    Please Login or Register  to view this content.
    Regards,
    lokicl

  3. #3
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Hello Lokicl,

    thanks for looking into that and sending me the code.

    I tried it and I see what this is doing. I think it is giving me the permutations - which will consider the order of products. The order in my case does not matter. I would need the combinations only.

    Also, I do not see how you can flexibly define N and S ... Does this make sense?

    In the meantime I found out one more thing: the total number of combinations you get with N and S is (N+S-1)! / (S!*(N-1)!).

    Perhaps that helps. Do you (or anyone else) have a different idea?

    Thanks again.
    Best regards

    Kai

  4. #4
    Forum Contributor
    Join Date
    09-26-2014
    Location
    Moscow, Russia
    MS-Off Ver
    MSE 10, MSE 13
    Posts
    179

    Re: Generating all possible combinations per macro

    Good time of day, Kai!

    I've tryed to solve this task and came to conclusion, that it may be possible to provide a 2-dimentional array, where number of columns shows Shelf-space and number of rows represents number of combinations. And I've stopped when I faced the task to count number of needed rows. So now your formula may let us step further. I'll try to organize circle for combinations later.

    It also will be very helpful if you will attach a file with your nonflexible example.
    Best wishes and have a nice day!

  5. #5
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Hi Rioran,

    thank you! Yes - xls will not have enough rows due to the combinatorical "explosion" by the above term (N+S-1)! / (S!*(N-1)!).

    I will post a short version of my non-flexible code below. Hope that helps.

    Have a good evening
    Kai

  6. #6
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Hi, this is the example for N=5 products.
    You see that I built in 5 of the loops, according to N = 5. This needs to become flexible. Shelf-space is S=10 (can already be flexibly set - so this at least is working).

    I am sure the code is not efficient at all :-)

    I shortened the code, so may be that this is not fully working if you copy it - but it should.
    Also, I simplified the evaluation formula, which is resultF(ip) = F(ip)+1 in the below.

    Thank you!

    Best regards
    Kai


    Please Login or Register  to view this content.
    Last edited by KaNi2015; 09-30-2014 at 03:20 PM.

  7. #7
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Hi Rioran,

    any chance you could progress on this topic?

    Thanks and cheers
    Kai

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Generating all possible combinations per macro

    You say order doesn't matter, yet you list 0,0,3, 0,3,0, and 3,0,0 ...?
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Hi shg,

    yes - choosing N=S=3 was perhaps not the best example to illustrate it here.

    The 3 digits do NOT represent the shelf spaces/slots but the numbers of each product.

    Example 1:
    0 3 0 means product 1 is not listed (0), product 2 is listed with 3 items (3) and product 3 again is not listed. Thus, product 2 consumes the total shelf-space (S=3) on its own.

    Example 2:
    0 0 3 means product 1 is not listed (0), product 2 is not listed either (0) and product 3 is listed with 3 items. Thus, product 3 consumes the total shelf-space (S=3) on its own.

    I am not interested in where the products are placed (left, middle or right slot of the S=3 spaces). I am just interested in how often the single products appear on the shelf.



    Cheers and good night
    Kai

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Generating all possible combinations per macro

    How large could N and S be?

    Generating the partitions of N for small numbers is easy enough, but calculating distinct permutations with repeated digits would be fussy.

  11. #11
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Hi shg,

    I think I would need to do this for up to N=15 products and shelf-space of S=50.

    I know that this macro will potentially run for hours.

    I am building a non-linear model for this shelf-space problem in GAMS and I use a standard solver. Therefore I need to show (through comparison to full enumeration by generating all combinations, exactly what I want to do with the VBA code) that the standard solver yields good results.

    Best regards
    Kai

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Generating all possible combinations per macro

    So -- list all possible ways to put 15 items in 50 slots? I think that's =COMBIN(15+50-1, 15) ~ 160 trillion arrangements. It would take a lifetime to generate them all, which would be good, because your descendants would need the time to round up all the disk drives they could find to store the file.

    As they say on Shark Tank, I'm out.
    Last edited by shg; 10-02-2014 at 09:37 AM.

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Generating all possible combinations per macro

    OK, I relent. Here's 4 items arranged in 5 bins:

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


    Please Login or Register  to view this content.
    Last edited by shg; 10-06-2014 at 03:51 PM.

  14. #14
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Good morning Shg - I will take a look at this!

    Thanks a lot for now - this looks very helpful. I will get back here after reviewing and testing the code!

    Have a good day!
    Kai

  15. #15
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Hi - I did a quick check already.

    First of all - thanks again. This is going into the right direction I think.

    However, I am not sure that the total number of combinations is correct. E.g. with N=4 and S = 3, I know that this should be 20 combinations in total. The code is giving me 15 only - so some are obviously skipped.

    Do you see what I mean?

    Thanks
    Kai

  16. #16
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Without fully understanding the code yet - I think you need to get rid of the -1 in the main sub.

    Please Login or Register  to view this content.
    At least I get now the right number of combinations. Will investigate further. Cheers

  17. #17
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Generating all possible combinations per macro

    Four items across three bins is 15; three items across four is 20.

    A
    B
    C
    1
    1
    2
    3
    2
    4
    3
    3
    1
    4
    3
    1
    5
    2
    2
    6
    2
    1
    1
    7
    2
    2
    8
    1
    3
    9
    1
    2
    1
    10
    1
    1
    2
    11
    1
    3
    12
    4
    13
    3
    1
    14
    2
    2
    15
    1
    3
    16
    4


    A
    B
    C
    D
    1
    1
    2
    3
    4
    2
    3
    3
    2
    1
    4
    2
    1
    5
    2
    1
    6
    1
    2
    7
    1
    1
    1
    8
    1
    1
    1
    9
    1
    2
    10
    1
    1
    1
    11
    1
    2
    12
    3
    13
    2
    1
    14
    2
    1
    15
    1
    2
    16
    1
    1
    1
    17
    1
    2
    18
    3
    19
    2
    1
    20
    1
    2
    21
    3
    Last edited by shg; 10-02-2014 at 12:24 PM.

  18. #18
    Forum Contributor
    Join Date
    09-26-2014
    Location
    Moscow, Russia
    MS-Off Ver
    MSE 10, MSE 13
    Posts
    179

    Re: Generating all possible combinations per macro

    Kai, hello.

    Sorry for silence, I am studying statistics more closely this days to try your task. I'm still following topic and if I will have anything significant - I will share it immidiatly.

  19. #19
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Thanks Riorian.

    Shg, I am confused. According to (N+S-1)!/(S!*(N-1)!) - which I am pretty sure about - N=4 items and S=3 spaces yields 20 combinations. And N=3 and S=4 yields 15.

    How do I read the first table of your last post?

    Columns A/B/C is bins?
    Rows is combinations. How do I read the 3rd row then: 4 _ _. I assume this means you assign product 4 to bin A ? But with which quantity and what about the other bins, are they empty?

    Perhaps I am confusing things, sorry.

    Asssumption is always to fill all bins.
    Last edited by KaNi2015; 10-02-2014 at 01:16 PM.

  20. #20
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Here is my manual enumeration for N=4 products and S=3.

    Read the table as follows: 4 columns because we have 4 products. Different combinations in rows. First number in a row = number of items of product 1 put to the shelf, second number in a row = number of items of product 2 put to the shelf.

    Row sum is always 3 as shelf-space is limited to S = 3.



    1 2 3 4
    0 0 0 3
    0 0 1 1
    0 0 2 1
    0 1 1 1
    0 1 2 0
    0 1 0 2
    0 0 3 0
    0 3 0 0
    0 2 0 1
    0 2 1 0
    1 0 0 2
    1 0 2 0
    1 2 0 0
    1 0 1 1
    1 1 0 1
    1 1 1 0
    2 0 0 1
    2 0 1 0
    2 1 0 0
    3 0 0 0

  21. #21
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Generating all possible combinations per macro

    The first row says put 4 items in the first bin and leave the other two empty.

    The next line says 3 in the first bin, 1 in the second, last one empty

    ... and so forth.

  22. #22
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Hey Shg,

    got it. I think we are talking different things. For me it it is important WHICH items are on the shelf - so I need to differentiate between items 1-4. I do not care where I put them onto the shelf, just whether I put them and how much of each ...

    Does that make sense?

    Still - I am playing with your code. I think it serves as a very good base for what I want to do. I am just not good enough in VBA to easily adapt it....


    Also: 1 bin can only contain 1 product in my case. S=3 means I have 3 spaces/bins. I can accordingly put a total of 3 products. The only questions is which. I can put 3x product 1 or only 2x product 1 and 1x product 2 or .... and so forth.
    Last edited by KaNi2015; 10-02-2014 at 01:41 PM.

  23. #23
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Generating all possible combinations per macro

    Then you can reverse my definition of bins and items and get exactly the same result as yours.

  24. #24
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Ok. Will try - that means exchanging just nProd and nShlf in the sub main()? Or do I need to do more?


    Please Login or Register  to view this content.

  25. #25
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Will look into that tomorrow again - so will not reply today anymore.

    Anyhow - thanks again for your support.

  26. #26
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Generating all possible combinations per macro

    Please Login or Register  to view this content.

  27. #27
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Hi Shg and Rioran,

    thanks for helping me with that.

    The question is solved - I am using your code Shg. Special thanks to you.

    That was immensely helpful!



    Cheers
    Kai
    Last edited by KaNi2015; 10-06-2014 at 10:36 AM.

  28. #28
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Generating all possible combinations per macro

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. generating combinations
    By fern4500 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 04-11-2012, 08:47 AM
  2. Problem with generating Combinations (Myrna Larson macro)
    By Excelstudent12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2012, 04:28 AM
  3. Generating Combinations of Text
    By financeguy57 in forum Excel General
    Replies: 8
    Last Post: 10-19-2010, 06:42 PM
  4. Help generating combinations
    By JamRock in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-02-2006, 03:40 PM
  5. [SOLVED] Generating excel combinations
    By mark4006 in forum Excel General
    Replies: 2
    Last Post: 03-06-2005, 01:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1