在规则中使用 CountableValueRange

我目前正在为共享一些资源的任务构建一个调度应用程序。每个任务可能使用一定百分比的资源。


我需要在 Drools 规则中检查的是并行任务对每个共享资源的使用率不超过 100%。


所以代码看起来像:


@Data

public class Resource {

   @PlanningId

   private Integer id;


   private String label;

}

public class ResourceUsage {

   @PlanningId

   private Integer id;


   private Resource resource;


   private int usagePercent;


}

要安排的实体


@Data

@PlanningEntity

public 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

@PlanningSolution

public 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 课程吗?


编辑:包含在一种影子规划实体中的影子变量可以解决这个问题吗?


翻翻过去那场雪
浏览 45回答 1
1回答

狐的传说

我终于找到了一种编写规则的方法,该规则允许我避免在时间段上进行迭代。这个想法是在作业开始时计算使用情况rule "Maximum usage of a resource"&nbsp; &nbsp; &nbsp; &nbsp;when&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$r : Resource()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$p : TaskAssignment($id1 : id, $ts : timeSlot != null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resourceUsage!.ressource==$r,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$usage : resourceUsage!.usagePercent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;accumulate(TaskAssignment(timeSlot != null, timeSlot <= $ts,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endingSlot > $ts, id != $id1,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resourceUsage!.ressource==$r,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rate : resourceUsage!.usagePercent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $s:sum($rate);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $s + $usage > 100)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scoreHolder.addHardConstraintMatch(kcontext, 100-($s + $usage));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java