def main():
    #TODO delete code from here
    go_state_obj = GoStateObject()
    from search import DeepLearningSearch
    from forward_prop_network import ForwardPropNetwork
    import tensorflow as tf
    import time
    #search_algorithm = DeepLearningSearch()
    sess = tf.Session()
    #forward_prop_network = ForwardPropNetwork(sess)
    import copy

    search_algorithm = MontecarloSearch()
    go_state_obj = GoStateObject()

    args = sys.argv
    user = '******'

    # NNGS set up
    nngs = NNGS()
    # connect nngs
    nngs.connect_nngs()
    # login
    nngs.login(user)
    # match
    if len(args) == 3 and args[2] in ('B', 'W'):
        nngs.match(args[1], args[2])
    # wait
    nngs.nngs_wait()

    init_time = time.time()
    sum_time = 0
    # Go, Player set up
    rule = Go()
    players = [
        Player(0, nngs.player_name['B']),
        Player(1, nngs.player_name['W'])
    ]
    players[0].next_player = players[1]
    players[1].next_player = players[0]
    player = players[0]
    #last_opponent_move=None
    last_opponent_move = None
    while True:
        print(rule.print_board(go_state_obj))
        if player.player_name == user:
            my_start_time = time.time()
            (go_state_obj,
             move) = search_algorithm.next_move(go_state_obj, player,
                                                1800 - (float(sum_time)),
                                                last_opponent_move)
            print("next_move:", move)
            #go_state_obj = rule.move_and_return_state(go_state_obj, player, move)
            nngs.send_data(move2nngs(move, rule))
            my_end_time = time.time()
            sum_time = sum_time + my_end_time - my_start_time
            print("Sum time" + str(sum_time))
            #search_algorithm.thread_close()
        else:
            z = nngs.nngs_wait()
            if z == None:
                print("None! None! なん!")
                exit(0)
            if z < 0: continue
            #go_state_obj = rule.move_and_return_state(go_state_obj,player, nngs2move(rule, player, z))
            nn_move = nngs2move(rule, player, z)
            print(nn_move)
            if nn_move == 0 or nn_move == 3:
                last_opponent_move = rule._PASS
            else:
                last_opponent_move = nn_move
                go_state_obj = rule.move_and_return_state(
                    go_state_obj, player, nn_move)

        player = player.next_player

    nngs.close_nngs()
Beispiel #2
0
from flask import Flask
from flask_restful import Resource, Api
from flask_cors import CORS, cross_origin


from go import Go
import logging

go = Go()
app = Flask(__name__)
CORS(app)

app.logger.setLevel(logging.INFO)

api = Api(app)



class Status(Resource):

    def get(self):
        app.logger.info('* Get called')

        return {
            'serviceName': 'Gene Ontology Utility Service',
            'description': 'GO Util'
        }

class Details(Resource):

    def get(self, goid):
Beispiel #3
0
    def test_play_game(self):
        go = Go(size=11)
        go.play(5, 5)
        expected_board = '\n'.join([
            '|             |', '|             |', '|             |',
            '|             |', '|             |', '|      W      |',
            '|             |', '|             |', '|             |',
            '|             |', '|             |'
        ])
        self.assertEqual(str(go), expected_board,
                         '\n' + repr(str(go)) + '\n' + repr(expected_board))

        go.play(4, 5)
        self.assertEqual(
            str(go), '\n'.join([
                '|             |', '|             |', '|             |',
                '|             |', '|      B      |', '|      W      |',
                '|             |', '|             |', '|             |',
                '|             |', '|             |'
            ]), str(go))

        with self.assertRaises(GoException):
            go.play(4, 5)
        self.assertEqual(
            str(go), '\n'.join([
                '|             |', '|             |', '|             |',
                '|             |', '|      B      |', '|      W      |',
                '|             |', '|             |', '|             |',
                '|             |', '|             |'
            ]), str(go))

        go.play(3, 5)
        expected_board = '\n'.join([
            '|             |', '|             |', '|             |',
            '|      W      |', '|      B      |', '|      W      |',
            '|             |', '|             |', '|             |',
            '|             |', '|             |'
        ])
        self.assertEqual(str(go), expected_board, str(go))

        go.play(5, 6)
        expected_board = '\n'.join([
            '|             |', '|             |', '|             |',
            '|      W      |', '|      B      |', '|      WB     |',
            '|             |', '|             |', '|             |',
            '|             |', '|             |'
        ])
        self.assertEqual(str(go), expected_board, str(go))

        go.play(4, 6)
        self.assertEqual(
            str(go), '\n'.join([
                '|             |', '|             |', '|             |',
                '|      W      |', '|      BW     |', '|      WB     |',
                '|             |', '|             |', '|             |',
                '|             |', '|             |'
            ]), str(go))

        go.play(5, 4)
        self.assertEqual(
            str(go), '\n'.join([
                '|             |', '|             |', '|             |',
                '|      W      |', '|      BW     |', '|     BWB     |',
                '|             |', '|             |', '|             |',
                '|             |', '|             |'
            ]), str(go))

        go.play(4, 4)
        self.assertEqual(
            str(go), '\n'.join([
                '|             |', '|             |', '|             |',
                '|      W      |', '|     WWW     |', '|     BWB     |',
                '|             |', '|             |', '|             |',
                '|             |', '|             |'
            ]), str(go))
def main():
    # TODO delete code from here
    go_state_obj = GoStateObject()
    from search import DeepLearningSearch
    from forward_prop_network import ForwardPropNetwork
    import tensorflow as tf
    search_algorithm = DeepLearningSearch()
    sess = tf.Session()
    forward_prop_network = ForwardPropNetwork(sess)
    import copy

    # search_algorithm=MontecarloSearch()
    go_state_obj = GoStateObject()

    args = sys.argv
    user = '******'

    # NNGS set up
    nngs = NNGS()
    # connect nngs
    nngs.connect_nngs()
    # login
    nngs.login(user)
    # match
    if len(args) == 3 and args[2] in ('B', 'W'):
        nngs.match(args[1], args[2])
    # wait
    nngs.nngs_wait()

    # Go, Player set up
    rule = Go()
    players = [
        Player(0, nngs.player_name['B']),
        Player(1, nngs.player_name['W'])
    ]
    players[0].next_player = players[1]
    players[1].next_player = players[0]
    player = players[0]
    last_opponent_move = None
    while True:
        print(rule.print_board(go_state_obj))
        if player.player_name == user:
            (go_state_obj,
             move) = search_algorithm.next_move(forward_prop_network, sess,
                                                go_state_obj, player,
                                                last_opponent_move)
            print("next_move:", move)
            # go_state_obj = rule.move_and_return_state(go_state_obj, player, move)
            if go_state_obj.turns_num >= 500:
                print("over 10 PASS in nngs_client_pass.py")
                move = rule._PASS
            nngs.send_data(move2nngs(move, rule))
        else:
            z = nngs.nngs_wait()
            if z < 0: continue
            nn_move = nngs2move(rule, player, z)
            print(nn_move)
            if nn_move == 0 or nn_move == 3:
                last_opponent_move = rule._PASS
            else:
                last_opponent_move = nn_move
                go_state_obj = rule.move_and_return_state(
                    go_state_obj, player, nn_move)

        player = player.next_player

    nngs.close_nngs()
Beispiel #5
0
        if _move_count() - sent_msgs[-1] > 10 and len(win_probs) > 4:
            delta_wp = win_probs[-1] - np.mean(win_probs[-5:-1])
            if delta_wp > 0.4 and win_probs[-1] > 0.6:
                ogs_api._game_chat(game_id, _winning_msg())
                sent_msgs.append(_move_count())
            if delta_wp < -0.4 and win_probs[-1] < 0.4:
                ogs_api._game_chat(game_id, _losing_msg())
                sent_msgs.append(_move_count())

if __name__ == '__main__':
    logger  = make_logger('debug')
    model  = Gen_Model().load('Go', '0.1')
    def new_player():
        return MCTS_Player(model, time_limit=2)
    # Initialise TF session
    MCTS_Player(model, time_limit=1).get_move(Go())
    max_concurrent_games = 0
    run_threads = {}
    config  = yaml.load(open('config.yml', 'r'))
    ogs_api = OnlineGoAPI(config['username'], config['password'])
    ogs_api.logon()
    time.sleep(1)
    while True:
        time.sleep(5)
        play_games    = [g for g in ogs_api.gamedata if ogs_api.gamedata[g]['phase'] == 'play']
        for game_id in play_games:
            if game_id not in run_threads:
                logger.info("start thread for %s" % game_id)
                # run(game_id, ogs_api, new_player())
                threading.Thread(target=run, args=(game_id, ogs_api, new_player(), logger)).start()
                # multiprocessing.Process(target=run, args=(game_id, ogs_api, new_player(), logger)).start()
    def train(self):
        players = [Player(0.0, 'human'), Player(1.0, 'human')]
        players[0].next_player = players[1]
        players[1].next_player = players[0]
        #player = players[0]
        rule = Go()

        print "starting tf.device(/gpu:0)"
        sess = tf.InteractiveSession()
        #with tf.device("/gpu:1"):
        with tf.variable_scope('policy_auto'):
            with tf.device("/gpu:0"):
                #5290000=23*23*100*100  88200=21*21*
                # データ用可変2階テンソルを用意
                x_input = tf.placeholder("float", shape=[None, 7, 361])
                # 正解用可変2階テンソルを用意
                y_ = tf.placeholder("float", shape=[None, 361])

                # 画像を2次元配列にリシェイプ 第1引数は画像数(-1は元サイズを保存するように自動計算)、縦x横、チャネル
                x_image = tf.reshape(x_input, [-1, 19, 19, 7])

                x_image_pad = tf.pad(x_image, [[0, 0], [2, 2], [2, 2], [0, 0]])
                # 畳み込み層のフィルタ重み、引数はパッチサイズ縦、パッチサイズ横、入力チャネル数、出力チャネル数
                # 5x5フィルタで100チャネルを出力(入力は白黒画像なので1チャンネル)
                W_conv1 = self.weight_variable([5, 5, 7, 75])  #[5,5,6,50]
                b_conv1 = self.bias_variable([75])  #[50]
                h_conv1 = tf.nn.relu(
                    self.conv2d(x_image_pad, W_conv1) + b_conv1)
                #4次元のpaddingをする
                ### 2層目 プーリング層
                # 2x2のマックスプーリング層を構築
                h_pool1 = self.max_pool_2x2(h_conv1)
                #次の層の畳み込み処理を行う前に、paddingを実行。
                # 3層目 畳み込み層

                # パッチサイズ縦、パッチサイズ横、入力チャネル(枚数)、出力チャネル(出力の枚数)
                # 3x3フィルタで64チャネルを出力
                W_conv2 = self.weight_variable([3, 3, 75, 75])  #[3,3,50,100]
                b_conv2 = self.bias_variable([75])
                h_conv2 = tf.nn.relu(self.conv2d(h_pool1, W_conv2) + b_conv2)

                # 4層目 プーリング層
                h_pool2 = self.max_pool_2x2(h_conv2)
                #次の層の畳み込み処理を行う前に、paddingを実行。

                ###5層目 畳み込み層
                W_conv3 = self.weight_variable([3, 3, 75, 75])
                b_conv3 = self.bias_variable([75])
                h_conv3 = tf.nn.relu(self.conv2d(h_pool2, W_conv3) + b_conv3)

                ### 6層目 プーリング層
                h_pool3 = self.max_pool_2x2(h_conv3)

                ###7層目 畳み込み層
                W_conv4 = self.weight_variable([3, 3, 75, 75])
                b_conv4 = self.bias_variable([75])
                h_conv4 = tf.nn.relu(self.conv2d(h_pool3, W_conv4) + b_conv4)
                h_pool4 = self.max_pool_2x2(h_conv4)

                W_conv5 = self.weight_variable([3, 3, 75, 75])
                b_conv5 = self.bias_variable([75])
                h_conv5 = tf.nn.relu(self.conv2d(h_pool4, W_conv5) + b_conv5)
                h_pool5 = self.max_pool_2x2(h_conv5)

                h_pool6_flat = tf.reshape(h_pool5,
                                          [-1, 23 * 23 * 75])  #29*29*100

                weight_fully_connected1 = self.weight_variable(
                    [23 * 23 * 75, 1000])
                bias_fc1 = self.weight_variable([1000])

                hiden_fully_connect1 = tf.nn.relu(
                    tf.matmul(h_pool6_flat, weight_fully_connected1) +
                    bias_fc1)

                # ドロップアウトを指定
                keep_prob = tf.placeholder("float")
                h_fc1_drop = tf.nn.dropout(hiden_fully_connect1, keep_prob)

                weight_fully_connected2 = self.weight_variable([1000, 361])
                bias_fc2 = self.bias_variable([361])
                y_conv = tf.nn.softmax(
                    tf.matmul(h_fc1_drop, weight_fully_connected2) + bias_fc2)
        cross_entropy = -tf.reduce_sum(
            y_ * tf.log(tf.clip_by_value(y_conv, 1e-10, 1.0)))
        #cross_entropy=tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=y_conv,labels=y_))
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
        correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        sess.run(tf.initialize_all_variables())
        saver = tf.train.Saver(self.get_particular_variables('policy_auto'))
        # 指定したパス内の全てのファイルとディレクトリを要素とするリストを返す
        #自分の環境でのsgf_filesへのパスを書く。
        print "y_!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        print y_

        tf.summary.scalar("cross_entropy", cross_entropy)
        tf.summary.scalar("accuracy", accuracy)
        #tf.summary.scalar("accuracy",accuracy)
        #tf.summary.scalar("correct_prediction",correct_prediction)
        #tf.summary.scalar("train_step",train_step)
        #merged = tf.summary.merge_all()
        files = os.listdir(os.getcwd() + "/kifu")

        init = tf.initialize_all_variables()
        xTrain = []
        yTrain = []

        with tf.Session() as sess:
            merged = tf.summary.merge_all()
            train_writer = tf.summary.FileWriter('tensorflow_logs/train',
                                                 sess.graph)
            test_writer = tf.summary.FileWriter('tensorflow_logs/test',
                                                sess.graph)

            sess.run(init)  #If it is first time of learning

            num = 0

            batch_count_num = 0
            train_count_num = 0

            #saver.restore(sess, './policy_network_1x1_bias')

            make_input = MakeInputPlane()

            step = 0
            ckpt_num = 0
            batch_count_sum_all = 0
            for _ in xrange(100):
                continue_kifu_num = 0

                for file_name in files:
                    continue_kifu_num += 1
                    if continue_kifu_num < 30000:
                        continue

                    step += 1
                    '''
                    if continue_kifu_num % 100 == 0:
                        result = sess.run([merged,cross_entropy], feed_dict=feed_dict(False))
                        summary_str=result[0]
                        acc = result[1]
                        train_writer.add_summary(summary_str,step)
                    '''
                    with open("kifu/" + file_name) as f:

                        go_state_obj = GoStateObject()
                        try:
                            collection = sgf.parse(f.read())
                            flag = False
                        except:
                            print "sgf_parse bugs"
                            continue

                        try:
                            #print "通過"
                            for game in collection:
                                for node in game:
                                    if flag == False:
                                        flag = True
                                        continue
                                    lists = node.properties.values()
                                    #print lists
                                    internal_lists = lists[0]
                                    position = internal_lists[0]
                                    xpos = self.character_list.index(
                                        position[0])
                                    ypos = self.character_list.index(
                                        position[1])
                                    pos_tuple = (xpos, ypos)
                                    #print xpos,ypos
                                    if node.properties.has_key('B') == True:
                                        current_player = players[0]
                                    elif node.properties.has_key('W') == True:
                                        current_player = players[1]
                                    go_state_obj = rule.move(
                                        go_state_obj, current_player,
                                        pos_tuple)
                                    rule.move(go_state_obj, current_player,
                                              pos_tuple)
                                    #print "move ends"

                                    num += 1

                                    if num > 50:

                                        input_board = make_input.generate_input(
                                            go_state_obj, current_player)

                                        answer_board = make_input.generate_answer(
                                            pos_tuple)
                                        xTrain.append(
                                            self.reshape_board(input_board))
                                        yTrain.append(
                                            self.reshape_answer_board(
                                                answer_board))
                                        #print self.reshape_answer_board(answer_board)
                                        #print self.reshape_answer_board(answer_board)
                                        input_board2 = self.rotate90_input(
                                            input_board)
                                        answer_board2 = self.rotate90_answer(
                                            answer_board)
                                        xTrain.append(
                                            self.reshape_board(input_board2))
                                        yTrain.append(
                                            self.reshape_answer_board(
                                                answer_board2))

                                        input_board3 = self.rotate90_input(
                                            input_board2)
                                        answer_board3 = self.rotate90_answer(
                                            answer_board2)
                                        xTrain.append(
                                            self.reshape_board(input_board3))
                                        yTrain.append(
                                            self.reshape_answer_board(
                                                answer_board3))

                                        input_board4 = self.rotate90_input(
                                            input_board3)
                                        answer_board4 = self.rotate90_answer(
                                            answer_board3)
                                        xTrain.append(
                                            self.reshape_board(input_board4))
                                        yTrain.append(
                                            self.reshape_answer_board(
                                                answer_board4))

                                        input_board5 = self.invert_board_input(
                                            input_board4)
                                        answer_board5 = self.invert_board_answer(
                                            answer_board4)
                                        xTrain.append(
                                            self.reshape_board(input_board5))
                                        yTrain.append(
                                            self.reshape_answer_board(
                                                answer_board5))

                                        input_board6 = self.rotate90_input(
                                            input_board5)
                                        answer_board6 = self.rotate90_answer(
                                            answer_board5)
                                        xTrain.append(
                                            self.reshape_board(input_board6))
                                        yTrain.append(
                                            self.reshape_answer_board(
                                                answer_board6))

                                        input_board7 = self.rotate90_input(
                                            input_board3)
                                        answer_board7 = self.rotate90_answer(
                                            answer_board3)
                                        xTrain.append(
                                            self.reshape_board(input_board7))
                                        yTrain.append(
                                            self.reshape_answer_board(
                                                answer_board7))

                                        input_board8 = self.rotate90_input(
                                            input_board7)
                                        answer_board8 = self.rotate90_answer(
                                            answer_board7)
                                        xTrain.append(
                                            self.reshape_board(input_board8))
                                        yTrain.append(
                                            self.reshape_answer_board(
                                                answer_board8))
                                        num = 0
                                        #xTrain,yTrainの中身を代入する処理をここに書く。
                                        batch_count_num += 1

                                    if (batch_count_sum_all + 101
                                        ) % 40 == 0 and batch_count_num == 30:
                                        #print xTrain
                                        print "tensorboard writing!"
                                        #train_step.run(feed_dict={x_input: xTrain, y_: yTrain,keep_prob: 0.5})

                                        summary_str, accuracy_value, cross_entropy_value = sess.run(
                                            [merged, accuracy, cross_entropy],
                                            feed_dict={
                                                x_input: xTrain,
                                                y_: yTrain,
                                                keep_prob: 1.0
                                            })
                                        train_writer.add_summary(
                                            summary_str, step)
                                        train_writer.flush()
                                        #train_writer.add_summary(summary_str[0],step)
                                        print accuracy_value
                                        print str(float(cross_entropy_value))
                                        #print cross_entropy
                                        #train_step.run(feed_dict={x_input: xTrain, y_: yTrain,keep_prob: 0.5})

                                        print summary_str[0]
                                        batch_count_sum_all += 1
                                        batch_count_num = 0
                                        train_count_num += 1
                                        xTrain = []
                                        yTrain = []

                                        print train_count_num
                                    elif batch_count_num == 30:
                                        train_step.run(
                                            feed_dict={
                                                x_input: xTrain,
                                                y_: yTrain,
                                                keep_prob: 0.5
                                            })
                                        #train_accuracy = accuracy.eval(feed_dict={x_input:xTrain, y_: yTrain, keep_prob: 1.0})
                                        batch_count_sum_all += 1
                                        batch_count_num = 0
                                        train_count_num += 1
                                        xTrain = []
                                        yTrain = []
                                        print train_count_num
                                    if train_count_num > 2000:
                                        train_count_num = 0
                                        ckpt_num += 1
                                        print "SAVED!"
                                        saver.save(
                                            sess,
                                            './Network_Backup/policy_network_relu'
                                            + str(ckpt_num))

                        except:
                            #import traceback
                            #traceback.print_exc()
                            f.close()