How Fun AlphaGo version of backgammon with CNN?

Author | Qiu key

Zebian | Guo Rui

Exhibition | CSDN (ID: CSDNnews)

In recent years, the development of AI in games is in full swing, especially since the alfa dog beat the AI ​​Go Go, but also caused a frenzy AI development, but also caused a lot of application development and deepening of the game's AI. In fact, the game's AI has a very long history, a considerable number of games are deployed around the fight against the "enemy", and the "enemy", is the AI, which contains some of the acts fixed no low-level AI a little bit of change, there are some High point of AI another random factors, but the AI ​​is essentially here is a fixed script, if the player to master the law of which, the game will instantly reduced. 

The AI ​​version of the depth of learning is different, he and multi-directional parameters and select a plurality of positions, expanding the intelligence which the AI, allowing the player to find the regularity which becomes highly unlikely, it is important to learn the depth one meaning. Today, we will use CNN intelligent backgammon.

Preparation before experiment

First, we use the python version is 3.6.5. The test system has windows10, windows7, Linux systems, and Apple systems. From this point you can also trim python multi-platform and multi-scalable, easy migration of advantages.

The python library use has tkinter, its purpose is to plan the layout of the board, chess implement functions; SGF file for reading and load the chess training model; os library used to read and store local files; TensorFlow library for establish matters CNN network model and training.

The establishment of the board

1. Initialize the board:

Where the significance of the parameters set as follows: Initialization: someoneWin: identify whether someone wins; humanChessed: whether human players down; IsStart: whether to start the game; players: a player is which side; How to play: mode, playing chess and robotics, or ai and board games; bla_start_pos: Black start position in the middle of nowadays; bla_chessed: Black has been to save the pieces; whi_chessed: White already stored at an excessive pieces; Board: chessboard; window: window; var: selecting a variable for marking the player color; var1: selection marker for a robot or a variable ai; can be: canvas, for drawing out the board; net_board: point information of the board; robot: robot; SGF: processing game record; CNN : cnnc neural network.

Wherein the code is as follows:

def __init__(self):
        self.someoneWin = False
        self.humanChessed = False
        self.IsStart = False
        self.player = 0
        self.playmethod = 0
        self.bla_start_pos = [235, 235]
        self.whi_chessed = []
        self.bla_chessed = []
        self.board = self.init_board()
        self.window = Tk()
        self.var = IntVar()
        self.var.set(0)
        self.var1 = IntVar()
        self.var1.set(0)
        self.window.title("myGoBang")
        self.window.geometry("600x470+80+80")
        self.window.resizable(0, 0)
        self.can = Canvas(self.window, bg="#EEE8AC", width=470, height=470)
        self.draw_board()
        self.can.grid(row=0, column=0)
        self.net_board = self.get_net_board()
        self.robot = Robot(self.board)
        self.sgf = SGFflie()
        self.cnn = myCNN()
        self.cnn.restore_save()
    def init_board(self):
        """初始化棋盘"""
        list1 = [[-1]*15 for i in range(15)]
        return list1

2. chessboard layout:

Its main function is to draw the board and pieces. Specific code as follows:

def draw_board(self):
        """画出棋盘"""
        for row in range(15):
            if row == 0 or row == 14:
                self.can.create_line((25, 25 + row * 30), (445, 25 + row * 30), width=2)
            else:
                self.can.create_line((25, 25 + row * 30), (445, 25 + row * 30), width=1)
        for col in range(15):
            if col == 0 or col == 14:
                self.can.create_line((25 + col * 30, 25), (25 + col * 30, 445), width=2)
            else:
                self.can.create_line((25 + col * 30, 25), (25 + col * 30, 445), width=1)
        self.can.create_oval(112, 112, 118, 118, fill="black")
        self.can.create_oval(352, 112, 358, 118, fill="black")
        self.can.create_oval(112, 352, 118, 358, fill="black")
        self.can.create_oval(232, 232, 238, 238, fill="black")
        self.can.create_oval(352, 352, 358, 358, fill="black")
def draw_chessed(self):
        """在棋盘中画出已经下过的棋子"""
        if len(self.whi_chessed) != 0:
            for tmp in self.whi_chessed:
                oval = pos_to_draw(*tmp[0:2])
                self.can.create_oval(oval, fill="white")
        if len(self.bla_chessed) != 0:
            for tmp in self.bla_chessed:
                oval = pos_to_draw(*tmp[0:2])
                self.can.create_oval(oval, fill="black")
    def draw_a_chess(self, x, y, player=None):
        """在棋盘中画一个棋子"""
        _x, _y = pos_in_qiju(x, y)
        oval = pos_to_draw(x, y)
        if player == 0:
            self.can.create_oval(oval, fill="black")
            self.bla_chessed.append([x, y, 0])
            self.board[_x][_y] = 1
        elif player == 1:
            self.can.create_oval(oval, fill="white")
            self.whi_chessed.append([x, y, 1])
            self.board[_x][_y] = 0
        else:
            print(AttributeError("请选择棋手"))
        return

3. Analyzing outcome conditions:

According to determine whether it is winning or losing even five children in the front line.

def have_five(self, chessed):
        """检测是否存在连五了"""
        if len(chessed) == 0:
            return False
        for row in range(15):
            for col in range(15):
                x = 25 + row * 30
                y = 25 + col * 30
                if self.check_chessed((x, y), chessed) == True and \
                                self.check_chessed((x, y + 30), chessed) == True and \
                                self.check_chessed((x, y + 60), chessed) == True and \
                                self.check_chessed((x, y + 90), chessed) == True and \
                                self.check_chessed((x, y + 120), chessed) == True:
                    return True
                elif self.check_chessed((x, y), chessed) == True and \
                                self.check_chessed((x + 30, y), chessed) == True and \
                                self.check_chessed((x + 60, y), chessed) == True and \
                                self.check_chessed((x + 90, y), chessed) == True and \
                                self.check_chessed((x + 120, y), chessed) == True:
                    return True
                elif self.check_chessed((x, y), chessed) == True and \
                                self.check_chessed((x + 30, y + 30), chessed) == True and \
                                self.check_chessed((x + 60, y + 60), chessed) == True and \
                                self.check_chessed((x + 90, y + 90), chessed) == True and \
                                self.check_chessed((x + 120, y + 120), chessed) == True:
                    return True
                elif self.check_chessed((x, y), chessed) == True and \
                                self.check_chessed((x + 30, y - 30), chessed) == True and \
                                self.check_chessed((x + 60, y - 60), chessed) == True and \
                                self.check_chessed((x + 90, y - 90), chessed) == True and \
                                self.check_chessed((x + 120, y - 120), chessed) == True:
                    return True
                else:
                    pass
        return False
    def check_win(self):
        """检测是否有人赢了"""
        if self.have_five(self.whi_chessed) == True:
            label = Label(self.window, text="White Win!", background='#FFF8DC', font=("宋体", 15, "bold"))
            label.place(relx=0, rely=0, x=480, y=40)
            return True
        elif self.have_five(self.bla_chessed) == True:
            label = Label(self.window, text="Black Win!", background='#FFF8DC', font=("宋体", 15, "bold"))
            label.place(relx=0, rely=0, x=480, y=40)
            return True
        else:
            return False

UI interface obtained as follows:

Deep learning model

1. Initialize the neural network:

Wherein the first and second layers is a convolution layer, the fourth layer is a layer fully connected, then followed by the connection pool and softmax. And general network CNN basically the same. The basic parameters, see the code as follows:

def __init__(self):
        '''初始化神经网络'''
        self.sess = tf.InteractiveSession()
        # paras
        self.W_conv1 = self.weight_varible([5, 5, 1, 32])
        self.b_conv1 = self.bias_variable([32])
        # conv layer-1
        self.x = tf.placeholder(tf.float32, [None, 225])
        self.y = tf.placeholder(tf.float32, [None, 225])
        self.x_image = tf.reshape(self.x, [-1, 15, 15, 1])
        self.h_conv1 = tf.nn.relu(self.conv2d(self.x_image, self.W_conv1) + self.b_conv1)
        self.h_pool1 = self.max_pool_2x2(self.h_conv1)
        # conv layer-2
        self.W_conv2 = self.weight_varible([5, 5, 32, 64])
        self.b_conv2 = self.bias_variable([64])
        self.h_conv2 = tf.nn.relu(self.conv2d(self.h_pool1, self.W_conv2) + self.b_conv2)
        self.h_pool2 = self.max_pool_2x2(self.h_conv2)
        # full connection
        self.W_fc1 = self.weight_varible([4 * 4 * 64, 1024])
        self.b_fc1 = self.bias_variable([1024])
        self.h_pool2_flat = tf.reshape(self.h_pool2, [-1, 4 * 4 * 64])
        self.h_fc1 = tf.nn.relu(tf.matmul(self.h_pool2_flat, self.W_fc1) + self.b_fc1)
        # dropout
        self.keep_prob = tf.placeholder(tf.float32)
        self.h_fc1_drop = tf.nn.dropout(self.h_fc1, self.keep_prob)
        # output layer: softmax
        self.W_fc2 = self.weight_varible([1024, 225])
        self.b_fc2 = self.bias_variable([225])
        self.y_conv = tf.nn.softmax(tf.matmul(self.h_fc1_drop, self.W_fc2) + self.b_fc2)
        # model training
        self.cross_entropy = -tf.reduce_sum(self.y * tf.log(self.y_conv))
        self.train_step = tf.train.AdamOptimizer(1e-3).minimize(self.cross_entropy)
        self.correct_prediction = tf.equal(tf.argmax(self.y_conv, 1), tf.argmax(self.y, 1))
        self.accuracy = tf.reduce_mean(tf.cast(self.correct_prediction, tf.float32))
        self.saver = tf.train.Saver()
        init = tf.global_variables_initializer()  # 不存在就初始化变量
        self.sess.run(init)
    def weight_varible(self, shape):
        '''权重变量'''
        initial = tf.truncated_normal(shape, stddev=0.1)
        return tf.Variable(initial)
    def bias_variable(self, shape):
        '''偏置变量'''
        initial = tf.constant(0.1, shape=shape)
        return tf.Variable(initial)
    def conv2d(self, x, W):
        '''卷积核'''
        return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
    def max_pool_2x2(self, x):
        '''池化核'''
        return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

2. Save and read the model:

def restore_save(self, method=1):
        '''保存和读取模型'''
        if method == 1:
            self.saver.restore(self.sess, 'save\model.ckpt')
            #print("已读取数据")
        elif method == 0:
            saver = tf.train.Saver(write_version=tf.train.SaverDef.V2)
            saver.save(self.sess, 'save\model.ckpt')
            #print('已保存')

3. Establish prediction function and training functions:

def predition(self, qiju):
        '''预测函数'''
        _qiju = self.createdataformqiju(qiju)
        pre = self.sess.run(tf.argmax(self.y_conv, 1), feed_dict={self.x: _qiju, self.keep_prob: 1.0})
        point = [0, 0]
        l = pre[0]
        for i in range(15):
            if ((i + 1) * 15) > l:
                point[0] = int(i*30 + 25)
                point[1] = int((l - i * 15) * 30 + 25)
                break
        return point
    def train(self, qiju):
        '''训练函数'''
        sgf = SGFflie()
        _x, _y = sgf.createTraindataFromqipu(qiju)
        for i in range(10):
            self.sess.run(self.train_step, feed_dict={
                self.x: _x,
                self.y: _y
            })
        self.restore_save(method=0)
    def train1(self, x, y):
        '''另一个训练函数'''
        for i in range(100):
            self.sess.run(self.train_step, feed_dict={
                self.x: x,
                self.y: y,
                self.keep_prob: 0.5
            })
        print('训练好了一次')
        #self.restore_save(method=0)

4. Generate Data:

def createdataformqiju(self, qiju):
        '''生成数据'''
        data = []
        tmp = []
        for row in qiju:
            for point in row:
                if point == -1:
                    tmp.append(0.0)
                elif point == 0:
                    tmp.append(2.0)
                elif point == 1:
                    tmp.append(1.0)
        data.append(tmp)
        return data

Here CNN which differs from the board image recognition applications and that the image recognition parameters from the loaded point in the image itself as a parameter value of the training, the training parameter here is the custom parameters chess board, for example said the upper left corner of the board position parameters, and so each location parameters are preset by loading chess which can let the computer know when Reversi child in that position. Then by loading various locations and determine the outcome of the case, the final load computer model to predict the victory of chess position possible to achieve the effect of intelligent play chess.

final effect:

About the author: Qiu key, CSDN blog expert, CSDN master class of authors who read the Lunar Mining University, Andrews has developed a martial arts game, VIP video resolution, the context convert writing robots and other projects, a number of published papers, many times the number of high competition award-winning, and so on.

Disclaimer: This article is submission of the original, do not reprint without permission.

【end】

Force plans

"Force plan [the second quarter] - learning ability Challenge" started! From now until March 21, must flow to support the original author! Exclusive [more] medal waiting for you to challenge

Recommended Reading

    Your point of each "look", I seriously as the AI

Released 1354 original articles · won praise 10000 + · views 6.25 million +

Guess you like

Origin blog.csdn.net/dQCFKyQDXYm3F8rB0/article/details/104831789