public void stepEnded(ConstructionHeuristicStepScope<Solution_> stepScope) {
   super.stepEnded(stepScope);
   entityPlacer.stepEnded(stepScope);
   decider.stepEnded(stepScope);
   if (logger.isDebugEnabled()) {
     long timeMillisSpent = stepScope.getPhaseScope().calculateSolverTimeMillisSpentUpToNow();
     logger.debug(
         "    CH step ({}), time spent ({}), score ({}), selected move count ({}),"
             + " picked move ({}).",
         stepScope.getStepIndex(),
         timeMillisSpent,
         stepScope.getScore(),
         stepScope.getSelectedMoveCount(),
         stepScope.getStepString());
   }
 }
Пример #2
0
  public ConstructionHeuristicMoveScope nominateMove(ConstructionHeuristicStepScope stepScope) {
    Object entity = stepScope.getEntity();
    if (!reinitializeVariableEntityFilter.accept(stepScope.getScoreDirector(), entity)) {
      return null;
    }
    // TODO extract to PlacerForager
    Score maxScore = null;
    ConstructionHeuristicMoveScope nominatedMoveScope = null;

    int moveIndex = 0;
    for (Iterator it = valueSelector.iterator(entity); it.hasNext(); ) {
      Object value = it.next();
      ConstructionHeuristicMoveScope moveScope = new ConstructionHeuristicMoveScope(stepScope);
      moveScope.setMoveIndex(moveIndex);
      Move move;
      if (variableDescriptor.isChained()) {
        move = new ChainedChangeMove(entity, variableDescriptor, value);
      } else {
        move = new ChangeMove(entity, variableDescriptor, value);
      }
      moveScope.setMove(move);
      if (!move.isMoveDoable(stepScope.getScoreDirector())) {
        logger.trace(
            "        Move index ({}) not doable, ignoring move ({}).",
            moveScope.getMoveIndex(),
            move);
      } else {
        doMove(moveScope);
        // TODO extract to PlacerForager
        if (maxScore == null || moveScope.getScore().compareTo(maxScore) > 0) {
          maxScore = moveScope.getScore();
          // TODO for non explicit Best Fit *, default to random picking from a maxMoveScopeList
          nominatedMoveScope = moveScope;
        }
        if (moveIndex >= selectedCountLimit) {
          break;
        }
      }
      moveIndex++;
      if (termination.isPhaseTerminated(stepScope.getPhaseScope())) {
        break;
      }
    }
    return nominatedMoveScope;
  }
  @Override
  public void solve(DefaultSolverScope<Solution_> solverScope) {
    ConstructionHeuristicPhaseScope<Solution_> phaseScope =
        new ConstructionHeuristicPhaseScope<>(solverScope);
    phaseStarted(phaseScope);

    for (Placement placement : entityPlacer) {
      ConstructionHeuristicStepScope<Solution_> stepScope =
          new ConstructionHeuristicStepScope<>(phaseScope);
      stepStarted(stepScope);
      decider.decideNextStep(stepScope, placement);
      if (stepScope.getStep() == null) {
        if (termination.isPhaseTerminated(phaseScope)) {
          logger.trace(
              "    Step index ({}), time spent ({}) terminated without picking a nextStep.",
              stepScope.getStepIndex(),
              stepScope.getPhaseScope().calculateSolverTimeMillisSpentUpToNow());
        } else if (stepScope.getSelectedMoveCount() == 0L) {
          logger.warn(
              "    No doable selected move at step index ({}), time spent ({})."
                  + " Terminating phase early.",
              stepScope.getStepIndex(),
              stepScope.getPhaseScope().calculateSolverTimeMillisSpentUpToNow());
        } else {
          throw new IllegalStateException(
              "The step index ("
                  + stepScope.getStepIndex()
                  + ") has selected move count ("
                  + stepScope.getSelectedMoveCount()
                  + ") but failed to pick a nextStep ("
                  + stepScope.getStep()
                  + ").");
        }
        // Although stepStarted has been called, stepEnded is not called for this step
        break;
      }
      doStep(stepScope);
      stepEnded(stepScope);
      phaseScope.setLastCompletedStepScope(stepScope);
      if (termination.isPhaseTerminated(phaseScope)) {
        break;
      }
    }
    phaseEnded(phaseScope);
  }
 private void doStep(ConstructionHeuristicStepScope<Solution_> stepScope) {
   Move nextStep = stepScope.getStep();
   nextStep.doMove(stepScope.getScoreDirector());
   predictWorkingStepScore(stepScope, nextStep);
 }