如何让OptaPlanner Construction Heuristics使用不同的值?

时间:2015-07-23 09:34:32

标签: optaplanner

当我使用Construction Heuristics时,我总是得到分配给PlanningVariables的相同值,这会导致错误的初始解决方案。

我很确定我在这里犯了一个错误,但看不到我失败的地方。

我的问题描述:

我尝试将资源分配给WorkOrders。一个WorkOrder可以有多个ResourceAssignments。此ResourceAssignment是我的主要PlanningEntity。 资源是PlanningVariable。 WorkOrders也是PlanningEntities,但是这里只需要显示我有多个PlanningEntity的信息,因此不能使用更简单的ConstructionHeuristic配置。

我使用来自实体的值范围提供程序,因此我可以为不同类型的ResourceAssignments返回不同的资源集。 (例如Human,Forklift,Handscanner ......)

我的问题是构造启发式似乎总是将相同的资源分配给所有ResourceAssignment。

以下是CH的输出 - 您可以看到为两个ResourceAssignments分配了相同的人力资源(id 100)。对于以下所有PlanningEntities,这都是相同的。

2015-07-23 10:57:12,291 [main] INFO  Solving started: time spent (185), best score (uninitialized/-9900hard/0soft), environment mode (FAST_ASSERT), random (JDK with seed 0).
2015-07-23 10:57:12,429 [main] DEBUG     CH step (0), time spent (325), score (-9900hard/0soft), selected move count (200), picked move (ResourceAssignment{id=0, workorder=WorkOrder{id=0, name=WO0, start=Thu Jul 23 12:57:11 CEST 2015, stop=Thu Jul 23 14:57:11 CEST 2015, offset=0}, resource=null, type=human} => Resource{type=human, id=100}).
2015-07-23 10:57:12,523 [main] DEBUG     CH step (1), time spent (419), score (-9900hard/0soft), selected move count (200), picked move (ResourceAssignment{id=1, workorder=WorkOrder{id=0, name=WO0, start=Thu Jul 23 12:57:11 CEST 2015, stop=Thu Jul 23 14:57:11 CEST 2015, offset=0}, resource=null, type=human} => Resource{type=human, id=100}).
2015-07-23 10:57:12,576 [main] DEBUG     CH step (2), time spent (472), score (-9900hard/0soft), selected move count (99), picked move (ResourceAssignment{id=2, workorder=WorkOrder{id=0, name=WO0, start=Thu Jul 23 12:57:11 CEST 2015, stop=Thu Jul 23 14:57:11 CEST 2015, offset=0}, resource=null, type=forklift} => Resource{type=forklift, id=0}).
2015-07-23 10:57:12,667 [main] DEBUG     CH step (3), time spent (563), score (-9900hard/0soft), selected move count (200), picked move (ResourceAssignment{id=3, workorder=WorkOrder{id=1, name=WO1, start=Thu Jul 23 12:57:11 CEST 2015, stop=Thu Jul 23 14:57:11 CEST 2015, offset=0}, resource=null, type=human} => Resource{type=human, id=100}).
2015-07-23 10:57:12,757 [main] DEBUG     CH step (4), time spent (653), score (-9900hard/0soft), selected move count (200), picked move (ResourceAssignment{id=4, workorder=WorkOrder{id=1, name=WO1, start=Thu Jul 23 12:57:11 CEST 2015, stop=Thu Jul 23 14:57:11 CEST 2015, offset=0}, resource=null, type=human} => Resource{type=human, id=100}).
2015-07-23 10:57:12,795 [main] DEBUG     CH step (5), time spent (691), score (-9900hard/0soft), selected move count (99), picked move (ResourceAssignment{id=5, workorder=WorkOrder{id=1, name=WO1, start=Thu Jul 23 12:57:11 CEST 2015, stop=Thu Jul 23 14:57:11 CEST 2015, offset=0}, resource=null, type=forklift} => Resource{type=forklift, id=0}).
2015-07-23 10:57:12,867 [main] DEBUG     CH step (6), time spent (763), score (-9900hard/0soft), selected move count (200), picked move (ResourceAssignment{id=6, workorder=WorkOrder{id=2, name=WO2, start=Thu Jul 23 12:57:11 CEST 2015, stop=Thu Jul 23 14:57:11 CEST 2015, offset=0}, resource=null, type=human} => Resource{type=human, id=100}).
2015-07-23 10:57:12,917 [main] DEBUG     CH step (7), time spent (813), score (-9900hard/0soft), selected move count (200), picked move (ResourceAssignment{id=7, workorder=WorkOrder{id=2, name=WO2, start=Thu Jul 23 12:57:11 CEST 2015, stop=Thu Jul 23 14:57:11 CEST 2015, offset=0}, resource=null, type=human} => Resource{type=human, id=100}).

它似乎忽略了所有其他动作 - 当然我有一条规则,惩罚同时使用相同的资源和硬分数。

这是我的解决方案配置。

<?xml version="1.0" encoding="UTF-8"?>
<solver>
  <environmentMode>FAST_ASSERT</environmentMode>
  <termination>
    <unimprovedSecondsSpentLimit>10000</unimprovedSecondsSpentLimit>
    <secondsSpentLimit>220000</secondsSpentLimit>
  </termination>

  <!-- Domain model configuration -->
  <solutionClass>com.opal.solver.resource.ResourceAssignmentSolution</solutionClass>
  <entityClass>com.opal.solver.resources.entity.ResourceAssignment</entityClass>
  <entityClass>com.opal.solver.resources.entity.WorkOrder</entityClass>
  <!-- Score configuration -->
  <scoreDirectorFactory>
    <scoreDefinitionType>HARD_SOFT</scoreDefinitionType>
    <scoreDrl>resourceAsssignmentRules.drl</scoreDrl>
  </scoreDirectorFactory>    
  <constructionHeuristic>
    <queuedEntityPlacer>
      <entitySelector id="placerEntitySelector">
        <cacheType>PHASE</cacheType>
        <entityClass>com.opal.solver.resources.entity.ResourceAssignment</entityClass>
      </entitySelector>
      <changeMoveSelector>
        <entitySelector mimicSelectorRef="placerEntitySelector"/>
        <valueSelector>
           <selectionOrder>ORIGINAL</selectionOrder>
          <variableName>resource</variableName>
        </valueSelector>
      </changeMoveSelector>
    </queuedEntityPlacer>
  </constructionHeuristic>
</solver>

我确信我在这里错过了一些optaplanner的核心概念。我会感激任何正确方向的暗示。

1 个答案:

答案 0 :(得分:1)

现在看起来你有一个详细的FIRST_FIT配置。使用FIRST_FIT_DECREASING,通过声明难度比较并使用该构造启发式类型。

<constructionHeuristic>
  <constructionHeuristicType>FIRST_FIT_DECREASING</>
</>

请参阅有关First Fit Decreasing的文档章节。通常情况下,FFD明显优于FF(例如平均为CloudBalancing的4%),但使用本地搜索来改进该解决方案。

如果你真的在寻找随机的不同CH结果(例如对于GeneticAlgorithm人口),请使用selectionOrder RANDOM而不是ORGINAL。