Exemplo n.º 1
0
 private ValueSelector applyCaching(
     SelectionCacheType resolvedCacheType,
     SelectionOrder resolvedSelectionOrder,
     ValueSelector valueSelector) {
   if (resolvedCacheType.isCached()
       && resolvedCacheType.compareTo(valueSelector.getCacheType()) > 0) {
     if (!(valueSelector instanceof EntityIndependentValueSelector)) {
       throw new IllegalArgumentException(
           "The valueSelectorConfig ("
               + this
               + ") with resolvedSelectionOrder ("
               + resolvedSelectionOrder
               + ") needs to be based on a EntityIndependentValueSelector."
               + " Check your @"
               + ValueRange.class.getSimpleName()
               + " annotations.");
     }
     valueSelector =
         new CachingValueSelector(
             (EntityIndependentValueSelector) valueSelector,
             resolvedCacheType,
             resolvedSelectionOrder.toRandomSelectionBoolean());
   }
   return valueSelector;
 }
Exemplo n.º 2
0
 public static ValueSelector mockValueSelectorForEntity(
     GenuineVariableDescriptor variableDescriptor, ListMultimap<Object, Object> entityToValues) {
   ValueSelector valueSelector = mock(ValueSelector.class);
   when(valueSelector.getVariableDescriptor()).thenReturn(variableDescriptor);
   for (Map.Entry<Object, Collection<Object>> entry : entityToValues.asMap().entrySet()) {
     Object entity = entry.getKey();
     final List<Object> valueList = (List<Object>) entry.getValue();
     when(valueSelector.getSize(entity))
         .thenAnswer(
             new Answer<Long>() {
               @Override
               public Long answer(InvocationOnMock invocation) {
                 return (long) valueList.size();
               }
             });
     when(valueSelector.iterator(entity))
         .thenAnswer(
             new Answer<Iterator<Object>>() {
               @Override
               public Iterator<Object> answer(InvocationOnMock invocation) {
                 return valueList.iterator();
               }
             });
     when(valueSelector.getSize(entity)).thenReturn((long) valueList.size());
   }
   when(valueSelector.isCountable()).thenReturn(true);
   when(valueSelector.isNeverEnding()).thenReturn(false);
   return valueSelector;
 }
Exemplo n.º 3
0
 public ValuePlacer(Termination termination, ValueSelector valueSelector, int selectedCountLimit) {
   this.termination = termination;
   this.valueSelector = valueSelector;
   variableDescriptor = valueSelector.getVariableDescriptor();
   reinitializeVariableEntityFilter = variableDescriptor.getReinitializeVariableEntityFilter();
   this.selectedCountLimit = selectedCountLimit;
   solverPhaseLifecycleSupport.addEventListener(valueSelector);
   // TODO don't use Integer.MAX_VALUE as a magical value
   if (valueSelector.isNeverEnding() && selectedCountLimit == Integer.MAX_VALUE) {
     throw new IllegalStateException(
         "The placer ("
             + this
             + ") with selectedCountLimit ("
             + selectedCountLimit
             + ") has valueSelector ("
             + valueSelector
             + ") with neverEnding ("
             + valueSelector.isNeverEnding()
             + ").");
   }
 }
  @Override
  protected S createUpcomingSelection() {
    // Ideally, this code should have read:
    //     Object entity = entityIterator.next();
    //     Iterator<Object> valueIterator = valueSelector.iterator(entity);
    //     Object toValue = valueIterator.next();
    // But empty selectors and ending selectors (such as non-random or shuffled) make it more
    // complex
    if (!entityIterator.hasNext()) {
      entityIterator = entitySelector.iterator();
      if (!entityIterator.hasNext()) {
        return noUpcomingSelection();
      }
    }
    Object entity = entityIterator.next();

    Iterator<Object> valueIterator = valueSelector.iterator(entity);
    int entityIteratorCreationCount = 0;
    // This loop is mostly only relevant when the entityIterator or valueIterator is non-random or
    // shuffled
    while (!valueIterator.hasNext()) {
      // Try the next entity
      if (!entityIterator.hasNext()) {
        entityIterator = entitySelector.iterator();
        entityIteratorCreationCount++;
        if (entityIteratorCreationCount >= 2) {
          // All entity-value combinations have been tried (some even more than once)
          return noUpcomingSelection();
        }
      }
      entity = entityIterator.next();
      valueIterator = valueSelector.iterator(entity);
    }
    Object toValue = valueIterator.next();
    return newChangeSelection(entity, toValue);
  }
Exemplo n.º 5
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;
  }
 public PillarChangeMoveSelector(
     PillarSelector pillarSelector, ValueSelector valueSelector, boolean randomSelection) {
   this.pillarSelector = pillarSelector;
   this.valueSelector = valueSelector;
   this.randomSelection = randomSelection;
   GenuineVariableDescriptor variableDescriptor = valueSelector.getVariableDescriptor();
   if (variableDescriptor.isChained()) {
     throw new IllegalStateException(
         "The selector ("
             + this
             + ") has a variableDescriptor ("
             + variableDescriptor
             + ") which is chained ("
             + variableDescriptor.isChained()
             + ").");
   }
   phaseLifecycleSupport.addEventListener(pillarSelector);
   phaseLifecycleSupport.addEventListener(valueSelector);
 }
Exemplo n.º 7
0
 public static ValueSelector mockValueSelector(
     GenuineVariableDescriptor variableDescriptor, Object... values) {
   ValueSelector valueSelector = mock(ValueSelector.class);
   when(valueSelector.getVariableDescriptor()).thenReturn(variableDescriptor);
   final List<Object> valueList = Arrays.<Object>asList(values);
   when(valueSelector.iterator(any()))
       .thenAnswer(
           new Answer<Iterator<Object>>() {
             @Override
             public Iterator<Object> answer(InvocationOnMock invocation) throws Throwable {
               return valueList.iterator();
             }
           });
   when(valueSelector.isCountable()).thenReturn(true);
   when(valueSelector.isNeverEnding()).thenReturn(false);
   when(valueSelector.getSize(any())).thenReturn((long) valueList.size());
   return valueSelector;
 }
 public boolean isNeverEnding() {
   return randomSelection || pillarSelector.isNeverEnding() || valueSelector.isNeverEnding();
 }
 public boolean isCountable() {
   return pillarSelector.isCountable() && valueSelector.isCountable();
 }