|
| 1 | +package ai.timefold.solver.core.impl.heuristic.selector.entity.decorator; |
| 2 | + |
| 3 | +import java.util.Iterator; |
| 4 | +import java.util.List; |
| 5 | +import java.util.ListIterator; |
| 6 | +import java.util.Objects; |
| 7 | +import java.util.Random; |
| 8 | + |
| 9 | +import ai.timefold.solver.core.impl.domain.entity.descriptor.EntityDescriptor; |
| 10 | +import ai.timefold.solver.core.impl.heuristic.selector.AbstractDemandEnabledSelector; |
| 11 | +import ai.timefold.solver.core.impl.heuristic.selector.common.ReachableValues; |
| 12 | +import ai.timefold.solver.core.impl.heuristic.selector.common.iterator.UpcomingSelectionIterator; |
| 13 | +import ai.timefold.solver.core.impl.heuristic.selector.entity.EntitySelector; |
| 14 | +import ai.timefold.solver.core.impl.heuristic.selector.value.IterableValueSelector; |
| 15 | +import ai.timefold.solver.core.impl.phase.scope.AbstractPhaseScope; |
| 16 | +import ai.timefold.solver.core.impl.solver.scope.SolverScope; |
| 17 | + |
| 18 | +/** |
| 19 | + * The decorator returns a list of reachable entities for a specific value. |
| 20 | + * It enables the creation of a filtering tier when using entity value selectors, |
| 21 | + * ensuring only valid and reachable entities are returned. |
| 22 | + * |
| 23 | + * e1 = entity_range[v1, v2, v3] |
| 24 | + * e2 = entity_range[v1, v4] |
| 25 | + * |
| 26 | + * v1 = [e1, e2] |
| 27 | + * v2 = [e1] |
| 28 | + * v3 = [e1] |
| 29 | + * v4 = [e2] |
| 30 | + * |
| 31 | + * @param <Solution_> the solution type |
| 32 | + */ |
| 33 | +public final class FilteringEntityValueRangeSelector<Solution_> extends AbstractDemandEnabledSelector<Solution_> |
| 34 | + implements EntitySelector<Solution_> { |
| 35 | + |
| 36 | + private final IterableValueSelector<Solution_> replayingValueSelector; |
| 37 | + private final EntitySelector<Solution_> childEntitySelector; |
| 38 | + private final boolean randomSelection; |
| 39 | + |
| 40 | + private ReachableValues reachableValues; |
| 41 | + private long entitiesSize; |
| 42 | + |
| 43 | + public FilteringEntityValueRangeSelector(EntitySelector<Solution_> childEntitySelector, |
| 44 | + IterableValueSelector<Solution_> replayingValueSelector, boolean randomSelection) { |
| 45 | + this.replayingValueSelector = replayingValueSelector; |
| 46 | + this.childEntitySelector = childEntitySelector; |
| 47 | + this.randomSelection = randomSelection; |
| 48 | + } |
| 49 | + |
| 50 | + // ************************************************************************ |
| 51 | + // Lifecycle methods |
| 52 | + // ************************************************************************ |
| 53 | + |
| 54 | + @Override |
| 55 | + public void solvingStarted(SolverScope<Solution_> solverScope) { |
| 56 | + super.solvingStarted(solverScope); |
| 57 | + this.childEntitySelector.solvingStarted(solverScope); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void phaseStarted(AbstractPhaseScope<Solution_> phaseScope) { |
| 62 | + super.phaseStarted(phaseScope); |
| 63 | + this.entitiesSize = childEntitySelector.getEntityDescriptor().extractEntities(phaseScope.getWorkingSolution()).size(); |
| 64 | + this.reachableValues = phaseScope.getScoreDirector().getValueRangeManager() |
| 65 | + .getReachableValeMatrix(childEntitySelector.getEntityDescriptor().getGenuineListVariableDescriptor()); |
| 66 | + this.childEntitySelector.phaseStarted(phaseScope); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void phaseEnded(AbstractPhaseScope<Solution_> phaseScope) { |
| 71 | + super.phaseEnded(phaseScope); |
| 72 | + this.reachableValues = null; |
| 73 | + } |
| 74 | + |
| 75 | + // ************************************************************************ |
| 76 | + // Worker methods |
| 77 | + // ************************************************************************ |
| 78 | + |
| 79 | + public EntitySelector<Solution_> getChildEntitySelector() { |
| 80 | + return childEntitySelector; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public EntityDescriptor<Solution_> getEntityDescriptor() { |
| 85 | + return childEntitySelector.getEntityDescriptor(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public long getSize() { |
| 90 | + return entitiesSize; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean isCountable() { |
| 95 | + return childEntitySelector.isCountable(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public boolean isNeverEnding() { |
| 100 | + return childEntitySelector.isNeverEnding(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Iterator<Object> endingIterator() { |
| 105 | + return new OriginalFilteringValueRangeIterator(replayingValueSelector.iterator(), reachableValues); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Iterator<Object> iterator() { |
| 110 | + if (randomSelection) { |
| 111 | + return new EntityRandomFilteringValueRangeIterator(replayingValueSelector.iterator(), reachableValues, |
| 112 | + workingRandom); |
| 113 | + } else { |
| 114 | + return new OriginalFilteringValueRangeIterator(replayingValueSelector.iterator(), reachableValues); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public ListIterator<Object> listIterator() { |
| 120 | + throw new UnsupportedOperationException(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public ListIterator<Object> listIterator(int index) { |
| 125 | + throw new UnsupportedOperationException(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean equals(Object other) { |
| 130 | + return other instanceof FilteringEntityValueRangeSelector<?> that |
| 131 | + && Objects.equals(childEntitySelector, that.childEntitySelector) |
| 132 | + && Objects.equals(replayingValueSelector, that.replayingValueSelector); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public int hashCode() { |
| 137 | + return Objects.hash(childEntitySelector, replayingValueSelector); |
| 138 | + } |
| 139 | + |
| 140 | + private static class OriginalFilteringValueRangeIterator extends UpcomingSelectionIterator<Object> { |
| 141 | + |
| 142 | + private final Iterator<Object> valueIterator; |
| 143 | + private final ReachableValues reachableValues; |
| 144 | + private Iterator<Object> otherIterator; |
| 145 | + |
| 146 | + private OriginalFilteringValueRangeIterator(Iterator<Object> valueIterator, ReachableValues reachableValues) { |
| 147 | + this.valueIterator = valueIterator; |
| 148 | + this.reachableValues = Objects.requireNonNull(reachableValues); |
| 149 | + } |
| 150 | + |
| 151 | + private void initialize() { |
| 152 | + if (otherIterator != null) { |
| 153 | + return; |
| 154 | + } |
| 155 | + var allValues = reachableValues.extractEntitiesAsList(Objects.requireNonNull(valueIterator.next())); |
| 156 | + this.otherIterator = Objects.requireNonNull(allValues).iterator(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + protected Object createUpcomingSelection() { |
| 161 | + initialize(); |
| 162 | + if (!otherIterator.hasNext()) { |
| 163 | + return noUpcomingSelection(); |
| 164 | + } |
| 165 | + return otherIterator.next(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private static class EntityRandomFilteringValueRangeIterator extends UpcomingSelectionIterator<Object> { |
| 170 | + |
| 171 | + private final Iterator<Object> valueIterator; |
| 172 | + private final ReachableValues reachableValues; |
| 173 | + private final Random workingRandom; |
| 174 | + private Object currentUpcomingValue; |
| 175 | + private List<Object> entityList; |
| 176 | + |
| 177 | + private EntityRandomFilteringValueRangeIterator(Iterator<Object> valueIterator, |
| 178 | + ReachableValues reachableValues, Random workingRandom) { |
| 179 | + this.valueIterator = valueIterator; |
| 180 | + this.reachableValues = Objects.requireNonNull(reachableValues); |
| 181 | + this.workingRandom = workingRandom; |
| 182 | + } |
| 183 | + |
| 184 | + private void initialize() { |
| 185 | + if (entityList != null) { |
| 186 | + return; |
| 187 | + } |
| 188 | + this.currentUpcomingValue = Objects.requireNonNull(valueIterator.next()); |
| 189 | + loadValues(); |
| 190 | + } |
| 191 | + |
| 192 | + private void loadValues() { |
| 193 | + upcomingCreated = false; |
| 194 | + this.entityList = reachableValues.extractEntitiesAsList(currentUpcomingValue); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public boolean hasNext() { |
| 199 | + if (valueIterator.hasNext() && currentUpcomingValue != null) { |
| 200 | + var updatedUpcomingValue = valueIterator.next(); |
| 201 | + if (updatedUpcomingValue != currentUpcomingValue) { |
| 202 | + // The iterator is reused in the ElementPositionRandomIterator, |
| 203 | + // even if the value has changed. |
| 204 | + // Therefore, |
| 205 | + // we need to update the value list to ensure it is consistent. |
| 206 | + this.currentUpcomingValue = updatedUpcomingValue; |
| 207 | + loadValues(); |
| 208 | + } |
| 209 | + } |
| 210 | + return super.hasNext(); |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + protected Object createUpcomingSelection() { |
| 215 | + initialize(); |
| 216 | + if (entityList.isEmpty()) { |
| 217 | + return noUpcomingSelection(); |
| 218 | + } |
| 219 | + var index = workingRandom.nextInt(entityList.size()); |
| 220 | + return entityList.get(index); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | +} |
0 commit comments