From 3704433489902acecc52f35726896fe22e82fe5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Petrovick=C3=BD?= Date: Mon, 11 Mar 2024 20:52:12 +0100 Subject: [PATCH] chore: allow for automatic node-sharing in examples --- ...onferenceSchedulingConstraintProvider.java | 2 +- .../NurseRosteringConstraintProvider.java | 2 +- .../TaskAssigningConstraintProvider.java | 2 +- .../score/TennisConstraintProvider.java | 111 ++++++++++-------- ...TravelingTournamentConstraintProvider.java | 2 +- .../tsp/score/TspConstraintProvider.java | 2 +- 6 files changed, 68 insertions(+), 53 deletions(-) diff --git a/examples/src/main/java/ai/timefold/solver/examples/conferencescheduling/score/ConferenceSchedulingConstraintProvider.java b/examples/src/main/java/ai/timefold/solver/examples/conferencescheduling/score/ConferenceSchedulingConstraintProvider.java index 8f354e9d0b..9dfa69af50 100644 --- a/examples/src/main/java/ai/timefold/solver/examples/conferencescheduling/score/ConferenceSchedulingConstraintProvider.java +++ b/examples/src/main/java/ai/timefold/solver/examples/conferencescheduling/score/ConferenceSchedulingConstraintProvider.java @@ -69,7 +69,7 @@ * In this case, it is better to reduce the size of the joins even at the expense of duplicating some calculations. * In other words, time saved by caching those calculations is far outweighed by the time spent in unrestricted joins. */ -public final class ConferenceSchedulingConstraintProvider implements ConstraintProvider { +public class ConferenceSchedulingConstraintProvider implements ConstraintProvider { @Override public Constraint[] defineConstraints(ConstraintFactory factory) { diff --git a/examples/src/main/java/ai/timefold/solver/examples/nurserostering/score/NurseRosteringConstraintProvider.java b/examples/src/main/java/ai/timefold/solver/examples/nurserostering/score/NurseRosteringConstraintProvider.java index 5291324de8..89a7f061f5 100644 --- a/examples/src/main/java/ai/timefold/solver/examples/nurserostering/score/NurseRosteringConstraintProvider.java +++ b/examples/src/main/java/ai/timefold/solver/examples/nurserostering/score/NurseRosteringConstraintProvider.java @@ -417,6 +417,6 @@ Constraint unwantedPatternShiftType3DaysPattern(ConstraintFactory constraintFact .asConstraint("unwantedPatternShiftType3DaysPattern"); } - private record Pair(A a, B b) { + public record Pair(A a, B b) { } } diff --git a/examples/src/main/java/ai/timefold/solver/examples/taskassigning/score/TaskAssigningConstraintProvider.java b/examples/src/main/java/ai/timefold/solver/examples/taskassigning/score/TaskAssigningConstraintProvider.java index 1ef57117f0..c0f5913819 100644 --- a/examples/src/main/java/ai/timefold/solver/examples/taskassigning/score/TaskAssigningConstraintProvider.java +++ b/examples/src/main/java/ai/timefold/solver/examples/taskassigning/score/TaskAssigningConstraintProvider.java @@ -9,7 +9,7 @@ import ai.timefold.solver.examples.taskassigning.domain.Priority; import ai.timefold.solver.examples.taskassigning.domain.Task; -public final class TaskAssigningConstraintProvider implements ConstraintProvider { +public class TaskAssigningConstraintProvider implements ConstraintProvider { private static final int BENDABLE_SCORE_HARD_LEVELS_SIZE = 1; private static final int BENDABLE_SCORE_SOFT_LEVELS_SIZE = 5; diff --git a/examples/src/main/java/ai/timefold/solver/examples/tennis/score/TennisConstraintProvider.java b/examples/src/main/java/ai/timefold/solver/examples/tennis/score/TennisConstraintProvider.java index 4f58233fb6..4ead3ed808 100644 --- a/examples/src/main/java/ai/timefold/solver/examples/tennis/score/TennisConstraintProvider.java +++ b/examples/src/main/java/ai/timefold/solver/examples/tennis/score/TennisConstraintProvider.java @@ -19,54 +19,14 @@ import ai.timefold.solver.examples.tennis.domain.TeamAssignment; import ai.timefold.solver.examples.tennis.domain.UnavailabilityPenalty; -public final class TennisConstraintProvider implements ConstraintProvider { - - private static UniConstraintCollector loadBalance( - Function groupKey) { - return new UniConstraintCollector() { - - @Override - public Supplier supplier() { - return LoadBalanceData::new; - } - - @Override - public BiFunction accumulator() { - return (resultContainer, a) -> { - Object mapped = groupKey.apply(a); - return resultContainer.apply(mapped); - }; - } - - @Override - public Function finisher() { - return Function.identity(); - } - }; +public class TennisConstraintProvider implements ConstraintProvider { + + private static UniConstraintCollector loadBalance(Function groupKey) { + return new LoadBalanceUniConstraintCollector<>(groupKey); } - private static BiConstraintCollector loadBalance( - BiFunction groupKey) { - return new BiConstraintCollector() { - - @Override - public Supplier supplier() { - return LoadBalanceData::new; - } - - @Override - public TriFunction accumulator() { - return (resultContainer, a, b) -> { - Object mapped = groupKey.apply(a, b); - return resultContainer.apply(mapped); - }; - } - - @Override - public Function finisher() { - return Function.identity(); - } - }; + private static BiConstraintCollector loadBalance(BiFunction groupKey) { + return new LoadBalanceBiConstraintCollector<>(groupKey); } @Override @@ -118,7 +78,7 @@ Constraint evenlyConfrontationCount(ConstraintFactory constraintFactory) { .asConstraint("evenlyConfrontationCount"); } - private static final class LoadBalanceData { + public static final class LoadBalanceData { private final Map groupCountMap = new LinkedHashMap<>(0); // the sum of squared deviation from zero @@ -143,8 +103,63 @@ public long getZeroDeviationSquaredSumRootMillis() { } - private record Pair(A key, B value) { + public record Pair(A key, B value) { + + } + + public static class LoadBalanceUniConstraintCollector + implements UniConstraintCollector { + + private final Function groupKey; + + public LoadBalanceUniConstraintCollector(Function groupKey) { + this.groupKey = groupKey; + } + + @Override + public Supplier supplier() { + return LoadBalanceData::new; + } + + @Override + public BiFunction accumulator() { + return (resultContainer, a) -> { + Object mapped = groupKey.apply(a); + return resultContainer.apply(mapped); + }; + } + @Override + public Function finisher() { + return Function.identity(); + } } + public static class LoadBalanceBiConstraintCollector + implements BiConstraintCollector { + + private final BiFunction groupKey; + + public LoadBalanceBiConstraintCollector(BiFunction groupKey) { + this.groupKey = groupKey; + } + + @Override + public Supplier supplier() { + return LoadBalanceData::new; + } + + @Override + public TriFunction accumulator() { + return (resultContainer, a, b) -> { + Object mapped = groupKey.apply(a, b); + return resultContainer.apply(mapped); + }; + } + + @Override + public Function finisher() { + return Function.identity(); + } + } } diff --git a/examples/src/main/java/ai/timefold/solver/examples/travelingtournament/score/TravelingTournamentConstraintProvider.java b/examples/src/main/java/ai/timefold/solver/examples/travelingtournament/score/TravelingTournamentConstraintProvider.java index a96372fb81..75281615fa 100644 --- a/examples/src/main/java/ai/timefold/solver/examples/travelingtournament/score/TravelingTournamentConstraintProvider.java +++ b/examples/src/main/java/ai/timefold/solver/examples/travelingtournament/score/TravelingTournamentConstraintProvider.java @@ -9,7 +9,7 @@ import ai.timefold.solver.examples.travelingtournament.domain.Day; import ai.timefold.solver.examples.travelingtournament.domain.Match; -public final class TravelingTournamentConstraintProvider implements ConstraintProvider { +public class TravelingTournamentConstraintProvider implements ConstraintProvider { @Override public Constraint[] defineConstraints(ConstraintFactory constraintFactory) { diff --git a/examples/src/main/java/ai/timefold/solver/examples/tsp/score/TspConstraintProvider.java b/examples/src/main/java/ai/timefold/solver/examples/tsp/score/TspConstraintProvider.java index 1fbb46bfd4..2761e98d9c 100644 --- a/examples/src/main/java/ai/timefold/solver/examples/tsp/score/TspConstraintProvider.java +++ b/examples/src/main/java/ai/timefold/solver/examples/tsp/score/TspConstraintProvider.java @@ -8,7 +8,7 @@ import ai.timefold.solver.examples.tsp.domain.Domicile; import ai.timefold.solver.examples.tsp.domain.Visit; -public final class TspConstraintProvider implements ConstraintProvider { +public class TspConstraintProvider implements ConstraintProvider { @Override public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {