简体   繁体   中英

Excel VBA Solver Loop taking too long

I created a code that runs through 60 optimizations through solver through 4 series of loops (each with 15 'iterations'). The code works great but it is taking FOREVER to run through (over an hour). Each optimization is a simple linear model (global solution is found), just changing which month I am looking at.

Problem setup: I am trying to minimize the number of workers/test rigs that will satisfy the intake with the constraint of being under capacity.

I am not sure how I can make it go any faster, but I cannot send this to other people and expect them to use it. Does anybody have any suggestions?

Below is my code:

Sub Optimization()

Application.ScreenUpdating = False

'Unlocks workbook to allow updating
Call Unlock_Workbook

'Makes visible and selects the tab where the optimization problem is set up
Sheets("Optimization").Visible = True
Sheets("Optimization").Select

'Clear variable ranges that solver will change
Range("Worker_All[[1]:[15]]").Clear
Range("TestRig_All[[1]:[15]]").Clear
Range("Worker_787[[1]:[15]]").Clear
Range("TestRig_787[[1]:[15]]").Clear

'Install the Add-in for users who have no done this already
AddIns("Solver Add-in").Installed = True


'Optimized All Workers

For i = 1 To 15

'Cell address for objective
Min = Cells(3, 2 + i).Address
'Cell adress for variable
Variable = Range("Worker_All[" & i & "]").Address
'Cell address for constraint range
ConstraintRange = Range("IntakeHours_NonKeyWO[" & i & "]").Address
'Cell address for constrants
Constraint = Range("IntakeHours_NonKeyWOC[" & i & "]").Address

SolverReset

SolverOk SetCell:=Min, MaxMinVal:=2, ValueOf:=0, ByChange:=Variable, _
    Engine:=2, EngineDesc:="Simplex LP"
SolverAdd CellRef:=ConstraintRange, Relation:=1, FormulaText:=Constraint
SolverSolve True

Next i

'Optimized All Test Rigs

For i = 1 To 15

Min = Cells(4, 2 + i).Address
Variable = Range("TestRig_All[" & i & "]").Address
ConstraintRange = Range("IntakeHours_NonKeyMO[" & i & "]").Address
Constraint = Range("IntakeHours_NonKeyMOC[" & i & "]").Address

SolverReset

SolverOk SetCell:=Min, MaxMinVal:=2, ValueOf:=0, ByChange:=Variable, _
    Engine:=2, EngineDesc:="Simplex LP"
SolverAdd CellRef:=ConstraintRange, Relation:=1, FormulaText:=Constraint
SolverSolve True

Next i

'Optimized 787 Workers

For i = 1 To 15

Min = Cells(5, 2 + i).Address
Variable = Range("Worker_787[" & i & "]").Address
ConstraintRange = Range("IntakeHours_Key787WO[" & i & "]").Address
Constraint = Range("IntakeHours_Key787WOC[" & i & "]").Address

SolverReset

SolverOk SetCell:=Min, MaxMinVal:=2, ValueOf:=0, ByChange:=Variable, _
    Engine:=2, EngineDesc:="Simplex LP"
SolverAdd CellRef:=ConstraintRange, Relation:=1, FormulaText:=Constraint
SolverSolve True

Next i

'Optimized 787 Test Rigs

For i = 1 To 15

Min = Cells(6, 2 + i).Address
Variable = Range("TestRig_787[" & i & "]").Address
ConstraintRange = Range("IntakeHours_Key787MO[" & i & "]").Address
Constraint = Range("IntakeHours_Key787MOC[" & i & "]").Address

SolverReset

SolverOk SetCell:=Min, MaxMinVal:=2, ValueOf:=0, ByChange:=Variable, _
    Engine:=2, EngineDesc:="Simplex LP"
SolverAdd CellRef:=ConstraintRange, Relation:=1, FormulaText:=Constraint
SolverSolve True

Next i

Sheets("Cell Summary").Select
Sheets("Optimization").Visible = False

Call Lock_Workbook

Application.ScreenUpdating = True

End Sub

Figured out the reason. My worksheet contained hundreds of vlookups and other reactive functions. This was causing the worksheet to re-calculate each iteration and slowing it down immensely. By hardcoding these values in, I was able to get the time down to <2 minutes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM