summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2007-12-17 13:34:22 +0100
committerGuillaume Seguin <guillaume@segu.in>2007-12-17 13:34:22 +0100
commit6ccf9f2041f6a1ac259c6e53bca486c2577b0159 (patch)
treef3287f44de3965947e2ccf83ecadd2bc2e5a0338
parente3bc3203c9662215660f305810bd0fd83f54299c (diff)
downloadtsp-6ccf9f2041f6a1ac259c6e53bca486c2577b0159.tar.gz
tsp-6ccf9f2041f6a1ac259c6e53bca486c2577b0159.tar.bz2
* Enable resuming current algorithm execution
-rwxr-xr-xtsp.py81
1 files changed, 56 insertions, 25 deletions
diff --git a/tsp.py b/tsp.py
index bc037f6..ba510a5 100755
--- a/tsp.py
+++ b/tsp.py
@@ -334,6 +334,9 @@ class AlgoThread (threading.Thread):
init = False
terminate = False
+ runner = None
+ best = None
+
def __init__ (self, context, algo, gui, canvas, init = None):
'''Initialize the thread'''
threading.Thread.__init__ (self)
@@ -356,26 +359,26 @@ class AlgoThread (threading.Thread):
'''Main thread loop: generate a path (randomly or not, according to
the current settings) and run the algorithm on it until the stop () function
is called.'''
- self.update_best (path = None)
- best = None
- if self.init == INIT_GREEDY_BEST:
- for i in self.context.map:
- if self.terminate:
- return
- path = self.context.nearest_neighbours (start = i)
- if not best or path.length < best.length:
- best = path
- elif self.init == INIT_GREEDY:
- best = self.context.nearest_neighbours ()
- else:
- best = self.context.random_path ()
- self.update_best (path = best)
- runner = self.algo.run (best)
+ if not self.runner:
+ self.update_best (path = None)
+ if self.init == INIT_GREEDY_BEST:
+ for i in self.context.map:
+ if self.terminate:
+ return
+ path = self.context.nearest_neighbours (start = i)
+ if not best or path.length < best.length:
+ self.best = path
+ elif self.init == INIT_GREEDY:
+ self.best = self.context.nearest_neighbours ()
+ else:
+ self.best = self.context.random_path ()
+ self.update_best (path = self.best)
+ self.runner = self.algo.run (self.best)
while not self.terminate:
- path = runner.next ()
- if not best or path.length < best.length:
- best = path
- self.update_best (path = best)
+ path = self.runner.next ()
+ if not self.best or path.length < self.best.length:
+ self.best = path
+ self.update_best (path = self.best)
def stop (self):
'''Stop thread'''
@@ -456,6 +459,7 @@ class AlgoGui (gtk.Window):
alignment = gtk.Alignment (0.5, 0.5, 0, 0)
alignment.add (self.canvas)
box.pack_start (alignment, False, False)
+ # Settings
hbox = gtk.HBox ()
alignment = gtk.Alignment (0.5, 0.5, 0, 0)
alignment.add (hbox)
@@ -475,6 +479,16 @@ class AlgoGui (gtk.Window):
map (lambda i: self.count_box.append_text ("%d cities" % i), COUNTS)
self.count_box.set_active (COUNTS.index (DEFAULT_COUNT))
hbox.pack_start (self.count_box, False, False)
+ # Show labels button
+ names_button = gtk.CheckButton (label = "Show labels")
+ names_button.connect ("toggled", self.toggle_show_names)
+ names_button.set_active (True)
+ hbox.pack_start (names_button, False, False)
+ # Buttons
+ hbox = gtk.HBox ()
+ alignment = gtk.Alignment (0.5, 0.5, 0, 0)
+ alignment.add (hbox)
+ box.pack_start (alignment, False, False)
# Generate button
self.gen_button = gtk.Button ("Generate")
self.gen_button.connect ("clicked", self.generate)
@@ -484,16 +498,16 @@ class AlgoGui (gtk.Window):
self.start_button.connect ("clicked", self.start)
self.start_button.set_sensitive (False)
hbox.pack_start (self.start_button, False, False)
+ # Resume button
+ self.resume_button = gtk.Button ("Resume")
+ self.resume_button.connect ("clicked", self.resume)
+ self.resume_button.set_sensitive (False)
+ hbox.pack_start (self.resume_button, False, False)
# Stop button
self.stop_button = gtk.Button ("Stop")
self.stop_button.connect ("clicked", self.stop)
self.stop_button.set_sensitive (False)
hbox.pack_start (self.stop_button, False, False)
- # Show labels button
- 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)
# Informative labels
self.best_label = gtk.Label ()
self.update_best (length = None)
@@ -526,6 +540,7 @@ class AlgoGui (gtk.Window):
self.context.count = self.get_count ()
self.context.prepare ()
self.start_button.set_sensitive (True)
+ self.resume_button.set_sensitive (False)
self.canvas.redraw (damage = True)
def start (self, *args):
@@ -539,6 +554,22 @@ class AlgoGui (gtk.Window):
self.thread.start ()
self.gen_button.set_sensitive (False)
self.start_button.set_sensitive (False)
+ self.resume_button.set_sensitive (False)
+ self.stop_button.set_sensitive (True)
+
+ def resume (self, *args):
+ '''Resume algorithm execution'''
+ if not self.thread or self.thread.isAlive ():
+ return
+ thread = AlgoThread (self.context, self.algo, self, self.canvas,
+ init = self.get_init ())
+ thread.runner = self.thread.runner
+ thread.best = self.thread.best
+ self.thread = thread
+ self.thread.start ()
+ self.gen_button.set_sensitive (False)
+ self.start_button.set_sensitive (False)
+ self.resume_button.set_sensitive (False)
self.stop_button.set_sensitive (True)
def stop (self, *args):
@@ -549,9 +580,9 @@ class AlgoGui (gtk.Window):
if self.thread.isAlive ():
gobject.timeout_add (100, self.stop)
return
- self.thread = None
self.gen_button.set_sensitive (True)
self.start_button.set_sensitive (True)
+ self.resume_button.set_sensitive (True)
self.stop_button.set_sensitive (False)
def update_best (self, length):