You are on page 1of 4

# Programming with Solver

## Solving Linear Programming Problems

Before you begin programming with Solver, its important to understand exactly how Solver works. In general, the task of solving a problem that includes multiple variables can be broken down into the following steps: 1. Identify the desired value for a single target cell. (objective function) 2. Identify the cells that can be changed to reach the desired target. (decision variables) 3. Specify constraints that will be adhered to in solving the problem. 4. Define parameters of the model or the way in which the problem will be solved. (options in Solver, such as Tolerance, Assume Linear Model, etc.) 5. Solve the problem. 6. Report the results. 7. Save the values of the variable cells as a scenario. Lets look at an example to see how Solver can help you solve LP problems. Wellington Cycle Works makes three models of racing bicycles: Titan1, Titan2, Titan3. Wellington contracts manufacturing of these bicycles to three independent companies: A, B, and C. Each company is capable of making each of the three models of racing bikes. However, because the different companies have different equipment, their costs differ in making the three models of bikes. The table below shows what each company charges Wellington for each bicycle: Company A B C Titan1 \$20 \$25 \$27 Titan2 \$37 \$22 \$33 Titan3 \$40 \$50 \$48

Lets assume that each manufacturing company can produce only 100 bicycles a month, no matter which model of bicycle is produced. Lets also assume that there is an unlimited demand for bicycles and that Wellington can sell a Titan1 bike for \$40, a Titan2 for \$50, and a Titan3 for \$60. In order to maximize profits, how many of each type of bicycle should Wellington order from each of the three manufacturing companies? This is a rather straightforward example of a common type of LP problem that requires a bit of thought to solve. However, Solver is well suited for solving these types of problems. To solve this problem, we would set up a table on an Excel worksheet that might look like the one on the following page. After building the table, we would then identify three aspects of the problem: the target cell, the variable cells, and the constraints. The target cell would be the last cell in the lower right corner of the table, labeled "Total Profit"; we want to maximize the value in this cell. The variable cells would be the units for the Titan1, Titan2, and Titan3 bicycles in Range(B7:D9). Three constraints would be used in solving this problem: Total units for each company in column E must be less than or equal to 100. Units for each bicycle for each company in Range(B7:D9) must be greater than or equal to 0. Units for each bicycle for each company in Range(B7:D9) must be integers. 162698847.doc

2 Next we could use Solver to specify the information listed above and to solve the problem. After running Solver on the above model, we see these results:

Solver example table solved This solution is only one of many that will provide the maximum profit of \$6,500. You might want to try duplicating the table above in Excel and experimenting with Solver manually before you move on to the programming topics that follow.

## Programming with Solver

The Solver add-in provides 13 functions that can be called from a VBA macro for solving multi-variable problems. To access Solver functions from a VBA module, you must first establish a reference to the SOLVER.XLA add-in by selecting the References command from the Tools menu on the VBA module menubar and then selecting SOLVER.XLA (in the \EXCEL\LIBRARY\SOLVER directory or wherever the Solver add-in file is located) . All Solver functions can be viewed from the Object Browser. You can obtain help on Solver functions through the equivalent Solver commands in the XLM function reference in Excels general online Help. The Solver functions that can be called from VBA are listed here.

Adds constraints to a Solver model. Takes three arguments: cellRefReference to a cell or a range of cells that forms the left side of a constraint. relation1 (<=), 2 (=), 3 (>=), or 4. (Cells referenced by cellRef must contain integers.) formulaTextRight side of the constraint.

SolverChange:
Changes an existing constraint. Takes three arguments: cellRefReference to a cell or a range of cells that forms the left side of a constraint. relation1 (<=), 2 (=), 3 (>=), or 4. (Cells referenced by cellRef must contain integers.) formulaTextRight side of the constraint.

SolverDelete:
Deletes an existing constraint. Takes three arguments: cellRefReference to a cell or a range of cells that forms the left side of a constraint. relation1 (<=), 2 (=), 3 (>=), or 4. (Cells referenced by cellRef must contain integers.) formulaTextRight side of the constraint.

SolverFinish:
After solving a problem, tells Excel to keep the results and to create a report. Takes two arguments: keepFinal1 - keep final results; or 2 - discard results and return to original values. reportArray1 - create an Answer report; 2 - create a Sensitivity report; or 3 - create a Limit report.

SolverFinishDialog:
Equivalent to SolverFinish; however, also displays the Solver Results dialog box after solving a problem. Takes two arguments: keepFinal1 - keep final results; or 2 - discard results and return to original values. reportArray1 - create an Answer report; 2 - create a Sensitivity report; or 3 - create a Limit report.

SolverGet:
Returns information about the Solver model. Takes two arguments: typeNumTakes values from 1 through 18 to return information about the Solver model. For details, use the Object Browser to view the SolverGet help topic in the XLM Function Reference section in Excel 5s online Help. sheetNameName of the sheet containing the Solver model.

Loads parameters for an existing Solver model that have been saved to the worksheet. Takes one argument: loadAreaRange on the active sheet that contains the Solver model.

SolverOk:
Defines a Solver model. Takes four arguments: setCellTarget cell for the Solver model. maxMinVal1 - maximize target; 2 - minimize target; 3 - match target to a specific value. valueOfIf the maxMinVal argument is set to 3, you must specify the value to which the target cell is to be matched. byChangeCell or range of cells that will be changed in setting the target cell.

SolverOkDialog:
Same as SolverOk but also displays the Solver dialog box. Takes four arguments: setCellTarget cell for the Solver model. maxMinVal1 - maximize target; 2 - minimize target; 3 - match target to a specific value. valueOfIf the maxMinVal argument is set to 3, you must specify the value to which the target cell is to be matched. byChangeCell or range of cells that will be changed in setting the target cell. Application.Run "SolverOptions", 3000, 200, 0.000001, False, False, 1, 1, 1, 5, False, 0.0001, False

SolverOptions:
Allows you to specify advanced options for your Solver model. Takes 10 arguments: maxTimeMaximum time Excel will spend solving the problem. iterationsMaximum iterations Excel will use in solving the problem. precisionNumber between 0 and 1 that specifies the degree of precision to be used in solving the problem. assumeLinearIf True, Solver assumes that the underlying model is linear. stepThruIf True, Solver pauses at each trial solution. estimates1 for tangent estimates; 2 for quadratic estimates. derivatives1 for forward; 2 for central. search1 for Quasi-Newton search; 2 for Conjugate Gradient search. intToleranceNumber between 0 and 1 that specifies tolerance. scalingIf Scaling is True and two or more constraints differ by several orders of magnitude, Solver scales the constraints to similar orders of magnitude during computation.

SolverReset:
Resets Solver options (cell selections and constraints in the Solver Parameters dialog box and all settings in the Solver Options dialog box) to their default values.

SolverSave:
Saves a Solver model definition to a range of cells on the worksheet. Takes one argument: saveAreaThe range of cells to which the Solver model is to be saved.

SolverSolve:
Starts a Solver solution run. Takes two arguments: userFinishIf True, Solver returns results without displaying anything. If False, Solver returns results and displays the Solver Results dialog box. showRefThis argument is used only if True is passed for the stepThru argument of the SolverOptions function. If so, you can pass the name of a subroutine (as a string) as the showRef argumentthis routine will be called whenever Solver returns an intermediate solution.

A Sample Routine
The following routine is an example of how you might use Solver to solve the problem presented earlier (this subroutine was generated using the Macro Recorder in Excel):
Sub WellingtonBicycles() SolverOk SetCell:="\$G\$19", MaxMinVal:=1, ValueOf:="0", ByChange:="\$B\$7:\$D\$9" SolverAdd CellRef:="\$B\$7:\$D\$9", Relation:=3, FormulaText:="0" SolverAdd CellRef:="\$B\$7:\$D\$9", Relation:=4, FormulaText:="integer" SolverAdd CellRef:="\$E\$7:\$E\$9", Relation:=1, FormulaText:="100" SolverOk SetCell:="\$G\$19", MaxMinVal:=1, ValueOf:="0", ByChange:="\$B\$7:\$D\$9" SolverOptions MaxTime:=100, Iterations:=100, Precision:=0.000001, AssumeLinear _ :=True, StepThru:=False, Estimates:=1, Derivatives:=1, SearchOption:=1, _ IntTolerance:=0, Scaling:=False, Convergence:=0.001, AssumeNonNeg:=False SolverSolve End Sub