org.optaplanner.examples.travelingtournament.persistence.TravelingTournamentSolutionFileIO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of optaplanner-examples Show documentation
Show all versions of optaplanner-examples Show documentation
OptaPlanner solves planning problems.
This lightweight, embeddable planning engine implements powerful and scalable algorithms
to optimize business resource scheduling and planning.
This module contains the examples which demonstrate how to use it in a normal Java application.
package org.optaplanner.examples.travelingtournament.persistence;
import java.io.File;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.optaplanner.examples.common.persistence.AbstractJsonSolutionFileIO;
import org.optaplanner.examples.travelingtournament.domain.Team;
import org.optaplanner.examples.travelingtournament.domain.TravelingTournament;
public class TravelingTournamentSolutionFileIO extends AbstractJsonSolutionFileIO {
public TravelingTournamentSolutionFileIO() {
super(TravelingTournament.class);
}
@Override
public TravelingTournament read(File inputSolutionFile) {
TravelingTournament travelingTournament = super.read(inputSolutionFile);
var teamsById = travelingTournament.getTeamList().stream()
.collect(Collectors.toMap(Team::getId, Function.identity()));
/*
* Replace the duplicate team instances in the distanceToTeamMap by references to instances from
* the teamList.
*/
for (Team team : travelingTournament.getTeamList()) {
var newTravelDistanceMap = deduplicateMap(team.getDistanceToTeamMap(),
teamsById, Team::getId);
team.setDistanceToTeamMap(newTravelDistanceMap);
}
return travelingTournament;
}
}