Skip to content

Commit

Permalink
export data, best tour info
Browse files Browse the repository at this point in the history
  • Loading branch information
A. Wilke committed Feb 4, 2014
1 parent fa35ea2 commit 208c71d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 38 deletions.
3 changes: 3 additions & 0 deletions MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: de.adrianwilke.acotspjava.AcoTsp
Class-Path: commons-cli-1.2.jar
38 changes: 30 additions & 8 deletions src/de/adrianwilke/acotspjava/AcoTsp.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package de.adrianwilke.acotspjava;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
* ACO algorithms for the TSP
*
Expand Down Expand Up @@ -275,8 +281,8 @@ static void search_control_and_statistics()
if ((InOut.iteration % 100) == 0) {
InOut.population_statistics();
InOut.branching_factor = InOut.node_branching(InOut.lambda);
System.out.println("best so far " + Ants.best_so_far_ant.tour_length + ", iteration: "
+ InOut.iteration + ", time " + Timer.elapsed_time() + ", b_fac " + InOut.branching_factor);
System.out.println("best so far " + Ants.best_so_far_ant.tour_length + ", iteration: " + InOut.iteration
+ ", time " + Timer.elapsed_time() + ", b_fac " + InOut.branching_factor);

if (Ants.mmas_flag && (InOut.branching_factor < InOut.branch_fac)
&& (InOut.iteration - InOut.restart_found_best > 250)) {
Expand Down Expand Up @@ -442,8 +448,7 @@ static void bwas_update()
Ants.init_pheromone_trails(Ants.trail_0);
InOut.restart_iteration = InOut.iteration;
InOut.restart_time = Timer.elapsed_time();
System.out.println("init Ants.pheromone trails with " + Ants.trail_0 + ", iteration "
+ InOut.iteration);
System.out.println("init Ants.pheromone trails with " + Ants.trail_0 + ", iteration " + InOut.iteration);
} else
Ants.bwas_pheromone_mutation();
}
Expand Down Expand Up @@ -541,9 +546,9 @@ public static void main(String[] args) {
* (SIDE)EFFECTS: none
* COMMENTS: this function controls the run of "max_tries" independent trials
*/
for(String argument : args){
System.out.println(argument);
}
for (String argument : args) {
System.out.println(argument);
}
Timer.start_timers();

InOut.init_program(args);
Expand Down Expand Up @@ -577,6 +582,23 @@ public static void main(String[] args) {
InOut.exit_try(InOut.n_try);
}
InOut.exit_program();


// Added by AW
int aw_best_tour_length = Utilities.best_of_vector(InOut.best_in_try, InOut.max_tries);
String aw_best_tour = InOut.aw_best_tour_in_try[Utilities.aw_best_tour_index()];
try {
Writer w = new OutputStreamWriter(new FileOutputStream("tour." + Tsp.instance.name), "UTF8");
BufferedWriter out = new BufferedWriter(w);
out.write(aw_best_tour_length + "\n");
out.write(aw_best_tour);
out.close();
} catch (IOException e) {
System.err.print("Could not write file tour." + Tsp.instance.name + " " + e.getMessage());
System.exit(1);
}
System.out.println();
System.out.println("Best tour:");
System.out.println(aw_best_tour_length);
System.out.println(aw_best_tour);
}
}
5 changes: 5 additions & 0 deletions src/de/adrianwilke/acotspjava/InOut.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -80,6 +81,7 @@ enum Distance_type {
static int[] best_found_at;
static double[] time_best_found;
static double[] time_total_run;
static String[] aw_best_tour_in_try;

static int n_try; /* try counter */
static int n_tours; /* counter of number constructed tours */
Expand Down Expand Up @@ -509,6 +511,7 @@ static void exit_try(int ntry)
best_found_at[ntry] = found_best;
time_best_found[ntry] = time_used;
time_total_run[ntry] = Timer.elapsed_time();
aw_best_tour_in_try[ntry] = Arrays.toString(Ants.best_so_far_ant.tour);

System.out.println("\ntry " + ntry + ", Best " + best_in_try[ntry] + ", found at iteration "
+ best_found_at[ntry] + ", found at time " + time_best_found[ntry] + "\n");
Expand Down Expand Up @@ -599,6 +602,8 @@ static void init_program(String[] args)
time_best_found = new double[max_tries];
time_total_run = new double[max_tries];

aw_best_tour_in_try = new String[max_tries];

// TRACE ( System.out.println("read problem data ..\n\n"); )

try {
Expand Down
50 changes: 25 additions & 25 deletions src/de/adrianwilke/acotspjava/Parse.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,31 @@ static int parse_commandline(String args[]) {
}

Options options = new Options();
options.addOption("r", "tries", true, " # number of independent trials");
options.addOption("s", "tours", true, " # number of steps in each trial");
options.addOption("t", "time", true, " # maximum time for each trial");
options.addOption("seed", true, " # seed for the random number generator");
options.addOption("i", "tsplibfile", true, " f inputfile (TSPLIB format necessary)");
options.addOption("o", "optimum", true, " # stop if tour better or equal optimum is found");
options.addOption("m", "ants", true, " # number of ants");
options.addOption("g", "nnants", true, " # nearest neighbours in tour construction");
options.addOption("a", "alpha", true, " # alpha (influence of pheromone trails)");
options.addOption("b", "beta", true, " # beta (influence of heuristic information)");
options.addOption("e", "rho", true, " # rho: pheromone trail evaporation");
options.addOption("q", "q0", true, " # q_0: prob. of best choice in tour construction");
options.addOption("c", "elitistants", true, " # number of elitist ants");
options.addOption("f", "rasranks", true, " # number of ranks in rank-based Ant System");
options.addOption("k", "nnls", true, " # No. of nearest neighbors for local search");
options.addOption("l", "localsearch", true, " 0: no local search 1: 2-opt 2: 2.5-opt 3: 3-opt");
options.addOption("d", "dlb", false, " 1 use don't look bits in local search");
options.addOption("u", "as", false, " apply basic Ant System");
options.addOption("v", "eas", false, " apply elitist Ant System");
options.addOption("w", "ras", false, " apply rank-based version of Ant System");
options.addOption("x", "mmas", false, " apply MAX-MIN ant_colony system");
options.addOption("y", "bwas", false, " apply best-worst ant_colony system");
options.addOption("z", "acs", false, " apply ant_colony colony system");
options.addOption("quiet", false, " reduce output to a minimum, no extra files");
options.addOption("h", "help", false, " display this help text and exit");
options.addOption("r", "tries", true, "# number of independent trials");
options.addOption("s", "tours", true, "# number of steps in each trial");
options.addOption("t", "time", true, "# maximum time for each trial");
options.addOption("seed", true, "# seed for the random number generator");
options.addOption("i", "tsplibfile", true, "f inputfile (TSPLIB format necessary)");
options.addOption("o", "optimum", true, "# stop if tour better or equal optimum is found");
options.addOption("m", "ants", true, "# number of ants");
options.addOption("g", "nnants", true, "# nearest neighbours in tour construction");
options.addOption("a", "alpha", true, "# alpha (influence of pheromone trails)");
options.addOption("b", "beta", true, "# beta (influence of heuristic information)");
options.addOption("e", "rho", true, "# rho: pheromone trail evaporation");
options.addOption("q", "q0", true, "# q_0: prob. of best choice in tour construction");
options.addOption("c", "elitistants", true, "# number of elitist ants");
options.addOption("f", "rasranks", true, "# number of ranks in rank-based Ant System");
options.addOption("k", "nnls", true, "# No. of nearest neighbors for local search");
options.addOption("l", "localsearch", true, "0:no local search 1:2-opt 2:2.5-opt 3:3-opt");
options.addOption("d", "dlb", false, "1 use don't look bits in local search");
options.addOption("u", "as", false, "apply basic Ant System");
options.addOption("v", "eas", false, "apply elitist Ant System");
options.addOption("w", "ras", false, "apply rank-based version of Ant System");
options.addOption("x", "mmas", false, "apply MAX-MIN ant_colony system");
options.addOption("y", "bwas", false, "apply best-worst ant_colony system");
options.addOption("z", "acs", false, "apply ant_colony colony system");
options.addOption("quiet", false, "reduce output to a minimum, no extra files");
options.addOption("h", "help", false, "display this help text and exit");

CommandLine cmd = null;
CommandLineParser parser = new BasicParser();
Expand Down
18 changes: 18 additions & 0 deletions src/de/adrianwilke/acotspjava/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,22 @@ static double[][] generate_double_matrix(int n, int m)
{
return new double[n][m];
}

static int aw_best_tour_index() {
int min, k;

int[] values = InOut.best_in_try;
int l = InOut.max_tries;

k = 0;
min = values[k];
int minIndex = 0;
for (k = 1; k < l; k++) {
if (values[k] < min) {
min = values[k];
minIndex = k;
}
}
return minIndex;
}
}
10 changes: 5 additions & 5 deletions src/de/adrianwilke/acotspjava/test/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ public static void main(String[] args) {

// System.out.println(new File("").getAbsolutePath());

// argList.add("-h");
argList.add("-h");

putTestParameters();
argList.add("-l");
argList.add("0");
// putTestParameters();
// argList.add("-l");
// argList.add("0");

// ./AcoTsp --ants 20 --time 2 --tours 2 --tries 2 -i ../../d1291.tsp -l 0 --as

// Comments: ACOTSP, ACOTSPJava

argList.add("--as");
// argList.add("--as");
// -l 0: 62571,67053
// -l 1: 52842,53925
// -l 2: 52547,53464
Expand Down

0 comments on commit 208c71d

Please sign in to comment.