def main(): w_0 = [.2, .5, 1] w_1 = [.1, .4, .7] w_2 = [.02, .04, .06] max0 = 0 w0_max = -1 w1_max = -1 w2_max = -1 for i in w_0: for j in w_1: for k in w_2: gameManager = GameManager() playerAI = PlayerAI() playerAI.w0 = i playerAI.w1 = j playerAI.w2 = k computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) v = gameManager.start() if v > max0: max0 = v w0_max = i w1_max = j w2_max = k print(max0) print(w0_max) print(w1_max) print(w2_max)
def learnModeGameOld(fileName='output_learning.csv'): tot = 0 trials = int(sys.argv[3]) if len(sys.argv) > 3 else 10 scores = [] max_heur = -float('inf') f = open(fileName, 'a') for _ in range(trials): playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start_training() f.write(playerAI.outstr) f.write('##### ' + str(maxTile) + '\n') scores.append(maxTile) print(maxTile) tot += math.log(maxTile, 2) max_heur = max(max_heur, playerAI.max_heur) f.close() print(tot / trials) print(sorted(scores, reverse=True)) print("Max value of heuristic: ", max_heur) print("percentage of 1024s: ", round(100 * scores.count(1024) / trials, 2), "%") print("percentage of 2048s: ", round(100 * scores.count(2048) / trials, 2), "%") print("percentage of 4096s: ", round(100 * scores.count(4096) / trials, 2), "%") print("percentage of 8192s: ", round(100 * scores.count(8192) / trials, 2), "%")
def main(): #gameManager.start() ft = FeatureTransformer() model = Model(ft) gamma = 0.99 lambda_ = 0.7 N = 500 totalrewards = np.empty(N) costs = np.empty(N) for n in range(N): gameManager = GameManager() playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) # eps = 1.0/(0.1*n+1) eps = 0.5 * (0.99**n) #eps = 0.5/np.sqrt(n+1) totalreward = gameManager.play_one(model, eps, gamma, lambda_) totalrewards[n] = totalreward #print("episode:", n, "total reward:", totalreward, "epsilon:", eps) print("avg reward for last 100 episodes:", totalrewards[-100:].mean()) print("total steps:", -totalrewards.sum())
def main(): runs = 0 # emptyVal: 9.7, smoothWeight: -0.05, maxWeight: 0.5 - 1024 weights = { 'monoWeight': 0, 'emptyWeight': 2.7, 'smoothWeight': 0, 'maxWeight': 0, 'medianWeight': 0, 'bottomNumsWeight': 0 } bestWeights = weights bestRunMax = 0 bestWeight = 0 while runs < 40: gameManager = GameManager() playerAI = PlayerAI(weights) computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) maxTile = gameManager.start() weights['emptyWeight'] = weights['emptyWeight'] + 0.5 if maxTile > bestRunMax: bestWeight = weights['emptyWeight'] bestRunMax = maxTile runs = runs + 1 print('Best run was: \n') print(maxTile) print('\n Best Weights were:') print(bestWeights)
def defaultGame(): playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start() print(maxTile)
def testModeGame(): tot = 0 trials = int(sys.argv[2]) if len(sys.argv) > 2 else 10 scores = [] max_heur = -float('inf') for _ in range(trials): playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start_no_disp() scores.append(maxTile) print(maxTile) tot += math.log(maxTile, 2) max_heur = max(max_heur, playerAI.max_heur) print(tot / trials) print(sorted(scores, reverse=True)) print("Max value of heuristic: ", max_heur) print("percentage of 1024s: ", round(100 * scores.count(1024) / trials, 2), "%") print("percentage of 2048s: ", round(100 * scores.count(2048) / trials, 2), "%") print("percentage of 4096s: ", round(100 * scores.count(4096) / trials, 2), "%") print("percentage of 8192s: ", round(100 * scores.count(8192) / trials, 2), "%")
def splitModeGameNew(trial_weights): num_trials = len(trial_weights) runs = int(sys.argv[2]) run_num = int(sys.argv[3]) filename = 'output_' + str(run_num) + '_of_' + str(runs) + '.txt' file = open(filename, 'w') file.write('RUN ' + str(run_num) + ' OF ' + str(runs) + '\n') file.write('TRIALS ' + str(num_trials * (run_num - 1) // runs) + ' TO ' + str(num_trials * run_num // runs) + ' OF ' + str(num_trials) + '\n\n') for weights in trial_weights[num_trials * (run_num - 1) // runs:num_trials * run_num // runs]: out_str = '' #print("weights: ", weights, " num: ", weights_as_int) trials = 20 scores = [] for _ in range(trials): playerAI = PlayerAI(weights) computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start_no_disp() scores.append(math.log(maxTile, 2)) out_str += str(maxTile) + ',' scores = sorted(scores, reverse=True) for score in scores: out_str += str(score) + ',' avg_score = sum(scores) * 2 / trials out_str += str(avg_score) + '\n' file.write(out_str) #print("weights: ", weights, " score: ", real_score) file.close()
def run(coefs): results = [] for i in range(20): gameManager = GameManager() playerAI = PlayerAI(coefs) computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) result = gameManager.start() print('result=', result) if result < 1024: print('Failed coefficients ', coefs) break # displayer.display(gameManager.grid) # playerAI.printEval(gameManager.grid) results.append(result) if len(results) >= 20: print('winner!', coefs) print('results', results) return {'coefs': coefs, 'results': results}
def splitModeGameOld(): num_vals_for_weights = 6 trial_weights_temp = list(range(pow(num_vals_for_weights, 3))) # trial_weights_temp = [73,103,121,122,133,140,151,163,169,177,187,191,193,201,212] trial_weights = [] for i in trial_weights_temp: weights = [0] * 4 i_copy = i for j in range(3): weights[j] = i % num_vals_for_weights i = i // num_vals_for_weights for n in range(2, num_vals_for_weights): for j in range(3): if weights[j] % n != 0: break else: break else: trial_weights.append(i_copy) num_trials = len(trial_weights) runs = int(sys.argv[2]) run_num = int(sys.argv[3]) filename = 'output_' + str(run_num) + '_of_' + str(runs) + '.txt' file = open(filename, 'w') file.write('RUN ' + str(run_num) + ' OF ' + str(runs) + '\n') file.write('TRIALS ' + str(num_trials * (run_num - 1) // runs) + ' TO ' + str(num_trials * run_num // runs) + ' OF ' + str(num_trials) + '\n\n') for i in trial_weights[num_trials * (run_num - 1) // runs:num_trials * run_num // runs]: out_str = '' weights_as_int = i out_str += str(weights_as_int) + ',' weights = [0] * 4 for j in range(3): weights[j] = i % num_vals_for_weights i = i // num_vals_for_weights out_str += str(weights[j]) + ',' #print("weights: ", weights, " num: ", weights_as_int) trials = 20 scores = [] for _ in range(trials): playerAI = PlayerAI(weights) computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start_no_disp() scores.append(math.log(maxTile, 2)) out_str += str(maxTile) + ',' scores = sorted(scores, reverse=True)[0:trials // 2] for score in scores: out_str += str(score) + ',' avg_score = sum(scores) * 2 / trials out_str += str(avg_score) + '\n' file.write(out_str) #print("weights: ", weights, " score: ", real_score) file.close()
def main(): playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile, maxHeuristic = gameManager.start() print(maxTile) print("Maximum Heuristic: {}".format(maxHeuristic))
def play(parameters): gameManager = GameManager() playerAI = PlayerAI(parameters) computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) return gameManager.start()
def __init__(self, size=4, playerAI=None, computerAI=None, displayer=None): self.grid = Grid(size) self.possibleNewTiles = [2, 4] self.probability = defaultProbability self.initTiles = defaultInitialTiles self.over = False # Initialize the AI players self.computerAI = computerAI or ComputerAI() self.playerAI = playerAI or PlayerAI() self.displayer = displayer or Displayer()
def main(): gameManager = GameManager() playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() sys.setrecursionlimit(10000) gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) gameManager.start()
def main(): gameManager = GameManager() playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) gameManager.start()
def main(): gameManager = GameManager() playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) silent_mode = True if len(sys.argv) > 1 else False gameManager.start(silent_mode)
def runProcess(a): gameManager = GameManager() playerAI = PlayerAI(a[1]) computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) res = gameManager.start() print('{:6} {:7}'.format(a[0], res), end='\r') return res, gameManager,
def main(): w = [100, 10] max_iterations = 2 average_score = 0 score_list = [] iter_list = [1, 10, 50, 100, 1000] # w = [10000, 1000, 10, 1500, 5000] max_score = 0 # [-1, (0, 0, 0, 0)] max_average = 0 # [-1, (0, 0, 0, 0)] f = open('output.txt', 'w') counter = 0 for w3 in iter_list: # itertools.product(iter_list, repeat=1): iterations = 0 while iterations < max_iterations: gameManager = GameManager() playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) score = gameManager.start(w[0], w[1], w3) max_score = max(max_score, score) average_score += max_score score_list.append(score) iterations += 1 counter += 1 f.write( str(counter) + " " + str(score) + " " + "[" + str(w[0]) + "," + str(w[1]) + "," + str(w3) + "]" + '\n') average_score /= max_iterations max_average = max(max_average, average_score) print(max_average, score_list) f.close()
def reinforceGame(): # Hyperparameters input_size = 21 num_layers = 3 num_hidden_units = 10 output_size = 1 learning_rate = 0.005 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = nets.NeuralNet(input_size, num_hidden_units, num_layers, output_size).to(device) if os.path.exists("reinforce.pt"): model.load_state_dict(torch.load('reinforce.pt')) print("Loaded reinforce.pt") tot = 0 trials = int(sys.argv[2]) if len(sys.argv) > 2 else 10 scores = [] max_heur = -float('inf') criterion = nn.MSELoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) playerAI = PlayerAIReinforce(model, criterion, optimizer, device) for _ in range(trials): computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start_reinforce() scores.append(maxTile) print(maxTile) tot += math.log(maxTile, 2) max_heur = max(max_heur, playerAI.max_heur) print(tot / trials) print(sorted(scores, reverse=True)) print("Max value of heuristic: ", max_heur) print("percentage of 1024s: ", round(100 * scores.count(1024) / trials, 2), "%") print("percentage of 2048s: ", round(100 * scores.count(2048) / trials, 2), "%") print("percentage of 4096s: ", round(100 * scores.count(4096) / trials, 2), "%") print("percentage of 8192s: ", round(100 * scores.count(8192) / trials, 2), "%") torch.save(playerAI.model.state_dict(), 'reinforce.pt')
def main(): results = 0 maxTiles = [] for i in range(1, 11): gameManager = GameManager() playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() print("Round {}".format(i)) gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) gameManager.start() displayer.display(gameManager.grid) results += gameManager.maxTile maxTiles.append(gameManager.maxTile) top5Tiles = sorted(maxTiles, reverse=True)[:5] print("Average Score {}".format(results / 10)) print("Average Top 5 Score {}".format(sum(top5Tiles) / 5))
def main(): for i in range(10): output = open("output{}.txt".format(i), "w") gameManager = GameManager() playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) result = gameManager.start() output.write('round {}: {}'.format(i, result)) output.close()
def run10(): i = 1 while i <= 10: print("Run #{}".format(i)) gameManager = GameManager() playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) gameManager.start() i += 1
def main(): playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() num8192 = 0 num4096 = 0 num2048 = 0 num1024 = 0 num512 = 0 num256 = 0 numFailed = 0 for i in range(5): gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start() print("TEST ", i, ": ", maxTile) if gameManager.TimedOut: numFailed += 1 if maxTile == 256: num256 += 1 elif maxTile == 512: num512 += 1 elif maxTile == 1024: num1024 += 1 elif maxTile == 2048: num2048 += 1 elif maxTile == 4096: num4096 += 1 elif maxTile == 8192: num8192 += 1 print("NUM FAILED: ", numFailed) print("NUM 256: ", num256) print("NUM 512: ", num512) print("NUM 1024: ", num1024) print("NUM 2048: ", num2048) print("NUM 4096: ", num4096) print("NUM 8192: ", num8192)
def main(): gamma = 0.9 copy_period = 50 D = 16 #?? K = len(actionDic) sizes = [256,256] model = DQN(D, K, sizes, gamma) tmodel = DQN(D, K, sizes, gamma) init = tf.global_variables_initializer() session = tf.InteractiveSession() session.run(init) model.set_session(session) tmodel.set_session(session) N = 500 totalrewards = np.empty(N) costs = np.empty(N) for n in range(N): gameManager = GameManager() playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) #eps = 1.0/np.sqrt(n+1) eps = 0.1*(0.99**n) totalreward = gameManager.play_one(model, tmodel, eps, gamma, copy_period) totalrewards[n] = totalreward if n % 50 == 0: print("episode:", n, "total reward:", totalreward, "eps:", eps, "avg reward (last 50):", totalrewards[max(0, n-50):(n+1)].mean())
def __init__(self): self.counter = 0 self.displayer = Displayer()
def main(): if len(sys.argv) < 2: playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start() maxHeur = -1 * float("inf") maxHeur = max(maxHeur, playerAI.max_heur) print("Max Tile: {} Score: {}".format(maxTile, playerAI.high_score)) print("Max Heuristic value:", maxHeur) print("Weights used:", playerAI.weights) print("Max depth:", playerAI.max_depth) print("Lowest probability of reaching:", playerAI.prob) elif sys.argv[1] == 'd': playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxHeur = -1 * float("inf") maxTile = gameManager.start() maxHeur = max(maxHeur, playerAI.max_heur) print("Max tile:", maxTile) print("Max Heuristic value:", maxHeur) print("Weights used:", playerAI.weights) elif sys.argv[1] == 't': tot = 0 trials = int(sys.argv[2]) if len(sys.argv) > 2 else 10 scores = [] max_heur = -float('inf') max_high_score = 0 for _ in range(trials): playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start_no_disp() memo_dict = playerAI.memo scores.append(maxTile) print("Max Tile: {} Score: {}".format(maxTile, playerAI.high_score)) tot += math.log(maxTile, 2) max_heur = max(max_heur, playerAI.max_heur) max_high_score = max(playerAI.high_score, max_high_score) print(tot / trials) print(sorted(scores, reverse=True)[0:5]) print("Max value of heuristic: ", max_heur) print("Highest score achieved: ", max_high_score) print("number of 1024s: ", scores.count(1024)) print("number of 2048s: ", scores.count(2048)) print("number of 4096s: ", scores.count(4096)) print("number of 8192s: ", scores.count(8192)) print("Weights used:", playerAI.weights) elif sys.argv[1] == 's': num_vals_for_weights = 6 trial_weights_temp = list(range(pow(num_vals_for_weights, 3))) trial_weights = [] for i in trial_weights_temp: weights = [0] * 4 i_copy = i for j in range(3): weights[j] = i % num_vals_for_weights i = i // num_vals_for_weights for n in range(2, num_vals_for_weights): for j in range(3): if weights[j] % n != 0: break else: break else: trial_weights.append(i_copy) num_trials = len(trial_weights) runs = int(sys.argv[2]) run_num = int(sys.argv[3]) filename = 'output_' + str(run_num) + '_of_' + str(runs) + '.txt' file = open(filename, 'w') file.write('RUN ' + str(run_num) + ' OF ' + str(runs) + '\n') file.write('TRIALS ' + str(num_trials * (run_num - 1) // runs) + ' TO ' + str(num_trials * run_num // runs) + ' OF ' + str(num_trials) + '\n\n') for i in trial_weights[num_trials * (run_num - 1) // runs:num_trials * run_num // runs]: out_str = '' weights_as_int = i out_str += str(weights_as_int) + ',' weights = [0] * 4 for j in range(3): weights[j] = i % num_vals_for_weights i = i // num_vals_for_weights out_str += str(weights[j]) + ',' #print("weights: ", weights, " num: ", weights_as_int) trials = 20 scores = [] for _ in range(trials): playerAI = PlayerAI(weights) computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start_no_disp() scores.append(math.log(maxTile, 2)) out_str += str(maxTile) + ',' scores = sorted(scores, reverse=True)[0:trials // 2] for score in scores: out_str += str(score) + ',' avg_score = sum(scores) * 2 / trials out_str += str(avg_score) + '\n' file.write(out_str) file.close() # CMA-ES elif sys.argv[1] == 'c': generations = 40 runs = 5 samples = 20 # Generate a random set of weight combinations and their mean weight_combinations, means = CMAES.generate_data(0, 2000, samples=samples) combinations = np.array(weight_combinations) print("CMA-ES") print("Generations:", generations) print("Runs per generation:", runs) print("Samples: {} \n".format(samples)) # transpose so that each row is a weight combination combinations = combinations.transpose() best_comb = [] best_avg_comb = [] best_avg_score = 0 max_tile = 0 best_score = 0 best_weights = [] good_gen = -1 good_avg_gen = -1 # perform CMA-ES for generation in range(generations): # hash map of avg score which maps to weight combinations tracker = {} comb = 1 # track which combination we are on gen_avg_score = 0 # Use weight combinations(for loop iterating for weight combinations) for combination in combinations: avg = 0 weights = [heur_weight for heur_weight in combination] for run in range(runs): # initialization playerAI = PlayerAI(weights=weights) computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start_no_disp() memo_dict = playerAI.memo # track run results curr_high_score = playerAI.high_score avg += curr_high_score # keep track of highest performance if best_score < curr_high_score: best_score = curr_high_score max_tile = maxTile best_weights = weights good_gen = generation avg /= runs # best average tracking if avg > best_avg_score: best_avg_score = avg best_avg_comb = combination good_avg_gen = generation # generation average score tracking gen_avg_score += avg tracker[avg] = weights print("Combination {} of {}".format(comb, samples)) print("Weights:", weights) print("Average Score: {}\n".format(avg)) comb += 1 gen_avg_score /= samples # 3. take 25 % best average and generate gaussian distribution best = round(0.25 * samples) cov_matrix, new_means = CMAES.generate_next_generation_data( tracker, means, best_samples=best) # 4. take N new samples and repeat the process new_data = CMAES.generate_normal_distribution( means=new_means, cov_matrix=cov_matrix, samples=samples) # put new parameters in place means = new_means combinations = new_data # print results from generation print("\nGeneration {} of {} finished".format( generation + 1, generations)) print("Generation average:", gen_avg_score) check = cov_matrix[0][0] + cov_matrix[1][1] + \ cov_matrix[2][2] + cov_matrix[3][3] print("Current variance among weights: {} \n".format(check)) # current best print("Current Best Results") print("Max tile: {}\nHigh Score: {}".format(max_tile, best_score)) print("Best Weight Combination:", best_weights) print("Generation:", good_gen + 1) # current best average print("\nCurrent Best Average Results") print("Best average score", best_avg_score) print("Best Average Weight Combination:", best_avg_comb) print("Generation: {}\n".format(good_avg_gen + 1)) # check for convergence if check < 5: print("Weights have converged") break print("Final Generation:\n {} ".format(combinations)) CMAES.visualize_covariance_matrix(cov_matrix) print("\nCMA-ES finished") else: playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start() print(maxTile) # ''' # ''' '''
class PlayerAI(BaseAI): def __init__(self): self.counter = 0 self.displayer = Displayer() def getMove(self, grid): # sys.setrecursionlimit(1500) moves = grid.getAvailableMoves() copyGrid = grid.clone() # move = self.decision(copyGrid) # return moves[randint(0, len(moves) - 1)] if moves else None self.counter += 1 print("counter ", self.counter) result = self.decision(copyGrid) print("max utility", result[1]) return result[0] def eval(self, grid): result = grid.getMaxTile() + len( grid.getAvailableCells()) + grid.getCellValue( [0, 0]) + self.meanOfFields(grid) # print('eval ', result) return result # # def evaluate(self, grid): # # # return grid.getcellValue([0,0]) # # print(type(grid)) # # return len(grid.getAvailableCells() * -1) def meanOfFields(self, grid): fieldsSum = 0 result = 0 for i in range(grid.size): for j in range(grid.size): fieldsSum += grid.getCellValue([i, j]) return fieldsSum / (grid.size * grid.size) def decision(self, grid): maxState = self.maximize(grid, -math.inf, math.inf) return maxState def maximize(self, grid, alpha, beta): if self.terminalTest(grid): return None, self.eval(grid) maxMove = None maxUtility = -math.inf availableMoves = grid.getAvailableMoves() for move in availableMoves: copyGrid = grid.clone() copyGrid.move(move) # minimum = self.minimize(copyGrid) # grid.move(move) minimum = self.minimize(copyGrid, alpha, beta) if minimum[1] >= maxUtility: maxMove = move maxUtility = minimum[1] if maxUtility >= beta: return maxMove, maxUtility if maxUtility > alpha: alpha = maxUtility return maxMove, maxUtility def minimize(self, grid, alpha, beta): if self.terminalTest(grid): print("terminal state reached with following grid") print(f'alpha {alpha} and beta {beta}') print("utility", self.eval(grid)) self.displayer.display(grid) print("---------------------------------------------------------") return None, self.eval(grid) minMove = None minUtility = math.inf cells = grid.getAvailableCells() availableCells = grid.getAvailableCells() for cell in availableCells: copyGrid = grid.clone() copyGrid.setCellValue(cell, 2) # grid.setCellValue(cell,2) maximum = self.maximize(copyGrid, alpha, beta) if maximum[1] <= minUtility: minMove = cell minUtility = maximum[1] if minUtility <= alpha: return minMove, minUtility if minUtility < beta: beta = minUtility return minMove, minUtility def terminalTest(self, grid): availableMoves = grid.getAvailableMoves() if len(availableMoves) == 0: return True else: return False
def main(): player = PlayerAI() testGrid = Grid() displayer = Displayer() displayer.display(testGrid) print(player.findPairs(testGrid)) testGrid.setCellValue([0, 0], 3) displayer.display(testGrid) print(player.findPairs(testGrid)) testGrid.setCellValue([0, 3], 3) displayer.display(testGrid) print(player.findPairs(testGrid)) testGrid.setCellValue([2, 3], 3) displayer.display(testGrid) print(player.findPairs(testGrid)) testGrid.setCellValue([0, 1], 3) displayer.display(testGrid) print(player.findPairs(testGrid)) testGrid.setCellValue([0, 2], 3) displayer.display(testGrid) print(player.findPairs(testGrid)) testGrid.setCellValue([1, 1], 3) displayer.display(testGrid) print(player.findPairs(testGrid)) #main()
def test_can_run_game(self): sut = GameBuilder().with_displayer(Displayer()).build() sut.start()
from Grid_3 import Grid from ComputerAI_3 import ComputerAI from PlayerAI_3 import PlayerAI from Displayer_3 import Displayer from GameManager_3 import * import time import random f= open("results.txt","a") i = 0 result = [] while i <= 30: playerAI = PlayerAI() computerAI = ComputerAI() displayer = Displayer() gameManager = GameManager(4, playerAI, computerAI, displayer) maxTile = gameManager.start() result.append(maxTile) f.write(str(maxTile)) f.write("\n") i+=1 print(result)
def __init__(self): super().__init__() self.displayers = [Displayer(), LogDisplayer()]