0

I'm completely new to constraint programming and am having trouble setting up this problem. Using the below dataset, which is a simple representation of a much larger problem, I would like to generate every combination subject to a set of constraints. The constraints are:

  1. Use 1 employee from each group, except for group A, which should have 2 employees
  2. An employee can only be used once in each combination (i.e. Michelle can only be in group B or C)
  3. Total Salary should be between 25 and 30

I've gotten this far with the code, but I'm having trouble setting up the constraints.

from ortools.sat.python import cp_model
import pandas as pd

model = cp_model.CpModel()

# sample data set
df = pd.DataFrame([['John', 'A',5], ['Andy', 'A/C', 10], ['Michelle', 'B/C', 15], 
                   ['Jennifer', 'B', 5], ['Frank', 'C',10], ['Zack','A',5]],
                   columns=['Employee','Group','Salary'])
# add employee variable
employees = [model.NewBoolVar(x) for x in df['Employee']]

# constraint: 2 employees for group A, 1 employee otherwise
code

# use employee only once
code

# total salary between 25 and 30
code

  • My advice: If you are new to LP and looking to *learn* it, then I'd pick a different framework than OR-Tools. It is excellent, but it masks many of the fundamentals that you should work with in sets and linear expressions by using some slick functionality that masks the basic concepts that you should work with. Ditch `pandas` also, make some sets & dictionaries and perhaps pick up an entry level text on LP or work through some of the many tutorials online. – AirSquid Jul 13 '22 at 17:39
  • This is not a LP problem :-) You can have a look at: https://github.com/google/or-tools/blob/stable/examples/python/shift_scheduling_sat.py – Laurent Perron Jul 13 '22 at 19:21
  • Ok. Fair enough.... as expressed it is a constraint programming problem, but it could also be expressed as a trivial ILP... Given this post reflects the asker is new to the field, some latitude in terms is probably OK. I'm not well versed in CP, so I'll defer. Cheers. – AirSquid Jul 13 '22 at 22:30
  • CP-SAT is a full ILP solver, with all the techniques from this field (presolve, linear relaxations, cuts, branching...). So the model will likely be the same. – Laurent Perron Jul 14 '22 at 11:54

0 Answers0