def heur(puzzle, item_total_util, total_util): """ heuristic template that provides the current and target position for each number and the total function. parameters: puzzle - the puzzle item_total_util - takes 4 parameters: current row, target row, current col, target col. returns int. total_util - takes 1 parameter, the sum of item_total_util over all entries, and returns int. """ t = 0 utilit = util.utility() for row in range(3): for col in range(3): val = utilit.peek( puzzle, row, col ) - 1 # value is -1 if the peeked index is 0. triggers if target_row < 0 below. target_col = val % 3 target_row = val / 3 # account for 0 as blank if target_row < 0: target_row = 2 t += item_total_util(row, target_row, col, target_col) return total_util(t)
def graph_expected_utility_vs_alpha(numpy_regular, numpy_margin, outdir_name): """alpha is as in utility(wealth) = wealth^alpha""" MIN_ALPHA = 0 MAX_ALPHA = 1 NUM_POINTS = 1000 alpha_values = numpy.linspace(MIN_ALPHA,MAX_ALPHA,NUM_POINTS) ratio_of_expected_utilities = map(lambda alpha: \ numpy.mean(map(lambda wealth: util.utility(wealth, alpha), numpy_margin)) / \ numpy.mean(map(lambda wealth: util.utility(wealth, alpha), numpy_regular)), alpha_values) pyplot.plot(alpha_values,ratio_of_expected_utilities) pyplot.title("Ratio of margin/regular expected utility vs. risk tolerance, alpha") pyplot.xlabel("alpha: measure of risk tolerance, as in utility(wealth) = (wealth)^alpha") pyplot.ylabel("expected_utility(margin) / expected_utility(regular)") pyplot.savefig("%s_%s" % (outdir_name, EXPECTED_UTILITY_GRAPH_PREFIX)) pyplot.close()
def graph_expected_utility_vs_alpha(numpy_regular, numpy_margin, outdir_name): """alpha is as in utility(wealth) = wealth^alpha""" MIN_ALPHA = 0 MAX_ALPHA = 1 NUM_POINTS = 1000 alpha_values = numpy.linspace(MIN_ALPHA, MAX_ALPHA, NUM_POINTS) ratio_of_expected_utilities = map(lambda alpha: \ numpy.mean(map(lambda wealth: util.utility(wealth, alpha), numpy_margin)) / \ numpy.mean(map(lambda wealth: util.utility(wealth, alpha), numpy_regular)), alpha_values) pyplot.plot(alpha_values, ratio_of_expected_utilities) pyplot.title( "Ratio of margin/regular expected utility vs. risk tolerance, alpha") pyplot.xlabel( "alpha: measure of risk tolerance, as in utility(wealth) = (wealth)^alpha" ) pyplot.ylabel("expected_utility(margin) / expected_utility(regular)") pyplot.savefig("%s_%s" % (outdir_name, EXPECTED_UTILITY_GRAPH_PREFIX)) pyplot.close()
def _generate_moves(self): utilit = util.utility() free = utilit._get_legal_moves(self) zero = utilit.find(self, 0) def swap_and_clone(a, b): p = self._clone() p = utilit.swap(p, a, b) p._depth = self._depth + 1 p._parent = self return p return map(lambda pair: swap_and_clone(zero, pair), free)
def main(): utility = util.utility() p = EightPuzzle() shuffle = utility.shuffle(p, 20) print "Starting puzzle:" print shuffle selection = raw_input( "Which algorithm would you like to use to solve this matrix? (DFS, BFS, UCS, A*, or All for all four) \n" ) if selection == "DFS": path, count = shuffle.solve_DFS() path.reverse() print "Path to goal state:" for i in path: print i print "Solved with DFS search in", count, "node expansions" elif selection == "BFS": path, count = shuffle.solve(h_default, 'BFS') path.reverse() print "Path to goal state:" for i in path: print i print "Solved with BFS search in", count, "node expansions" elif selection == "UCS": path, count = shuffle.solve(h_default, 'UCS') path.reverse() print "Path to goal state:" for i in path: print i print "Solved with UCS search in", count, "node expansions" elif selection == "A*": path, count = shuffle.solve(h_manhattan, 'A*') path.reverse() print "Path to goal state:" for i in path: print i print "Solved with A* search utilizing Manhattan distance hueuristic in", count, "node expansions" elif selection == "All" or "all": path, count = shuffle.solve(h_manhattan, 'A*') path.reverse() print "Path to goal state:" for i in path: print i print "Solved with A* search utilizing Manhattan distance hueuristic in", count, "node expansions" path, count = shuffle.solve(h_default, 'UCS') print "Solved with UCS search in", count, "node expansions" path, count = shuffle.solve(h_default, 'BFS') print "Solved with BFS search in", count, "node expansions" path, count = shuffle.solve_DFS() print "Solved with DFS search in", count, "node expansions"
class color: PURPLE = "\033[95m" CYAN = "\033[96m" DARKCYAN = "\033[36m" BLUE = "\033[94m" GREEN = "\033[92m" YELLOW = "\033[93m" RED = "\033[91m" BOLD = "\033[1m" UNDERLINE = "\033[4m" END = "\033[0m" utility() boys = open('./boys.csv') girls = open('./girls.csv') getboy = csv.reader(boys, delimiter=',') getgirl = csv.reader(girls, delimiter=',') boy_list = [] girl_list = [] for i in getboy: boy_list += [Boy(i[0], int(i[1]), int(i[2]), int(i[3]), int(i[4]), i[5])] for j in getgirl: girl_list += [Girl(j[0], int(j[1]), int(j[2]), int(j[3]), j[4])] for g in girl_list: for b in boy_list: