Using Solver to Assign Items to Buckets

Share

Facebook
Twitter
LinkedIn

In April 2011, Mdsuhair asked a question at the Chandoo.org Forums

What formula can I use to divide a group of numbers into 2 groups so that the Sum of each of the 2 groups is as equal as possible?

This is a candidate for Solver.

This post will walk us through the solution to the problem using Solver. It uses Solver and screen shots from Excel 2007.

All versions of Excel have solver available. Users should note that the screen shots may not match your version although the functionality will, some of the functions appear in different locations in different versions of Solver.

What and Where Is Solver

What is Solver

Solver is an Excel add-in that can solve problems by enabling a Target cell to achieve some goal.

This goal may be to minimse, maximise, or achieve some target value.

It solves the problem by adjusting a number of input cells according to a set of criteria or constraints which are defined by the user.

Where is Solver

Solver is an Excel add-in supplied with Excel, but not enabled by default.

To enable solver

File, Excel Options, Add-ins, Manage Excel Add-ins, Select the Solver Add-in checkbox

Solver will now appear as a New Tab on the Data, Analysis Tab

Solver Example

This post is based around a worked example Solver Example File, the file is compatible with all versions of Excel.

Defining the Problem

Mdsuhair had a series of 8 Items each which had a value:

 

He wants to know which items should be combined so that the sum of the values of the items is nearly equal.

To do this we need to assign each item into a Bucket. Namely Bucket A and Bucket B.

We will put values of 1 into a Bucket for each Item to show that it is assigned to that Bucket and a value of 0 to show that the bucket is empty (In/Out) Value.

We can start by adding a Total Column, which counts items in Buckets A + Bucket B for each Item, It then totals the totals.

We also know that we need to work out the Value of each Item in each Bucket

We do this by multiplying the Items Value by the Buckets (In/Out) value

Finally we need to add up the values in each Bucket and work out the difference between them

Mdsuhair’s original problem was to minimise the difference between the sum of the values in the 2 buckets.

Now we can place values of 1 in the buckets manually and Excel will show us the value of each Bucket and the Difference between them in Cell G11.

I have applied some conditional formatting to show when a cell has a value > 0.

The problem is that there are 28 or 256 combinations of answers, and to test them all manually at 1 every 5 seconds would take 21.3 minutes, assuming we can keep up that pace and remember which was the best combination.

This is where solver comes to the fore.

Applying Solver

To apply solver we need to define a series of requirements, rules and constraints.

These requirements, rules and constraints guide solver and set limits which allow solver to quickly narrow in on the answer.

What are our rules

Our main requirement is to minimise the difference between the value of the 2 buckets.

The difference between the 2 buckets in our example is cell G11, the sum of Bucket 2 values minus the Sum of Bucket 1 values.

We want to have G11 as low as possible but greater than or equal to 0.

 

We also know that an item can only be in Bucket A or Bucket B, it can’t be in both and can’t be Broken apart.

That is 2 Constraints for each Item

Firstly The Total column must be equal to 1

Secondly the Buckets Values must be Integers

We also know that the Total Number of Items is 8, this is another constraint.

We will discuss how these constraints are used in the next section

The Solver Window

This section will explain the solver window and its use in defining the problem within solver.

A Blank Solver Window

A Filled Solver Window

Set Target Cell:

This is the Target cell which is the cell which you are trying to solve the problem for.

Our Target cell is G11, The difference between the 2 Buckets values

Equal To:

The Equal To: section defines what we want to do with our Target Cell.

We want to achieve the same value in each Bucket and so the difference between the Buckets will be 0.

It might sound strange but we don’t want to minimise that difference. A minimal value will be achieved when all the Items are placed in Bucket A, as our equation for G11 will then have 0 – Total which is –Total, which is more minimal than 0.

Another way to constrain this is to Change G11 to =Abs(G10-H10)

This allows us to use Min as an Equal To: Value

But for now we can just leave G11 as =G10-H10 and we will set the Equal To: section as 0.

By Changing Cells:

Changing Cells: refers to the cells which will be modified by Solver to try and solve the problem.

We want to let Solver change the number of items in each bucket, this is the range: $C$2:$D$9

Hint: You can try the Guess button next to the Range Reference and Solver will take a Guess at what cells the problem is dependent on.

Always check this if you use it, especially in complex models.

Subject to the Constraints:

Constraints are the rules which define the limits of the possible solutions to the problem

We will add several constraints for our rules:

1. The Total column must be equal to 1 for each Item

2. The Bucket Value must be an Integer

3. The total contents of the 2 buckets must be 8 items

4. You could add a further constraint that each Bucket should hold the same number of items

Hint: As a general Rule, Under Constrain rather than over constrain! You can always add more constraints later.

To do this we will use the Add Constraint Button

1. We need to add a constraint for each cell in the Range E2:E9 that it is only allowed to be = 1

This constraint must be applied for each cell in the range E2:E9

2. We need to add a constraint for each cell in the Range C2:D9 that it is only allowed to be an integer

This constraint must be applied for each cell in the range C2:D9

3. We need to add a constraint for the Total of the 2 Buckets, E10=8

You can Change or Delete Constraints if you make a mistake by selecting the appropriate constraint and using the Change or Delete Buttons

Save and Load Solves Parameters

Selecting the Options Button there is the Option to Save Model and Load Models.

Hint: The Save/Load Models has been shifted onto the main Solver dialog in Excel 2010.

Using the Save Model and Load Model options you can Save and the Load the Solver Parameters for your model. The Save Model saves the parameters in a Range of cells as shown below.

This allows an easy way to actually setup and/or change the solver parameters.

Hint: Setup one constraint using solver then Save the model. Edit the model on the worksheet and re-load the model as required.

Note: That the parameters although when saved show as True/False or Numbers are all Excel Equations, see above.

This means you can edit them to change the Constraints and Parameters as required and re-load them into solver.

I have included 3 sets of Parameters for our model.

These are:

  • Base Case – Forces bucket values to be equal, Allows uneven bucket counts
  • Equal Sized Buckets – Forces each bucket to contain the same number of items
  • Force an error – Which forces an error in the solver model

Load each model and try them at your leisure.

Running the Solver Model

Warning: Solver is a computationally complex add-in, so once your model is setup, Save your Workbook.

Prior to running the model there are a few parameters we should look at to ensure the model solves correctly.

On the main Solver window select the Options button. (Some of these parameters are on the Main Solver window in Solver 2010)

Generally you can accept the defaults but in this case we will change the following

Assume Linear Model – Select

Assume Non-Negative – Select

Note: Solver in Excel 2010 will return a better answer without these 2 parameters enabled by default

The other 2 parameters which you may need to change from time to time is

Precision: Precision is a number from 0 to 1 and higher means more precise

Tolerance: Tolerance shows how far away from a Number, an Integer constraint is allowed to be

The use of the Estimates, Derivatives and Search parameters are beyond the scope of this post. I direct you to the Excel Help on these subjects, by selecting the Help button.

Run the Model

To Run the Model, select the Solve Button from the main Solver menu.

The main status bar in Excel will flash up a number of statistics about the internal workings of the Solver add-in. Generally these flash by and are too fast to read. If a model is too complex it may stall and you won’t see any movement for a while. Solver generally recovers from these problems itself.

 

Once the Solver model finishes it will display a dialog of the results and allow you to do several things

First thing to note is that “Solver Found a Solution.

If it has found a solution, the worksheet cells will be changed to show the solution

You now have 4 options:

  • Run a Report
  • Save a Scenario
  • Return to the model
  • Check Your Results

Run a Report

Run a report by clicking the report you want.

A new sheet will be added to your workbook depending on the report but will be called:

  • Answer Report 1,
  • Sensitivity Report 1
  • Limits Reports 1

etc.

Note, that not all reports are relevant at all times, depending on the Constraints you have applied.

Save a Scenario

Selecting the Save a Scenario button takes you to the Save Scenario dialog.

 

Type in a Name and the Scenario of your model is saved as a Scenario.

Scenarios as available for use in the Scenario Manager, which is accessed from the Data, What-If-Analysis Tab

Return to the model

You can return to your model and either:

  • Keep Solver Solution
  • Restore Original Values

Check the Results

Solver is probably the most Black Box’ish of systems within Excel. As such any results it puts out must be manually checked for suitableness before further use.

These checks for realness, should as a start confirm that the results meet all the criteria supplied.

Are the results roughly what were expected?

Are any Minimums or Maximums violated?

 

What If Solver Doesn’t Find a Solution?

From time to time Solver will return with an error that a “Solver could not find a feasible solution.

When this happens it is indicating one of several possibilities:

  • Your model is over or under constrained
  • Your model constraints are impossible to meet
  • Your model constraints have an error

Start by checking the current constraints for errors and ambiguities

Eg: in our case we have 8 items so requiring the Count of the two buckets to be 20 is impossible to meet

 

My Solver Answer has Strange Numbers?

In solver up to and including Excel 2007, solver would commonly return numbers like 3.5E-18.

This is 0.0000000000000000035, which is effectively 0

If your model returns these, feel free to go through the model and change them to 0, in our model we should also check as the corresponding 1, may in fact be 0.9999999999999999965.

It should be noted that this problem in Solver in Excel 2010 does not occur as often but will still occur.

 

What and How have you used solver in the past?

What and How have you used solver in the past?

Let us know in the comments below:

Facebook
Twitter
LinkedIn

Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Overall I learned a lot and I thought you did a great job of explaining how to do things. This will definitely elevate my reporting in the future.
Rebekah S
Reporting Analyst
Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

30 Responses to “18 Tips to Make you an Excel Formatting Pro”

  1. Hui... says:

    For my 2 cents worth:
    Less is more !
    Keep styles simple and in line with the corporate requirements of your employer/client

  2. Deb says:

    The table formatting is really useful, but I have found two sticky points:
    1. Cannot move or copy a sheet with a table in it.
    2. Cannot 'table format' multiple sheets at once.

    May be ways around these issues, but these are what keep me from using the table format more than I already do.

  3. Ulrik says:

    Remove gridlines in sheet
    Use dotted lines as internal borders in tables
    And just keep it simple - it's the substance that matters and there's already way too much eye candy out there

  4. Stephen says:

    I write a lot of financial reports conveying complex data in a userfriendly manner. I don't use colour (as it costs 7p/sheet verses B/W at 1p/sheet). The trick is to generate a table that someone will skim over for "the story" and then can refer back to understand it. very muck like Ulrik said, keep it simple.

    Some simple guidelines that I use:
    (a) align headings based on data (if data is text that means left, if data is numbers that means right)
    (b) do not align central numbers (unless all similar) i.e. how hard is it to read a column of numbers that contains €1.25 and €125
    (c) use borders to group columns and rows, don't format every line/column but allow the data to draw your eyes along it. "White lines" are as useful as borders
    (d) thin borders are better than fat borders - the fatter they are, the more they draw the eye... so use them to draw attention to key numbers (like a total) only.
    (e) use units to make numbers easier to read. Generally people cannot skim numbers with more than 3 d.p or 5 significant figures. so report in millions/thousands (or the other way as in ml)
    (f) avoid making text too small or too big. too small (less than 10) and people can't read it. too big (>14) and people struggle to skim over it (their eyes have to move too much)

    • Manjunath says:

      ......I don’t use colour (as it costs 7p/sheet verses B/W at 1p/sheet).....

      Not necessarily..
      Don't compromise on how good a sheet can be made to look on monitor. To print black and white, simply configure in page setup to print in black and white.

  5. Istiyak says:

    Like This post !!

    I m always using ALT + EST, not verymuch confirtable with cell style. will try to use color schemes (new feature)

    Regards
    !$T!

  6. Winston says:

    Hi Stephen,

    Do you have some non-proprietary samples you may share on drop box or Windows Live SkyDrive?

    Thanks
    w

  7. Carsten says:

    Great post!

    Which key ist EST from the shortcut "ALT+EST".
    I am using a german keyboard layout and have never heard something about an EST key.

    Thanks
    Carsten

    • Chandoo says:

      Hi Carsten...

      If you are using English version of Excel, then press ALT+E then leave the alt key, E key and then press S, then press T
      For German version of Excel, the keys would be different. I am not sure what they are.

  8. Fred says:

    it was nice MS come up with all the color schemes. However, corporate culture (or your boss) sometimes dominate or predetermine what style a spreadsheet should look like. So I hardly get a chance to use #1 to #3 shown above.

    Most of the times, it is someone else who wants a certain report or analysis gets to decide how s/he wants it to look like. I see myself more like a line chef or engineer. Others get to be the architect and I'm just a builder transforming a design into a real home. I don't get much say in it unless they are asking me to build a multistoried building on a single tooth pick as foundation.

  9. Carsten says:

    Hi Chandoo,

    thank you for your reply. Now I understand. It's something like searching for the ANY Key, because some program is displaying "Press any key to continue..."

    But to find the german version of this shortcut:
    ALT+E calls the Edit-menue? And for what are the S and T. Just tell me the english names of the menueitems, please.
    I think then I will find it.

    Carsten

  10. Adam says:

    @Carsten

    Alt+EST is
    (E)dit;
    paste (S)pecial;
    forma(T)s

    Excellent post guys!

  11. SARAN KUMAR says:

    @Carsten,

    Try to know how to find the shortcuts in the excel menu bar itself.

    You click Alt + any of the underline character in the menu bar, then excel will take you to that particular menu field.

    Now you can find different options in the dropdown menu. And each option has the name. Each name has underline in any of the characeter. That underline character is nothing but the shortcut key to execute that option.

    Like this you can find in excel all the options and their shortcut keys.

    Coming to the above example..

    Once you click alt + E, it will take you to the "EDIT" drop down menu. Under Edit there are so many options like cuT, Copy, Paste, paste Special, fIll.... etc., I think you can find underline under 't' in cut..'p' in paste..'s' in paste Special. You need to click the underlined character for the required options...Here the 'S' underlines for Paste Special option...

    Once you click 'S' it will open paste special options box...again you will find the same underlines in each of the names...here you can find different opetions like All, Formulas, Values, formaTs...etc. 'v' is nothing but Values option. Once you click V in the key board..it will execute paste special values option.

    As Summary Alt + (E)dit + paste (S)pecial + (V)alues

    Now you can find the shortcuts your own. all the best.

    Regards,
    Saran
    lostinexcel.blogspot.com

    • Manjunath says:

      You can also customize the quick access toolbar.. Once you find the icon you regularly use, right click and then select Add to quick access toolbar and once you are done, when you press Alt key it will be highlighted 1,2,3,4 etc depending upon the sequence of the icon..

  12. sixseven says:

    Ctrl-ES is sooooo 2003.

    Ctrl+Alt+V all the way baby!!!

  13. Jinesh says:

    You can DOUBLE-CLICK Format painter button to copy the formatting multiple times. Once you are done, press ESC key.
    //

  14. satheesh says:

    Hi,
    How to apply the custom styles for cells from the sql table, by using c# program.

    Thanks & Regards,
    Satheesh

  15. […] You can use the Page Layout section in Excel to apply colour themes to your reports. Chandoo.org has some useful Excel tips.  […]

  16. sujit says:

    Hi i want to print a page which have bottom line to print on each page end how to do that pls explain

  17. jay sharma says:

    Thanks Sir

  18. Srinivasan says:

    Thanks alot

  19. Srinivasan says:

    Very useful thanks

  20. mohamed salah says:

    thank you too much

  21. VIJAI S says:

    your tips are awesome.

  22. Kiran says:

    How to show a table with around 20-25 columns in the dashboard in the first page itself? I mean, within the dashboard area.
    Is there anyway we can add a horizontal scroll bar for the table?

    • Hui... says:

      @Kiran

      You never add tables directly to a dashboard

      You add cells that reference a table
      By reference I mean it gives you the ability via Formula or VBA to scroll up/down, Left/right or re-order the data
      Think of it as a window into the table

      This is discussed regularly in Chandoo's dashboard samples
      Have a look at the 2 links in Item 1: http://chandoo.org/wp/welcome/

      I'd then suggest asking a specific question in the Chandoo.org Forums and attach a sample file for a specific answer.

  23. sandra says:

    love it!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  24. Venkat says:

    I have a table of value for a month, with no data for few dates.
    I created a chart basing on above data.
    In the chart I find calendar dates, even though few dates with no data are not available in the table.
    How to remove the dates in the chart for those without data?

Leave a Reply