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
36 stars 3 forks source link

chore: Remove old wrapper generation code and use more Pythonic API #23

Closed Christopher-Chianelli closed 3 months ago

Christopher-Chianelli commented 3 months ago

This refactor remove the old wrapper generation code (which was used to add annotations to fields) and uses the new jpyinterpreter API to add annotations to the fields instead. As a result, the following was able to be removed:

Additionally, the API and annotations where refactored to be more Pythonic. For instance,

class A:
    @planning_variable(int)
    def get_x():
        return x

becomes

class A:
    x: Annotated[int, PlanningVariable]

and

solver_config = \
SolverConfig().withSolutionClass(...).withEntityClasses(...)
solver = solver_factory_create(solver_config).buildSolver()

becomes

solver_config = SolverConfig(
    solution_class=SolutionClass,
    entity_classes=[EntityClass],
    ...)
solver = SolverFactory.create(solver_config).build_solver()

This process also uncovered several bugs in jpyinterpreter:

The tests also had several typing errors which are now fixed.

Additionally, no more extremely unuseful error message with generic advice; the stack trace is now preserved for non-signal exceptions (cause, Python really likes using exceptions in it bytecode; whenever you do a for-loop, that actually a try { while True {...} } catch(StopIteration) {} block).

As a side-effect, if no best solution listener is registered, the code never returns to Python until Solving finishes (before, it returned to Python on each new best solution).

To allow Timefold Solver to use the class object generated by jpyinterpreter, several changes need to be made:

The following two features are currently unsupported in this version:

Additionally, upgraded min version to Python 3.10, since numpy no longers support Python 3.9 (https://numpy.org/neps/nep-0029-deprecation_policy.html) and the scientific python community also recommends dropping support for Python 3.9 since the start of this year (https://scientific-python.org/specs/spec-0000/).

triceo commented 3 months ago

Wrt. the "currently unsupported features".

Let's start making a list somewhere, so that we have a good overview. Maybe a Github issue with a checklist?

Christopher-Chianelli commented 3 months ago

Created issue https://github.com/TimefoldAI/timefold-solver/issues/971 for tracking (Copied from the optapy list and added the two things I noted)