Commit c7c06f56 authored by leswing's avatar leswing
Browse files

a3c tic tac toe value search

parent 454d8b38
Loading
Loading
Loading
Loading
+28 −9
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ import deepchem as dc
import numpy as np
import random
import tensorflow as tf
import json
import time
import copy

@@ -119,7 +120,6 @@ class TicTacToeEnvironment(dc.rl.Environment):


class TicTacToePolicy(dc.rl.Policy):

    def create_layers(self, state, **kwargs):
        d1 = Flatten(in_layers=state)
        d2 = Dense(
@@ -148,21 +148,40 @@ class TicTacToePolicy(dc.rl.Policy):
        return {'action_prob': probs, 'value': value}


def main():
def eval_tic_tac_toe(value_weight, games=10 ** 4, rollouts=10 ** 5):
    """
    Returns the average reward over 1k games after 10k rollouts
    :param value_weight:
    :return:
    """
    env = TicTacToeEnvironment()
    policy = TicTacToePolicy()
  a3c = dc.rl.A3C(env, policy, entropy_weight=0, value_weight=0.25)
    a3c = dc.rl.A3C(env, policy, entropy_weight=0.01, value_weight=value_weight)
    a3c.optimizer = dc.models.tensorgraph.TFWrapper(
        tf.train.AdamOptimizer, learning_rate=0.01)
  a3c.fit(100000)
    a3c.fit(rollouts)
    rewards = []
    for i in range(games):
        env.reset()
        reward = -float('inf')
        while not env._terminated:
    print(env.display())
    print(a3c.predict(env._state))
            action = a3c.select_action(env._state)
    print(action)
    print(env.step(action))
  print(env.display())
            reward = env.step(action)
        rewards.append(reward)
    return np.mean(rewards)


def main():
    scores = {}
    value_weight = 0.05
    while value_weight <= 1.0:
        print(value_weight)
        score = eval_tic_tac_toe(value_weight)
        scores[value_weight] = score
        with open('tictactoe_value_search.json', 'w') as fout:
            fout.write(json.dumps(scores))
        value_weight += 0.05



if __name__ == "__main__":