[英]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'). 我创建了一个代码,该代码通过4个循环的循环(每个循环包含15个“迭代”),通过求解器进行了60次优化。 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.
我的工作表包含数百个vlookup和其他反应式功能。 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.
通过对这些值进行硬编码,我可以将时间缩短到不到2分钟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.