Professional Documents
Culture Documents
NelsonCPAPLLC
Optimization Modeling
with Solver in Excel
ByStephenL.NelsonLeaveaComment
ExcelsSolvertoolletsyousolveoptimizationmodelingproblems,alsocommonlyknownas
linearprogrammingprograms.Withanoptimizationmodelingproblem,youwanttooptimize
anobjectivefunctionbutatthesametimerecognizethatthereareconstraints,orlimits.
Whilethisabstractdefinitionsoundscomplicated,atleastattheconceptuallevel,
optimizationmodelingmakescommonsenseonceyouprovideaconcreteexample.
EasyRefresher: How
Optimization Modeling Works
Suppose,forexample,thatyourearesidentialrealestatedeveloperandcontractor.You
createandselltwoproducts:buildinglotsandhouses.Supposethatyoumake$20,000on
eachhomeyoubuildand$15,000oneachbuildinglotyoudevelopandthensell.Yourprinci
palfinancialobjectiveistomaximizeyourprofits,andthisobjectivecanbeexpressedasan
objectivefunction,orequation,thatyouwanttomaximize:
$15,000*Lots+$20,000*Houses=Profits
Ofcourse,anyobjectivefunctionislimitedbycertainconstraints.Tocontinuewiththe
fictionalcaseofresidentialdevelopment,supposethatyouhavetwoprincipallimitingfac
tors:workingcapitalandbulldozercapacity.Yourworkingcapitalof$1,200,000limitsthe
numberoflotsandhousesyoucanannuallysellbecauseeverylotrequiresa$50,000cash
investmentandeveryhouserequiresa$25,000cashinvestment.Thefactthatyouhavea
singlebulldozeravailableforonly3,000hourseachyearalsolimitsthenumberoflotsand
housesyoucanannuallysellbecauseeverylotrequires80hoursofbulldozingandevery
houserequires200hoursofbulldozing.Thesetwoconstraintscanalsobeexpressedas
equations.Forexample,theworkingcapitalconstraintcanbeexpressedasfollows:
$50,000*Lots+$25,000*Houses<=$1,200,000
Thisformulasaystheresultoftheformula$50,000timesthenumberoflotsplus$25,000
timesthenumberofhousesmustbelessthanorequaltotheworkingcapitallimitof
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 1/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
$1,200,000.Thelessthanorequaltosymbolisrepresentedbythe<=operator.
Thebulldozercapacityconstraintcanbeexpressedasfollows:
80*Lots+200*Houses<=3000
Thisformulasaystheresultoftheformula80timesthenumberoflotsplus200timesthe
numberofhousesmustbelessthanorequaltothebulldozerhourslimitof3,000.Again,the
lessthanorequaltosymbolisrepresentedbythe<=operator.
Typically,youalsohavepolicyconstraintswhenyouworkwithanoptimizationmodeling
problem.Supposethatasamatterofpolicyyouwanttomaintainacertainlevelofactivity
bothindevelopinglotsandbuildinghouses.Youmightsay,forexample,thatbecauseyou
mustmaintainyourteamsexpertiseinbothrawlanddevelopmentandresidential
contractingthatyouwanttodevelopatleast10lotseveryyearandbuildatleast5houses.
Thesetwoconstraintsalsoneedtobeexpressedasequations.Theminimumnumberoflots
policyconstraintcanbeexpressedasfollows:
Lots>=10
Thisformulasaysthatyouwanttodevelopatleast10buildinglots.Or,restated,thisfor
mulasaysthatthelotsvariablemustbegreaterthanorequalto10.Thegreaterthanor
equaltosymbolisrepresentedbythe>=operator.
Theminimumnumberofhousespolicyconstraintcanbeexpressedasfollows:
Houses>=5
Thisformulasaysthatyouwanttobuildatleast5houses.Or,restated,thisformulasays
thatthehousesvariablemustbegreaterthanorequalto10.Again,thegreaterthanorequal
tosymbolisrepresentedbythe>=operator.
WiththeinformationprovidedintheprecedingparagraphsofthisEasyRefresherTM,Ive
describedyourfictionaloptimizationmodelingproblem.Youwanttomaximizeyourprofits,
whichcanbedescribedusingthefollowingobjectivefunction:
$15,000*Lots+$20,000*Houses=Profits
butyoucantdevelopunlimitednumbersofbuildinglotsorbuildunlimitednumbersof
houses.Youaresubjecttothefollowingconstraints:
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 2/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
$50,000*Lots+$25,000*Houses<=$1,200,000
80*Lots+200*Houses<=3000
Lots>=10
Houses>=5
Youcansolvethisequationinavarietyofways,includinggraphically,iteratively,orusinga
techniquelikesimplexalgebra.Or,youcanprovidetheobjectivefunctionandtheconstraint
equationstoExcelandhaveitsolvetheproblem,whichisthesolutiontechniquedescribed
intheparagraphsthatfollow.
Solving an Optimization
Problem
TouseExcelsSolver,firstbuildaworkbookthatdescribesyouroptimizationmodeling
problem,includingitsobjectivefunctionandanyconstraints,andthentellSolvertolookfor
anoptimalsolution.Aslongasyouunderstandtheconceptsofoptimizationmodeling,as
describedintheprecedingEasyRefresher,thisprocessissimple.
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 3/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
Figure617.Aworkbooksetupfor
optimizationmodeling.
IfyouwanttoexperimentwithSolverbutdontwanttoconstructtheworksheetshownin
Figure617fromscratch,rememberthatalltheworkbookexamplesinthisbookareincluded
onthecompanionCD.
Tobuildthisoranyoptimizationmodelworkbook,followthesesteps:
1.Optionally,tellExceltodisplaytheactualformulasratherthanresults.
oudonthavetotakethisstep,butbecausewithoptimizationmodelingyourereally
moreinterestedinwhattheformulaslooklikeasopposedtotheresultstheyproduce,
youprobablywanttotellExceltodisplayformulasratherthanformularesults.Todo
this,choosetheToolsmenusOptionscommand,clicktheViewtab,andselectthe
Formulascheckbox.
Figure617displaysformulasinsteadofformularesults.
2.Providestartingguessesforthevariables.
Youneedtoprovidestartingguessesforthevariablesyouretryingtooptimize.Youcan
dothissimplybyenteringvaluesincells,butIrecommendyoucreateasmallschedule
ofvariablenamesandvariableguesses,asshowninFigure617intheworksheet
rangeA1:B3.IfyousetupaworksheetrangelikethatshowninFigure617andyou
reallyshouldyoullalsowanttonamethecellsthatholdyourguesses.Inthiscase,
youcandothisbyselectingtheworksheetrangethatholdsthevariablenames(Lots,
Houses)andguessesA2:B3inFigure617andthenbychoosingtheInsertmenus
NamecommandandthenchoosingtheNamesubmenusCreatecommand.When
ExceldisplaystheCreateNamesdialogbox,selecttheLeftColumncheckboxand
clickOK.
3.Describetheobjectivefunction.
InFigure617,theworksheetdescribestheequationwiththefollowingformulalocated
incellB5:=15000*Lots+20000*HousesBecausethecellsholdingthevariableguesses
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 4/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
havebeennamedLotsandHouses,theobjectivefunctionusesthesenamesinplace
ofcellreferences.NotethatthelabelincellA5identifiestheequation,butyouonly
needtoentertheactualequationshownincellB5.
4.Describeeachconstraint.
InFigure617,theconstraintsaredescribedintheworksheetrangeB8:C11.To
describeasingleconstraint,youentertheconstraintequationinonecellandthelim
itingconstantvalueinanothercell.Forexample,theworkingcapitalconstraintmen
tionedintheearlierEasyRefresherTMsaystheformula$50,000timesthenumberof
lotsplus$25,000timesthenumberofhousesmustbelessthanorequalto$1,200,000
(thelimitingconstant).Todescribethisfirstconstraint,youenterthefollowingformulain
cellB8:=Lots*50000+Houses*25000andyouentertheconstantvaluewhichlimitsthis
formulaincellC8:
1200000
Todescribethesecondconstrainttheonethatquantifiesthelimitonbulldozercapac
ityyouenterthefollowingformulaincellB9:
=Lots*80+Houses*200
andyouentertheconstantvaluewhichlimitsthisformulaincellC9:
3000
Todescribethethirdconstraintwhichcomesfromyourminimumnumberoflotspolicy
constraintyouenterthefollowingformulaincellB10:
=Lots
andyouentertheconstantvaluewhichlimitsthisformulaincellC10:
10
Finally,todescribethefourthconstraintwhichcomesfromyourminimumnumberof
housespolicyconstraintyouenterthefollowingformulaincellB11:
=Houses
andyouentertheconstantvaluewhichlimitsthisformulaincellC11:
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 5/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
Onceyouvecompletedtheprecedingsteps,yourereadytouseSolvertolookforan
optimalsolutiontoyourobjectivefunction.
Using Solver
IfyousetupyourworkbookssimilartotheoneshowninFigure617,youwillfindSolver
easytouse.Yousimplyfollowthesesteps:
1.ChoosetheToolsmenusSolvercommand.
ExceldisplaystheSolverParametersdialogbox(seeFigure618).
IfyoudontseetheSolvercommandonyourToolsmenu,itmeanstheSolveraddin
isntyetinstalled.ToinstallSolver,choosetheToolsmenusAddInscommand.When
ExceldisplaystheAddInsdialogbox,scrolldowntheAddInsAvailablelistboxuntil
youseetheSolverAddInentry.SelecttheSolverAddIncheckbox,clickOK,andthen
ifExcelrequestsit,providetheExcelorOfficeinstallationCD.
Figure618.TheSolver
Parametersdialogbox.
2.Identifytheobjectivefunction.
EntertheaddressofthecellthatholdsyourobjectiveintheSetTargetCellbox.For
example,inFigure617,cellB5holdstheobjectivefunction,soyouwouldenterB5in
theSetTargetCellbox.
3.DescribehowSolvershouldoptimizetheobjectivefunction.
UsetheEqualTooptionbuttonstospecifyhowSolveroptimizestheobjectivefunction.
Inthecaseofaprofitfunction,forexample,youwanttomaximizethefunctionsoyou
clicktheMaxbutton.ThisisthecasefortheworkbookshowninFigure617.Ifyour
objectivefunctiondescribedcosts,youwouldinsteadwanttominimizethefunctionand
sowouldclicktheMinbutton.Youmayalsohavesituationsinwhichyouwanttohave
theobjectivefunctionreturnaspecificvalue,andsointhisspecialcaseyouwouldclick
theValueOfbuttonandthenprovidethespecifiedvalue.
4.TellSolverwhichcellsholdyourvariableguesses.
UsetheByChangingCellsboxtotellExcelwhereyouvestoredthevariablesusedin
theobjectivefunctionandconstraintequations.InFigure617,forexample,thework
bookstoresthesevariablesincellsB2andB3,soyoucouldenterthesetwocell
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 6/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
addressesintheByChangingCellsbox.Ifyouvenamedthevariablecells,youcan
alsotypethecellnames,asshowninFigure618.CellB2isnamedLots,andcellB3is
namedHouses.
5.TellSolveryouwanttobegindescribingconstraints.
ClicktheAddbutton.ExceldisplaystheAddConstraintdialogbox(seeFigure619).
Figure619.TheAdd
Constraintdialogbox.
6.Describethefirstconstraint.
Toaddaconstraint,usetheCellReferenceboxtoidentifythecellholdingthefirst
constraintsequation,usetheunnamedoperatorboxtoselectanappropriateconstraint
operator,andthenusetheConstraintboxtoidentifythecellholdingthefirstconstraints
constantvalue.InthecaseoftheworkbookshowninFigure617,forexample,you
mightdothisbyclickingtheCellReferenceboxandthenclickingcellB8,byselecting
<=operator,andthenbyclickingtheConstraintboxandthenclickingcellC8.Figure6
19showshowtheAddConstraintdialogboxshouldlooktospecifythisconstraint.Click
Addtoaddtheconstraint.Thenrepeatthistasktoaddmoreconstraints.
Theunnameddropdownlistboxprovidesfiveconstraintoperators:<=forindicating
lessthanorequalto,=forindicatingequalto,>=forindicatinggreaterthanorequalto,
intforindicatingthatvariablemustbeinteger,andbinforindicatingthatavariablemust
bebinary(equaleitherto1or0).
7.Addanyimplicitintegerconstraints.
Inmanyoptimizationmodelingproblems,youllalsohaveimplicitintegerconstraints.
Whatthismeans,forexample,isthatyoucantusedecimalvaluesaspartofthe
optimalsolution.Forexample,youmightsaythatyoumustdevelopanintegernumber
ofbuildinglotsorbuildanintegernumberofhouses.Orrestatedslightly,youmightsay
thatyoucantgettotheendoftheyearandhaveoneofyourbuildinglotsonlyhalf
doneoroneofyourhousesonlypartiallycomplete.Tospecifyanintegerconstraint,use
theCellReferenceboxtoidentifythevariablecellthatmustbeintegerandthenselect
theintoperatorfromtheunnameddropdownlistbox.Figure620showshowtheAdd
Constraintdialogboxlookswhenyouspecifyanintegerconstraint.Notethatyoudont
enterthewordintegerintheConstraintbox.Exceldoesthat.
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 7/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
Figure620.TheAdd
Constraintdialogbox,this
timeshowinghowaninteger
constraintlooks.
8.Addanybinaryconstraints.
Inahandfulofoptimizationmodelingproblems,youmayalsohavebinaryconstraints.A
binaryconstraintisoneinwhichthevariablemustequaleither0or1.Tospecifya
binaryconstraint,usetheCellReferenceboxtoidentifythevariablecellthatmustbe
binaryandthenselectthebinoperatorfromtheunnameddropdownlistbox.
9.TellExcelyouredoneaddingconstraints.
ToleavetheAddConstraintdialogboxafteryoufinishdescribingyourlastconstraint,
clickOK.ExcelclosestheAddConstraintdialogboxandreturnsyoutotheSolver
Parametersdialogbox.AnyconstraintsyouveaddedshowintheSubjectTo
Constraintslistbox.
Ifyouaddaconstraintandlaterwanttodeleteit,displaytheSolverParametersdialog
box,selecttheconstraint,andthenclicktheDeletebutton.Ifyouaddaconstraintand
laterwanttoeditit,displaytheSolverParametersdialogbox,selecttheconstraint,and
thenclicktheChangebutton.WhenExceldisplaystheEditConstraintdialogbox,useit
tomakeyourchanges.TheEditConstraintdialogboxworksliketheAddConstraint
dialogbox.
10.TellExceltolookforasolution.
ClicktheSolvebuttontodirectExceltolookforasolutiontoyouroptimizationmod
elingproblem.ExcellooksforasolutionandthendisplaystheSolverResultsdialogbox
(seeFigure621).
Figure621.TheSolver
Resultsdialogbox.
Thisdialogboxidentifiesthevariablevaluesthatoptimizeyourobjectivefunctionand
askswhatyouwanttodowiththesevalues.
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 8/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
TotellExceltosaveitssolution,clicktheKeepSolverSolutionbuttonandclick
OK.
TotellExceltodiscarditssolution,clicktheRestoreOriginalValuesbuttonand
clickOK.
TotellExceltosaveitssolutionasascenario,clicktheSaveScenariobuttonand
thenprovideascenarionamewhenprompted.
Theearliersection,WhatIfAnalysiswithScenarioManager,discusseshowscenarios
work.
ExceldoesnotallowIntegerconstraintsinSensitivityandLimitreports.Youwillneedto
returntotheSolverParametersdialogbox(seeFigure618)anddeletetheinteger
parametersforHousesandLotstogetthesereports.Thesereportsinvolvegraphingseveral
parametersinseveralequations.Whenyougraphacomplexequation,notalloftheresults
willhaveintegervalues.
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 9/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
Figure622.Theanswerreport.
Beneaththecomparisonoftheoriginalandfinalvaluesoftheobjectivefunctionsformula
results,Excelcomparestheoriginalvaluesandfinalvaluesofthevariables(seeFigure6
22).ThisinformationletsyouseeexactlybyhowmuchExceladjuststhevariablesinorder
tooptimizeyourobjectivefunction.
Atthebottomoftheanswerreport,Excelanalyzestheconstraintsbycalculatingthefor
mularesultsfortheconstraintsandthencomparingtheseformularesultstotheconstraint
constants.Thissoundslikebusyworkatfirstblush,butthisinformationisoftenveryuseful
intwoimportantways:First,youcanusetheStatusinformationtoseewhichconstraintsare
binding,orlimiting.InFigure622,thebindingconstraintisthebulldozerhours.Second,you
canusetheSlackinformationtoseehowcloseagivenconstraintcomestobecoming
binding.InFigure622,theworkingcapitalconstraintshowsonly25000ofslackinother
words,youhaveonlya2%marginoferrorwithyourworkingcapital($25,000/$1,200,000).
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 10/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
Figure623.Thesensitivityreport.
SomereducedgradientvaluesandsomeLagrangemultiplierswillalwaysshowas0.The
reducedgradientequals0ifthevariablevaluecantbeincreased,andtheLagrange
multipliershowsas0ifaconstraintconstantisntbinding.
AcloserinspectionofthesensitivityreportshowninFigure623,forexample,showsthatthe
reducedgradientvaluesforboththeLotsandHousesvariablesequal0.Thisindicatesthat
neithervaluecanbeincreased.ThesensitivityreportdoesshowLagrangemultipliersforthe
workingcapitalconstraintandforthebulldozerhoursconstraint.TheLagrangemultiplierfor
thebulldozerhourslimit,78.125,indicatesthata1hourincreaseinthenumberofbull
dozinghoursavailableincreasestheobjectivefunction(yourprofits)by78.125.
IfyouvecreatedalinearoptimizationmodelandIlldiscusslinearmodelsbrieflyinthenext
section,CustomizingSolversOperationyoursensitivityreportsincludeseveralad
ditionalpiecesofinformation,includingreducedcosts,shadowprices,objectivecoefficients,
andconstraintrighthandsideranges.
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 11/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
Figure624.Thelimitsreport.
Figure625.TheSolverOptions
dialogbox.
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 12/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
Ahandfuloftheseoptionsareessentiallyselfdescriptive.TheMaxTimebox,forexample,
letsyouspecifyhowlongSolvershouldworkonaproblem,andcanitbesetashighas
32,767seconds(whichisoverninehours).TheIterationsboxletsyouspecifyhowmany
iterationsSolvershouldworkonaproblem,anditcanbesetashighas32,767.
Precision
ThePrecisionboxletsyouspecifyhowpreciseSolvershouldbeincheckingapossible
optimalsolutionagainstyourconstraints.Aprecisionsettingof0.000001,thedefaultset
ting,tellsExcelthatifaconstraintformulavalueiswithin0.000001oftheconstraintcon
stant,itmeetstheconstraint.YoucansetthePrecisionboxtoanyvaluefrom0to1.To
loosenyourprecision,usealargerPrecisionvalue.Totightenyourprecision,useasmaller
Precisionvalue.Asyouboostyourprecision,predictably,Exceltakeslongertoreacha
solution.
Tolerance
TheToleranceboxletsyouspecifyhowpreciseSolvershouldbeinmakingsurethatany
integerconstraintsaremet.ThedefaultTolerancesettingof5,or5%,meansthatifan
objectivefunctionvariableiswithin5%ofanintegervaluefrom95%to105%,inother
wordsExcelcanconsiderittobeaninteger.TheTolerancesetting,bytheway,applies
onlytooptimizationproblemsthatuseintegerconstraints.Asyouincreaseyourprecision,
predictably,Exceltakeslongertoreachasolution.
Convergence
TheConvergenceboxletsyouindicatewhenExcelshouldstoplookingforabettersolu
tion.YoucansettheConvergencevaluetoanyfractionalvaluebetween0and1.Whenthe
changeintheobjectivefunctionislessthanthevalueshownintheConvergencebox,Ex
celstopslookingforabettersolution.TheConvergencesetting,bytheway,appliesonlyto
nonlinearoptimizationmodelingproblems.Asyoureducetheconvergencesetting(i.e.,
increasetheprecision),predictably,Exceltakeslongertoreachasolution.
Assume NonNegative
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 13/14
5/1/2017 OptimizationModelingwithSolverinExcelStephenL.NelsonCPAPLLC
IfyouwanttotellExcelthatyourvariablesmustbeequaltoorgreaterthan0whenyou
haventsetalowerlimitconstraint,youcanselecttheAssumeNonNegativecheckbox.In
effect,whenyoucheckthisbox,youtellExceltocreateanother,implicitsetofconstraints.
Estimates
YouusetheEstimatesoptionbuttonsTangentandQuadratictochoosetheapproachthat
youwantExceltousetocomeupwiththefirsttrialsolution.SelectTangentifyouwantExcel
toextrapolatelinearlyfromatangentvector.SelectQuadraticifyouwantExcelto
extrapolatequadraticlyatechniquewhichmayyieldbetterresultsfornonlinear
optimizationmodelingproblems.
Derivatives
YouusetheDerivativesoptionbuttonsForwardandCentraltospecifythedifferencing
usedtoestimatepartialderivativesoftheobjectivefunctionandconstraintfunctionfor
mulas.Typically,youcanclicktheForwardbutton.However,ifanoptimizationproblemcant
besolvedwithForwardderivatives,youcanclicktheCentralbutton.Usingdifferentials
nearthecenterofatargetoftentakesmorecalculationstosolve,butcanbebetterwith
highlyconstrainedproblemssuchasairlineticketprices.
Search
TheSearchoptionbuttonsNewtonandConjugateletyouchoosethealgorithmExcel
usestofindanoptimalsolution.Ifyourpersonalcomputerhaslotsoffreememory,clickthe
Newtonbuttontoreducethenumberofcalculationiterations(albeitattheexpenseofusing
http://stephenlnelson.com/articles/optimizationmodelingsolverexcel/ 14/14