首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OptaPlanner ConstraintProvider

OptaPlanner ConstraintProvider
EN

Stack Overflow用户
提问于 2022-06-21 11:17:46
回答 1查看 59关注 0票数 0

我在usecase下面创建了一个小项目解决方案。

用例:

  • 我们有DocYards -它可以处理80/40/20.每小时集装箱。DocYards将把集装箱放在卡车上
  • 我们有卡车-可以载10/20/6。集装箱容量。

问题解决方案:我们需要根据集装箱的容量,在什么时候计划哪辆卡车去哪辆DocYard。

示例:

如果DocYard有40个容量,我们可以在8:00-9:00时隙发送两辆容量为25、15 (与DicYard容量匹配或不足)的卡车。

如果DocYard有10个容量,我们只能在9:00-10:00时隙发送一辆容量大于10 (匹配或低于DicYard容量)的卡车。

我创建了下面的约束来解决这个问题

代码语言:javascript
复制
    Constraint requiredCapacityConstraint(ConstraintFactory constraintFactory) {
    return constraintFactory.forEach(Truck.class)
            .groupBy(Truck::getDocYard, Truck::getTruckCapacity)
            .filter((docYard, reqCapacity) -> reqCapacity > docYard.getCapacity())
            .penalize("requiredCapacityTotal",
                    HardSoftScore.ONE_HARD,
                    (docYard, truckCapacity) -> truckCapacity - docYard.getCapacity());
}

我的产量低于产量,这是错误的-因为它分配的卡车比它的能力更大。

代码语言:javascript
复制
|            |DocYard-A-40|DocYard-B-20|DocYard-C-10|
|------------|------------|------------|------------|
| 08:00      | T40        | T19        | T10        |
|------------|------------|------------|------------|
| 09:00      | T15,T30    | T11,T12    | T03        |
|------------|------------|------------|------------|
| 10:00      | T22        | T20        | T05        |
|------------|------------|------------|------------|
here number after alphabet is CAPACITY of that Truck / DocYard

有人能帮我吗?我怎么解决这个问题?

解决上述问题的github回购

Github链接

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-21 14:55:25

主要的问题是groupBy(Truck::getDocYard, Truck::getTruckCapacity)只对卡车进行分组,而不是把它们加在一起。此外,它没有考虑到卡车的时隙。要计算在特定时隙期间使用的容量,需要使用第二个groupBy键和ConstraintCollector.sum来计算和:

代码语言:javascript
复制
Constraint requiredCapacityConstraint(ConstraintFactory constraintFactory) {
    return constraintFactory.forEach(Truck.class)
        .groupBy(Truck::getDocYard, Truck::getTimeslot, ConstraintCollectors.sum(Truck::getTruckCapacity))
        .filter((docYard, timeslot, reqCapacity) -> reqCapacity > docYard.getCapacity())
        .penalize("requiredCapacityTotal",
                HardSoftScore.ONE_HARD,
                (docYard, timeslot, truckCapacity) -> truckCapacity - docYard.getCapacity());
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72699824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档