1

I'm trying to create an optimization model using Microsoft Solver Foundation with VB.NET in Visual Studio 2010. Basically I have a list of 3 types of employees that I need to hire (bartenders, waiters, and hostesses) each with different wages and performance ratings. I have created a decision variable for each potential employee. It is set to 0 for a no hire decision and 1 for a hire decision.

When I try to calculate the total cost of employees, the total performance score, or the sum of each type of employee, I receive an error that states that my decision variables do not yet have a value.

Is there an easier way to add these constraints (possibly in a loop) without individually listing every decision variable for the entire employee database in each constraint?

Here is the code I'm working with.

    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
JStew
  • 437
  • 3
  • 7
  • 19

1 Answers1

1

The decisions won't have values until after you call context.Solve(...).

Brian Templeton
  • 106
  • 2
  • 10