I need to build a csp scheduler that given a set of students with name,competence,disponibility and a set of tasks with name,competence_required,duration prints (or better show it in a graph) what task a student has to do. I'm at the beginning of building this scheduler so i tried with few variables, but i cannot resolve an error. If a student has an ability that cover more task, multiple task can be assigned to him if the total time does not exced his disponibility. I need to code it in a future vision where i can extend the number of students and tasks importing a csv with pandas.
This is the code:
from constraint import *
students = (("Name1", 10, "Competence1"),("Name2", 8, "Competence2"))
tasks = (("Task1", 4, "Competence1"),("Task2", 2, "Competence2"))
problem = Problem()
variables = tuple([(student + task) for student in students for task in tasks])
for variable in variables:
problem.addVariable(variable, [False, True])
for variable in variables:
print(variable)
problem.addConstraint(lambda variable: True if variable[2] == variable[5] and variable[1] >= variable[4] else False , variable)
solutions = problem.getSolutions()
if solutions:
print("Solutions:")
for solution in solutions:
for key in solution.keys():
if solution[key]:
print(key, solution[key])
else:
print("No solution")
The error is:
Traceback (most recent call last):
File "main.py", line 93, in <module>
solutions = problem.getSolutions()
File "/home/runner/CSP-esempio-libro-funzionante/venv/lib/python3.10/site-packages/constraint/__init__.py", line 268, in getSolutions
domains, constraints, vconstraints = self._getArgs()
File "/home/runner/CSP-esempio-libro-funzionante/venv/lib/python3.10/site-packages/constraint/__init__.py", line 309, in _getArgs
vconstraints[variable].append((constraint, variables))
KeyError: 'Nome1'
I tried a lot of different strategies for implementing the problem but every bring me to this. Can someone help me please?