Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  Nurse scheduling model using Python Cplex

    Posted Tue April 05, 2022 09:29 AM
    Edited by System Admin Fri January 20, 2023 04:41 PM

    Hello i am a beginner with python and am trying to implement a nurse scheduling model which i found on google or-tools into python cplex. I have porblems when trying to setup the constraints: "Each shift is assigned to exactly one nurse in the schedule period" and "Each nurse works at most one shift per day" With my current code i get the following error message:

    DOcplexException: Expecting constraint, got: \<generator object \<genexpr\> at 0x000001CBF38644A0\> with type: \<class 'generator'\>

    What do i have to change in my code to set upt the constraints?

    Thank you for every help in advance!

    Thats my current code:

    ´´´´

    from docplex.mp.model import Model
    
    # Data.
    num_nurses = 4
    num_shifts = 3
    num_days = 3
    all_nurses = range(num_nurses)
    all_shifts = range(num_shifts)
    all_days = range(num_days)
    
    # Creates the model.
    model = Model()
    
    # Creates shift variables.
    # shifts[(n, d, s)]: nurse 'n' works shift 's' on day 'd'.
    shifts = {}
    for n in all_nurses:
        for d in all_days:
            for s in all_shifts:
                shifts[(n, d, s)] = model.binary_var
    
    # Each shift is assigned to exactly one nurse in the schedule period.
    for d in all_days:
        for s in all_shifts:
            model.add_constraint(shifts [(n, d, s)] for n in all_nurses) == 1
    
    # Each nurse works at most one shift per day.
    for n in all_nurses:
        for d in all_days:
            model.add_constraint(shifts[(n, d, s)] for s in all_shifts) <=1
    

    That is the full error message:

    runfile('C:/Program Files/IBM/ILOG/CPLEX_Studio221/python/examples/cp/basic/untitled0.py', wdir='C:/Program Files/IBM/ILOG/CPLEX_Studio221/python/examples/cp/basic')
    Traceback (most recent call last):
    
      File "C:\Program Files\IBM\ILOG\CPLEX_Studio221\python\examples\cp\basic\untitled0.py", line 33, in <module>
        model.add_constraint(shifts [(n, d, s)] for n in all_nurses) ==1
    
      File "C:\Users\admin\anaconda3\lib\site-packages\docplex\mp\model.py", line 4049, in add_constraint
        ct = self._add_constraint_internal(ct, ctname)
    
      File "C:\Users\admin\anaconda3\lib\site-packages\docplex\mp\model.py", line 3549, in _add_constraint_internal
        if self._prepare_constraint(ct, used_ct_name, check_for_trivial_ct=check_trivial):
    
      File "C:\Users\admin\anaconda3\lib\site-packages\docplex\mp\model.py", line 3515, in _prepare_constraint
        checker.typecheck_ct_to_add(ct, self, 'add_constraint')
    
      File "C:\Users\admin\anaconda3\lib\site-packages\docplex\mp\tck.py", line 355, in typecheck_ct_to_add
        self.fatal("Expecting constraint, got: {0!r} with type: {1!s}", ct, type(ct))
    
      File "C:\Users\admin\anaconda3\lib\site-packages\docplex\mp\tck.py", line 256, in fatal
        self._logger.fatal(msg, args)
    
      File "C:\Users\admin\anaconda3\lib\site-packages\docplex\mp\error_handler.py", line 210, in fatal
        raise DOcplexException(resolved_message)
    
    DOcplexException: Expecting constraint, got: <generator object <genexpr> at 0x000002AAA9ACB200> with type: <class 'generator'>

    #DecisionOptimization


  • 2.  RE: Nurse scheduling model using Python Cplex

    Posted Tue April 05, 2022 11:49 AM
    You need to specify that the sum over all nurses equals 1 in the first constraint and that the sum over shifts is less than or equal to 1 in the second constraint. You omitted the summations. There are a number of examples (python source code) that ship with CPLEX Studio, and I would wager pretty much all of them contain examples of summing variables within a constraint.

    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------



  • 3.  RE: Nurse scheduling model using Python Cplex

    Posted Wed April 06, 2022 03:23 AM
    Hi,

    I answered the same question in stackoverflow : https://stackoverflow.com/questions/71750060/nurse-scheduling-model-using-python-cplex

    from docplex.mp.model import Model
    
    # Data.
    num_nurses = 4
    num_shifts = 3
    num_days = 3
    all_nurses = range(num_nurses)
    all_shifts = range(num_shifts)
    all_days = range(num_days)
    
    # Creates the model.
    model = Model()
    # Creates shift variables.
    # shifts[(n, d, s)]: nurse 'n' works shift 's' on day 'd'.
    shifts = {}
    
    for n in all_nurses:
        for d in all_days:
            for s in all_shifts:
                shifts[(n, d, s)] = model.binary_var()
    
    for d in all_days:
        for s in all_shifts:
            model.add_constraint(model.sum(shifts [(n, d, s)] for n in all_nurses) == 1 )     
    
    
    
    # Each nurse works at most one shift per day.
    for n in all_nurses:
        for d in all_days:
            model.add_constraint(model.sum(shifts[(n, d, s)] for s in all_shifts) <=1)
            
    model.solve(log_output=True,)​

    works fine



    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------