Skip to content

Commit

Permalink
chore: allow for automatic node-sharing in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
triceo committed Mar 12, 2024
1 parent 8fca9c5 commit 3704433
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,6 @@ Constraint unwantedPatternShiftType3DaysPattern(ConstraintFactory constraintFact
.asConstraint("unwantedPatternShiftType3DaysPattern");
}

private record Pair<A, B>(A a, B b) {
public record Pair<A, B>(A a, B b) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <A> UniConstraintCollector<A, ?, LoadBalanceData> loadBalance(
Function<A, Object> groupKey) {
return new UniConstraintCollector<A, LoadBalanceData, LoadBalanceData>() {

@Override
public Supplier<LoadBalanceData> supplier() {
return LoadBalanceData::new;
}

@Override
public BiFunction<LoadBalanceData, A, Runnable> accumulator() {
return (resultContainer, a) -> {
Object mapped = groupKey.apply(a);
return resultContainer.apply(mapped);
};
}

@Override
public Function<LoadBalanceData, LoadBalanceData> finisher() {
return Function.identity();
}
};
public class TennisConstraintProvider implements ConstraintProvider {

private static <A> UniConstraintCollector<A, ?, LoadBalanceData> loadBalance(Function<A, Object> groupKey) {
return new LoadBalanceUniConstraintCollector<>(groupKey);
}

private static <A, B> BiConstraintCollector<A, B, ?, LoadBalanceData> loadBalance(
BiFunction<A, B, Object> groupKey) {
return new BiConstraintCollector<A, B, LoadBalanceData, LoadBalanceData>() {

@Override
public Supplier<LoadBalanceData> supplier() {
return LoadBalanceData::new;
}

@Override
public TriFunction<LoadBalanceData, A, B, Runnable> accumulator() {
return (resultContainer, a, b) -> {
Object mapped = groupKey.apply(a, b);
return resultContainer.apply(mapped);
};
}

@Override
public Function<LoadBalanceData, LoadBalanceData> finisher() {
return Function.identity();
}
};
private static <A, B> BiConstraintCollector<A, B, ?, LoadBalanceData> loadBalance(BiFunction<A, B, Object> groupKey) {
return new LoadBalanceBiConstraintCollector<>(groupKey);
}

@Override
Expand Down Expand Up @@ -118,7 +78,7 @@ Constraint evenlyConfrontationCount(ConstraintFactory constraintFactory) {
.asConstraint("evenlyConfrontationCount");
}

private static final class LoadBalanceData {
public static final class LoadBalanceData {

private final Map<Object, Long> groupCountMap = new LinkedHashMap<>(0);
// the sum of squared deviation from zero
Expand All @@ -143,8 +103,63 @@ public long getZeroDeviationSquaredSumRootMillis() {

}

private record Pair<A, B>(A key, B value) {
public record Pair<A, B>(A key, B value) {

}

public static class LoadBalanceUniConstraintCollector<A>
implements UniConstraintCollector<A, LoadBalanceData, LoadBalanceData> {

private final Function<A, Object> groupKey;

public LoadBalanceUniConstraintCollector(Function<A, Object> groupKey) {
this.groupKey = groupKey;
}

@Override
public Supplier<LoadBalanceData> supplier() {
return LoadBalanceData::new;
}

@Override
public BiFunction<LoadBalanceData, A, Runnable> accumulator() {
return (resultContainer, a) -> {
Object mapped = groupKey.apply(a);
return resultContainer.apply(mapped);
};
}

@Override
public Function<LoadBalanceData, LoadBalanceData> finisher() {
return Function.identity();
}
}

public static class LoadBalanceBiConstraintCollector<A, B>
implements BiConstraintCollector<A, B, LoadBalanceData, LoadBalanceData> {

private final BiFunction<A, B, Object> groupKey;

public LoadBalanceBiConstraintCollector(BiFunction<A, B, Object> groupKey) {
this.groupKey = groupKey;
}

@Override
public Supplier<LoadBalanceData> supplier() {
return LoadBalanceData::new;
}

@Override
public TriFunction<LoadBalanceData, A, B, Runnable> accumulator() {
return (resultContainer, a, b) -> {
Object mapped = groupKey.apply(a, b);
return resultContainer.apply(mapped);
};
}

@Override
public Function<LoadBalanceData, LoadBalanceData> finisher() {
return Function.identity();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3704433

Please sign in to comment.