+ Reply to Thread
Results 1 to 15 of 15

Excel Solver - binary variable constraints

Hybrid View

  1. #1
    Registered User
    Join Date
    05-07-2018
    Location
    turkey
    MS-Off Ver
    2013
    Posts
    5

    Excel Solver - binary variable constraints

    Hi everyone,

    I would appreciate if you could help me. I have a set of values (B2:B126) and binary variables (C2:C126). My target cell : Sumproduct(C2:C126;B2:B126). I would like to add this parameter as a constraint : Sum(C2:C126) <= 4. By doing this, I would like to limit my number of binary variables which is required for my problem. Do you know how could i add this on Solver? Thanks in advance

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

    Re: Excel Solver - binary variable constraints

    It seems so obvious to me that I wonder if there is more to the question. I would expect:

    1) Enter a formula in a convenient cell =SUM(C2:C126).
    2) Add constraint to Solver that cell used in step 1 is less than or equal to 4.

    Is that what you are trying to do, or is there more to it than that?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    05-07-2018
    Location
    turkey
    MS-Off Ver
    2013
    Posts
    5
    Quote Originally Posted by MrShorty View Post
    It seems so obvious to me that I wonder if there is more to the question. I would expect:

    1) Enter a formula in a convenient cell =SUM(C2:C126).
    2) Add constraint to Solver that cell used in step 1 is less than or equal to 4.

    Is that what you are trying to do, or is there more to it than that?

    Yes, this is exactly what i am trying to do! But, i had tried this before and the binary variables didnt come as 0 or 1. In the solution, they were decimal numbers between 0 and 1. Is it because there werent a feasible solution? Do i need to change the constraint or add a new one?

  4. #4
    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: Excel Solver - binary variable constraints

    In the solution, they were decimal numbers between 0 and 1. Is it because there werent a feasible solution?
    That is a possibility as solver may give a decimal solution if there are no solution with the current constraint setting. Another possibility is that the solver setting under "Options" is the default setting i.e. "Ignore integer Constraints"

    a_int.jpg

    Alf

  5. #5
    Registered User
    Join Date
    05-07-2018
    Location
    turkey
    MS-Off Ver
    2013
    Posts
    5

    Re: Excel Solver - binary variable constraints

    When i tried my own, I found the solution by using only 6 binary variables out of 125. I expect from Solver doing the same thing. However, it gives the decimal values to all of my binary variables.
    Attached Files Attached Files

  6. #6
    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: Excel Solver - binary variable constraints

    I've set up your model as your uploaded file did not have the proper settings for "cells to change" and constraints for these and target value.

    Alf
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    05-07-2018
    Location
    turkey
    MS-Off Ver
    2013
    Posts
    5

    Re: Excel Solver - binary variable constraints

    Hi Alf,

    I really appreciate for your help. It works now. I have one further question regarding limiting the number of binary variables for the solution. In the file you have sent, the number of binary variables used for the solution is 11. However, I can see that we can reach the target value by using only 6 binary variables. I added a constraint like : SUM(C2:C125) <=7 and it didn't work. Is there any other way for doing it?

    Thanks.

  8. #8
    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: Excel Solver - binary variable constraints

    However, I can see that we can reach the target value by using only 6 binary variables. I added a constraint like : SUM(C2:C125) <=7 and it didn't work. Is there any other way for doing it?
    Perhaps you did not set the constraint right? As I had no problem with that.

    Have modified the file a bit as the constraint

    $C$127=$H$1
    will always assure that the target value will be equal to the H1 value as long as solver find a solution to the problem so you can set "Objective" (target cell) to max or min and forget about setting target cell to a specific value as long as you have the constraint "C127 = H1"

    Alf
    Attached Files Attached Files
    Last edited by Alf; 05-08-2018 at 04:12 AM.

  9. #9
    Registered User
    Join Date
    05-07-2018
    Location
    turkey
    MS-Off Ver
    2013
    Posts
    5

    Re: Excel Solver - binary variable constraints

    I had set the same constraint. I don't know what the problem is but it works now, thanks so much! One more question, I also want to minimize SUM(C2:C125) as I want to use the minimum number of binary variables if possible. Because the constraint SUM(C2:C125) <=6 won't work if i change the target value. Each time i need to change this constraint. So, do i need to set my target as SUM(C2:C125) and minimize it?

  10. #10
    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: Excel Solver - binary variable constraints

    I tried a setting like this that should work theoretically but it seems like this setup is to complex for solver to find a solution to this problem setting
    a_solv.jpg
    so you may have to run solver by trial and error i.e. setting a value <= for the sum(C2:C125) and if this gives you a solution you reduce the value.

    An other alternative would be to set solver to run in a loop using VBA.

    Alf

  11. #11
    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: Excel Solver - binary variable constraints

    The problem with problems like this isn't finding a solution, it's that the number of solutions is overwhelming. For your 125 numbers, there are

    4 4-element combinations that total 30060
    251 5-element combos
    1270 6-element combos

    ... and no doubt many more larger combos.
    Last edited by shg; 05-08-2018 at 11:55 AM.
    Entia non sunt multiplicanda sine necessitate

  12. #12
    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: Excel Solver - binary variable constraints

    it's that the number of solutions is overwhelming
    I never thought about that. Thanks for explanation shg I may be able to build solver models but my mathematical know-how is not that profound.

    Alf

  13. #13
    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: Excel Solver - binary variable constraints

    It's the pigeonhole principle, Alf.

    If you have a bunch of (non-negative) numbers, then every sum must fall somewhere between 0 and the sum of all the numbers. Those are the pigeonholes. The OP's 125 (whole) numbers total 590,308, so there are at most that many pigeonholes.

    Every combination is a pigeon, and for N numbers, there are 2^N of them. For 125 numbers, that's 42,535,295,865,117,300,000,000,000,000,000,000,000 combinations.

    That means there are, on average, 72,056,106,075,332,400,000,000,000,000,000 ways to arrive at every possible sum.

    If you restrict the choice to at most 6 numbers, then there are =SUM(COMBIN(125, {1,2,3,4,5,6})) = 4,935,173,775 combinations. In that case, there is an average of a mere 8,360 pigeons (combinations) sharing each pigeonhole (possible sum).

  14. #14
    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: Excel Solver - binary variable constraints

    Thanks for the more extensive explanation.

    f you have a bunch of (non-negative) numbers, then every sum must fall somewhere between 0 and the sum of all the numbers.
    This I understands / make sense to me but I think I do need to a bit of serious googling to see if I can understand the combination part. I see the principle can be used in the "Sock-picking" problem which I have thought was a probability problem. Is this a case of different "labels" for using the same technique in solving the problem?

    It looks interesting and I'll have a go at seeing how much I can understand of the pigeon principle.

    Alf

  15. #15
    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: Excel Solver - binary variable constraints

    The combination part is easy; every item is either included in the sum or it's not. So if there are N items, there are 2^N combinations.

+ 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] Excel Solver VBA - Solver Violating Constraints
    By lespaul00 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 11-19-2015, 04:00 PM
  2. [SOLVED] excel solver constraints
    By hcyeap in forum Excel General
    Replies: 5
    Last Post: 06-29-2015, 01:51 PM
  3. Solver Portfolio Optimization, using binary variable
    By Willie68 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-06-2014, 02:42 PM
  4. Excel Solver binary constraints
    By jharaldson in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-18-2014, 03:37 AM
  5. How can I set >= and <= constraints for 1 variable in SOLVER?
    By RAISETOWIN in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-16-2009, 12:17 AM
  6. Excel Solver Constraints
    By jcoleman52 in forum Excel General
    Replies: 2
    Last Post: 06-01-2006, 03:00 PM
  7. [SOLVED] Constraints in Excel Solver
    By Donna in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-04-2005, 06:30 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