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 test_against_dumb_move_2(self): """Take from a real gameplay scenarios where a fatal wrong move was made: starting position: 128 32 8 4 32 2 128 16 0 2 8 8 0 4 4 8 position after going down (which should not be the preferred choice) 0 0 8 0 0 32 128 4 128 4 8 16 32 4 4 16 """ p = PlayerAI() g1 = self.create_slowgrid_from_list([[128, 32, 8, 4], [32, 2, 128, 16], [0, 2, 8, 8], [0, 4, 4, 8]]) m = p.getMove(g1) self.assertNotEqual(m, DOWN, 'should not have chosen move Down')
def test_against_dumb_move_1(self): """Take from a real gameplay scenarios where a fatal wrong move was made: Computer's turn: 256 32 128 2 4 16 16 64 32 4 2 2 4 0 4 4 Player's Turn:DOWN (should have been RIGHT) 256 0 128 2 4 32 16 64 32 16 2 2 4 4 4 4 RIGHT: [ [256, 32, 128, 2], [0, 4, 32, 64], [0, 32, 4, 4], [0, 0, 4, 8] ] """ p = CompositeCalculation.CompositeUtilityCalculator() gstart = self.create_fastgrid_from([256, 32, 128, 2, 4, 16, 16, 64, 32, 4, 2, 2, 4, 0, 4, 4]) gDOWN = self.create_fastgrid_from([256, 0, 128, 2, 4, 32, 16, 64, 32, 16, 2, 2, 4, 4, 4, 4]) ustart = p.compute_utility(gstart) udown = p.compute_utility(gDOWN) self.assertGreater(ustart, udown) gright = gstart.clone() gright.move(RIGHT) uright = p.compute_utility(gright) self.assertGreater(uright, udown) available_moves = gstart.moves self.assertNotEquals(1, len(available_moves)) p1 = PlayerAI() suggestedMove = p1.getMove(gstart.to_slowgrid()) self.assertNotEquals(suggestedMove, DOWN)
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 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 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 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 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 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() gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) gameManager.start()
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(): 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 test_against_dumb_move_3(self): """Take from a real gameplay scenarios where a fatal wrong move was made: Computer's turn: 32 16 32 2 4 16 4 2 4 2 8 2 2 4 8 2 Player's Turn:DOWN (should have been UP) 0 0 0 0 32 32 32 0 8 2 4 4 2 4 16 4 UP would have been: 32 32 32 4 8 2 4 4 2 4 16 0 0 0 0 0 """ p = PlayerAI() gstart = self.create_fastgrid_from([32, 16, 32, 2, 4, 16, 4, 2, 4, 2, 8, 2, 2, 4, 8, 2]) ok, gdown = gstart.move(DOWN) ok, gup = gstart.move(UP) ce = CompositeUtilityCalculator() ustart = ce.compute_utility(gstart) udown = ce.compute_utility(gdown) uup = ce.compute_utility(gup) self.assertGreater(ustart, udown) self.assertGreater(uup, udown, "the UP move should have a higher score than the DOWN move") available_moves = gstart.moves self.assertNotEquals(1, len(available_moves), "there are two options (UP or DOWN)") chosen_move = p.getMove(gstart.to_slowgrid()) self.assertNotEquals(chosen_move, DOWN, "down is the inferior move, and should not have been chosen")
def training_task(individual, i=None): gameManager = GameManager() playerAI = PlayerAI(heuristic=individual) computerAI = ComputerAI() displayer = BaseDisplayer( ) # Dummy. we don't want to have anything displayed. gameManager.setDisplayer(displayer) gameManager.setPlayerAI(playerAI) gameManager.setComputerAI(computerAI) fitness = gameManager.start() return fitness, individual, i
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(): 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(): 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.grid = Grid() self.game_manager = GameManager() self.playerAI = PlayerAI() self.computerAI = ComputerAI() self.displayer = BaseDisplayer()
current_value = self.__log2(grid.map[row][current_col]) next_value = self.__log2( grid.map[row][grid.size - 1 if next_col == grid.size else next_col]) if (current_value < next_value): monotonicity[RIGHT] += current_value - next_value else: monotonicity[LEFT] += next_value - current_value current_col = next_col return max(monotonicity[UP], monotonicity[DOWN]) + max( monotonicity[LEFT], monotonicity[RIGHT]) if __name__ == '__main__': from PlayerAI_3 import PlayerAI from Grid_3 import Grid player_ai = PlayerAI() grid = Grid() grid.map = [[0, 0, 2, 4], [0, 0, 2, 4], [0, 2, 2, 2], [0, 2, 2, 1024]] # Test case for PlayerAI.__maximize: utility = player_ai._PlayerAI__maximize(grid, 3, 0, 0) print("[Maximized Utility]: {}".format(utility)) # Test case for PlayerAI.__eval: utility = player_ai._PlayerAI__eval(grid) print("[Utility]: {}".format(utility))
from random import randint from BaseAI_3 import BaseAI from Grid_3 import Grid from PlayerAI_3 import PlayerAI grid = Grid() grid.map = [[4, 64, 8, 4], [32, 8, 64, 8], [256, 512, 32, 4], [4, 8, 4, 2]] moves = [1, 2, 3, 4] print(moves) moves[2] = 7 print(moves) print(len(grid.getAvailableCells())) print(grid.map) print(PlayerAI.is_state_terminal(grid, 'min'))
def create_player(self) -> PlayerAI: return PlayerAI()
print(f"new best weights {best_weights}") with open("previous_best_weight.txt", "w+") as f: f.write(str(best_weights)) f.close() if not children_to_try: manager.init_offspring(player, children_to_try, best_weights) manager.try_again(browser) if __name__ == "__main__": browser = None try: player = PlayerAI() path = os.path.dirname(os.path.abspath(__file__)) browser = webdriver.Chrome(ChromeDriverManager().install()) browser.get(os.path.join(path, "index.html")) manager = Board() body = browser.find_element_by_tag_name("body") play_2028(player, manager, browser, body) except KeyboardInterrupt: if browser != None: browser.quit() except Exception as e: print("An error occured: ")
def test_can_create_player_a_i(self): sut = PlayerAI() self.assertIsNotNone(sut)