Esempio n. 1
0
    def choose_action(self, state, greedy=False):
        # 决策
        if greedy or np.random.random() < self.epsilon:
            board = np.expand_dims(state, axis=0)
            actions_value = self.model1.predict(board)
            action = np.argmax(actions_value)

            print('=====================================')
            print('action_value', actions_value)
            print('-------------------------------------')
            print(action)
            print('=====================================')

        else:
            # action = np.random.randint(0, 4)
            gameboard = np.zeros((4, 4))
            for i in range(4):
                for j in range(4):
                    num = np.argmax(state[i][j])
                    if num == 0:
                        gameboard[i][j] = 0
                    else:
                        gameboard[i][j] = 2**num

            action = board_to_move(gameboard)

        return action
Esempio n. 2
0
 def move(self, game):
     ohe_board = grid_one(vmap(game.board))
     d = board_to_move(game.board)
     self.mem.push(ohe_board, d)
     if random.random() < 0.6:
         game.move(d)
     else:
         game.move(self.predict(ohe_board).argmax())
Esempio n. 3
0
 def move(self, game):
     ohe_board = game.board
     suggest = board_to_move(game.board)
     direction = self.predict(game.board).argmax()
     game.move(direction)
     a = random.randint(0, 9)
     self.memory.push(ohe_board, suggest)
     return game
Esempio n. 4
0
    def move(self, game):
        ohe_board = grid_ohe(game.board)

        from game2048.expectimax import board_to_move
        suggest = board_to_move(game.board)

        direction = self.predict(ohe_board).argmax()
        game.move(direction)
        self.memory.push(ohe_board, suggest)
Esempio n. 5
0
 def move(self, game):
     ohe_board = grid_one(vmap(game.board))
     d = board_to_move(game.board)
     self.mem.push(ohe_board, d)
     game.move(self.predict(ohe_board).argmax())
Esempio n. 6
0
 def move(self, game):
     ohe_board = grid_one(vmap(game.board))
     suggestion = board_to_move(game.board)        
     direction = self.predict(ohe_board).argmax()
     game.move(direction)
     self.mem.push(ohe_board, suggestion)
Esempio n. 7
0
    for i3 in range(8):
        gameeasy = Game(4, score_to_win=256, random=False)
        agente = ExpectiMaxAgent(gameeasy)
        agente.play(verbose=True)
   '''

    #***********************************************************************

    #*********************************************************************

    #boardsets=np.loadtxt('b1024-'+str(i+1)+'.csv',usecols=(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15),delimiter=',')
    #boardsets=csv.reader(open('resulttest.csv'))
    #for row in boardsets:
    #print row
    boardsets = yboard
    rest = np.empty(shape=[100000, 17])
    i3 = 0
    print(1)
    for item2 in boardsets:
        tmpitem2 = item2[0:16]
        boardtmp = tmpitem2.reshape(4, 4)
        direction = board_to_move(boardtmp)
        item2tmp = np.append(tmpitem2, [direction], axis=0)
        rest[i3] = item2tmp
        i3 = i3 + 1
    saveboard = rest[0:i3]
    #with open('b2048-'+str(i+1)+'.csv', 'a', newline='') as csv_file:
    #csv_writer = csv.writer(csv_file)
    #for iw in range(i3):
    #csv_writer.writerow(rest[iw])
Esempio n. 8
0
 def play(self):
     onehot_board = change_to_onehot(self.game.board)
     self.push(onehot_board, board_to_move(self.game.board))
     self.game.move(self.predict())