import numpy as np from game2048.game import Game from game2048.agents import ExpectiMaxAgent from game2048.agents import MyAgent from game2048.displays import Display import csv import os game_size = 4 score_to_win = 2048 iter_num = 3000 game = Game(game_size, score_to_win) board = game.board agenta = ExpectiMaxAgent(game, Display()) agentb = MyAgent(game, Display()) directiona = agenta.step() directionb = agentb.step() board = game.move(directionb) i = 0 dic = {} idx = 0 # save file filename = '/home/olivia/PycharmProjects/2048/game2048/data/traindata10.csv' if os.path.exists(filename): start = True else: start = False os.mknod(filename)
"direction": direction, "control": control}) return app if __name__ == "__main__": GAME_SIZE = 4 SCORE_TO_WIN = 2048 APP_PORT = 5005 APP_HOST = "localhost" from game2048.game import Game game = Game(size=GAME_SIZE, score_to_win=SCORE_TO_WIN) try: from game2048.agents import ExpectiMaxAgent,MyAgent agent = MyAgent(game=game) except: from game2048.agents import RandomAgent print("WARNING: Please compile the ExpectiMaxAgent first following the README.") print("WARNING: You are now using a RandomAgent.") agent = RandomAgent(game=game) print("Run the webapp at http://<any address for your local host>:%s/" % APP_PORT) app = get_flask_app(game, agent) app.run(port=APP_PORT, threaded=False, host=APP_HOST) # IMPORTANT: `threaded=False` to ensure correct behavior
if __name__ == '__main__': GAME_SIZE = 4 SCORE_TO_WIN = 2048 N_TESTS = 50 '''==================== Use your own agent here.''' from game2048.agents import MyAgent as TestAgent '''====================''' size = 4 score_to_win = 2048 # 59*16*12 game = Game(size, score_to_win) agent = TestAgent(game, display=Display()) scores = [] agent.net7.load_state_dict( torch.load("net_3_params_7.pkl", map_location='cpu')) agent.net6.load_state_dict( torch.load("net_3_params_6.pkl", map_location='cpu')) agent.net5.load_state_dict( torch.load("net_3_params_5.pkl", map_location='cpu')) agent.net4.load_state_dict( torch.load("net_3_params_4.pkl", map_location='cpu')) agent.net2.load_state_dict( torch.load("net_3_params_2.pkl", map_location='cpu')) step = 0 for i in range(N_TESTS):
from game2048.game import Game from game2048.displays import Display, IPythonDisplay from game2048.agents import Agent, RandomAgent, ExpectiMaxAgent, MyAgent import numpy as np import pandas as pd num = 10000 i = 0 results = [] direction = [] while i < num: game = Game(4, score_to_win=2048, random=False) agent_exp = ExpectiMaxAgent(game) agent = MyAgent(game) while (game.score <= 1024) and (not game.end): A = game.board A[A == 0] = 1 A = np.log2(A) A = np.int32(A) A = A.reshape(16) dir = agent.step() # you can change the condition to get different data if game.score >= 512: dir_exp = agent_exp.step() results.append(A) direction.append(dir_exp) game.move(dir) if 0 == i % 100: # save the result every 100 games results = np.array(results) direction = np.array(direction)
# Device configuration device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Hyper-parameters num_classes = 4 if __name__ == '__main__': # load dataset dataset_path = '../data/' dataset_name = dataset_path + 'data_train.csv' model = ConvNet(num_classes).to(device) # create model model_name = 'checkpoint_test.ckpt' torch.save(model.state_dict(), model_name) print('save model to {}.'.format(model_name)) # random initialize data set print('data initalizing ...') datagen = data.dataprocess.Data(display=None, model_path=model_name) path = dataset_name score = 0 # test net structure close to 180(random) is better. for _ in range(100): game = Game(size=4, score_to_win=2048) agent = MyAgent(game, display=None, model_path=model_name) agent.play() score += np.sum(game.board) print("Average total score @50 times is :{:.1f}".format(score / 50))