def compare_searchers(problems, header, searchers=[]): best = {} bestNode = {} for p in problems: best[p.label] = inf bestNode[p.label] = None def do(searcher, problem): nonlocal best, bestNode p = search.InstrumentedProblem(problem) goalNode = searcher(p) cost = goalNode.path_cost if cost < best[p.label]: best[p.label] = cost bestNode[p.label] = goalNode return p, cost table = [[search.name(s)] + [do(s, p) for p in problems] for s in searchers] print_table(table, header) print('----------------------------------------') for p in problems: bestPath = [] node = bestNode[p.label] while node != None: bestPath.append(node.state) node = node.parent summary = "Best Path for " + p.label + ": " for state in reversed(bestPath): try: summary += "\n" + p.prettyPrint(state) + "\n---------" except: summary += " " + state print(summary) print('----------------------------------------')
def makeMoveTable(game, states): header = [['state:', 'moves']] table = [] for state in states: row = [str(state) + ':'] moves = game.actions(state) moveString = str(moves) row.append(moveString) table.append(row) print_table(table, header)
def compare_searchers(problems, header, searchers=[]): best = {} bestNode = {} gradeInfo = {} for p in problems: best[p.label] = inf bestNode[p.label] = None def do(searcher, problem): nonlocal best, bestNode, gradeInfo ip = search.InstrumentedProblem(problem) try: ipLabel = ip.label if not ipLabel in gradeInfo: gradeInfo[ipLabel] = {} goalNode = searcher(ip) gradeInfo[ipLabel][searcher] = \ gradeProblem(searcher, ip, goalNode) cost = goalNode.path_cost if cost < best[ipLabel]: best[ipLabel] = cost bestNode[ipLabel] = goalNode return ip, cost except: # print('searcher(' + p.label + ') raised an exception:') # traceback.print_exc() return ip, inf table = [[search.name(s)] + [do(s, p) for p in problems] for s in searchers] print_table(table, header) print('----------------------------------------') for p in problems: bestPath = [] node = bestNode[p.label] while node != None: bestPath.append(node.state) node = node.parent summary = "Best Path for " + p.label + ": " ppFun = getattr(p, "prettyPrint", None) if ppFun == None: ppFun = str ppSep = ' ' pathLength = len(bestPath) if pathLength > 0: stateLength = len(ppFun(bestPath[0])) if pathLength * stateLength > 100: ppSep = "\n" for state in reversed(bestPath): summary += ppSep + ppFun(state) print(summary) print('----------------------------------------') return gradeInfo
def tryOne(label, frame): fit = gnb.fit(frame.data, frame.target) print('') print_table(fit.theta_, header=[frame.feature_names], topLeft=['Means:'], leftColumn=frame.target_names, numfmt='%6.3f', njust='center', tjust='rjust', ) y_pred = fit.predict(frame.data) print("Number of mislabeled points out of a total %d points : %d" % (len(frame.data), (frame.target != y_pred).sum()))
def makeABtable(game, states): topLeft = str(game)[1:-1] header = [[str(topLeft)]] maxChars = len(topLeft) for i in range(len(AB_searches)): header[0].append('AB(' + str(i) + ')') table = [] for state in states: row = [str(state)[:maxChars]] maxDepth = len(AB_searches) if hasattr(state, 'maxDepth'): maxDepth = min(maxDepth, state.maxDepth + 1) for abi in range(len(AB_searches)): if(abi > maxDepth): row.append(None) continue abSearch = AB_searches[abi] bestMove = abSearch(game, state) row.append(str(bestMove)) table.append(row) print_table(table, header, tjust='rjust')
def compare_searchers(problems, header, searchers=[]): best = {} bestNode = {} for p in problems: best[p.label] = inf bestNode[p.label] = None def do(searcher, problem): nonlocal best, bestNode p = search.InstrumentedProblem(problem) try: goalNode = searcher(p) cost = goalNode.path_cost if cost < best[p.label]: best[p.label] = cost bestNode[p.label] = goalNode return p, cost except: # print('searcher(' + p.label + ') raised an exception:') # traceback.print_exc() return p, inf table = [[search.name(s)] + [do(s, p) for p in problems] for s in searchers] print_table(table, header) print('----------------------------------------') for p in problems: bestPath = [] node = bestNode[p.label] while node != None: bestPath.append(node.state) node = node.parent summary = "Best Path for " + p.label + ": " for state in reversed(bestPath): try: summary += "\n" + p.prettyPrint(state) + "\n---------" except: summary += " " + str(state) print(summary) print('----------------------------------------')
def compare_searchers( games, header, # searchers=[] players=[]): best = {} bestNode = {} gradeInfo = {} for g in games: best[g.label] = -inf bestNode[g.label] = None # def do(searcher, game): def do(player, game): nonlocal best, bestNode, gradeInfo igame = gameSearch.InstrumentedGame(game) try: ipLabel = igame.label if not ipLabel in gradeInfo: gradeInfo[ipLabel] = {} # utility, bestMoves = searcher(igame) utility, bestMoves = player.search(igame) depth = len(bestMoves) game.predicted_utility = utility game.bestMoves = bestMoves if len(bestMoves) > 0: nextMove = bestMoves[0] else: nextMove = None # gradeInfo[ipLabel][searcher] = \ # gradeGame(searcher, igame, utility) gradeInfo[ipLabel][player] = \ gradeGame(player, igame, depth, utility) initial = igame.game.initial # utility = igame.utility(initial) if utility > best[ipLabel]: best[ipLabel] = utility bestNode[ipLabel] = initial return igame, utility, nextMove except Exception as e: if skipErrors: return igame, -inf, None print('Game "%s" raised an exception:' % g.label) traceback.print_exc() print(e) sys.exit(1) # table = [[utils.name(s)] + [do(s, p) for p in games] # for s in searchers] table = [[str(p)] + [do(p, g) for g in games] for p in players] print_table(table, header) print('----------------------------------------') for g in games: # bestPath = [] # node = bestNode[g.label] # while node != None: # bestPath.append(node.state) # node = node.parent summary = 'Best Moves for %s: %s' % (g.label, g.bestMoves) # ppFun = getattr(p, "prettyPrint", None) # if ppFun == None: # ppFun = str # ppSep = ' ' # pathLength = len(bestPath) # if pathLength > 0: # stateLength = len(ppFun(bestPath[0])) # if pathLength * stateLength > 100: # ppSep = "\n" # for state in reversed(bestPath): # summary += ppSep + ppFun(state) print(summary) print('----------------------------------------') return gradeInfo
print('Scores for: %s' % batch) maxScores[batch] = {} for pLabel in gradeInfo[batch]: table = [] maxScores[batch][pLabel] = newScores() for searcher in gradeInfo[batch][pLabel]: sLabel = utils.name(searcher) info = gradeInfo[batch][pLabel][searcher] scoreSet = info['score'] table.append(['%s, %s:' % (sLabel, pLabel), scoreList(scoreSet)]) for label in scoreSet: accumulator = accuMethods[label] maxScores[batch][pLabel][label] = accumulator( maxScores[batch][pLabel][label], scoreSet[label]) if len(table) > 1: table.append([ '%s summary:' % (pLabel), scoreList(maxScores[batch][pLabel]) ]) print_table(table) if len(table) > 1: print() for label in allScores: allScores[label] = max(allScores[label], maxScores[batch][pLabel][label]) realName = mod.name sl = [int(round(x)) for x in scoreList(allScores)] print(realName, 'summary:', str(sl)) print(realName, ' total:', str(min(100, sum(sl))))
def display(self, state): print_table(state.board, njust='center', sep=',') print('Score: ' + str(state.scores))
def display(self, state): print_table(state.board, njust='center', sep=',') print('Score: ' + str(state.scores))