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

Spring Boot中如何通过TimeFold SolverManager访问初始化中的解决方案?

解决Timefold构建阶段无法实时获取进度/中间解的问题

1. 开启构建阶段的中间解事件推送

默认情况下,Timefold的构建启发式阶段不会触发最佳解更新事件,需修改Solver配置,让它在构建过程中定期推送中间解:

方法1:XML配置文件修改

在你的Solver配置XML(如your-solver-config.xml)中,给<constructionHeuristic>添加事件监听配置,设置每次计算N个解后触发事件:

<solver>
    <!-- 其他配置(如评分器、终止条件等) -->
    <constructionHeuristic>
        <eventListenerConfig>
            <!-- 每生成100个候选解就触发一次更新,可根据问题规模调整数值 -->
            <triggeredScoreCalculationCount>100</triggeredScoreCalculationCount>
        </eventListenerConfig>
    </constructionHeuristic>
</solver>

方法2:Java API动态配置

如果用Java代码配置Solver,可在初始化SolverConfig时添加该配置:

SolverConfig solverConfig = SolverConfig.createFromXmlResource("your-solver-config.xml");
// 开启构建阶段的中间事件推送,每100次评分计算触发一次
solverConfig.getConstructionHeuristicConfig()
            .getEventListenerConfig()
            .setTriggeredScoreCalculationCount(100);
// 将配置传入SolverManager
SolverManager<YourSolutionType, Long> solverManager = SolverManager.create(solverConfig);

2. 使用solveAndListen获取实时更新

配置完成后,使用solveAndListen方法替代原有的solveBuilder,此时回调会在构建阶段就开始触发,而非等到构建完成:

this.solverJob = solverManager.solveAndListen(
        currentSolution.getId(),
        jobId -> currentSolution,
        solution -> {
            this.currentSolution = solution;
            // 这里可添加进度更新逻辑,比如推送给前端或记录日志
            log.info("构建阶段中间解更新,当前评分:{}", solution.getScore());
        },
        (jobId, exception) -> {
            log.error("求解任务失败,jobId: {}", jobId, exception);
        }
);

3. 直接访问构建中的解(可选)

若需主动获取当前构建中的解(不推荐频繁轮询,建议优先用事件回调),可通过SolverManager获取对应Solver实例,再调用getBestSolution():

// 注意:仅在非求解线程调用,避免频繁调用影响性能
Solver<YourSolutionType> solver = solverManager.getSolver(currentSolution.getId());
if (solver != null && solver.isSolving()) {
    YourSolutionType intermediateSolution = solver.getBestSolution();
    // 处理中间解
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 15:42:48