@Override
 public MoveSelector buildBaseMoveSelector(
     HeuristicConfigPolicy configPolicy,
     SelectionCacheType minimumCacheType,
     boolean randomSelection) {
   EntitySelectorConfig entitySelectorConfig_ =
       entitySelectorConfig == null ? new EntitySelectorConfig() : entitySelectorConfig;
   EntitySelector entitySelector =
       entitySelectorConfig_.buildEntitySelector(
           configPolicy,
           minimumCacheType,
           SelectionOrder.fromRandomSelectionBoolean(randomSelection));
   ValueSelectorConfig valueSelectorConfig_ =
       valueSelectorConfig == null ? new ValueSelectorConfig() : valueSelectorConfig;
   ValueSelector[] valueSelectors = new ValueSelector[K - 1];
   for (int i = 0; i < valueSelectors.length; i++) {
     valueSelectors[i] =
         valueSelectorConfig_.buildValueSelector(
             configPolicy,
             entitySelector.getEntityDescriptor(),
             minimumCacheType,
             SelectionOrder.fromRandomSelectionBoolean(randomSelection));
   }
   return new KOptMoveSelector(entitySelector, valueSelectors, randomSelection);
 }
Exemplo n.º 2
0
 /**
  * @param configPolicy never null
  * @param minimumCacheType never null, If caching is used (different from {@link
  *     SelectionCacheType#JUST_IN_TIME}), then it should be at least this {@link
  *     SelectionCacheType} because an ancestor already uses such caching and less would be
  *     pointless.
  * @param inheritedSelectionOrder never null
  * @return never null
  */
 public PillarSelector buildPillarSelector(
     HeuristicConfigPolicy configPolicy,
     SelectionCacheType minimumCacheType,
     SelectionOrder inheritedSelectionOrder) {
   if (minimumCacheType.compareTo(SelectionCacheType.STEP) > 0) {
     throw new IllegalArgumentException(
         "The pillarSelectorConfig ("
             + this
             + ")'s minimumCacheType ("
             + minimumCacheType
             + ") must not be higher than "
             + SelectionCacheType.STEP
             + " because the pillars change every step.");
   }
   // EntitySelector uses SelectionOrder.ORIGINAL because a SameValuePillarSelector STEP caches the
   // values
   EntitySelectorConfig entitySelectorConfig_ =
       entitySelectorConfig == null ? new EntitySelectorConfig() : entitySelectorConfig;
   EntitySelector entitySelector =
       entitySelectorConfig_.buildEntitySelector(
           configPolicy, minimumCacheType, SelectionOrder.ORIGINAL);
   Collection<GenuineVariableDescriptor> variableDescriptors =
       entitySelector.getEntityDescriptor().getVariableDescriptors();
   return new SameValuePillarSelector(
       entitySelector, variableDescriptors, inheritedSelectionOrder.toRandomSelectionBoolean());
 }
Exemplo n.º 3
0
 public SwapMoveSelector(
     EntitySelector leftEntitySelector,
     EntitySelector rightEntitySelector,
     Collection<GenuineVariableDescriptor> variableDescriptors,
     boolean randomSelection) {
   this.leftEntitySelector = leftEntitySelector;
   this.rightEntitySelector = rightEntitySelector;
   this.variableDescriptors = variableDescriptors;
   this.randomSelection = randomSelection;
   EntityDescriptor leftEntityDescriptor = leftEntitySelector.getEntityDescriptor();
   EntityDescriptor rightEntityDescriptor = rightEntitySelector.getEntityDescriptor();
   if (!leftEntityDescriptor.getEntityClass().equals(rightEntityDescriptor.getEntityClass())) {
     throw new IllegalStateException(
         "The selector ("
             + this
             + ") has a leftEntitySelector's entityClass ("
             + leftEntityDescriptor.getEntityClass()
             + ") which is not equal to the rightEntitySelector's entityClass ("
             + rightEntityDescriptor.getEntityClass()
             + ").");
   }
   boolean anyChained = false;
   if (variableDescriptors.isEmpty()) {
     throw new IllegalStateException(
         "The selector ("
             + this
             + ")'s variableDescriptors ("
             + variableDescriptors
             + ") is empty.");
   }
   for (GenuineVariableDescriptor variableDescriptor : variableDescriptors) {
     if (!variableDescriptor
         .getEntityDescriptor()
         .getEntityClass()
         .isAssignableFrom(leftEntityDescriptor.getEntityClass())) {
       throw new IllegalStateException(
           "The selector ("
               + this
               + ") has a variableDescriptor with a entityClass ("
               + variableDescriptor.getEntityDescriptor().getEntityClass()
               + ") which is not equal or a superclass to the leftEntitySelector's entityClass ("
               + leftEntityDescriptor.getEntityClass()
               + ").");
     }
     if (variableDescriptor.isChained()) {
       anyChained = true;
     }
   }
   this.anyChained = anyChained;
   phaseLifecycleSupport.addEventListener(leftEntitySelector);
   if (leftEntitySelector != rightEntitySelector) {
     phaseLifecycleSupport.addEventListener(rightEntitySelector);
   }
 }
Exemplo n.º 4
0
 public static EntitySelector mockEntitySelector(
     EntityDescriptor entityDescriptor, Object... entities) {
   EntitySelector entitySelector = mock(EntitySelector.class);
   when(entitySelector.getEntityDescriptor()).thenReturn(entityDescriptor);
   final List<Object> entityList = Arrays.<Object>asList(entities);
   when(entitySelector.iterator())
       .thenAnswer(
           new Answer<Iterator<Object>>() {
             @Override
             public Iterator<Object> answer(InvocationOnMock invocation) throws Throwable {
               return entityList.iterator();
             }
           });
   when(entitySelector.listIterator())
       .thenAnswer(
           new Answer<ListIterator<Object>>() {
             @Override
             public ListIterator<Object> answer(InvocationOnMock invocation) throws Throwable {
               return entityList.listIterator();
             }
           });
   for (int i = 0; i < entityList.size(); i++) {
     final int index = i;
     when(entitySelector.listIterator(index))
         .thenAnswer(
             new Answer<ListIterator<Object>>() {
               @Override
               public ListIterator<Object> answer(InvocationOnMock invocation) throws Throwable {
                 return entityList.listIterator(index);
               }
             });
   }
   when(entitySelector.endingIterator())
       .thenAnswer(
           new Answer<Iterator<Object>>() {
             @Override
             public Iterator<Object> answer(InvocationOnMock invocation) throws Throwable {
               return entityList.iterator();
             }
           });
   when(entitySelector.isCountable()).thenReturn(true);
   when(entitySelector.isNeverEnding()).thenReturn(false);
   when(entitySelector.getSize()).thenReturn((long) entityList.size());
   return entitySelector;
 }
  @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);
  }
 public AbstractRandomChangeIterator(EntitySelector entitySelector, ValueSelector valueSelector) {
   this.entitySelector = entitySelector;
   this.valueSelector = valueSelector;
   entityIterator = entitySelector.iterator();
   // Don't do hasNext() in constructor (to avoid upcoming selections breaking mimic recording)
 }
Exemplo n.º 7
0
 public boolean isNeverEnding() {
   return randomSelection
       || leftEntitySelector.isNeverEnding()
       || rightEntitySelector.isNeverEnding();
 }
Exemplo n.º 8
0
 public boolean isCountable() {
   return leftEntitySelector.isCountable() && rightEntitySelector.isCountable();
 }
Exemplo n.º 9
0
 public Iterator<Placement> iterator() {
   return new QueuedEntityPlacingIterator(entitySelector.iterator());
 }