4
4
5
5
import math
6
6
import random
7
+ import sys
7
8
8
9
"""
9
10
2.5
@@ -93,7 +94,7 @@ def perturbation(cities, best):
93
94
candidate ["cost" ] = path_cost (candidate ["vector" ], cities )
94
95
return candidate
95
96
96
- def search (cities , max_iterations , max_no_improv ):
97
+ def search (cities , max_iterations , max_no_improv , output_format = "human" ):
97
98
best = {}
98
99
best ["vector" ] = random_permutation (cities )
99
100
best ["cost" ] = path_cost (best ["vector" ], cities )
@@ -106,7 +107,10 @@ def search(cities, max_iterations, max_no_improv):
106
107
if candidate ["cost" ] < best ["cost" ]:
107
108
best = candidate
108
109
109
- print ("Iteration #" + str (i ) + " best = " + str (best ["cost" ]))
110
+ if output_format == "csv" :
111
+ print ("%s,%s" % (i , best ["cost" ]))
112
+ else :
113
+ print ("Iteration #" + str (i ) + " best = " + str (best ["cost" ]))
110
114
111
115
return best
112
116
@@ -116,7 +120,15 @@ def search(cities, max_iterations, max_no_improv):
116
120
# Algorithm configuration
117
121
max_iterations = 100
118
122
max_no_improv = 50
123
+ output_format = "human"
124
+
125
+ if len (sys .argv ) >= 2 :
126
+ output_format = sys .argv [1 ]
119
127
120
128
# Execute the algorithm
121
- best = search (berlin52 , max_iterations , max_no_improv )
122
- print ("Done. Best solution: c = " + str (best ["cost" ]) + ", v = " + str (best ["vector" ]))
129
+ best = search (berlin52 , max_iterations , max_no_improv , output_format )
130
+
131
+ if output_format == "csv" :
132
+ print ("%s,%s" % (best ["cost" ], "," .join ([str (i ) for i in best ["vector" ]])))
133
+ else :
134
+ print ("Done. Best solution: c = " + str (best ["cost" ]) + ", v = " + str (best ["vector" ]))
0 commit comments