Programming with Solver

Document Sample
Programming with Solver Powered By Docstoc
					Programming with Solver
Microsoft Excel’s Solver add-in provides a powerful tool for solving complex problems that involve multiple variables subject to multiple constraints. The Solver add-in is contained in a file named SOLVER.XLA, which by default is stored in the \EXCEL\LIBRARY\SOLVER directory (assuming that \EXCEL is the directory in which you have installed Excel). You can use Solver manually from a worksheet by selecting the Add-Ins command from the Tools menu of the worksheet menubar and then selecting Solver Add-In. After loading the Solver add-in, Excel will add a Solver command to the Tools menu, allowing you to access Solver functionality. You can also access Solver through a VBA routine, as will be discussed later in this appendix.

Solving Linear Programming Problems
Before you begin programming with Solver, it’s 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. Let’s 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

Let’s assume that each manufacturing company can produce only 100 bicycles a month, no matter which model of bicycle is produced. Let’s 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.

fa682dd4-273c-49b2-a55c-766d38f70cfe.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 Excel’s general online Help. The Solver functions that can be called from VBA are listed here.

SolverAdd:
Adds constraints to a Solver model. Takes three arguments:  cellRef—Reference to a cell or a range of cells that forms the left side of a constraint.  relation—1 (<=), 2 (=), 3 (>=), or 4. (Cells referenced by cellRef must contain integers.)  formulaText—Right side of the constraint.

SolverChange:
Changes an existing constraint. Takes three arguments:  cellRef—Reference to a cell or a range of cells that forms the left side of a constraint.  relation—1 (<=), 2 (=), 3 (>=), or 4. (Cells referenced by cellRef must contain integers.)  formulaText—Right side of the constraint.

SolverDelete:
Deletes an existing constraint. Takes three arguments:  cellRef—Reference to a cell or a range of cells that forms the left side of a constraint.  relation—1 (<=), 2 (=), 3 (>=), or 4. (Cells referenced by cellRef must contain integers.)  formulaText—Right side of the constraint.

3

SolverFinish:
After solving a problem, tells Excel to keep the results and to create a report. Takes two arguments:  keepFinal—1 - keep final results; or 2 - discard results and return to original values.  reportArray—1 - 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:  keepFinal—1 - keep final results; or 2 - discard results and return to original values.  reportArray—1 - 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:  typeNum—Takes 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 5’s online Help.  sheetName—Name of the sheet containing the Solver model.

SolverLoad:
Loads parameters for an existing Solver model that have been saved to the worksheet. Takes one argument:  loadArea—Range on the active sheet that contains the Solver model.

SolverOk:
Defines a Solver model. Takes four arguments:  setCell—Target cell for the Solver model.  maxMinVal—1 - maximize target; 2 - minimize target; 3 - match target to a specific value.  valueOf—If the maxMinVal argument is set to 3, you must specify the value to which the target cell is to be matched.  byChange—Cell 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:  setCell—Target cell for the Solver model.  maxMinVal—1 - maximize target; 2 - minimize target; 3 - match target to a specific value.  valueOf—If the maxMinVal argument is set to 3, you must specify the value to which the target cell is to be matched.  byChange—Cell or range of cells that will be changed in setting the target cell.

SolverOptions:
Allows you to specify advanced options for your Solver model. Takes 10 arguments:  maxTime—Maximum time Excel will spend solving the problem.  iterations—Maximum iterations Excel will use in solving the problem.  precision—Number between 0 and 1 that specifies the degree of precision to be used in solving the problem.  assumeLinear—If True, Solver assumes that the underlying model is linear.  stepThru—If True, Solver pauses at each trial solution.  estimates—1 for tangent estimates; 2 for quadratic estimates.  derivatives—1 for forward; 2 for central.  search—1 for Quasi-Newton search; 2 for Conjugate Gradient search.  intTolerance—Number between 0 and 1 that specifies tolerance.  scaling—If 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.

4

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:  saveArea—The range of cells to which the Solver model is to be saved.

SolverSolve:
Starts a Solver solution run. Takes two arguments:  userFinish—If True, Solver returns results without displaying anything. If False, Solver returns results and displays the Solver Results dialog box.  showRef—This 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 argument—this 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


				
Jun Wang Jun Wang Dr
About Some of Those documents come from internet for research purpose,if you have the copyrights of one of them,tell me by mail vixychina@gmail.com.Thank you!