summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2007-12-17 03:03:48 +0100
committerGuillaume Seguin <guillaume@segu.in>2007-12-17 03:03:48 +0100
commit4a5e8358cabfa1a694849194d2922f456f55b844 (patch)
tree2cb780dbac3a576b70f648a60d84800ed16ef0fd
parente292fa26f0bbb8a82f6ec8d0b24f61e144f62206 (diff)
downloadtsp-4a5e8358cabfa1a694849194d2922f456f55b844.tar.gz
tsp-4a5e8358cabfa1a694849194d2922f456f55b844.tar.bz2
* Add combo box to choose between Random and Greedy inits
-rwxr-xr-xtsp.py44
1 files changed, 32 insertions, 12 deletions
diff --git a/tsp.py b/tsp.py
index 5eb81d4..6bbb207 100755
--- a/tsp.py
+++ b/tsp.py
@@ -44,9 +44,11 @@ CITY_RADIUS = 3
SHOW_NAMES = True
+INITS = ["Random", "Greedy"]
ALGOS = ["2opt"]
COUNTS = [8, 10, 25, 50, 100, 250]
+DEFAULT_INIT = "Random"
DEFAULT_ALGO = "2opt"
DEFAULT_COUNT = 50
@@ -228,14 +230,16 @@ class AlgoThread (threading.Thread):
algo = None
gui = None
canvas = None
+ nearest = False
terminate = False
- def __init__ (self, context, algo, gui, canvas):
+ def __init__ (self, context, algo, gui, canvas, use_nearest = False):
threading.Thread.__init__ (self)
self.context = context
self.algo = algo
self.gui = gui
self.canvas = canvas
+ self.nearest = use_nearest
def update_best (self, path):
gtk.gdk.threads_enter ()
@@ -248,12 +252,15 @@ class AlgoThread (threading.Thread):
def run (self):
self.update_best (path = None)
best = None
- for i in self.context.map.keys ():
- if self.terminate:
- return
- path = self.context.nearest_neighbours (start = i)
- if not best or path.length < best.length:
- best = path
+ if self.nearest:
+ for i in self.context.map.keys ():
+ if self.terminate:
+ return
+ path = self.context.nearest_neighbours (start = i)
+ if not best or path.length < best.length:
+ best = path
+ else:
+ best = self.context.random_path ()
self.update_best (path = best)
runner = self.algo.run (best)
while not self.terminate:
@@ -309,6 +316,7 @@ class AlgoGui (gtk.Window):
thread = None
canvas = None
+ init_box = None
algo_box = None
count_box = None
gen_button = None
@@ -319,7 +327,7 @@ class AlgoGui (gtk.Window):
config_label = None
def __init__ (self):
- global ALGOS, COUNTS
+ global INITS, ALGOS, COUNTS
gtk.Window.__init__ (self)
self.context = Context (delayed = True)
self.algo = None
@@ -331,6 +339,10 @@ class AlgoGui (gtk.Window):
self.canvas = AlgoCanvas (self.context)
box.pack_start (self.canvas, False, False)
hbox = gtk.HBox ()
+ self.init_box = gtk.combo_box_new_text ()
+ map (self.init_box.append_text, INITS)
+ self.init_box.set_active (INITS.index (DEFAULT_INIT))
+ hbox.pack_start (self.init_box, False, False)
self.algo_box = gtk.combo_box_new_text ()
map (self.algo_box.append_text, ALGOS)
self.algo_box.set_active (ALGOS.index (DEFAULT_ALGO))
@@ -350,7 +362,7 @@ class AlgoGui (gtk.Window):
self.stop_button.connect ("clicked", self.stop)
self.stop_button.set_sensitive (False)
hbox.pack_start (self.stop_button, False, False)
- names_button = gtk.CheckButton (label = "Show names")
+ names_button = gtk.CheckButton (label = "Labels")
names_button.connect ("toggled", self.toggle_show_names)
names_button.set_active (True)
hbox.pack_start (names_button, False, False)
@@ -377,6 +389,9 @@ class AlgoGui (gtk.Window):
def get_algo (self):
return self.algo_box.get_active_text ()
+ def get_init (self):
+ return self.init_box.get_active_text ()
+
def generate (self, *args):
self.context.count = self.get_count ()
self.context.prepare ()
@@ -384,10 +399,13 @@ class AlgoGui (gtk.Window):
self.canvas.redraw (damage = True)
def start (self, *args):
+ global INITS
self.stop ()
self.update_config ()
self.algo = eval ("Algo_%s" % self.get_algo ()) (self.context)
- self.thread = AlgoThread (self.context, self.algo, self, self.canvas)
+ use_nearest = self.init_box.get_active () == INITS.index ("Greedy")
+ self.thread = AlgoThread (self.context, self.algo, self, self.canvas,
+ use_nearest = use_nearest)
self.thread.start ()
self.gen_button.set_sensitive (False)
self.start_button.set_sensitive (False)
@@ -413,9 +431,11 @@ class AlgoGui (gtk.Window):
if not config:
self.config_label.set_markup ("<b>Configuration</b>: N/A")
return
- config = "<b>Configuration</b>: %d cities, using %s algorithm"
+ config = '''<b>Configuration</b>: %d cities, using %s algorithm and \
+%s init'''
self.config_label.set_markup (config % (self.get_count (),
- self.get_algo ()))
+ self.get_algo (),
+ self.get_init ()))
if __name__ == "__main__":
gui = AlgoGui ()