+ Reply to Thread
Results 1 to 12 of 12

Question: Optimizing a set of numbers in a sum

  1. #1
    Registered User
    Join Date
    11-11-2015
    Location
    USA
    MS-Off Ver
    Office Plus 2013
    Posts
    4

    Question: Optimizing a set of numbers in a sum

    Hi All,

    Couldn't find this on google or on the forums so I was hoping someone in this forum could help with some input.

    I have 2 columns:
    1) A unique identifier
    2) Value

    There are two basic rules I'm trying to follow:
    1) Three items must be added to equal a minimum of 4200
    2) No item can be used more than once (both in the set of 3, and the total sheet)

    The goal in this scenario is to create some sort of optimization formula/macro or excel solver that will allow me to see the greatest amount of combinations (of three unique items) that can be created from this dataset. I've created 3 scenarios in the attachment, but they are all manual processes and I'm not sure if they have the greatest optimization. I'm trying to create an automated process as the values in this dataset will be constantly changing and growing, which means a manual process will be tedious.

    Would much appreciate if anyone has ideas!

    Thanks!Excel Sum Help.xlsx

  2. #2
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Question: Optimizing a set of numbers in a sum

    Hi and welcome to the forum.

    Is there an upper limit on the amount of values to be considered (currently 43)?

    Can you explain your reference to the "total sheet" (which is not in this workbook)?

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,410

    Re: Question: Optimizing a set of numbers in a sum

    If I may ask, how much of this is an Excel specific question and how much is a more generic "algorithm development" question (independent of programming language used)? It looks to me like it is more of a generic algorithm development question.

    Do you know what the final answer should be for this data set? If not, do you have a data set that you know the final answer for? If this is a generic algorithm development question, you may need enough data sets where you know what the final answer should be so that you can test any algorithm that is proposed to see if it is robust enough to find the correct answer on several different data sets.

    I tried a simple algorithm (detailed below) that gave me 12 possible combinations. Without knowing the final solution, though, I cannot say if this is THE maximum number of combinations or not. We also have only the one data set to test it on.

    Algorithm detail:

    1) Select maximum value and subtract from desired total (4200-2377=1823).
    2) Divide this subtraction by 3 (1823/3=607 2/3) (I wonder if changing the 3 would give more or fewer results?)
    3) Lookup a value near step 2 (794)
    4) Subtract (1823-794=1029)
    5) Lookup a value just larger than step 4 (1050) At this point, I have a large, small, and middle value that add up to just over 4200.
    6) Remove these values from the list
    7) Go back to 1 with the new list
    8) Repeat until the algorithm can not find a new combination that adds up to 4200.

    This "works", but I cannot really test if it can find the maximum number of combinations on different data sets or how to know that it has found THE maximum number.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    11-11-2015
    Location
    USA
    MS-Off Ver
    Office Plus 2013
    Posts
    4

    Re: Question: Optimizing a set of numbers in a sum

    Quote Originally Posted by XOR LX View Post
    Hi and welcome to the forum.

    Is there an upper limit on the amount of values to be considered (currently 43)?

    Can you explain your reference to the "total sheet" (which is not in this workbook)?

    Regards
    There is no upper limit to the sheet, but if it helps, we can say it's 1000 unique identifiers.

    The reference to "total sheet" to clarify by way of example, means I can have unique identifiers A, B, C, D, E, F, G. If I use ABC in the first "set of 3," then I can't use AFG because A has already been used. Additionally, I can't use AAA or FFF.

  5. #5
    Registered User
    Join Date
    11-11-2015
    Location
    USA
    MS-Off Ver
    Office Plus 2013
    Posts
    4

    Re: Question: Optimizing a set of numbers in a sum

    @ MrShorty

    Thanks for the response, I don't really have an algorithm development background which is why I was trying to see if it was possible to optimize in excel since that's what I use on a day to day basis at work. I don't have a final answer for this data set nor another data set which could be used to help give additional samples.

    Although your approach is manual, it does seem to give the greater amount of combinations allowed vs. my haphazard manual approach so it is much appreciated, in addition to giving some direction of how to accomplish this.

    With regards to a slight bit of automation such that I don't have to manually search for each number in Step 3, I was thinking of creating a conditional formatting IF formula which would highlight the range of numbers that would be close to the new value needed. So for your example above, if we're looking for a value close to 607.667, the conditional formatting would highlight all amounts that are greater than 607.667, thus giving me easy visibility to seeing the lowest available amount able to be used. Don't know if you had a more efficient solution than this, but it seems like it would work

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,410

    Re: Question: Optimizing a set of numbers in a sum

    I don't really have an algorithm development background which is why I was trying to see if it was possible to optimize in excel since that's what I use on a day to day basis at work.
    I cannot think of any preprogrammed tools in Excel that will do this. I expect that Excel's Solver (after figuring out what to use for an objective function and decision variables and other algorithm development steps) could possibly find a solution for this. I expect there would be a significant amount of effort trying to figure out what OF to use and how to know when you have found THE maximum -- the same kind of problems you will have with developing your own algorithm.

    Although your approach is manual,...With regards to a slight bit of automation such that I don't have to manually search for each number in Step 3, I was thinking of creating a conditional formatting IF formula which would highlight the range of numbers that would be close to the new value needed.
    I described the approach in steps -- not necessarily manual steps -- but steps that made sense in terms of the available Excel functions that I am familiar with. For example, instead of using "conditional formatting" to highlight values I can manually choose from, I noted that the list is sorted in descending order which makes a simple INDEX(MATCH()) function an obvious choice for looking up and extracting a value from the list. =INDEX(B2:B44,MATCH(608,B2:B44,-1)) [the -1 as the third argument of the MATCH function is what makes it work on descending lists] easily extracts the value just larger than 608. If you are unfamiliar with these or the other lookup and reference functions -- https://support.office.com/en-us/art...__toc309306714

    Another thing I do with something like this is make sure to perform all of the calculations in the column immediately below the list. Then, I make sure the result of step 7 ends up in column C adjacent to the starting list. Copy the calculations below column B into column C (pay attention to absolute and relative references as needed to make sure that this copy works out easily), then copy column C and paste 15 or 20 copies to the right.

  7. #7
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Question: Optimizing a set of numbers in a sum

    Perhaps a solver solution?

    As this solver setup runs in a loop with a macro you must

    a) Have the Solver add-in installed on you PC

    b) Set a reference to solver in Visual Basic "Developers" tab -> Visual Basic -> Tools -> References and tick box marked "Solver"

    To test run macro "SolvLoop"

    To exclude values used binary values of 1 is checked (D column) and if condition fullfilled the E value on the same row is set to 0 and as the the F column value is a product of D and E this ensure that a value is only used once.

    Alf
    Attached Files Attached Files

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

    Re: Question: Optimizing a set of numbers in a sum

    Here's what I get:

    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    1
    Item #
    Amount
    Item 1
    Item 2
    Item 3
    Sum
    2
    1
    2377
    1
    20
    23
    4226
    G2: =SUMPRODUCT(($A$2:$A$44 = D2:F2) * $B$2:$B$44)
    3
    2
    2229
    2
    18
    19
    4349
    4
    3
    2174
    3
    16
    17
    4332
    5
    4
    1832
    4
    13
    15
    4427
    6
    5
    1748
    5
    12
    14
    4406
    7
    6
    1716
    6
    10
    11
    4758
    8
    7
    1695
    7
    8
    9
    4898
    9
    8
    1625
    0
    10
    9
    1578
    0
    11
    10
    1523
    0
    12
    11
    1519
    0
    13
    12
    1477
    0
    14
    13
    1445
    0
    15
    14
    1181
    0
    16
    15
    1150
    0
    17
    16
    1082
    0
    18
    17
    1076
    0
    19
    18
    1070
    0
    20
    19
    1050
    0
    21
    20
    996
    0
    22
    21
    874
    0
    23
    22
    867
    0
    24
    23
    853
    0
    25
    24
    794
    0
    26
    25
    561
    0
    27
    26
    557
    0
    28
    27
    513
    0
    29
    28
    477
    0
    30
    29
    475
    0
    31
    30
    437
    0
    32
    31
    390
    0
    33
    32
    382
    0
    34
    33
    380
    0
    35
    34
    375
    0
    36
    35
    370
    0
    37
    36
    370
    0
    38
    37
    369
    0
    39
    38
    359
    0
    40
    39
    354
    0
    41
    40
    351
    0
    42
    41
    344
    0
    43
    42
    343
    0
    44
    43
    335
    0


    Please Login or Register  to view this content.
    Last edited by shg; 11-12-2015 at 12:45 PM.
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Question: Optimizing a set of numbers in a sum

    Most impressive code and fast!!!

    Alf

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

    Re: Question: Optimizing a set of numbers in a sum

    WTY, Alf

    It would be easy enough to generalize for more items in each combination.

  11. #11
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Question: Optimizing a set of numbers in a sum

    generalize for more items in each combination
    My math level is not up to that but I assume this would involve changing the line

    Please Login or Register  to view this content.
    and

    Please Login or Register  to view this content.
    By the way The "Simple LP" will only find 6 solutions as well as the "OpenSolver", the "GRG nonlinear" manages to find 7 but the time it takes to do so!!!

    And the "Evolutionary" will only present 1 solution, the other 5 are rubish, well its probably my setup that don't "agree" with the "Evolutionary" model.

    Alf

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

    Re: Question: Optimizing a set of numbers in a sum

    Right now, it's hard-coded for three items (the three concentric loops); it picks the largest value, then looks for the smallest combination of two other items that meets or exceeds the required total.

    Generalized code would do about the same thing: pick the largest number remaining and generate combinations of the others from small to large, but without using concentric loops.

+ 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. [SOLVED] Mathematical question or excel question? Multiple several numbers in a row.
    By douglascaixeta in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 08-22-2015, 05:48 PM
  2. [SOLVED] Optimizing code
    By geri_n in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-30-2014, 01:26 PM
  3. need help optimizing my code
    By NirXY in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-11-2013, 11:49 AM
  4. [SOLVED] Optimizing VBA
    By walduxas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-01-2012, 02:28 PM
  5. Optimizing the code
    By kmlprtsngh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-10-2010, 01:27 AM
  6. Optimizing Revenue
    By cmgrubb in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-26-2007, 01:30 AM
  7. [SOLVED] Optimizing Code
    By Jim Thomlinson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-02-2005, 10:11 PM

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