summaryrefslogtreecommitdiff
path: root/tsp.py
diff options
context:
space:
mode:
Diffstat (limited to 'tsp.py')
-rwxr-xr-xtsp.py33
1 files changed, 24 insertions, 9 deletions
diff --git a/tsp.py b/tsp.py
index 8eeb24d..178095d 100755
--- a/tsp.py
+++ b/tsp.py
@@ -35,6 +35,7 @@ import random
import math
import cairo
import threading
+import datetime
MAX_X = 500 # Max X (virtual) coordinate
MAX_Y = 500 # Max Y (virtual) coordinate
@@ -366,6 +367,7 @@ class AlgoThread (threading.Thread):
canvas = None
init = False
terminate = False
+ time = None
runner = None
best = None
@@ -379,12 +381,12 @@ class AlgoThread (threading.Thread):
self.canvas = canvas
self.init = init
- def update_best (self, path):
+ def update_best (self, path, time):
'''Update GUI to reflect the finding of a better solution'''
gtk.gdk.threads_enter ()
if not path: length = None
else: length = path.length
- self.gui.update_best (length)
+ self.gui.update_best (length, time)
self.canvas.redraw (path = path, damage = True)
gtk.gdk.threads_leave ()
@@ -392,13 +394,15 @@ 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.'''
+ start = datetime.datetime.now ()
if not self.runner:
- self.update_best (path = None)
+ self.time = datetime.timedelta ()
+ self.update_best (path = None, time = None)
if self.algo.need_init:
if self.init == INIT_GREEDY_BEST:
for i in self.context.map:
if self.terminate:
- return
+ self.time = self.time + self.get_time (start)
path = self.context.nearest_neighbours (start = i)
if not best or path.length < best.length:
self.best = path
@@ -406,14 +410,21 @@ is called.'''
self.best = self.context.nearest_neighbours ()
else:
self.best = self.context.random_path ()
- self.update_best (path = self.best)
+ self.update_best (path = self.best,
+ time = self.get_time (start))
self.runner = self.algo.run (self.best)
self.runner = self.algo.run ()
while not self.terminate:
path = self.runner.next ()
if not self.best or path.length < self.best.length:
self.best = path
- self.update_best (path = self.best)
+ self.update_best (path = self.best,
+ time = self.get_time (start))
+ self.time = self.time + self.get_time (start)
+
+ def get_time (self, start):
+ '''Return timedelta since a given date'''
+ return self.time + datetime.datetime.now () - start
def resume (self):
'''Resume thread: since we can't start a Thread twice, let's copy
@@ -422,6 +433,7 @@ its data into a new Thread, start the new one and return it'''
self.init)
thread.runner = self.runner
thread.best = self.best
+ thread.time = self.time
thread.start ()
return thread
@@ -555,7 +567,7 @@ class AlgoGui (gtk.Window):
hbox.pack_start (self.stop_button, False, False)
# Informative labels
self.best_label = gtk.Label ()
- self.update_best (length = None)
+ self.update_best (length = None, time = None)
box.pack_start (self.best_label, False, False)
self.config_label = gtk.Label ()
self.update_config (config = False)
@@ -582,6 +594,7 @@ class AlgoGui (gtk.Window):
def generate (self, *args):
'''Generate a new map'''
+ self.thread = None
self.context.count = self.get_count ()
self.context.prepare ()
self.start_button.set_sensitive (True)
@@ -625,13 +638,15 @@ class AlgoGui (gtk.Window):
self.resume_button.set_sensitive (True)
self.stop_button.set_sensitive (False)
- def update_best (self, length):
+ def update_best (self, length, time):
'''Update best solution length label'''
if not length:
self.best_label.set_markup ("<b>Best length yet</b>: N/A")
return
length = round (length, 2)
- self.best_label.set_markup ("<b>Best length yet</b>: %s" % length)
+ time = round (time.seconds + float (time.microseconds) / 1000000, 2)
+ label = '''<b>Best length yet</b>: %s (found in %ss)'''
+ self.best_label.set_markup (label % (length, time))
def update_config (self, config = True):
'''Update current configuration label'''