Example #1
0
 def worker(self, q, startboard):
     '''
     The main task of annealing that will be split up for different processes to work on. Places its finished work on
     a multi-prossess queue.
     
     parameters:
         q             - type multiprocessing.Queue
         startboard    - type nested list representing game board
     '''
     def _schedule(t):
         return ((self.iter - t)*100)/self.iter
     
     cur_board = board.board(self.size, self.dict)
     cur_board.board = copy.deepcopy(startboard)
     cur_board.score = cur_board.find_max()
     
     next_board = board.board(self.size, self.dict)
     next_board.board = copy.deepcopy(startboard)
     
     #print "cur:", cur_board
     #print "next:", next_board
     T = 0
     
     for i in range(self.iter):
         T = _schedule(i)
         if T == 0:
             break
         
         next_board.smart_permute()
         score = next_board.find_max()
         delta_e = score - cur_board.score
         
         if delta_e > 0:
             cur_board.board = copy.deepcopy(next_board.board)
             cur_board.score = score
             cur_board.words = next_board.words
         elif ( random.random() < (math.exp(float(delta_e) / float(T))) ):
             cur_board.board = copy.deepcopy(next_board.board)
             cur_board.score = score
             cur_board.words = next_board.words
         else:
             next_board.board = copy.deepcopy(cur_board.board)
     
     q.put((cur_board.score, cur_board.board, cur_board.words))
Example #2
0
 def __init__(self, proc, iter, filename, size, retries):
     '''
     Constructor
     '''
     self.proc = proc
     self.iter = iter
     self.dict = trie.trie()
     self.dict.readDict(filename)
     self.size = size
     self.board = board.board(size, self.dict)
     self.retries = retries
Example #3
0
 def start(self):
     q = Queue()
     startboard = board.board(self.size, self.dict)
     startboard = startboard.board
     
     for i in range(self.retries):
         processes = [Process(target=self.worker, args=(q, startboard,)) for i in range(self.proc)]
         # Start all the worker threads, and wait for them to finish
         for p in processes:
             p.start()
         for p in processes:
             p.join()
         
         # When the workers are done, check the results which they placed in the queue.
         res = []
         for i in range(self.proc):
             res.append(q.get())
         
         # Take the best one, and tell them all to start again using this best found board
         max_score = 0
         max_board = []
         for x,y,z in res:
             if x > max_score:
                 max_score = x
                 max_board = y
         
         startboard = max_board
     
     # Once we have done all repeats, find the best candidate from the final round and return it
     max_score = 0
     for x,y,z in res:
         if x > max_score:
             max_score = x
             self.board.score = x
             self.board.board = y
             self.board.words = z
     return self.board