Sub topic 7
Setting up Solver: objective, variables, and constraints
This chapter assumes you can build a basic Excel model with absolute and relative references. It also assumes you have used Goal Seek to solve for one input, and that you know PMT, NPV, and IRR. Goal Seek changes one cell to hit one target. Solver changes several cells at once, respects limits you set, and can maximize or minimize a result instead of only matching a number.
What Solver needs from you
Every Solver problem has three parts.
- Objective cell. One cell containing a formula whose value you want to maximize, minimize, or set to a specific number. It must depend, directly or through other formulas, on the variable cells.
- Variable cells. The input cells Solver is allowed to change. They must hold numbers, not formulas. If a variable cell contains a formula, Solver overwrites it with a value and the link is lost.
- Constraints. Rules the answer must satisfy, such as "machine hours used must not exceed 100" or "units must be whole numbers".
Solver is an add-in that ships with Excel but is switched off by default. Enable it under File > Options > Add-ins > Manage Excel Add-ins > Go, then tick Solver Add-in. A Solver button then appears on the Data tab.
A worked example: product mix
A company makes two products, x and y. Product x earns 20 profit per unit and product y earns 30. Each unit of x uses 2 machine hours and each unit of y uses 4. The machine has 100 hours available, and total units are capped at 40 by demand. How many of each should the company make?
Lay out the model
Separate the inputs, the decision cells, and the outputs, as described in the earlier chapter on model structure.
| Cell | Label | Content |
|---|---|---|
| B2 | Profit per unit, x | 20 |
| B3 | Profit per unit, y | 30 |
| B4 | Hours per unit, x | 2 |
| B5 | Hours per unit, y | 4 |
| B6 | Hours available | 100 |
| B7 | Demand cap (total units) | 40 |
| B9 | Units of x (variable) | 1 |
| B10 | Units of y (variable) | 1 |
| B12 | Total profit (objective) | =B2*B9+B3*B10 |
| B13 | Hours used | =B4*B9+B5*B10 |
| B14 | Total units | =B9+B10 |
Start the variable cells at a sensible nonzero value. Some methods work better when they begin from a feasible point, and a starting value of 0 can hide a division by zero elsewhere in a larger model.
Fill in the Solver dialog
Open Data > Solver and enter the following.
- Set Objective: B12. Choose Max.
- By Changing Variable Cells: B9:B10.
- Subject to the Constraints: click Add and enter each rule.
- B13 <= B6 (hours used within hours available)
- B14 <= B7 (units within the demand cap)
- Tick Make Unconstrained Variables Non-Negative.
- Set Select a Solving Method to Simplex LP, because every formula is a straight-line function of the variable cells.
- Click Solve, then choose Keep Solver Solution.
Solver should return 30 units of x and 10 units of y, for a profit of 900. Check it by hand: hours used are 2×30 + 4×10 = 100, which uses all the hours, and total units are 40, which meets the cap. Both constraints are binding, meaning they are used up exactly. That is typical of a linear optimum, which tends to sit at a corner where constraints meet.
Check it outside Excel
The same problem in Python confirms the answer. linprog minimizes by default, so the objective is negated.
from scipy.optimize import linprog
# Maximize 20*x + 30*y, so minimize the negative.
c = [-20, -30]
A_ub = [[2, 4], # machine hours per unit of each product
[1, 1]] # total units limited by a demand cap
b_ub = [100, 40]
res = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=[(0, None), (0, None)])
print(res.x, -res.fun)The output is [30. 10.] 900.0. If Excel and Python disagree, suspect the model setup before suspecting either engine.
The three objective choices
The Set Objective box offers three options, and picking the wrong one is a common error.
| Option | Use it when | Example |
|---|---|---|
| Max | More is better | Profit, NPV, return |
| Min | Less is better | Cost, risk, variance |
| Value Of | The cell must hit an exact number | Operating profit equals 0 |
"Value Of" with one variable cell reproduces Goal Seek. With several variable cells, Solver finds one of many possible combinations that reach the value, and which one you get depends on the starting point. If you want the best of those combinations, switch to Max or Min and express the target as a constraint instead.
Writing constraints in the dialog
Each constraint has a cell reference, a relation, and a right-hand side. The relations available are <=, =, >=, int (whole numbers), bin (zero or one), and dif (all different, used with the Evolutionary method).
Some practical rules:
- Compare a formula cell to a limit cell. Do not type numbers directly into the constraint when you can point at an input. A hidden 100 in the dialog is a hardcode that nobody will find later.
- A constraint can cover a range.
B9:B10 >= 0applies to both cells. - Use
intfor items you cannot split, such as machines or stores. Integer constraints make the problem harder to solve and can slow the search a lot. - Use
binfor yes-or-no decisions, such as whether to fund a project. - Prefer the non-negative checkbox to writing
>= 0for every variable, but remember it applies only to variable cells that have no constraint of their own.
The next chapter covers how to write limits that reflect real business rules. For now, the key point is that every business limit that matters must appear as a constraint, or Solver will happily violate it.
Saving and reusing a setup
The Solver settings belong to the worksheet. Excel stores the most recent setup with the sheet when you save the workbook. To keep several scenarios, click Load/Save in the dialog and save the model to a block of cells. Then you can reload a setup later.
Choose Keep Solver Solution only after you have looked at the values. Choose Restore Original Values to put the variable cells back. The restore option is useful when a run ends with a warning, because you do not have to remember what the inputs were.
Common setup errors
- The objective does not depend on the variables. Solver reports that it converged, but nothing changes. Trace precedents from the objective cell to confirm the link.
- A variable cell holds a formula. Replace it with a plain value, and build the formula elsewhere.
- The method does not fit the model. Simplex LP on a model with
IF,MAX, or a division of two variable cells produces a message that the linearity conditions are not satisfied. Switch to GRG Nonlinear. - Mixed scales. If one variable is in the millions and another is a rate near 0.05, precision suffers. Express values in comparable units, as discussed in the model hygiene chapter.
- Constraints that conflict. If hours must be at most 100 and at least 120, Solver reports that it could not find a feasible solution. Fix the rules, not the solver.
- Starting far from the answer. Nonlinear methods can stop at a local optimum, a point that is best nearby but not best overall. Try several starting values and compare.
Verifying the result
After each run, perform three checks.
- Read the status message at the top of the results dialog. "Solver found a solution. All Constraints and optimality conditions are satisfied" is the one you want.
- Recalculate the constraint cells and confirm each one holds, allowing for the small tolerance in Solver options.
- Nudge a variable by a small amount in the direction you expect to help. If profit rises while the constraints still hold, the answer was not optimal and the setup needs review.
Exercise
In the product mix model, the demand cap in B7 changes from 40 to 30 units. You rerun Solver with the same setup. Without running it, which constraint do you expect to bind, and what happens to the hours constraint? (Hint: x earns 10 per machine hour, y earns 7.5 per machine hour.)
Show answer
With a cap of 30 units, the best plan is to make only as much y as is needed. x gives more profit per machine hour (20 / 2 = 10 versus 30 / 4 = 7.5), but per unit y earns more, and the unit cap limits units. Solve the two binding equations: x + y = 30 and 2x + 4y = 100, which gives y = 20 and x = 10, so profit is 200 + 600 = 800. Check that this beats making only y: 30 units of y use 120 hours, which breaks the hours limit. So both constraints bind, the hours constraint stays binding, and profit falls from 900 to 800. Because the answer is at the corner of both constraints, the demand cap is now the limit that costs you profit.
Exercise
A colleague sets Solver to maximize profit with a variable cell that contains the formula =D5*1.1. Solver runs and returns a result, but the model no longer updates when D5 changes. What went wrong and how do you fix it?
Show answer
Solver overwrites variable cells with plain numbers, so the formula =D5*1.1 was replaced by a value and the link to D5 was lost. Restore the formula, then make the variable cell a plain input (for example a growth multiplier) and calculate any dependent value, such as D5 times the multiplier, in a separate formula cell.