optaplanner学习笔记(十一) 查看分数:哪些约束被打破了

本文介绍了在开发过程中如何查看和解析评分系统的约束分数。通过ScoreManager API,可以获取解决方案的详细评分解释,包括约束匹配总数和实体的指控信息。约束匹配总数展示了每个约束对总分的贡献,而指控信息则揭示了每个规划实体对分数的影响。这在实现如会议安排等复杂问题的优化解决方案时特别有用,有助于在webUI中呈现评分和约束匹配的可视化信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发过程中查看约束分数的最简单方法是打印 explainScore()的返回值,但此方法只能看到大概的信息。

System.out.println(scoreManager.getSummary(solution));

例如,在会议安排中,这意味着Task S51打破了硬约束:Speaker required room tag:

分数说明(-1hard/-806soft):
    约束匹配总数:
        -1hard:约束(需要发言者的房间标签)有 1 个匹配项:
            -1hard:理由([S51]-340soft:约束(主题轨道冲突)有 32 个匹配项:
            -20soft:理由([S68,S66]-20soft:理由([S61,S44]...
        ...
    起诉书(72 项中的前 5 项):
        -1hard/-22soft:对齐 (S51)12 个匹配项:
            -1hard:约束(演讲者需要的房间标签)
            -10soft:约束(主题轨道冲突)
            ...
        ...
Explanation of score (-1hard/-806soft):
    Constraint match totals:
        -1hard: constraint (Speaker required room tag) has 1 matches:
            -1hard: justifications ([S51])
        -340soft: constraint (Theme track conflict) has 32 matches:
            -20soft: justifications ([S68, S66])
            -20soft: justifications ([S61, S44])
            ...
        ...
    Indictments (top 5 of 72):
        -1hard/-22soft: justification (S51) has 12 matches:
            -1hard: constraint (Speaker required room tag)
            -10soft: constraint (Theme track conflict)
            ...
        ...

这些信息都是概略性的信息,如果要查看每个约束的匹配结果及分值,则可以通过ConstraintMatch API进行查看。

6.1。在求解器外使用Solver

如果您的应用程序是webUI,需要呈现一个解决方案的分数和约束匹配信息,请使用ScoreManager API。

ScoreManager<CloudBalance, HardSoftScore> scoreManager = ScoreManager.create(solverFactory);
ScoreExplanation<CloudBalance, HardSoftScore> scoreExplanation = scoreManager.explainScore(cloudBalance);

获取求解结果的总分值:

HardSoftScore score = scoreExplanation.getScore();

此外,ScoreExplanation可以通过约束匹配总数and/or 来帮助查看分数:在这里插入图片描述

6.2. 约束匹配总数:按约束分解得分

要查看每个约束条件的得分,可以从ScoreExplanation中获得ConstraintMatchTotals。

Collection<ConstraintMatchTotal<HardSoftScore>> constraintMatchTotals = scoreExplanation.getConstraintMatchTotalMap().values();
for (ConstraintMatchTotal<HardSoftScore> constraintMatchTotal : constraintMatchTotals) {
    String constraintName = constraintMatchTotal.getConstraintName();
    // The score impact of that constraint
    HardSoftScore totalScore = constraintMatchTotal.getScore();

    for (ConstraintMatch<HardSoftScore> constraintMatch : constraintMatchTotal.getConstraintMatchSet()) {
        List<Object> justificationList = constraintMatch.getJustificationList();
        HardSoftScore score = constraintMatch.getScore();
        ...
    }
}

每个ConstraintMatchTotal数代表一个约束匹配信息,并占总分数的一部分。所有的总和ConstraintMatchTotal.getScore()等于总分数。

约束流和Drools 分数计算自动支持约束匹配,但增量 Java 分数计算需要 实现一个额外的接口。

6.3. 规划实体热图

需要在用户界面中显示一个热图,可以查看每个PlanningEntity所匹配到的约束Score信息,突出PlanningEntity和ProblemFact对Score的影响,可以从ScoreExplanation中获取Indictment信息:

Map<Object, Indictment<HardSoftScore>> indictmentMap = scoreExplanation.getIndictmentMap();
for (CloudProcess process : cloudBalance.getProcessList()) {
    Indictment<HardSoftScore> indictment = indictmentMap.get(process);
    if (indictment == null) {
        continue;
    }
    // The score impact of that planning entity
    HardSoftScore totalScore = indictment.getScore();

    for (ConstraintMatch<HardSoftScore> constraintMatch : indictment.getConstraintMatchSet()) {
        String constraintName = constraintMatch.getConstraintName();
        HardSoftScore score = constraintMatch.getScore();
        ...
    }
}

每个Indictment是对象所涉及的所有约束的总和。所有Indictment.getScoreTotal()的总和与总体分数不同,因为多个Indictments可以共享同一个ConstraintMatch。

约束流和Drools 分数计算自动支持约束匹配,但增量 Java 分数计算需要 实现一个额外的接口。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值