I am currently trying to learn constraint programming. I am trying to add some conditions to the model that is a string, and a length both should satisfy the model but for some reason it does not. This is the code :
from ortools.sat.python import cp_model
# Create model
model = cp_model.CpModel()
number_choices = []
for i in range(5):
# numbers from 1 to max_value
number_choices.append(model.NewIntVar(1, 5, f"number {i}"))
model.Add(sum(number_choices) == 12)
# - this works
# model.Add(number_choices[1] - number_choices[0] == 2)
# - this should satisfy the model but does not work
model.Add(str(number_choices[1] - number_choices[0]) == "2")
model.Add(len(str(number_choices[1] - number_choices[0])) == 1)
solver = cp_model.CpSolver()
status = solver.Solve(model)
if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
print([solver.Value(v) for v in number_choices])
else:
print(None) # no solution found
If you uncomment the condition with length and string, this would be the result :
[1, 3, 5, 2, 1]
so 3 - 1 = 2, 2 has a length of 1. I am quite confused as to why this does not satisfy the condition. Sorry if this impossible, I am new to constraint programming