Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] [PLANNER-2933] Fix the secondary super cache problem #2959

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.optaplanner.core.impl.heuristic.selector.common.iterator.UpcomingSelectionIterator;
import org.optaplanner.core.impl.heuristic.selector.entity.EntitySelector;
import org.optaplanner.core.impl.heuristic.selector.move.MoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.generic.ChangeMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.generic.SwapMoveSelector;

public class QueuedEntityPlacer<Solution_> extends AbstractEntityPlacer<Solution_> implements EntityPlacer<Solution_> {

Expand Down Expand Up @@ -71,8 +73,17 @@ protected Placement<Solution_> createUpcomingSelection() {
entityIterator.next();
moveSelectorIterator = moveSelectorList.iterator();
}
MoveSelector<Solution_> moveSelector = moveSelectorIterator.next();
moveIterator = moveSelector.iterator();

// See https://issues.redhat.com/browse/PLANNER-2933
Object moveSelector = moveSelectorIterator.next();

if (moveSelector instanceof SwapMoveSelector) {
moveIterator = ((SwapMoveSelector) moveSelector).iterator();
} else if (moveSelector instanceof ChangeMoveSelector) {
moveIterator = ((ChangeMoveSelector) moveSelector).iterator();
} else if (moveSelector instanceof MoveSelector) {
moveIterator = ((MoveSelector) moveSelector).iterator();
}
}
return new Placement<>(moveIterator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private <T> List<T> transformCollectionToList(Collection<T> collection) {
// - If only ValueRange.createOriginalIterator() is used, cloning a Set to a List is a waste of time.
// - If the List is a LinkedList, ValueRange.createRandomIterator(Random)
// and ValueRange.get(int) are not efficient.
return (collection instanceof List ? (List<T>) collection : new ArrayList<>(collection));
return new ArrayList<>(collection);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ Collection<EntityNotifiable<Solution_>> get(EntityDescriptor<?> entityDescriptor
return sourceEntityToNotifiableMap.get(entityDescriptor);
}

Collection<VariableListenerNotifiable<Solution_>> get(VariableDescriptor<?> variableDescriptor) {
return sourceVariableToNotifiableMap.getOrDefault(variableDescriptor,
Collections.emptyList()); // Avoids null for chained swap move on an unchained var.
ArrayList<VariableListenerNotifiable<Solution_>> get(VariableDescriptor<?> variableDescriptor) {
return (ArrayList<VariableListenerNotifiable<Solution_>>) sourceVariableToNotifiableMap.getOrDefault(
variableDescriptor,
new ArrayList<>()); // Avoids null for chained swap move on an unchained var.
}

Collection<ListVariableListenerNotifiable<Solution_>> get(ListVariableDescriptor<?> variableDescriptor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,40 @@

package org.optaplanner.core.impl.heuristic.selector.move.composite;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import org.optaplanner.core.impl.heuristic.move.Move;
import org.optaplanner.core.impl.heuristic.selector.common.iterator.SelectionIterator;
import org.optaplanner.core.impl.heuristic.selector.move.MoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.generic.ChangeMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.generic.SwapMoveSelector;

final class UniformRandomUnionMoveIterator<Solution_> extends SelectionIterator<Move<Solution_>> {

private final List<Iterator<Move<Solution_>>> moveIteratorList;
private final Random workingRandom;

public UniformRandomUnionMoveIterator(List<MoveSelector<Solution_>> childMoveSelectorList, Random workingRandom) {
this.moveIteratorList = childMoveSelectorList.stream()
.map(Iterable::iterator)
.filter(Iterator::hasNext)
.collect(Collectors.toList());
ArrayList<Iterator<Move<Solution_>>> list = new ArrayList<>();

// See https://issues.redhat.com/browse/PLANNER-2933
Iterator iterator;
for (Object s : childMoveSelectorList) {
if (s instanceof SwapMoveSelector) {
iterator = ((SwapMoveSelector) s).iterator();
} else if (s instanceof ChangeMoveSelector) {
iterator = ((ChangeMoveSelector) s).iterator();
} else {
iterator = ((MoveSelector) s).iterator();
}
if (iterator.hasNext()) {
list.add(iterator);
}
}
this.moveIteratorList = list;
this.workingRandom = workingRandom;
}

Expand All @@ -56,5 +71,4 @@ public Move<Solution_> next() {
}
return next;
}

}