1
我試圖在Visual Studio 2010中使用Microsoft Solver Foundation與VB.NET創建優化模型。基本上,我有一個3種類型的員工列表,需要僱用(調酒師,服務員和女主人),每個人都有不同的工資和績效評級。我爲每個潛在員工創建了一個決策變量。它被設置爲0作爲無僱傭決定,1作爲僱用決定。Microsoft Solver Foundation:將循環中的決策變量添加到約束條件
當我嘗試計算員工總成本,總績效分數或每種員工類型的總和時,我收到一個錯誤,指出我的決策變量尚未具有價值。
是否有一種更簡單的方法來添加這些約束(可能在一個循環中),而無需在每個約束中單獨列出整個僱員數據庫的每個決策變量?
這是我正在使用的代碼。
Dim myEmployee As Employee
Dim context As SolverContext = SolverContext.GetContext()
Dim model As Model = context.CreateModel()
For Each myEmployee In employeeList
If myEmployee.Type = "Bartender" Then
Dim BartenderHire As New Decision(Domain.IntegerRange(0, 1), myEmployee.ID)
barDecisionList.Add(BartenderHire)
ElseIf myEmployee.Type = "Host(ess)" Then
Dim HostHire As New Decision(Domain.IntegerRange(0, 1), myEmployee.ID)
hostDecisionList.Add(HostHire)
ElseIf myEmployee.Type = "Waiter/Waitress" Then
Dim WaiterHire As New Decision(Domain.IntegerRange(0, 1), myEmployee.ID)
waitDecisionList.Add(WaiterHire)
End If
Next
For i = 0 To barDecisionList.Count - 1
model.AddDecision(barDecisionList.Item(i))
Next
For i = 0 To hostDecisionList.Count - 1
model.AddDecision(hostDecisionList.Item(i))
Next
For i = 0 To waitDecisionList.Count - 1
model.AddDecision(waitDecisionList.Item(i))
Next
'Calculate cost of hired employees.
Dim cost As Double
cost = 0
For i = 0 To model.Decisions.Count - 1
Dim thisEmployee As Employee = employeeList.Item(i)
cost = cost + (model.Decisions(i).ToDouble * thisEmployee.Wage * 6)
Next
'Calculate total score of hired employees.
Dim totalScore As Double
totalScore = 0
For i = 0 To model.Decisions.Count - 1
Dim thisEmployee As Employee = employeeList.Item(i)
totalScore = totalScore + (model.Decisions(i).ToDouble * thisEmployee.Score)
Next
'Calculate total bartenders hired.
Dim barSum As Integer
barSum = 0
For i = 0 To barDecisionList.Count - 1
barSum = barSum + barDecisionList.Item(i)
Next
'Calculate total waiters hired.
Dim waitSum As Integer
waitSum = 0
For i = 0 To waitDecisionList.Count - 1
waitSum = waitSum + waitDecisionList.Item(i)
Next
'Calculate total hosts hired.
Dim hostSum As Integer
hostSum = 0
For i = 0 To hostDecisionList.Count - 1
hostSum = hostSum + hostDecisionList.Item(i)
Next
'Add constraints
model.AddConstraint("Bartenders_Required", barSum = bartendersRequired)
model.AddConstraint("WaitStaff_Required", waitSum = waitersRequired)
model.AddConstraint("Hosts_Required", hostSum = hostsRequired)
model.AddConstraint("Budget", cost <= budget)
model.AddGoal("Total_Score", GoalKind.Maximize, totalScore)
Dim solution As Solution = context.Solve(New SimplexDirective())
Dim report As Report = solution.GetReport
For i = 0 To model.Decisions.Count - 1
solutionList.Add(model.Decisions(i))
Next