Microsoft Solver Foundation:将循环中的决策变量添加到约束中

时间:2013-04-12 17:26:20

标签: asp.net vb.net visual-studio-2010 solver ms-solver-foundation

我正在尝试在Visual Studio 2010中使用Microsoft Solver Foundation和VB.NET创建优化模型。基本上我有一个我需要雇用的3种类型员工的列表(调酒师,服务员和礼仪小姐)不同的工资和绩效评级。我为每个潜在员工创建了一个决策变量。它被设置为0表示没有雇用决定,1表示雇用决定。

当我尝试计算员工的总成本,总绩效得分或每种员工类型的总和时,我收到一个错误,指出我的决策变量还没有值。

是否有更简单的方法来添加这些约束(可能在循环中),而无需在每个约束中单独列出整个员工数据库的每个决策变量?

这是我正在使用的代码。

    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

1 个答案:

答案 0 :(得分:1)

在您致电context.Solve(...)之后,决定才会有价值。