def runSimulation(start, solution): """ Returns minumum number of time steps needed to get to solution """ stringsol = copy.copy(solution) str(stringsol) nextGeneration = [] solutionNodes = [] queue = [] g = 0 result = 5000 solution_found = False pare_node = Node(start) m = (0, pare_node) heappush(queue, m) while ((queue != []) and (g <= maxGenerations) and (solution_found == False)): print "--- Computing generation", g, "---" print "Queue length ", len(queue) g += 1 if (g < generationsBeam): for b in range(beam1): if (queue != []): pare_node = heappop(queue) children = generateAllChildren(pare_node[1].cargo) for i in range(len(children)): nextGeneration.append(children[i]) else: for b in range(beam2): if (queue != []): pare_node = heappop(queue) children = generateAllChildren(pare_node[1].cargo) for i in range(len(children)): nextGeneration.append(children[i]) c = selectChildren(nextGeneration, g) queue = [] for i in range(len(c)): # create nodes node = Node(c[i], pare_node[1]) if (c[i] == solution): solutionNodes.append(node) solution_found = True inversions = 0 while((node.prev != None)): node = node.prev inversions += 1 print "No. of inversions:", inversions result = inversions else: score = generationScore(c[i], g) l = (score, node) if (len(queue) <= maxQueue): heappush(queue, l) else: heappushpop(queue, l) queue[:] = [] return result
def runSimulation(start, solution): """ Returns minumum number of time steps needed to get to solution """ solutionNodes = [] queue = [] g = 0 solution_found = False pare_node = Node(start) m = (0, pare_node) heappush(queue, m) while ((queue != []) and (g <= maxGenerations) and (solution_found == False)): print "--- Computing generation", g, "---" nextGeneration = [] g += 1 if (g < generationsBeam): for b in range(beam1): if (queue != []): pare_node = heappop(queue) children = generateAllChildren(pare_node[1].cargo) for i in range(len(children)): node = Node(children[i], pare_node[1]) nextGeneration.append(node) else: for b in range(beam2): if (queue != []): pare_node = heappop(queue) children = generateAllChildren(pare_node[1].cargo) for i in range(len(children)): node = Node(children[i], pare_node[1]) nextGeneration.append(node) c = selectChildren(nextGeneration, g) queue = [] for i in range(len(c)): # print c[i].cargo if (c[i].cargo == solution): solutionNodes.append(c[i]) solution_found = True inversions = 0 while((node.prev != None)): node = node.prev print "step:", node.cargo inversions += 1 print "No. of inversions:", inversions break else: score = generationScore(c[i].cargo, g) l = (score, c[i]) if (len(queue) <= maxQueue): heappush(queue, l) else: heappushpop(queue, l)
def selectChildren(children, g): scores = [] # calculate "fitness" scores for i in range(len(children)): s = generationScore(children[i], g) scores.append(s) # check which 3 genomes have the best scores dictionary = heapq.nsmallest(beam, zip(scores, children)) # put the best genomes in a list before returning best_children = [] for j in range(len(dictionary)): best_children.append(dictionary[j][1]) children = [] scores = [] return best_children