การ Solver ด้วยการเขียนโค้ด
Posted: Thu Mar 21, 2019 11:06 am
สวัสดีค่ะ
เขียนโค้ด VBA เพื่อใช้สำหรับการรันหาค่า optimize แต่โค้ดที่เขียนไม่สามารถทำงานได้ค่ะ และแจ้ง "user-defined type not defined" ค่ะ
ด้านล่างคือโค้ดที่ให้ในการรันค่ะ และแนบไฟล์ที่ลองทำมาด้วยค่ะ
เขียนโค้ด VBA เพื่อใช้สำหรับการรันหาค่า optimize แต่โค้ดที่เขียนไม่สามารถทำงานได้ค่ะ และแจ้ง "user-defined type not defined" ค่ะ
ด้านล่างคือโค้ดที่ให้ในการรันค่ะ และแนบไฟล์ที่ลองทำมาด้วยค่ะ
Code: Select all
Sub RunSovel()
Dim prob As New RSM.Problem
Dim obj As New RSM.Function
Dim var As New RSM.Variable
Dim constr As New RSM.Function
With prob
.Variables.Clear
.Function.Clear
.Engine.ParamReset
'Objective
.obj.Init Range("TotalCost")
.obj.FunctionType = Function_Type_Objective
.Function.Add obj
'Min Value
.Solver.SolveType = Solver_Type_Minimize
'Variable cells
.var.Init Range("NumberWorking")
.var.IntegerType.Array = Integer_Type_Binary
.Variables.Add var
'Constraint
constr.Init Range("TotalWorking")
constr.FunctionType = Function_Type_Constraint
constr.LowerBound.Array = "MinimumNeeded"
.Function.Add constr
.Solver.Optimize
.Engine = .Engines(prob.Engine.Simplex)
End Sub