1

我正在尝试在 Visual Studio 2010 中使用带有 VB.NET 的 Microsoft Solver Foundation 创建一个优化模型。基本上,我列出了我需要雇用的 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
4

1 回答 1

1

在您调用之前,这些决定不会有任何价值context.Solve(...)

于 2013-07-09T19:58:30.450 回答