pythonoptimizationoptaplanneroptapy

Optapy Error "return SolverFactory.create(solver_config) TypeError: Unable to convert"


I am working on an optapy project and I am getting this error in the solver phase.

\optapy\optaplanner_api_wrappers.py", line 310, in solver_factory_create
    return SolverFactory.create(solver_config)
TypeError: Unable to convert

You can find here my domain.py file and the main.py file domain.py

  #I defined the problem facts here
    @planning_solution
    class ReservationTable:
        def __init__(self, timeslot_list, seat_list, reservation_list, employee_list, score = None):
            self.timeslot_list = timeslot_list
            self.seat_list = seat_list
            self.reservation_list = reservation_list
            self.employee_list = employee_list
            self.score = score
            
    @problem_fact_collection_property(Timeslot)
    @value_range_provider('timeslotRange')
    def get_timeslot_list(self):
        return self.timeslot_list
    @problem_fact_collection_property(Seat)
    @value_range_provider('seatRange')
    def get_seat_range(self):
        return self.seat_list
    @problem_fact_collection_property(Employee)
    @value_range_provider('employeeRange')
    def get_seat_list(self):
        return self.employee_list
    
    @planning_entity_collection_property(Reservation)
    def get_reservation_list(self):
        return self.reservation_list
    
    @planning_score(HardSoftScore)
    def get_score(self):
        return self.score
    def set_score(self, newScore):
        self.score = newScore
        
    
def generate_problem():
    #I created here an example of data
    return ReservationTable(timeslot_list, seat_list, reservation_list, employee_list)

main.py

solver_config = optapy.config.solver.SolverConfig() \
    .withEntityClasses(Reservation) \
    .withSolutionClass(ReservationTable) \
    .withConstraintProviderClass(define_constraints) \
    .withTerminationSpentLimit(Duration.ofSeconds(30))
solver_factory = solver_factory_create(solver_config)
solver = solver_factory.buildSolver()
solution = solver.solve(generate_problem())
print(solution)

Does anyone have an idea about this problem ? Thank you


Solution

  • The issue is in your @constraint_provider (which is not shown in the question). The error was raised when trying to convert the list returned by defined_constraints into a list of Constraint. In particular, the @constraint_provider must return a list of Constraint generated by the constraint_factory parameter. For instance:

    from optapy import constraint_provider
    from optapy.score import HardSoftScore
    from optapy.constraint import Joiners
    from domain import Employee, Reservation
    
    @constraint_provider
    def define_constraints(constraint_factory):
        return [
            constraint_factory.for_each(Employee)
                .if_not_exists(Reservation, Joiners.equal(lambda employee: employee.name, lambda reservation: reservation.employee.name))
                .penalize('Employee not assigned', HardSoftScore.ONE_HARD)
        ]