Пример #1
0
 def _get_go():
     try:
         go = Go(board_size=gamedata()['width'])
         for x,y,_ in gamedata()['moves']:
             go = go.place((x,y))
         return go
     except:
         return Go()
Пример #2
0
 def __init__(self, parent, action, evaluator, conf):
     if parent is None:
         self.go = Go(board_size=conf.BOARD_SIZE, komi=conf.KOMI)
     else:
         self.go = Go(copy=parent.go)
         if action == conf.PASS:
             self.go.pass_()
         else:
             self.go.play(*divmod(action, conf.BOARD_SIZE))
     self.parent = parent
     self.children = [None] * conf.NUM_ACTIONS
     self.action = action
     self.n = np.zeros(conf.NUM_ACTIONS, dtype=np.int)
     self.w = np.zeros(conf.NUM_ACTIONS, dtype=np.float32)
     self.p, self.v = predict(evaluator, self, conf, random_trans=True)
Пример #3
0
 def __init__(self):
     self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.str_data = None
     self.player_name = {"B": None, "W": None}
     self.user_name = None
     self.my_color = None
     self.opponent_name = None
     self.rule = Go()
     self.time_data = {"B": None, "W": None}
Пример #4
0
    def setUp(self):
        go = Go(size=4)
        go[2][2].state = WHITE
        go[1][2].state = BLACK
        go[3][2].state = BLACK
        go[2][1].state = BLACK
        go[2][3].state = BLACK

        self.small_board_white_surrounded = go

        go = Go(size=4)
        go[2][2].state = WHITE
        go[1][2].state = WHITE
        go[3][2].state = WHITE
        go[2][1].state = WHITE
        go[2][3].state = WHITE

        self.small_board_all_white = go

        go = Go(size=4)
        go[2][2].state = WHITE
        go[1][2].state = None
        go[3][2].state = BLACK
        go[2][1].state = BLACK
        go[2][3].state = BLACK

        self.small_board_white_almost_surrounded = go

        board = [
            '|             |',
            '|             |',
            '|             |',
            '|             |',
            '|      BB     |',
            '|     BWWB    |',
            '|      BB     |',
            '|             |',
            '|             |',
            '|             |',
            '|             |',
        ]

        self.big_board_white_captured = parse_board(board)
Пример #5
0
def sgf_to_npy(sgf_collection):
    positions = "abcdefghijklmnopqrs"
    game = sgf_collection[0]
    black_win = True if game.root.properties["RE"][0] == "B" else False

    initial_stones = []
    for key, value in game.root.properties:
        if key == "AB":
            initial_stones.append(value[0])

    sim = Go()
    if initial_stones:
        sim.reset(initial_stones=initial_stones)
    else:
        sim.reset()

    for move in game.rest:
        try:
            stone_position = move.properties["B"][0]
        except KeyError:
            stone_position = move.properties["W"][0]

        if stone_position == "":
            action = 361
        else:
            x = positions.index(stone_position[0])
            y = positions.index(stone_position[1])
            action = np.ravel_multi_index((y, x), (19, 19))

        sim.step(action)

    examples = np.zeros((19, 19, 17, len(sim)), dtype=bool)
    actions = np.zeros((362, len(sim)))
    for idx in range(len(sim)):
        state, action_idx = sim.get_history_step(idx)
        examples[:, :, :, idx] = state
        actions[action_idx, idx] = 1

    results = np.ones(len(sim))
    if black_win:
        results[1::2] = -1
    else:
        results[::2] = -1

    return examples, actions, results
Пример #6
0
def play_sgf(sgf: str) -> Optional[Dict]:
    try:
        board_size = int(SZ(sgf))
    except:
        raise Exception('Illegal Board Size %s' % SZ(sgf))
    if '(;W[' in sgf or '(;B[' in sgf:
        raise Exception('Analysis Currently Unsupported')
    go = Go(board_size)
    black, white = sgf_add_stones(sgf)
    print(black, white)
    for loc in black:
        go._board[loc] = Go.BLACK
    for loc in white:
        go._board[loc] = Go.WHITE
    # print(go)
    for p, move in sgf_moves(sgf):
        # print(p,move)
        go._turn = p
        go = go.place(move)
        # print(go)
    return {
        'board': go,
        'RE': RE(sgf),
        'WR': WR(sgf),
        'BR': BR(sgf),
        'SZ': SZ(sgf),
    }
Пример #7
0
    def __init__(self, worker, client):
        super(Lit, self).__init__()

        self.worker = worker
        self.client = client

        from go import Go
        from run import Run
        #from recent import Recent
        #from iciba import Iciba
        #from f import F
        #lit = Lit([Go(), Run(), Recent(), Iciba()])
        plugins = [
            Go(worker=worker, client=client),
            Run(worker=worker, client=client)
        ]

        lay = QVBoxLayout()

        # spacing of search box
        lay.setSpacing(0)
        lay.setMargin(0)

        self.inp = Input(self.act, self)
        self.inp.textChanged.connect(self._try_query)
        self.completer = Suggest(self.inp)
        #self.inp.setCompleter(self.completer)
        self.completer.activated[QModelIndex].connect(self.select)
        lay.addWidget(self.inp)
        self.setLayout(lay)
        self._install_plugins(plugins)
        self.setWindowFlags(
            Qt.FramelessWindowHint
            | Qt.Popup
            | Qt.WindowStaysOnTopHint
        )
        self.setWindowTitle('lit')

        self.mutex = QMutex()

        self.jobs = []
Пример #8
0
from PyQt5.QtWidgets import QApplication
from go import Go
import sys

app = QApplication([])
myGo = Go()
sys.exit(app.exec_())


Пример #9
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):

        term = go.getGoDag()[goid]
Пример #10
0
def run_rounds(opts,args):
    def get_cmd_wd(cmd, exec_rel_cwd=False):
        ''' get the proper working directory from a command line '''
        new_cmd = []
        wd = None
        for i, part in reversed(list(enumerate(cmd.split()))):
            if wd == None and os.path.exists(part):
                wd = os.path.dirname(os.path.realpath(part))
                basename = os.path.basename(part)
                if i == 0:
                    if exec_rel_cwd:
                        new_cmd.insert(0, os.path.join(".", basename))
                    else:
                        new_cmd.insert(0, part)
                else:
                    new_cmd.insert(0, basename)
            else:
                new_cmd.insert(0, part)
        return wd, ' '.join(new_cmd)
    def get_cmd_name(cmd):
        ''' get the name of a bot from the command line '''
        for i, part in enumerate(reversed(cmd.split())):
            if os.path.exists(part):
                return os.path.basename(part)
# this split of options is not needed, but left for documentation
    game_options = {
        "map": opts.map,
        "timebank": opts.loadtime,
        "time_per_move": opts.turntime,
	"player_names" : args, #opts.player_names,
        "scenario": opts.scenario }
    if opts.player_seed != None:
        game_options['player_seed'] = opts.player_seed
    if opts.engine_seed != None:
        game_options['engine_seed'] = opts.engine_seed
    engine_options = {
        "timebank": opts.loadtime,
        "time_per_move": opts.turntime,
        "player_names" : args, #opts.player_names,
        "loadtime": opts.loadtime,
        "turntime": opts.turntime,
        "ack_turn_zero" : opts.ack_turn_zero,
        "map_file": opts.map,
        "turns": opts.turns,
        "log_replay": opts.log_replay,
        "log_stream": opts.log_stream,
        "log_input": opts.log_input,
        "log_output": opts.log_output,
        "log_error": opts.log_error,
        "serial": opts.serial,
        "strict": opts.strict,
        "capture_errors": opts.capture_errors,
        "secure_jail": opts.secure_jail,
        "end_wait": opts.end_wait }
    for round in range(opts.rounds):
        # initialize game
        game_id = round + opts.game_id
#        with open(opts.map, 'r') as map_file:
        game_options['map'] = ""# map_file.read()
        if opts.engine_seed:
            game_options['engine_seed'] = opts.engine_seed + round
        game = Go(game_options)
        # initialize bots
        bots = [get_cmd_wd(arg, exec_rel_cwd=opts.secure_jail) for arg in args]
        bot_count = len(bots)
        # insure correct number of bots, or fill in remaining positions
        if game.num_players != len(bots):
            if game.num_players > len(bots) and opts.fill:
                extra = game.num_players - len(bots)
                for _ in range(extra):
                    bots.append(bots[-1])
            else:
                print("Incorrect number of bots for map.  Need {0}, got {1}"
                      .format(game.num_players, len(bots)), file=stderr)
                for arg in args:
                    print("Bot Cmd: {0}".format(arg), file=stderr)
                break
        bot_count = len(bots)
        # move position of first bot specified
        if opts.position > 0 and opts.position <= len(bots):
            first_bot = bots[0]
            bots = bots[1:]
            bots.insert(opts.position, first_bot)

        # initialize file descriptors
        if opts.log_dir and not os.path.exists(opts.log_dir):
            os.mkdir(opts.log_dir)
        if not opts.log_replay and not opts.log_stream and (opts.log_dir or opts.log_stdout):
            opts.log_replay = True
        replay_path = None # used for visualizer launch

        if opts.log_replay:
            if opts.log_dir:
                replay_path = os.path.join(opts.log_dir, '{0}.replay'.format(game_id))
                engine_options['replay_log'] = open(replay_path, 'w')
            if opts.log_stdout:
                if 'replay_log' in engine_options and engine_options['replay_log']:
                    engine_options['replay_log'] = Tee(sys.stdout, engine_options['replay_log'])
                else:
                    engine_options['replay_log'] = sys.stdout
        else:
            engine_options['replay_log'] = None

        if opts.log_stream:
            if opts.log_dir:
                engine_options['stream_log'] = open(os.path.join(opts.log_dir, '{0}.stream'.format(game_id)), 'w')
            if opts.log_stdout:
                if engine_options['stream_log']:
                    engine_options['stream_log'] = Tee(sys.stdout, engine_options['stream_log'])
                else:
                    engine_options['stream_log'] = sys.stdout
        else:
            engine_options['stream_log'] = None

        if opts.log_input and opts.log_dir:
            engine_options['input_logs'] = [open(os.path.join(opts.log_dir, '{0}.bot{1}.input'.format(game_id, i)), 'w')
                             for i in range(bot_count)]
        else:
            engine_options['input_logs'] = None
        if opts.log_output and opts.log_dir:
            engine_options['output_logs'] = [open(os.path.join(opts.log_dir, '{0}.bot{1}.output'.format(game_id, i)), 'w')
                              for i in range(bot_count)]
        else:
            engine_options['output_logs'] = None
        if opts.log_error and opts.log_dir:
            if opts.log_stderr:
                if opts.log_stdout:
                    engine_options['error_logs'] = [Tee(Comment(stderr), open(os.path.join(opts.log_dir, '{0}.bot{1}.error'.format(game_id, i)), 'w'))
                                      for i in range(bot_count)]
                else:
                    engine_options['error_logs'] = [Tee(stderr, open(os.path.join(opts.log_dir, '{0}.bot{1}.error'.format(game_id, i)), 'w'))
                                      for i in range(bot_count)]
            else:
                engine_options['error_logs'] = [open(os.path.join(opts.log_dir, '{0}.bot{1}.error'.format(game_id, i)), 'w')
                                  for i in range(bot_count)]
        elif opts.log_stderr:
            if opts.log_stdout:
                engine_options['error_logs'] = [Comment(stderr)] * bot_count
            else:
                engine_options['error_logs'] = [stderr] * bot_count
        else:
            engine_options['error_logs'] = None

        if opts.verbose:
            if opts.log_stdout:
                engine_options['verbose_log'] = Comment(sys.stdout)
            else:
                engine_options['verbose_log'] = sys.stdout

        engine_options['game_id'] = game_id
        if opts.rounds > 1:
            print('# playgame round {0}, game id {1}'.format(round, game_id))

        # intercept replay log so we can add player names
        if opts.log_replay:
            intcpt_replay_io = StringIO()
            real_replay_io = engine_options['replay_log']
            engine_options['replay_log'] = intcpt_replay_io

        result = run_game(game, bots, engine_options)

        # add player names, write to proper io, reset back to normal
        if opts.log_replay:
            replay_json = json.loads(intcpt_replay_io.getvalue())
            replay_json['playernames'] = [get_cmd_name(arg) for arg in args]
            real_replay_io.write(json.dumps(replay_json))
            intcpt_replay_io.close()
            engine_options['replay_log'] = real_replay_io

        # close file descriptors
        if engine_options['stream_log']:
            engine_options['stream_log'].close()
        if engine_options['replay_log']:
            engine_options['replay_log'].close()
        if engine_options['input_logs']:
            for input_log in engine_options['input_logs']:
                input_log.close()
        if engine_options['output_logs']:
            for output_log in engine_options['output_logs']:
                output_log.close()
        if engine_options['error_logs']:
            for error_log in engine_options['error_logs']:
                error_log.close()
        if replay_path:
            if opts.nolaunch:
                if opts.html_file:
                    visualizer.visualize_locally.launch(replay_path, True, opts.html_file)
            else:
                if opts.html_file == None:
                    visualizer.visualize_locally.launch(replay_path,
                            generated_path="replay.{0}.html".format(game_id))
                else:
                    visualizer.visualize_locally.launch(replay_path,
                            generated_path=opts.html_file)
Пример #11
0
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()
Пример #12
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()
Пример #14
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()