AI and DS Skills

 View Only
  • 1.  Docplex CP. Add 'first' constraint to sequence variable

    Posted 23 days ago

    Hi

    I have added a sequence variable to cover all interval variables assigned to each resource in my model

    I would like to add an interval variable so that it is the first in the sequence.

    My code looks like:

    1. Create the sequence_variable
      for r in resources :      

                int_vars = [value for entry in intervals for key, value in entry.items() if r in key]   # list of intvars for this resource

                seq = model.sequence_var(int_vars,name = 'seq_var_'+r)      # create the sequence_var for each resource & include all int_vars for the resource

                mdl.resources[r].seq_var = seq

     

    1. Create an interval to put first in the sequence

    itv = model.interval_var(size=service_time, name="Service_Start_"+r, optional=False)    # create interval var for the activity

    model.add(model.first(seq_var,itv))

     

    When I add step 1, there appears no issue

    When I do the second part, the program just goes to sleep. No response for 20 minutes

    Clearly my attempt to post the 'first' constraint is incorrect

     

    Can someone tell me the correct way to post this constraint

     

    Thanks

     

    Ross Dye

     



  • 2.  RE: Docplex CP. Add 'first' constraint to sequence variable

    Posted 23 days ago

    To correctly add an interval variable as the first in a sequence in your model, you need to ensure that the interval variable is appropriately integrated into the sequence constraint. Here's how you can do it:

    1. Create the sequence variable for each resource:

      • Collect all interval variables associated with a resource.
      • Create the sequence variable including all these intervals.
    2. Create the interval variable that should be first in the sequence:

      • Define the new interval variable for the activity.
    3. Add the interval as the first in the sequence:

      • Ensure the new interval variable is included in the sequence variable.
      • Add the constraint to enforce this interval variable to be the first in the sequence.

    Here is an example of how you can achieve this:

    from docplex.cp.model import CpoModel # Create the model model = CpoModel() # Assuming you have your resources and intervals defined resources = ['resource1', 'resource2'] # Example resources intervals = [ {'resource1': model.interval_var(size=5, name='int1')}, {'resource1': model.interval_var(size=3, name='int2')}, {'resource2': model.interval_var(size=4, name='int3')}, {'resource2': model.interval_var(size=2, name='int4')} ] # Create sequence variables for r in resources: int_vars = [value for entry in intervals for key, value in entry.items() if r in key] # list of intvars for this resource seq = model.sequence_var(int_vars, name='seq_var_' + r) # create the sequence_var for each resource & include all int_vars for the resource model.add(seq) # Create an interval to put first in the sequence itv = model.interval_var(size=5, name="Service_Start_" + r, optional=False) # create interval var for the activity # Add the new interval to the sequence variable int_vars.append(itv) seq = model.sequence_var(int_vars, name='seq_var_' + r) # recreate the sequence variable with the new interval # Enforce the interval to be the first in the sequence model.add(model.first(seq, itv)) # Solve the model solution = model.solve(TimeLimit=20) if solution: print(solution) else: print("No solution found")

    Explanation:

    1. Creating the Sequence Variable: We gather all interval variables associated with a specific resource and create a sequence variable (seq).
    2. Creating the Interval Variable: We define a new interval variable (itv) that we want to place first in the sequence.
    3. Adding the Interval to the Sequence: We add the new interval to the list of interval variables and recreate the sequence variable to include this new interval.
    4. Adding the Constraint: We add a constraint using model.first(seq, itv) to ensure the new interval variable is the first in the sequence.

    Notes:

    • Ensure that the interval variable (itv) is included in the sequence variable before enforcing it to be first.
    • Recreating the sequence variable after adding the new interval ensures that the constraint model.first(seq, itv) is correctly applied.

    By following these steps, you should be able to enforce the interval variable to be the first in the sequence without causing the program to hang.



    ------------------------------
    Mark Williams
    ------------------------------



  • 3.  RE: Docplex CP. Add 'first' constraint to sequence variable

    Posted 23 days ago

    Thanks Mark

    Thanks for the detailed response.

    I will implement this a little today (its early now) and will advise

     

    Thanks again

     

    Ross Dye

    0400669880