Skip to main content

Constrains to a shift start

Answered

Comments

1 comment

  • Gurobot (AI-generated response)
    • Gurobi Staff

    To add a constraint that ensures a nurse's shift starts only on specific days, which can be multiples of 3 or 5 or any other specified set of days, you can modify your Gurobi model. This constraint should be flexible enough to cater to different shift lengths and minimum days off, as well as varying for each nurse. Here's how you can implement this:

    1. Define Start Days: First, define the allowed start days for each nurse. This can be a dictionary where each nurse is associated with a list of days they are allowed to start their shift.

    2. Add Constraint: Add a new constraint to your model that ensures that if a nurse is scheduled to start a shift, it must be on one of their allowed start days.

    Let's implement these steps in your existing code

    from gurobipy import *

    numday = 15
    num_nurses = ["a", "b", "c", "Telly", "e"]
    multis = { "a": [1, 3], "b": [1, 3], "c": [3, 6], "Telly": [3, 4], "e": [2, 5] }

    # Define the allowed start days for each nurse
    allowed_start_days = {
        "a": list(range(0, numday, 3)),  # Starts on days 0, 3, 6, 9, 12...
        "b": list(range(4, numday, 8)),  # Starts on days 4, 12...
        "c": list(range(0, numday, 5)),  # Example for nurse c
        "Telly": list(range(0, numday, 3)),  # Starts on days 0, 3, 6, 9, 12...
        "e": list(range(2, numday, 7)),  # Example for nurse e
    }

    m = Model('nsp')
    x = m.addVars(num_nurses, range(numday), vtype=GRB.BINARY, name='x')

    # Existing constraints...

    # Add constraint for shift starts based on allowed days
    for nurse in num_nurses:
        for day in range(numday):
            if day not in allowed_start_days[nurse]:
                x[nurse, day].ub = 0

    # Rest of your model...

    m.optimize()
    # Rest of your code...



    In this code snippet, allowed_start_days is a dictionary where each nurse has a list of days they are allowed to start their shifts. The constraint ensures that if a day is not in a nurse's allowed start days, the upper bound for the variable x[nurse, day] is set to 0, effectively disallowing a shift to start on that day.

    This approach provides the flexibility to set different allowed start days for each nurse and can accommodate various shift lengths and off days by adjusting the ranges in the allowed_start_days dictionary.

    0

Please sign in to leave a comment.