I want make K different elements in bool array,I use code: model.Add(len(set([shifts[(i)] for i in range(10)]))==4) ,but it not work! How can I do this?
model = cp_model.CpModel()
solver = cp_model.CpSolver()
shifts = {}
ones = [model.NewBoolVar("") for _ in range(10)]
for i in range(10):
shifts[(i)] = model.NewIntVar(0, 10, "shifts(%i)" % i)
for i in range(10):
model.Add(shifts[(i)] >0).OnlyEnforceIf(ones[(i)])
model.Add(shifts[(i)] == 0).OnlyEnforceIf(ones[(i)].Not())
model.Add(sum(ones[(i)] for i in range(10)) == 5)
# I want make 4 different but it not work!
#model.Add(len(set([shifts[(i)] for i in range(10)]))==4)
status = solver.Solve(model)
print("status:",status)
res=[]
for i in range(10):
res.append(solver.Value(shifts[(i)]))
print(res)
Encode your integers using booleans and add another boolean for each value to mark it as used