Skip to content

Commit 208c71d

Browse files
author
A. Wilke
committed
export data, best tour info
1 parent fa35ea2 commit 208c71d

File tree

6 files changed

+86
-38
lines changed

6 files changed

+86
-38
lines changed

MANIFEST.MF

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: de.adrianwilke.acotspjava.AcoTsp
3+
Class-Path: commons-cli-1.2.jar

src/de/adrianwilke/acotspjava/AcoTsp.java

+30-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package de.adrianwilke.acotspjava;
22

3+
import java.io.BufferedWriter;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.OutputStreamWriter;
7+
import java.io.Writer;
8+
39
/**
410
* ACO algorithms for the TSP
511
*
@@ -275,8 +281,8 @@ static void search_control_and_statistics()
275281
if ((InOut.iteration % 100) == 0) {
276282
InOut.population_statistics();
277283
InOut.branching_factor = InOut.node_branching(InOut.lambda);
278-
System.out.println("best so far " + Ants.best_so_far_ant.tour_length + ", iteration: "
279-
+ InOut.iteration + ", time " + Timer.elapsed_time() + ", b_fac " + InOut.branching_factor);
284+
System.out.println("best so far " + Ants.best_so_far_ant.tour_length + ", iteration: " + InOut.iteration
285+
+ ", time " + Timer.elapsed_time() + ", b_fac " + InOut.branching_factor);
280286

281287
if (Ants.mmas_flag && (InOut.branching_factor < InOut.branch_fac)
282288
&& (InOut.iteration - InOut.restart_found_best > 250)) {
@@ -442,8 +448,7 @@ static void bwas_update()
442448
Ants.init_pheromone_trails(Ants.trail_0);
443449
InOut.restart_iteration = InOut.iteration;
444450
InOut.restart_time = Timer.elapsed_time();
445-
System.out.println("init Ants.pheromone trails with " + Ants.trail_0 + ", iteration "
446-
+ InOut.iteration);
451+
System.out.println("init Ants.pheromone trails with " + Ants.trail_0 + ", iteration " + InOut.iteration);
447452
} else
448453
Ants.bwas_pheromone_mutation();
449454
}
@@ -541,9 +546,9 @@ public static void main(String[] args) {
541546
* (SIDE)EFFECTS: none
542547
* COMMENTS: this function controls the run of "max_tries" independent trials
543548
*/
544-
for(String argument : args){
545-
System.out.println(argument);
546-
}
549+
for (String argument : args) {
550+
System.out.println(argument);
551+
}
547552
Timer.start_timers();
548553

549554
InOut.init_program(args);
@@ -577,6 +582,23 @@ public static void main(String[] args) {
577582
InOut.exit_try(InOut.n_try);
578583
}
579584
InOut.exit_program();
580-
585+
586+
// Added by AW
587+
int aw_best_tour_length = Utilities.best_of_vector(InOut.best_in_try, InOut.max_tries);
588+
String aw_best_tour = InOut.aw_best_tour_in_try[Utilities.aw_best_tour_index()];
589+
try {
590+
Writer w = new OutputStreamWriter(new FileOutputStream("tour." + Tsp.instance.name), "UTF8");
591+
BufferedWriter out = new BufferedWriter(w);
592+
out.write(aw_best_tour_length + "\n");
593+
out.write(aw_best_tour);
594+
out.close();
595+
} catch (IOException e) {
596+
System.err.print("Could not write file tour." + Tsp.instance.name + " " + e.getMessage());
597+
System.exit(1);
598+
}
599+
System.out.println();
600+
System.out.println("Best tour:");
601+
System.out.println(aw_best_tour_length);
602+
System.out.println(aw_best_tour);
581603
}
582604
}

src/de/adrianwilke/acotspjava/InOut.java

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.OutputStreamWriter;
1111
import java.io.Reader;
1212
import java.io.Writer;
13+
import java.util.Arrays;
1314
import java.util.HashMap;
1415
import java.util.Map;
1516

@@ -80,6 +81,7 @@ enum Distance_type {
8081
static int[] best_found_at;
8182
static double[] time_best_found;
8283
static double[] time_total_run;
84+
static String[] aw_best_tour_in_try;
8385

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

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

605+
aw_best_tour_in_try = new String[max_tries];
606+
602607
// TRACE ( System.out.println("read problem data ..\n\n"); )
603608

604609
try {

src/de/adrianwilke/acotspjava/Parse.java

+25-25
Original file line numberDiff line numberDiff line change
@@ -94,31 +94,31 @@ static int parse_commandline(String args[]) {
9494
}
9595

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

123123
CommandLine cmd = null;
124124
CommandLineParser parser = new BasicParser();

src/de/adrianwilke/acotspjava/Utilities.java

+18
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,22 @@ static double[][] generate_double_matrix(int n, int m)
331331
{
332332
return new double[n][m];
333333
}
334+
335+
static int aw_best_tour_index() {
336+
int min, k;
337+
338+
int[] values = InOut.best_in_try;
339+
int l = InOut.max_tries;
340+
341+
k = 0;
342+
min = values[k];
343+
int minIndex = 0;
344+
for (k = 1; k < l; k++) {
345+
if (values[k] < min) {
346+
min = values[k];
347+
minIndex = k;
348+
}
349+
}
350+
return minIndex;
351+
}
334352
}

src/de/adrianwilke/acotspjava/test/Test.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ public static void main(String[] args) {
1515

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

18-
// argList.add("-h");
18+
argList.add("-h");
1919

20-
putTestParameters();
21-
argList.add("-l");
22-
argList.add("0");
20+
// putTestParameters();
21+
// argList.add("-l");
22+
// argList.add("0");
2323

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

2626
// Comments: ACOTSP, ACOTSPJava
2727

28-
argList.add("--as");
28+
// argList.add("--as");
2929
// -l 0: 62571,67053
3030
// -l 1: 52842,53925
3131
// -l 2: 52547,53464

0 commit comments

Comments
 (0)