You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Optapy求解器阶段报错TypeError: Unable to convert 求助

Optapy求解器创建时TypeError: Unable to convert问题排查

报错信息

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

核心问题分析

你的ReservationTable类所有带装饰器的属性方法(get_timeslot_list、get_seat_range等)都定义在了类外部,Optapy无法识别这些属于@planning_solution类的必要属性方法,导致无法将ReservationTable正确转换为OptaPlanner所需的Solution类型,从而抛出转换错误。

修复步骤

1. 修正类方法缩进

将所有装饰器方法缩进,使其成为ReservationTable类的成员方法,同时修正命名歧义问题:

# domain.py 修正后代码片段
@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_employee_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

2. 确认Planning Entity定义

确保Reservation类正确标记@planning_entity,并包含至少一个@planning_variable(这是求解器规划的核心对象):

@planning_entity
class Reservation:
    def __init__(self, id, timeslot=None, seat=None, employee=None):
        self.id = id
        self.timeslot = timeslot
        self.seat = seat
        self.employee = employee
    
    @planning_variable(Timeslot, value_range_provider_refs=['timeslotRange'])
    def get_timeslot(self):
        return self.timeslot
    
    def set_timeslot(self, timeslot):
        self.timeslot = timeslot
    
    # 按需添加其他规划变量,比如座位、员工分配

3. 确认约束提供者定义

define_constraints需要是一个返回ConstraintProvider的函数,示例如下:

def define_constraints(constraint_factory):
    return [
        # 示例约束:同一时间段同一座位不能重复分配
        constraint_factory.for_each(Reservation)
            .join(Reservation,
                  Joiners.equal(lambda r: r.timeslot),
                  Joiners.equal(lambda r: r.seat),
                  Joiners.less_than(lambda r: r.id))
            .penalize("同一座位重复分配", HardSoftScore.ONE_HARD)
    ]

4. 检查数据生成逻辑

确保generate_problem函数中生成的timeslot_list、seat_list等都是对应类(Timeslot、Seat等)的实例列表,而非原始数据类型。

额外注意事项

  • 确保main.py中导入了所有必要模块(如optapy、Duration、HardSoftScore)及自定义类;
  • 验证define_constraints函数已正确导入到main.py中。

内容的提问来源于stack exchange,提问作者Karim Omrane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 01:45:35