为了账号安全,请及时绑定邮箱和手机立即绑定

在规则中使用 CountableValueRange

在规则中使用 CountableValueRange

翻翻过去那场雪 2023-09-13 15:40:24
我目前正在为共享一些资源的任务构建一个调度应用程序。每个任务可能使用一定百分比的资源。我需要在 Drools 规则中检查的是并行任务对每个共享资源的使用率不超过 100%。所以代码看起来像:@Datapublic class Resource {   @PlanningId   private Integer id;   private String label;}public class ResourceUsage {   @PlanningId   private Integer id;   private Resource resource;   private int usagePercent;}要安排的实体@Data@PlanningEntitypublic class TaskAssignment {   @PlanningId   private Integer id;   @PlanningVariable(valueRangeProviderRefs = { "slotRange" })   private Integer timeSlot;   private int duration;   private ResourceUsage resourceUsage;   public Integer getEndingSlot() {        return timeSlot + duration;   }}最后是解决方案@Data@PlanningSolutionpublic class PlanningSolution {  @PlanningId  private Integer id;  @PlanningEntityCollectionProperty  private List<TaskAssignment> tasks = new ArrayList<>();  @ValueRangeProvider(id = "slotRange")  public CountableValueRange<Integer> getSlotRange() {        return ValueRangeFactory.createIntValueRange(0, 10_000);  }  @ProblemFactCollectionProperty  private Set<Resource> resources = new TreeSet<>();}Setter 和 getter 不存在,因为我使用 Lombok 来避免编写它们。过去,我使用一个类来表示时隙,编写一个规则来迭代时隙集合很容易,我能够按时隙检查每个资源的全局使用情况,并在使用率大于 100% 时进行惩罚。由于我在内存使用方面遇到问题,我决定将 TimeSlot 类转换为 CountableValueRange ,但现在,我不知道如何创建与该范围的每个值相匹配的规则。执行与之前相同的计算。有什么办法或者我必须切换回我的 TimeSlot 课程吗?编辑:包含在一种影子规划实体中的影子变量可以解决这个问题吗?
查看完整描述

1 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

我终于找到了一种编写规则的方法,该规则允许我避免在时间段上进行迭代。这个想法是在作业开始时计算使用情况


rule "Maximum usage of a resource"

       when

             $r : Resource()

             $p : TaskAssignment($id1 : id, $ts : timeSlot != null,

                                 resourceUsage!.ressource==$r,

                                 $usage : resourceUsage!.usagePercent);

             accumulate(TaskAssignment(timeSlot != null, timeSlot <= $ts, 

                                      endingSlot > $ts, id != $id1, 

                                      resourceUsage!.ressource==$r, 

                                      $rate : resourceUsage!.usagePercent);

                                  $s:sum($rate);

                                  $s + $usage > 100) 

       then

             scoreHolder.addHardConstraintMatch(kcontext, 100-($s + $usage));         

end


查看完整回答
反对 回复 2023-09-13
  • 1 回答
  • 0 关注
  • 44 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信