-
Notifications
You must be signed in to change notification settings - Fork 207
feat(neighborhoods): add groupBy() support #2334
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1753a76
feat(neighborhoods): add groupBy() support
triceo a987e6d
Update docs/src/modules/ROOT/pages/optimization-algorithms/neighborho…
triceo 48a4329
review
triceo 558a60e
introduce collector composing
triceo 12fb5c5
introduce collectAndThen
triceo 59df6af
copilot
triceo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...fold/solver/core/impl/neighborhood/stream/enumerating/bi/BiGroupUniEnumeratingStream.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package ai.timefold.solver.core.impl.neighborhood.stream.enumerating.bi; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import ai.timefold.solver.core.impl.bavet.common.tuple.UniTuple; | ||
| import ai.timefold.solver.core.impl.neighborhood.stream.enumerating.EnumeratingStreamFactory; | ||
| import ai.timefold.solver.core.impl.neighborhood.stream.enumerating.common.DataNodeBuildHelper; | ||
| import ai.timefold.solver.core.impl.neighborhood.stream.enumerating.common.NeighborhoodsGroupNodeConstructor; | ||
| import ai.timefold.solver.core.impl.neighborhood.stream.enumerating.common.bridge.AftBridgeUniEnumeratingStream; | ||
|
|
||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| @NullMarked | ||
| final class BiGroupUniEnumeratingStream<Solution_, A, B, NewA> | ||
| extends AbstractBiEnumeratingStream<Solution_, A, B> { | ||
|
|
||
| private final NeighborhoodsGroupNodeConstructor<Solution_, UniTuple<NewA>> nodeConstructor; | ||
| private @Nullable AftBridgeUniEnumeratingStream<Solution_, NewA> aftStream; | ||
|
|
||
| BiGroupUniEnumeratingStream(EnumeratingStreamFactory<Solution_> enumeratingStreamFactory, | ||
| AbstractBiEnumeratingStream<Solution_, A, B> parent, | ||
| NeighborhoodsGroupNodeConstructor<Solution_, UniTuple<NewA>> nodeConstructor) { | ||
| super(enumeratingStreamFactory, parent); | ||
| this.nodeConstructor = Objects.requireNonNull(nodeConstructor); | ||
| } | ||
|
|
||
| void setAftBridge(AftBridgeUniEnumeratingStream<Solution_, NewA> aftStream) { | ||
| this.aftStream = aftStream; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean guaranteesDistinct() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void buildNode(DataNodeBuildHelper<Solution_> buildHelper) { | ||
| var view = buildHelper.getSessionContext().solutionView(); | ||
| nodeConstructor.build(buildHelper, parent.getTupleSource(), aftStream, | ||
| aftStream.getChildStreamList(), this, enumeratingStreamFactory.getEnvironmentMode(), view); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object object) { | ||
| if (this == object) | ||
| return true; | ||
| if (object == null || getClass() != object.getClass()) | ||
| return false; | ||
| var that = (BiGroupUniEnumeratingStream<?, ?, ?, ?>) object; | ||
| return Objects.equals(parent, that.parent) && Objects.equals(nodeConstructor, that.nodeConstructor); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(parent, nodeConstructor); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "BiGroupUni()"; | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
.../core/impl/neighborhood/stream/enumerating/collector/AndThenBiNeighborhoodsCollector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package ai.timefold.solver.core.impl.neighborhood.stream.enumerating.collector; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.function.Function; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import ai.timefold.solver.core.preview.api.neighborhood.stream.enumerating.collector.BiNeighborhoodsCollector; | ||
| import ai.timefold.solver.core.preview.api.neighborhood.stream.enumerating.collector.BiNeighborhoodsCollectorAccumulator; | ||
|
|
||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| @NullMarked | ||
| public final class AndThenBiNeighborhoodsCollector<Solution_, A, B, ResultContainer_, Intermediate_, Result_> | ||
| implements BiNeighborhoodsCollector<Solution_, A, B, ResultContainer_, Result_> { | ||
|
|
||
| private final BiNeighborhoodsCollector<Solution_, A, B, ResultContainer_, Intermediate_> delegate; | ||
| private final Function<@Nullable Intermediate_, @Nullable Result_> mappingFunction; | ||
|
|
||
| public AndThenBiNeighborhoodsCollector( | ||
| BiNeighborhoodsCollector<Solution_, A, B, ResultContainer_, Intermediate_> delegate, | ||
| Function<Intermediate_, Result_> mappingFunction) { | ||
| this.delegate = Objects.requireNonNull(delegate); | ||
| this.mappingFunction = Objects.requireNonNull(mappingFunction); | ||
| } | ||
|
|
||
| @Override | ||
| public Supplier<ResultContainer_> supplier() { | ||
| return delegate.supplier(); | ||
| } | ||
|
|
||
| @Override | ||
| public BiNeighborhoodsCollectorAccumulator<Solution_, A, B, ResultContainer_> accumulator() { | ||
| return delegate.accumulator(); | ||
| } | ||
|
|
||
| @Override | ||
| public Function<ResultContainer_, @Nullable Result_> finisher() { | ||
| var finisher = delegate.finisher(); | ||
| return container -> mappingFunction.apply(finisher.apply(container)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| return o instanceof AndThenBiNeighborhoodsCollector<?, ?, ?, ?, ?, ?> other | ||
| && Objects.equals(delegate, other.delegate) | ||
| && Objects.equals(mappingFunction, other.mappingFunction); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(delegate, mappingFunction); | ||
| } | ||
|
|
||
| } |
58 changes: 58 additions & 0 deletions
58
...core/impl/neighborhood/stream/enumerating/collector/AndThenUniNeighborhoodsCollector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package ai.timefold.solver.core.impl.neighborhood.stream.enumerating.collector; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.function.Function; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import ai.timefold.solver.core.preview.api.neighborhood.stream.enumerating.collector.UniNeighborhoodsCollector; | ||
| import ai.timefold.solver.core.preview.api.neighborhood.stream.enumerating.collector.UniNeighborhoodsCollectorAccumulator; | ||
|
|
||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| @NullMarked | ||
| public final class AndThenUniNeighborhoodsCollector<Solution_, A, ResultContainer_, Intermediate_, Result_> | ||
| implements UniNeighborhoodsCollector<Solution_, A, ResultContainer_, Result_> { | ||
|
|
||
| private final UniNeighborhoodsCollector<Solution_, A, ResultContainer_, Intermediate_> delegate; | ||
| private final Function<@Nullable Intermediate_, @Nullable Result_> mappingFunction; | ||
|
|
||
| public AndThenUniNeighborhoodsCollector( | ||
| UniNeighborhoodsCollector<Solution_, A, ResultContainer_, Intermediate_> delegate, | ||
| Function<Intermediate_, Result_> mappingFunction) { | ||
| this.delegate = Objects.requireNonNull(delegate); | ||
| this.mappingFunction = Objects.requireNonNull(mappingFunction); | ||
| } | ||
|
|
||
| @Override | ||
| public Supplier<ResultContainer_> supplier() { | ||
| return delegate.supplier(); | ||
| } | ||
|
|
||
| @Override | ||
| public UniNeighborhoodsCollectorAccumulator<Solution_, A, ResultContainer_> accumulator() { | ||
| return delegate.accumulator(); | ||
| } | ||
|
|
||
| @Override | ||
| public Function<ResultContainer_, @Nullable Result_> finisher() { | ||
| var finisher = delegate.finisher(); | ||
| return container -> mappingFunction.apply(finisher.apply(container)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| return o instanceof AndThenUniNeighborhoodsCollector<?, ?, ?, ?, ?> other | ||
| && Objects.equals(delegate, other.delegate) | ||
| && Objects.equals(mappingFunction, other.mappingFunction); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(delegate, mappingFunction); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.