Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add 10-fold run tables

"fix" swap matches by returning only valid solutions
  • Loading branch information...
commit a9aeda41d2ecddd1cf3238dd43f3de3bcf79ecda 1 parent 5ad42a5
@mikazuki mikazuki authored
View
BIN  10-fold-5-min-runs.numbers
Binary file not shown
View
BIN  10-fold-5-min-runs.pdf
Binary file not shown
View
BIN  10-fold-5-min-runs.xls
Binary file not shown
View
9 TTP/src/ttp/localsearch/neighborhood/impl/SwapMatchRoundNeighborhood.java
@@ -46,7 +46,9 @@ public TTPSolution getNext() {
&& baseSolution.getSchedule()[roundIndex1][teamIndex] == -baseSolution
.getSchedule()[roundIndex2][teamIndex]);
- return next;
+ return next;
+ /*if(next.isLegal()) return next;
+ else return null;*/
} else {
return null;
}
@@ -77,6 +79,7 @@ private void swapMatchRound(int[][] schedule, int[][] baseSchedule,
int baseRound = round2;
int targetRount = round1;
+ //Set<Integer> alreadyRepaired = new HashSet<Integer>();
while (repairChainEntry != (team + 1)) {
// search for game of repairChainEntry in round2
@@ -94,7 +97,11 @@ private void swapMatchRound(int[][] schedule, int[][] baseSchedule,
schedule[targetRount][-opponent - 1] = repairChainEntry;
}
+ //alreadyRepaired.add(repairChainEntry);
repairChainEntry = Math.abs(opponent);
+
+ //if(alreadyRepaired.contains(repairChainEntry)) break;
+
if (targetRount == round1) {
baseRound = round1;
targetRount = round2;
View
241 TTP/src/ttp/localsearch/neighborhood/impl/SwapMatchesNeighborhood.java
@@ -10,130 +10,119 @@
import java.util.Set;
public class SwapMatchesNeighborhood extends TTPNeighborhoodBase {
-
- int round = 0;
- int team1 = 0;
- int team2 = 1;
-
- @Override
- public INeighborhood<TTPSolution> clone() throws CloneNotSupportedException {
- return super.clone();
- }
-
- @Override
- public TTPSolution getNext() {
- if (!hasNext())
- return null;
-
- TTPSolution nextSolution = new TTPSolution(baseSolution);
- int[][] schedule = nextSolution.getSchedule();
-
- // find next possible move, might return null if no more moves are
- // possible
- while (true) {
- if (!hasNext())
- return null;
-
- boolean swapped = swapMatches(schedule, round, team1, team2, true);
-
- if (!swapped)
- moveNext();
- else
- break;
- }
-
- Set<Integer> roundsRepaired = new HashSet<Integer>();
- Queue<Integer> repairNeeded = new LinkedList<Integer>();
- repairNeeded.offer(round);
-
- while (!repairNeeded.isEmpty()) {
- int roundToRepair = repairNeeded.remove();
-
- boolean team1Repaired = false;
- boolean team2Repaired = false;
-
- for (int repairRound = 0; repairRound < noRounds
- && !(team1Repaired && team2Repaired); repairRound++) {
- if (roundToRepair == repairRound
- || roundsRepaired.contains(repairRound))
- continue;
-
- if (schedule[repairRound][team1] == schedule[roundToRepair][team1]) {
- swapMatches(schedule, repairRound, team1, team2, false);
- if (!repairNeeded.contains(repairRound))
- repairNeeded.offer(repairRound);
- roundsRepaired.add(repairRound);
- team1Repaired = true;
- } else if (schedule[repairRound][team2] == schedule[roundToRepair][team2]) {
- swapMatches(schedule, repairRound, team1, team2, false);
- if (!repairNeeded.contains(repairRound))
- repairNeeded.offer(repairRound);
- roundsRepaired.add(repairRound);
- team2Repaired = true;
- }
- }
- }
-
- TtpSolutionHelper.updateAll(nextSolution);
-
- moveNext();
-
- if (nextSolution.isLegal())
- return nextSolution;
- else
- return null;
- }
-
- private boolean swapMatches(int[][] schedule, int round, int team1,
- int team2, boolean abortIfSame) {
- int opponent1 = Math.abs(schedule[round][team1]);
-
- if (abortIfSame && opponent1 - 1 == team2)
- return false;
-
- int opponent2 = Math.abs(schedule[round][team2]);
- boolean team1Home = schedule[round][team1] > 0;
- boolean team2Home = schedule[round][team2] > 0;
-
- schedule[round][team1] = team1Home ? opponent2 : -opponent2;
- schedule[round][team2] = team2Home ? opponent1 : -opponent1;
- opponent1--;
- opponent2--;
- team1++;
- team2++;
- schedule[round][opponent1] = team2Home ? -team2 : team2;
- schedule[round][opponent2] = team1Home ? -team1 : team1;
-
- return true;
- }
-
- private void moveNext() {
- team2++;
-
- if (team2 == noTeams) {
- team1++;
- team2 = team1 + 1;
- }
-
- if (team1 == noTeams - 1) {
- round++;
- team1 = 0;
- team2 = 1;
- }
- }
-
- @Override
- public boolean hasNext() {
- return !(round >= noRounds - 1 && team1 >= noTeams - 2 && team2 >= noTeams - 1);
- }
-
- @Override
- public void init(TTPSolution solution) {
- super.init(solution);
-
- round = 0;
- team1 = 0;
- team2 = 1;
- }
-
+ int round = 0;
+ int team1 = 0;
+ int team2 = 1;
+
+ @Override
+ public INeighborhood<TTPSolution> clone() throws CloneNotSupportedException {
+ return super.clone();
+ }
+
+ @Override
+ public TTPSolution getNext() {
+ if (!hasNext()) return null;
+
+ TTPSolution nextSolution = new TTPSolution(baseSolution);
+ int[][] schedule = nextSolution.getSchedule();
+
+ // find next possible move, might return null if no more moves are possible
+ while (true) {
+ if (!hasNext()) return null;
+
+ boolean swapped = swapMatches(schedule, round, team1, team2, true);
+
+ if (!swapped)
+ moveNext();
+ else
+ break;
+ }
+
+ Set<Integer> roundsRepaired = new HashSet<Integer>();
+ Queue<Integer> repairNeeded = new LinkedList<Integer>();
+ repairNeeded.offer(round);
+
+ while (!repairNeeded.isEmpty()) {
+ int roundToRepair = repairNeeded.remove();
+
+ boolean team1Repaired = false;
+ boolean team2Repaired = false;
+
+ for (int repairRound = 0; repairRound < noRounds && !(team1Repaired && team2Repaired); repairRound++) {
+ if (roundToRepair == repairRound || roundsRepaired.contains(repairRound)) continue;
+
+ if (schedule[repairRound][team1] == schedule[roundToRepair][team1]) {
+ swapMatches(schedule, repairRound, team1, team2, false);
+ if (!repairNeeded.contains(repairRound))
+ repairNeeded.offer(repairRound);
+ roundsRepaired.add(repairRound);
+ team1Repaired = true;
+ } else if (schedule[repairRound][team2] == schedule[roundToRepair][team2]) {
+ swapMatches(schedule, repairRound, team1, team2, false);
+ if (!repairNeeded.contains(repairRound))
+ repairNeeded.offer(repairRound);
+ roundsRepaired.add(repairRound);
+ team2Repaired = true;
+ }
+ }
+ }
+
+ TtpSolutionHelper.updateAll(nextSolution);
+
+ moveNext();
+
+ if (TtpSolutionHelper.checkSolution(nextSolution))
+ return nextSolution;
+ else return null;
+ }
+
+ private boolean swapMatches(int[][] schedule, int round, int team1, int team2, boolean abortIfSame) {
+ int opponent1 = Math.abs(schedule[round][team1]);
+
+ if (abortIfSame && opponent1 - 1 == team2) return false;
+
+ int opponent2 = Math.abs(schedule[round][team2]);
+ boolean team1Home = schedule[round][team1] > 0;
+ boolean team2Home = schedule[round][team2] > 0;
+
+ schedule[round][team1] = team1Home ? opponent2 : -opponent2;
+ schedule[round][team2] = team2Home ? opponent1 : -opponent1;
+ opponent1--;
+ opponent2--;
+ team1++;
+ team2++;
+ schedule[round][opponent1] = team2Home ? -team2 : team2;
+ schedule[round][opponent2] = team1Home ? -team1 : team1;
+
+ return true;
+ }
+
+ private void moveNext() {
+ team2++;
+
+ if (team2 == noTeams) {
+ team1++;
+ team2 = team1 + 1;
+ }
+
+ if (team1 == noTeams - 1) {
+ round++;
+ team1 = 0;
+ team2 = 1;
+ }
+ }
+
+ @Override
+ public boolean hasNext() {
+ return !(round >= noRounds - 1 && team1 >= noTeams - 2 && team2 >= noTeams - 1);
+ }
+
+ @Override
+ public void init(TTPSolution solution) {
+ super.init(solution);
+
+ round = 0;
+ team1 = 0;
+ team2 = 1;
+ }
}
View
273 TTP/src/ttp/main/TenFoldFiveMinuteRun.java
@@ -10,149 +10,132 @@
import java.util.List;
public class TenFoldFiveMinuteRun {
-
- @Argument(required = true, index = 0, usage = "Problem instance directory (named files named dataXX.txt)")
- private File inputDirectory;
-
- @Argument(required = true, index = 1, usage = "Directory to write statistics to.")
- private File outputDirectory = null;
-
- public void run() throws Exception {
- List<TravelingTournamentProblem.Neighborhood> neighborhoods = new ArrayList<TravelingTournamentProblem.Neighborhood>();
- neighborhoods
- .add(TravelingTournamentProblem.Neighborhood.TWO_OPT_SWAP_ROUNDS);
- neighborhoods
- .add(TravelingTournamentProblem.Neighborhood.TWO_OPT_SWAP_TEAMS);
- neighborhoods
- .add(TravelingTournamentProblem.Neighborhood.SWAP_HOME_VISITOR);
- neighborhoods.add(TravelingTournamentProblem.Neighborhood.SHIFT_ROUND);
- neighborhoods
- .add(TravelingTournamentProblem.Neighborhood.SWAP_MATCH_ROUND);
- neighborhoods.add(TravelingTournamentProblem.Neighborhood.SWAP_MATCHES);
-
- for (int i = 4; i <= 12; i += 2) {
- String fileToRun = "data" + i + ".txt";
- File instanceFile = new File(inputDirectory, fileToRun);
- File instanceOut = new File(outputDirectory, fileToRun
- + "_statistics");
-
- File sumFile = new File(instanceOut, instanceFile.getName()
- + "_tabulistlen_sum.csv");
- File avgFile = new File(instanceOut, instanceFile.getName()
- + "_tabulistlen_avg.csv");
- boolean headerWritten = false;
-
- for (int j = 0; j < 10; j++) {
- File subOutDir = new File(instanceOut, fileToRun
- + "_statistics_" + (j + 1));
-
- TravelingTournamentProblem travelingTournamentProblem = new TravelingTournamentProblem();
- TTPParameters parameters = new TTPParameters(
- TravelingTournamentProblem.Method.GRASP,
- neighborhoods,
- TravelingTournamentProblem.ConstructionHeuristic.GRASP,
-
- VirtualScheduleConstructionMethod.FIRSTPOLYGONTHENGREEK,
- 70, 40, 10000, 40, instanceFile, subOutDir,
- 5L * 60L * 1000L, 0.4);
- TTPResult result = travelingTournamentProblem.run(parameters);
-
- if (!headerWritten) {
- writeResultHeader(sumFile, "run", result);
- writeResultHeader(avgFile, "run", result);
- headerWritten = true;
- }
-
- writeResultSum(sumFile, "" + (j + 1), result);
- writeResultAvg(avgFile, "" + (j + 1), result);
- }
- }
- }
-
- public static void main(String[] args) throws Exception {
- TenFoldFiveMinuteRun tenFoldFiveMinuteRun = new TenFoldFiveMinuteRun();
- CmdLineParser parser = new CmdLineParser(tenFoldFiveMinuteRun);
- parser.setUsageWidth(80); // width of the error display area
-
- try {
- parser.parseArgument(args);
- } catch (CmdLineException e) {
- System.err.println(e.getMessage());
- System.err
- .println("java ttp.main.TravelingTournamentProblem [options...] arguments...");
- // print the list of available options
- parser.printUsage(System.err);
- System.err.println();
- System.exit(1);
- }
-
- tenFoldFiveMinuteRun.run();
- }
-
- private static void writeResultHeader(File outputFile, String prefix,
- TTPResult result) throws IOException {
- if (!outputFile.exists()) {
- if (!outputFile.createNewFile()) {
- System.err.println("failed to create result file "
- + outputFile.getCanonicalPath());
- return;
- }
- }
-
- PrintWriter writer = new PrintWriter(outputFile, "utf-8");
-
- writer.print(prefix);
- writer.print(',');
-
- if (result.getLocalSearchStatistics() != null) {
- result.getLocalSearchStatistics().writeInformationHeader(writer);
- } else if (result.getSearchStatistics() != null) {
- if (result.getSearchStatistics().getSolution() == null)
- writer.println();
- else
- result.getSearchStatistics().writeStatisticsHeader(writer);
- }
-
- writer.close();
- }
-
- private static void writeResultSum(File outputFile, String prefix,
- TTPResult result) throws FileNotFoundException {
- if (!outputFile.exists())
- return;
-
- FileOutputStream fso = new FileOutputStream(outputFile, true);
- PrintWriter writer = new PrintWriter(fso);
-
- writer.print(prefix);
- writer.print(',');
-
- if (result.getLocalSearchStatistics() != null) {
- result.getLocalSearchStatistics().writeInformation(writer);
- } else if (result.getSearchStatistics() != null) {
- result.getSearchStatistics().writeStatisticSum(writer);
- }
-
- writer.close();
- }
-
- private static void writeResultAvg(File outputFile, String prefix,
- TTPResult result) throws FileNotFoundException {
- if (!outputFile.exists())
- return;
-
- FileOutputStream fso = new FileOutputStream(outputFile, true);
- PrintWriter writer = new PrintWriter(fso);
-
- writer.print(prefix);
- writer.print(',');
-
- if (result.getLocalSearchStatistics() != null) {
- result.getLocalSearchStatistics().writeInformation(writer);
- } else if (result.getSearchStatistics() != null) {
- result.getSearchStatistics().writeStatisticAverage(writer);
- }
-
- writer.close();
- }
+ @Argument(required = true, index = 0, usage = "Problem instance directory (named files named dataXX.txt)")
+ private File inputDirectory;
+
+ @Argument(required = true, index = 1,
+ usage = "Directory to write statistics to.")
+ private File outputDirectory = null;
+
+ public void run() throws Exception {
+ List<TravelingTournamentProblem.Neighborhood> neighborhoods =
+ new ArrayList<TravelingTournamentProblem.Neighborhood>();
+ neighborhoods.add(TravelingTournamentProblem.Neighborhood.TWO_OPT_SWAP_ROUNDS);
+ neighborhoods.add(TravelingTournamentProblem.Neighborhood.TWO_OPT_SWAP_TEAMS);
+ neighborhoods.add(TravelingTournamentProblem.Neighborhood.SWAP_HOME_VISITOR);
+ neighborhoods.add(TravelingTournamentProblem.Neighborhood.SHIFT_ROUND);
+ neighborhoods.add(TravelingTournamentProblem.Neighborhood.SWAP_MATCH_ROUND);
+ neighborhoods.add(TravelingTournamentProblem.Neighborhood.SWAP_MATCHES);
+
+ for(int i=6; i<=12; i+=2) {
+ String fileToRun = "data" + i + ".txt";
+ File instanceFile = new File(inputDirectory, fileToRun);
+ File instanceOut = new File(outputDirectory, fileToRun + "_statistics");
+
+ File sumFile = new File(instanceOut, instanceFile.getName() + "_tabulistlen_sum.csv");
+ File avgFile = new File(instanceOut, instanceFile.getName() + "_tabulistlen_avg.csv");
+ boolean headerWritten = false;
+
+ for (int j = 0; j < 10; j++) {
+ File subOutDir = new File(instanceOut, fileToRun + "_statistics_" + (j + 1));
+
+ TravelingTournamentProblem travelingTournamentProblem = new TravelingTournamentProblem();
+ TTPParameters parameters = new TTPParameters(TravelingTournamentProblem.Method.GRASP,
+ neighborhoods, TravelingTournamentProblem.ConstructionHeuristic.GRASP,
+ VirtualScheduleConstructionMethod.FIRSTPOLYGONTHENGREEK, 70, 40, 10000, 40, instanceFile,
+ subOutDir, 5L * 60L * 1000L, 0.4);
+ TTPResult result = travelingTournamentProblem.run(parameters);
+
+ if (!headerWritten) {
+ writeResultHeader(sumFile, "run", result);
+ writeResultHeader(avgFile, "run", result);
+ headerWritten = true;
+ }
+
+ writeResultSum(sumFile, "" + (j+1), result);
+ writeResultAvg(avgFile, "" + (j+1), result);
+ }
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ TenFoldFiveMinuteRun tenFoldFiveMinuteRun = new TenFoldFiveMinuteRun();
+ CmdLineParser parser = new CmdLineParser(tenFoldFiveMinuteRun);
+ parser.setUsageWidth(80); // width of the error display area
+
+ try {
+ parser.parseArgument(args);
+ } catch (CmdLineException e) {
+ System.err.println(e.getMessage());
+ System.err.println("java ttp.main.TravelingTournamentProblem [options...] arguments...");
+ // print the list of available options
+ parser.printUsage(System.err);
+ System.err.println();
+ System.exit(1);
+ }
+
+ tenFoldFiveMinuteRun.run();
+ }
+
+
+ private static void writeResultHeader(File outputFile, String prefix, TTPResult result) throws IOException {
+ if (!outputFile.exists()) {
+ if (!outputFile.createNewFile()) {
+ System.err.println("failed to create result file " + outputFile.getCanonicalPath());
+ return;
+ }
+ }
+
+ PrintWriter writer = new PrintWriter(outputFile, "utf-8");
+
+ writer.print(prefix);
+ writer.print(',');
+
+ if (result.getLocalSearchStatistics() != null) {
+ result.getLocalSearchStatistics().writeInformationHeader(writer);
+ } else if (result.getSearchStatistics() != null) {
+ if (result.getSearchStatistics().getSolution() == null)
+ writer.println();
+ else
+ result.getSearchStatistics().writeStatisticsHeader(writer);
+ }
+
+ writer.close();
+ }
+
+ private static void writeResultSum(File outputFile, String prefix, TTPResult result) throws FileNotFoundException {
+ if (!outputFile.exists()) return;
+
+ FileOutputStream fso = new FileOutputStream(outputFile, true);
+ PrintWriter writer = new PrintWriter(fso);
+
+ writer.print(prefix);
+ writer.print(',');
+
+ if (result.getLocalSearchStatistics() != null) {
+ result.getLocalSearchStatistics().writeInformation(writer);
+ } else if (result.getSearchStatistics() != null) {
+ result.getSearchStatistics().writeStatisticSum(writer);
+ }
+
+ writer.close();
+ }
+
+ private static void writeResultAvg(File outputFile, String prefix, TTPResult result) throws FileNotFoundException {
+ if (!outputFile.exists()) return;
+
+ FileOutputStream fso = new FileOutputStream(outputFile, true);
+ PrintWriter writer = new PrintWriter(fso);
+
+ writer.print(prefix);
+ writer.print(',');
+
+ if (result.getLocalSearchStatistics() != null) {
+ result.getLocalSearchStatistics().writeInformation(writer);
+ } else if (result.getSearchStatistics() != null) {
+ result.getSearchStatistics().writeStatisticAverage(writer);
+ }
+
+ writer.close();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.