コード例 #1
0
ファイル: policy_rpc.py プロジェクト: neoblazes/wonpago
def get_forwards(feature_csvs):
    global x_iter, predicts
    if not type(feature_csvs) is list:
        feature_csvs = [feature_csvs]
    x_tests = []
    for feature_csv in feature_csvs:
        feature = [int(x) for x in feature_csv.split(',')]
        board, ko, turn = play_go.FromFeature((feature + [0, 0, 0])[:84])
        feature = play_go.ToFeature(board, ko, turn, 0, 0, True, True)
        x_test, _ = train_lib.parse_row(feature, True)
        x_tests.append(np.asarray(x_test, dtype=np.float32))
    x_iter.update(x_tests)
    if predicts == None:
        predicts = estimator.predict(
            x=x_iter)  # Only calls once with opened iterator.
    #predicts = estimator.predict(x=np.asarray(x_tests, dtype=np.float32))
    rets = []
    for _ in range(len(x_tests)):  # predict in predicts:
        #for predict in predicts:
        probabilities = predicts.next()['probabilities']
        actions = []
        for i in range(len(probabilities)):
            actions.append([
                str(train_lib.UnpackAction(i))
                if i > 0 and i < 82 else ACTION_CODE[i],
                float(probabilities[i])
            ])
        rets.append(sorted(actions, key=lambda x: x[1], reverse=True)[:10])
    if len(rets) == 1:
        return rets[0]
    return rets
コード例 #2
0
def get_forwards(feature_csvs):
    if not type(feature_csvs) is list:
        feature_csvs = [feature_csvs]
    x_tests = []
    for feature_csv in feature_csvs:
        feature = [int(x) for x in feature_csv.split(',')]
        board, ko, turn = play_go.FromFeature(feature + [0])
        feature = play_go.ToFeature(board, ko, turn, 0, 0, True, True)
        x_test, _ = train_lib.parse_row(feature, True)
        x_tests.append(np.asarray(x_test, dtype=np.float32))
    predicts = estimator.predict(np.array(x_tests))
    rets = []
    for predict in predicts:
        probabilities = list(predict['probabilities'])
        rets.append([float(x) for x in probabilities])
    if len(rets) == 1:
        return rets[0]
    return rets
コード例 #3
0
ファイル: opening_predict.py プロジェクト: neoblazes/wonpago
num_sequence = int(sys.argv[2])
num_predict = int(sys.argv[3])
print('Test %d sequecne with model %s' % (num_sequence, model_dir))

# Set up kifu
kifus = []
stones = [0] * 81
for i in range(num_sequence):
    stones[i] = (i % 2) * (-2) + 1
tail = [(num_sequence % 2) * 2 - 1, 0, 0]

# Shuffle board and add to kifu list
for _ in range(num_predict):
    random.shuffle(stones)
    board, last_move, ko = play_go.FromFeature(stones + tail)
    kifus.append(play_go.ToFeature(board, last_move, ko, 0, True,
                                   True)[:-1])  # remove result column
x_test = np.array(kifus, dtype=np.float32)

# Load model and predict
model_fn = importlib.import_module('%s.model_fn' % model_dir)
estimator = model_fn.GetEstimator(model_dir)
ds_predict_tf = estimator.predict(x_test)


# Print out human readable.
def PrintBoard(feature, pred):
    print('%s, predict(W(-1)~B(1)): %f\n' %
          (play_go.SPrintBoard(feature), pred))


# Print out human readable.
コード例 #4
0
        logging.warning('Drops too short game')
        continue

    winner = GetWinner(summary, sequence)
    surrendered = WinBySurrender(winner, summary)
    win_count[winner] = win_count[winner] + 1

    board, ko, turn = play_go.InitBoard()
    seq_cnt = 0
    for move in sequence:
        # Print before play.
        if len(move) == 0:
            continue
        turn, action = ParseMove(move)
        discount = min(1, seq_cnt / (len(sequence) * 1.0))
        feature = play_go.ToFeature(board, ko, turn, action, winner,
                                    RICH_OUTPUT, RICH_OUTPUT)
        print(','.join(list(map(str, feature))))

        valid, ko = play_go.PlayGo(board, turn, action)
        # TODO(neochio): fix encode.
        #if ko == None:
        #  ko = 0
        #else:
        #  ko = ko[0] * 9 + ko[1] - 9
        seq_cnt = seq_cnt + 1
    if surrendered:
        feature = play_go.ToFeature(board, ko, play_go.FlipTurn(turn),
                                    play_go.SURRENDER, winner, RICH_OUTPUT,
                                    RICH_OUTPUT)
        print(','.join(list(map(str, feature))))
logging.warning('win_count: %s' % str(win_count))
コード例 #5
0
ファイル: autoplay_policy.py プロジェクト: neoblazes/wonpago
if len(sys.argv) == 3:
  epsilon = float(sys.argv[2])

STONE_CODE = ' BW'
POS_CODE = ' abcdefghi'
ACTION_CODE = { 0: 'P', 82: 'S' }

move_sequences = []

# Load model and predict
model_fn = importlib.import_module('%s.model_fn' % model_dir)
estimator = model_fn.GetEstimator(model_dir)
board, ko, turn = play_go.InitBoard()
passed = False
while True:
  feature = play_go.ToFeature(board, ko, turn, 0, 0, True, True)
  print(play_go.SPrintBoard(feature[:-1]))
  x_test, _ = train_lib.parse_row(feature, True)  # TODO: make configuable
  predict = estimator.predict(np.asarray(x_test, dtype=np.float32))
  probabilities = list(predict)[0]['probabilities']

  actions = []
  for i in range(len(probabilities)):
    actions.append([train_lib.UnpackAction(i) if i > 0 and i < 82
                    else ACTION_CODE[i], probabilities[i]])

  actions[0][1] = actions[0][1] / 5  # suppress pass
  sorted_actions = sorted(actions, key=lambda x:x[1], reverse = True)
  print(sorted_actions[:5])
  if sorted_actions[0][0] == 'P':
    move_sequences.append('%s[]' % STONE_CODE[turn])
コード例 #6
0
                  int) or len(feature) == 1:  # Select from candidates mode.
        feature = int(feature)
        if feature == 0:
            feature = [0] * 81 + [-1] + [0] * 3
        else:
            feature = forwards[int(feature) -
                               1] + ',0'  # TODO: unify result column
    if isinstance(feature, (list, tuple)):
        feature = list(feature)
    else:
        feature = feature.split(',')
    feature = list(map(float, feature))[:-1]  # Remove result column
    board, last_move, ko = play_go.FromFeature(feature[:81] +
                                               feature[-3:])  # Uses board only
    x_test = np.array(
        [play_go.ToFeature(board, last_move, ko, 0, True, True)[:-1]],
        dtype=np.float32)
    PrintBoard(feature, list(estimator.predict(x_test))[0])

    # get all features one step forward
    features = play_go.FowardFeatures(feature)
    # Batch eval (for performance)
    x_test = np.array(features, dtype=np.float32)
    pred_tf = estimator.predict(x_test)
    move_scores = {}
    idx = 0
    for pred in pred_tf:
        move_scores[','.join(list(map(str, features[idx])))] = pred
        idx = idx + 1
    surrender = False
    forwards = []
コード例 #7
0
ファイル: policy_rpc.py プロジェクト: neoblazes/wonpago
def genmove():
    global board, ko, turn
    feature = play_go.ToFeature(board, ko, turn, 0, 0)
    return get_forwards(','.join(str(x) for x in feature[:-2]))
コード例 #8
0
     move_count = 0
     print('=')
     print
 elif tokens[0] == 'play':
     turn = TURN_STR[tokens[1]]
     if tokens[2] != 'pass':
         pos0 = tokens[2][0].replace('j', 'i')
         pos = (ord(pos0) - ord('a') + 1) * 10 + int(tokens[2][1])
         valid, ko = play_go.PlayGo(board, turn, pos)
     turn = play_go.FlipTurn(turn)
     move_count += 1
     print('=')
     print
 elif tokens[0] == 'genmove':
     turn = TURN_STR[tokens[1]]
     feature = play_go.ToFeature(board, ko, turn)[:-2]
     actions = s.get_forwards(','.join(list(map(str, feature))))
     prob_sum = 0.0
     for ap in actions:
         a = ap[0]
         if a in ('P', 'S'):
             continue
         prob_sum += ap[1]
     if move_count > 10 and (actions[0][0] == 'P' or prob_sum == 0.0):
         print('= PASS')
     else:
         rand = random.random() * prob_sum
         for ap in actions:
             a = ap[0]
             if a in ('P', 'S'):
                 continue