+ Reply to Thread
Results 1 to 5 of 5

Permutations problem

  1. #1
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Permutations problem

    Range A1:A20 ("Source") contains a column of constant numerical values. If necessary or helpful I can arrange that it be sorted by (eg) value.
    Cell Z1 ("Target") contains a single constant numerical value.
    It is a certainty that there is at least one combination of cells in "Source" the sum of which equals the value of "Target".
    I wish to populate range B1:B20 ("Solution") with the corresponding entries in "Source" the total of which adds up to "Target".
    I don't need all possible solutions. Any one will do (Eventually if I solve this problem I may look to find a method of cycling through alternative solutions but that sounds difficult).

    I think that this is likely to be a wheel already invented, so I throw myself on your kindness. Am happy to use VBA if appropriate.

    Thanks
    Last edited by 1eyedjack; 03-07-2016 at 12:14 PM. Reason: Solved

  2. #2
    Forum Expert ben_hensel's Avatar
    Join Date
    01-26-2012
    Location
    Northeast USA
    MS-Off Ver
    Office 365
    Posts
    2,043

    Re: Permutations problem

    Eh I was able to get it with Solver, but kind of a hacky solution.

    A1:A20 = source range
    Z1 = target

    B1:B20 = zeroes or ones
    //A boolean TRUE/FALSE, one is include and zero is do not include

    Z2 = (SUMPRODUCT(A1:A20, B1:B20) -Z1) ^2

    Then run Solver where:
    --Set Objective = Z2
    --To: Value of: 0
    // (you can also use "Min" but in my very limited testing that took longer to iterate, I'm not sure it matters)
    --By changing variable cells:
    B1:B20
    --Subject to Constraints:
    B1:B20 <=1
    B1:B20 >= 0
    B1:B20 = integer

    Then hit solve and it will return whatever solution is most local to wherever the algorithm happens to go.

    If Solver says "no solution" then just try fiddling with the default B1:B20 values to get that Z2 value a little closer, it doesn't have to be exact, you just need to get it into the same local area.

    Note that this will find a solution, and it will be whichever solution happens to be "closest" to your initial guesses for having 1s in B1:B20.

    Finding all solutions, like you expect, is a pretty serious order; I'd have to dig up some books, and getting an exhaustive answer is going to require significantly more rigor than the half-hearted Least Squares I've slapped together here.
    Click the [* Add Reputation] Button to thank people for help!
    Post Attachment: Click [Go Advanced] under the reply box; then click [Manage Attachments] to launch the attachments dialogue.
    Mark Thread Solved: Go to the top of the first post; Select [Thread Tools] dropdown; Pick the [Mark thread as Solved] option.

  3. #3
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Re: Permutations problem

    That's great thanks. never used solver before. Will do from now on.

    Tweaked your suggestion a bit I set column B to be formula such as
    B1 formula =A1*C1
    And copied down
    then in B22, formula =SUM(B1:B20)
    Then ran solver on B22 to equal Z1 with constraints C1:C20 set to binary.

  4. #4
    Forum Expert ben_hensel's Avatar
    Join Date
    01-26-2012
    Location
    Northeast USA
    MS-Off Ver
    Office 365
    Posts
    2,043

    Re: Permutations problem

    You're probably going to take a performance hit moving the SUMPRODUCT out of a single cell and into the C1:C20 range (well, that probably won't matter too much though), and doing just the difference instead of the difference^2 is kind of odd, but yes, that would work.

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Permutations problem

    Hi 1eyedjack,

    These are fun problems that show the power of Excel and VBA. Find the attached where you can type in how many numbers to pick and the sum you want it to equal. Then click the button and random picks will happen until an answer is reached. You can change the numbers in A1 to A20 to match your problem. See the code and attached.
    Please Login or Register  to view this content.
    VBA Random Pick to sum to Total.xlsm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

+ 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] Basic Excel statistics problem ... need help (combinations/permutations)
    By Book Guy in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 07-11-2014, 05:39 PM
  2. [SOLVED] Permutations - combinations problem
    By pshls in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-22-2013, 04:56 PM
  3. Permutations
    By kajabiwan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2012, 01:08 PM
  4. Permutations
    By alan73 in forum Excel General
    Replies: 3
    Last Post: 11-07-2011, 06:37 PM
  5. Permutations
    By Christopher_1 in forum Excel General
    Replies: 7
    Last Post: 07-05-2010, 02:28 PM
  6. [SOLVED] permutations
    By newyorkjoy in forum Excel General
    Replies: 3
    Last Post: 11-02-2005, 06:17 PM
  7. [SOLVED] Permutations
    By RedChequer in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-25-2005, 12:05 AM

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