def runSimulation(start, solution): """ Returns minumum number of time steps needed to get to solution """ scoredict = {} pathway = [] childrenlist = [] currentmin = 20 solution_found = False pare_node = Node(start) m = (0, pare_node) heappush(queue, m) tempnode = heappop(queue) tempchildren = generateAllChildren(tempnode[1].cargo) for k in range(len(tempchildren)): tempscore = seriesScore(tempchildren[k]) initnode = Node(tempchildren[k], tempnode[1]) v = (tempscore, initnode) heappush(queue, v) # print queue[k][1] print while (queue != [] and not solution_found): pare_node = heappop(queue) # print pare_node[1] children = generateAllChildren(pare_node[1].cargo) c = selectChildren(children,10) for i in range(len(c)): score = seriesScore(c[i]) node = Node(c[i], pare_node[1]) l = (score, node) heappush(queue, l) if (c[i] == solution): print "Solution: ", c[i] solution_found = True inversions = 0 while(node.prev != None): print node pathway.append(node.cargo) node = node.prev inversions += 1 print "Inversions: ", inversions if (inversions < currentmin): scoredict = {} scoredict[inversions] = pathway print scoredict for key, val in scoredict.items(): currentmin = key print currentmin
def runSimulation(start, solution): """ Returns minumum number of time steps needed to get to solution """ solution_found = False pare_node = Node(start) m = (0, pare_node) heappush(queue, m) while (queue != [] and not solution_found): pare_node = heappop(queue) children = generateAllChildren(pare_node[1].cargo) c = selectChildren(children) for i in range(len(c)): score = seriesScore(c[i]) node = Node(c[i], pare_node[1]) l = (score, node) heappush(queue, l) if (c[i] == solution): print "Solution: ", c[i] solution_found = True inversions = 0 while(node.prev != None): print node node = node.prev inversions += 1 print "Inversions: ", inversions
def selectChildren(children): scores = [] # calculate "fitness" scores for i in range(len(children)): s = seriesScore(children[i]) scores.append(s) if (fittest == 1): return children[scores.index(max(scores))]
def selectChildren(children): scores = [] # calculate "fitness" scores for i in range(len(children)): s = seriesScore(children[i]) scores.append(s) # check which 3 genomes have the best scores dictionary = heapq.nlargest(3, 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
def runSimulation(start, solution): """ Returns minumum number of time steps needed to get to solution """ solutionNodes = [] lowest = 645758 pare_node = Node(start) m = (0, pare_node) heappush(queue, m) while (queue != []): pare_node = heappop(queue) children = generateAllChildren(pare_node[1].cargo) c = selectChildren(children) for i in range(len(c)): # create nodes node = Node(c[i], pare_node[1]) if (c[i] == solution): solutionNodes.append(node) print "length of solutionNodes: ", len(solutionNodes) else: score = seriesScore(c[i]) l = (score, node) heappush(queue, l) inversions = 0 for j in range(len(solutionNodes)): node = solutionNodes[j] while((node.prev != None) and (inversions < lowest)): print "Step", node node = node.prev inversions += 1 if ((inversions < lowest) and (node.prev == None)): lowest = inversions print "Inversions: ", inversions j += 1 print "Start: ", start
def selectChildren(children): scores = [] # calculate "fitness" scores for i in range(len(children)): s = 1 - (seriesScore(children[i])/len(start)) print "SERIESSCORE=: ", s scores.append(s) print "SCORES: ", scores # check which 3 genomes have the best scores dictionary = heapq.nlargest(4, zip(scores, children)) print print dictionary # 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