2017-03-15 83 views
0

我創建了一個代碼,它通過求解器通過4個循環(每次15'迭代)完成60次優化。該代碼工作得很好,但它需要永久運行(超過一個小時)。每個優化都是一個簡單的線性模型(找到全局解決方案),只是改變我正在查看的月份。Excel VBA求解器循環花費時間過長

問題設置:我試圖儘量減少工人/測試平臺的數量,以滿足攝入量和容量不足的限制。

我不知道我怎麼能讓它走得更快,但我不能把這個發送給其他人,並期望他們使用它。有人有任何建議嗎?

下面是我的代碼:

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 
+0

你可能會考慮問這裏:http://codereview.stackexchange.com/help/on-topic instead – Adam

+0

你可以試試'Engine:= 1'嗎? –

+0

通過調試器來查找瓶頸或將計時器放在不同的部分,並讓他們debug.print到達這些部分所用的時間。 –

回答

0

想通了的原因。我的工作表包含數百個vlookups和其他反應函數。這導致工作表重新計算每個迭代並極大地減慢它的速度。通過對這些值進行硬編碼,我可以將時間縮短到2分鐘。