Пример #1
0
def data_generator_for_CRNN(score_to_begin, score_to_win, batch_size):
    datas = []
    labels = []
    cnt = 0
    while 1:
        game = Game(score_to_win = score_to_win, random = False)
        agent = ExpectiMaxAgent(game)
        while game.end == 0:
            step = agent.step()
            if game.score >= score_to_begin:
                board = board2array(game)
                board1 = np.swapaxes(board, 1, 2)
                board2 = np.swapaxes(board1, 0, 1).reshape((16, 4, 4, 1))
                
                datas.append(board2)
                labels.append(step2array(step))
                cnt += 1
            game.move(step)
            if cnt == batch_size:
                cnt = 0
                datas = np.array(datas)
                labels = np.array(labels)
                yield (datas, labels)
                datas = []
                labels = []
def testAgent(n_tests, game_size, score_to_win, model, max_iter=1000):
    acc, total = 0, 0
    for i in trange(n_tests):
        game, n_iter = Game(game_size, score_to_win), 0
        target = ExpectiMaxAgent(game)
        while n_iter < max_iter and not target.game.end:
            dir_ = Greedy_Action(game, model)
            target_dir = target.step()
            n_iter += 1
            total += 1
            if dir_ == target_dir:
                acc += 1
            target.game.move(dir_)
    return acc / total
Пример #3
0
def data_generator_for_CNN(score_to_begin, score_to_win, batch_size):
    datas = []
    labels = []
    cnt = 0
    while 1:
        game = Game(score_to_win = score_to_win, random = False)
        agent = ExpectiMaxAgent(game)
        while game.end == 0:
            step = agent.step()
            if game.score >= score_to_begin:
                datas.append(board2array(game))
                labels.append(step2array(step))
                cnt += 1
            game.move(step)
            if cnt == batch_size:
                cnt = 0
                datas = np.array(datas)
                labels = np.array(labels)
                yield (datas, labels)
                datas = []
                labels = []
Пример #4
0
def data_generator(batch_size):
    datas = []
    labels = []
    cnt = 0
    while 1:
        game = Game(score_to_win = 2048, random = False)
        agent = ExpectiMaxAgent(game)
        while game.end == 0:
            step = agent.step()
            board = game.board / 11
            board1 = board.T
            datas.append(np.hstack((board, board1)))
            labels.append(step2array(step))
            cnt += 1
            game.move(step)
            if cnt == batch_size:
                cnt = 0
                datas = np.array(datas)
                labels = np.array(labels)
                yield (datas, labels)
                datas = []
                labels = []
Пример #5
0
# #        print ("direction: ", direction)
#         for i in range(4):
#             for j in range(4):
#                 #f.write(game.board[i,j])
#                 print(game.board[i, j], file = f2)
#         print(direction, file = f2)
#         #f.write(direction)

#         game.move(direction)

#     #f.write('\n')

for i in range(300):
    print("i = ", i)
    game = Game(size=GAME_SIZE, score_to_win=SCORE_TO_WIN0)
    agent = ExpectiMaxAgent(game=game)
    while True:
        direction = agent.step()
        if (game.end != 0):
            break
#        print (game.board)
#        print ("direction: ", direction)
        if game.board.max() < 256:

            for i in range(4):
                for j in range(4):
                    #f.write(game.board[i,j])
                    print(game.board[i, j], file=f1)
            print(direction, file=f1)

        elif game.board.max() < 512:
Пример #6
0
        direction = -1
        control = "USER"
        if request.method == "POST":
            direction = request.json
            if direction == -1:
                direction = agent.step()
                control = 'AGENT'
            game.move(direction)
        return jsonify({"board": game.board.tolist(),
                        "score": game.score,
                        "end": game.end,
                        "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
    from game2048.agents import ExpectiMaxAgent

    game = Game(size=GAME_SIZE, score_to_win=SCORE_TO_WIN)
    agent = ExpectiMaxAgent(game=game)
    app = get_flask_app(game, agent)
    app.run(port=APP_PORT, threaded=False, host=APP_HOST)  # IMPORTANT: `threaded=False` to ensure correct behavior
Пример #7
0
from game2048.game import Game
from game2048.displays import Display, IPythonDisplay
from game2048.agents import Agent, RandomAgent, ExpectiMaxAgent

for i in range(0, 1):
    game = Game(4, random=False, score_to_win=2048)

    agent = ExpectiMaxAgent(game)
    agent.play(verbose=False, save=True, savedir='data/2048.txt')
    print(str(i + 1) + "th game completed")
Пример #8
0
display1 = Display()
display2 = IPythonDisplay()


def log2(board):
    for i in range(16):
        if board[i] != 0:
            board[i] = np.log2(board[i])
    return board


for i in range(0, 100):
    print(i, "is running")
    game = Game(4, 2048, random=False)
    agent = ExpectiMaxAgent(game, display=display2)
    n_iter = 0
    max_iter = np.inf
    data = np.zeros((0, 17), dtype=float)
    while (n_iter < max_iter) and (not game.end):
        arr1 = log2(np.reshape(agent.game.board, newshape=(16, )))
        direction = agent.step()
        arr3 = np.hstack((arr1, direction))
        data = np.vstack([data, arr3])
        agent.game.move(direction)
        n_iter += 1
    df = pd.DataFrame(data, columns=None, index=None)
    df.to_csv('/home/huanning/big_project/2048-api-master/data_test.csv',
              index=0,
              mode='a',
              header=0)
Пример #9
0
import sys

sys.path.append("..")
from game2048.expectimax import board_to_move
from game2048.game import Game
from game2048.agents import ExpectiMaxAgent
from game2048.displays import Display
import csv, os

GAME_SIZE = 4
SCORE_TO_WIN = 2048
iter_num = 300

game = Game(GAME_SIZE, SCORE_TO_WIN)
board = game.board
agent = ExpectiMaxAgent(game, Display())
direction = agent.step()
board = game.move(direction)

i = 0
dic = {}
idx = 0

# ------------------------------------------------------
# save each board and its direction to a dict
# -------------------------------------------------------
filename = '/home/zhouykai/Workspace/MachinceLearning/Dataset_2048/Train.csv'
# filename = './Dataset/Train.csv'
if os.path.exists(filename):
    head = True
else:
Пример #10
0
model_256 = load_model('myAgent_256.h5')
model_512 = load_model('myAgent_512.h5')
model_1024 = load_model('myAgent_1024.h5')

boards_256 = []
boards_512 = []
boards_1024 = []
directions_256 = []
directions_512 = []
directions_1024 = []

for i in range(30000):
    print('i = ', i)
    game = Game(size=4)
    expectiMaxAgent = ExpectiMaxAgent(game=game)
    while True:  # one turn
        rightDirection = expectiMaxAgent.step()
        if (game.end == True):  # game over
            break
        maxNum = 0
        for p in range(4):
            for q in range(4):
                if game.board[p, q] > maxNum:
                    maxNum = game.board[p, q]
        if maxNum == 2048:  # start the next turn
            break
        inputboard = np.zeros((1, 4, 4, 16))
        for p in range(4):
            for q in range(4):
                num = game.board[p, q]
Пример #11
0
import numpy as np
from sklearn.model_selection import train_test_split

BATCH_SIZE = 128
NUM_CLASSES = 4
NUM_EPOCHS = 20

display1 = Display()
display2 = IPythonDisplay()
model = keras.models.load_model('model.h5')

image = []
label = []
for i in range(0, 10):
    game = Game(4, score_to_win=2048, random=False)
    agent = ExpectiMaxAgent(game, display=display1)

    while game.end == False:

        direction = agent.step()
        image.append(game.board)
        label.append(direction)
        game.move(direction)

    display1.display(game)
#运行10次游戏并记录棋盘和方向
x_train = np.array(image)
y_train = np.array(label)

x_train = np.log2(x_train + 1)
x_train = np.trunc(x_train)
Пример #12
0
def board_log2(board):
    for i in range(16):
        if board[i] != 0:
            board[i] = np.log2(board[i])
    return board


# Set the needed number of games
# Limit the max number of steps given by Expectimax each game
NUM_GAMES = 10000
MAX_ITER = 1e3

# This loop takes really a long time
# Use trange to monitor the execution status
for i in trange(NUM_GAMES):
    game = Game(size=4, score_to_win=2048)
    target = ExpectiMaxAgent(game)
    n_iter = 0
    data = np.zeros((0, 17), dtype=float)
    # To avoid frequent I/O operation,
    # data is written into .csv after an entire game rather than one step
    while n_iter < MAX_ITER and not target.game.end:
        dir_ = target.step()
        x = board_log2(np.reshape(target.game.board, newshape=(16, )))
        item = np.hstack((x, dir_))
        data = np.vstack([data, item])
        target.game.move(dir_)
        n_iter += 1
    df = pd.DataFrame(data, columns=None, index=None)
    df.to_csv('./Data_Compressed.csv', mode='a', index=False, header=False)
# print(df)
Пример #13
0
from game2048.game import Game
from game2048.displays import Display, IPythonDisplay
from game2048.agents import Agent, RandomAgent, ExpectiMaxAgent,YourOwnAgent,SimpleNet
import numpy as np
import pandas as pd

display1 = Display()
display2 = IPythonDisplay()

%%time

for i in range(0,3000):
    print(i)
    game = Game(4, score_to_win=2048, random=False)
    #display2.display(game)
    agent = ExpectiMaxAgent(game, display=display2)
    max_iter=np.inf
    n_iter = 0
    
    while (n_iter < max_iter) and (not game.end):
        tmp1 = game.board.reshape(1,16)
        direction = np.array(agent.step())
        tmp3 = direction.reshape(1,1)

        a = np.hstack((tmp1,tmp3))
        
        
        n_iter += 1
        agent.game.move(direction)
        df1 = pd.DataFrame((a))
        df1.to_csv('lastdata.xls',index=0,mode='a',header=0)
Пример #14
0
        weak_agent.game.reset()
        # record board between min and max score
        # maybe cannot gennerate min score, lower it by mul 0.6
        while (not weak_agent.game.end) and (weak_agent.game.score <=
                                             board_max):
            if weak_agent.game.score > board_min * 0.6:
                buffer.append(weak_agent.game.board)
            direction = weak_agent.step()
            weak_agent.game.move(direction)
        # return a batch data
        while len(buffer) > batch_size:
            yield (buffer[:batch_size])
            buffer = buffer[batch_size:]


if __name__ == "__main__":
    tmpGame = Game(size=4, enable_rewrite_board=True)
    expertAgent = ExpectiMaxAgent(tmpGame)

    myAgent = myOwnAgent(game=tmpGame,
                         maxPower=16,
                         modelLevel=[256, 512, 900],
                         firstTime=False)

    myAgent.train(expertAgent,
                  train_batch_size=32,
                  batches_round=1000,
                  epoch_per_train=10,
                  max_loop=30)

    myAgent.save_model()
Пример #15
0
boards = []
directions = []

while count < 98:
    score_train = []
    score_test = []
    boards = []
    directions = []
    count = 0

    print('array deleted')

    for i in range(30):
        game = Game(4, score_to_win=2048, random=False)
        agent1 = ExpectiMaxAgent(game, display=display1)

        while game.end == False:
            if np.sum(game.board) > 384:
                break
            a = np.array(game.board)
            a = np.log2(a + 1)
            a = np.trunc(a)
            a = keras.utils.to_categorical(a, board_class)
            a = a.reshape(1, 4, 4, board_class)
            prediction = model.predict(a, batch_size=128)
            b = prediction[0]
            b = b.tolist()
            direction2 = b.index(max(b))
            direction1 = agent1.step()
Пример #16
0
from keras.optimizers import Adam
import numpy as np
from sklearn.model_selection import train_test_split

display1 = Display()
display2 = IPythonDisplay()
model = load_model('2048_new2_2048.h5')
image = []
label = []
#清空棋盘和方向的矩阵
count1 = 0
while count1 < 1:
    count = 0
    for j in range(0, 50):
        game = Game(4, score_to_win=1024, random=False)
        agent1 = ExpectiMaxAgent(game)
        #agent2 = MyOwnAgent(game, display=display1)
        while game.end == False:
            direction1 = agent1.step()  #用强Agent判断方向

            x = np.array(game.board)

            x = np.log2(x + 1)
            x = np.trunc(x)
            x = keras.utils.to_categorical(x, 12)
            x = x.reshape(1, 4, 4, 12)
            pred = model.predict(x, batch_size=128)
            r = pred[0]
            r1 = r.tolist()
            direction2 = r1.index(max(r1))
            #用新跑出来的模型判断方向
Пример #17
0
from keras.optimizers import RMSprop
import numpy as np
from sklearn.model_selection import train_test_split

image=[]
label=[]

display1 = Display()
display2 = Display()

stop_number = 2048
size = int(np.log2(stop_number)) +1    #跑到stop number时所需的one-hot编码位数

for i in range(0,500):   #跑500次棋盘,跑到stop_number停止
    game = Game(4, score_to_win=2048, random=False)
    agent = ExpectiMaxAgent(game, display=display1)  #使用强Agent
    
    while game.end==False:
        a=np.array(game.board)
        
        direction=agent.step()
        image.append(game.board)
        label.append(direction)
        game.move(direction)
        if np.amax(a)==stop_number:
            break
       
    display1.display(game)
    
image=np.array(image)   #将得到的数据和标签转换为numpy数组
label=np.array(label)
Пример #18
0
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)
Пример #19
0
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)