TimefoldAI / timefold-solver-python

Timefold Solver is an AI constraint solver for Python to optimize the Vehicle Routing Problem, Employee Rostering, Maintenance Scheduling, Task Assignment, School Timetabling, Cloud Optimization, Conference Scheduling, Job Shop Scheduling, Bin Packing and many more planning problems.
https://timefold.ai
Apache License 2.0
27 stars 3 forks source link

Better error message: KeyError: "'__name__' not in globals" #93

Open ge0ffrey opened 1 week ago

ge0ffrey commented 1 week ago

This constraint on employee scheduling

def minimize_overtime(constraint_factory: ConstraintFactory):
    return (constraint_factory.for_each(Shift)
            .group_by(lambda shift: shift.employee, lambda shift: shift.start.date() - timedelta(days=shift.start.date().weekday()),
                      lambda shift: ConstraintCollectors.sum(get_shift_duration_in_minutes(shift)))
            .filter(lambda employee, week_start_date, duration: duration > 40 * 60)
            .penalize(HardSoftScore.ONE_SOFT,
                    lambda employee, week_start_date, duration: duration - 40 * 60)
            .as_constraint("Minimize overtime")
            )

doesn't give a good error message

INFO:timefold.solver:Solving started: time spent (33), best score (-2850init/0hard/0soft), environment mode (REPRODUCIBLE), move thread count (NONE), random (JDK with seed 0).
INFO:timefold.solver:Problem scale: entity count (2850), variable count (2850), approximate value count (500), approximate problem scale (1.160145 × 10^7692).
Traceback (most recent call last):
...
  File "python/src/employee_scheduling/main.py", line 18, in timefold
    solution = timefold_solve(problem, time_spent_seconds)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python/src/employee_scheduling/timefold/solve.py", line 25, in solve
    solution = solver.solve(problem)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/python/.venv/lib/python3.12/site-packages/timefold/solver/_solver.py", line 109, in solve
    java_solution = self._delegate.solve(java_problem)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python/.venv/lib/python3.12/site-packages/_jpyinterpreter/jvm_setup.py", line 256, in apply
    __import__(module_name, python_globals, python_locals, python_from_list, level),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: "'__name__' not in globals"
ge0ffrey commented 1 week ago

The problem lies in

lambda shift: ConstraintCollectors.sum(get_shift_duration_in_minutes(shift))

that should be

ConstraintCollectors.sum(lambda shift: get_shift_duration_in_minutes(shift)))