import specmine.tools.write_ttt_optimal

if __name__ == "__main__":
    specmine.script(specmine.tools.write_ttt_optimal.main)

import cPickle as pickle
import specmine

logger = specmine.get_logger(__name__)

specmine.annotations(
    out_path=("path to write policy pickle", ),
    player=("player number", "option", None, int),
)


def main(out_path, player=-1):
    """Compute the optimal TTT policy for the specified player."""

    # construct domain
    opponent_domain = specmine.rl.TicTacToeDomain(player=-1 * player)
    opponent_policy = specmine.rl.RandomPolicy(opponent_domain)

    domain = specmine.rl.TicTacToeDomain(player=player,
                                         opponent=opponent_policy)

    # compute the optimal policy
    policy = {}

    for state in domain.states:
        (board, player_to_move) = state
import specmine.experiments.ttt_analyze_evs

if __name__ == "__main__":
    specmine.script(specmine.experiments.ttt_analyze_evs.main)

import csv
import specmine

logger = specmine.get_logger(__name__)

specmine.annotations(
    out_path=("path to write output CSV",), number=("number of eigenvectors to compute", "option", None, int)
)


def main(out_path, number=9):
    """Analyze eigenvectors in the TTT domain."""

    B = number
    adict = specmine.tictac.load_adjacency_dict()
    (gameplay_NN, index) = specmine.discovery.adjacency_dict_to_matrix(adict)
    basis_NB = specmine.spectral.laplacian_basis(gameplay_NN, B)
    start = specmine.tictac.BoardState()
    rows = []

    for i in xrange(3):
        for j in xrange(3):
            board = start.make_move(1, i, j)
            n = index[(board, -1)]

            for b in xrange(B):
import specmine.tools.write_ttt_values

if __name__ == "__main__":
    specmine.script(specmine.tools.write_ttt_values.main)

import cPickle as pickle
import specmine

logger = specmine.get_logger(__name__)

specmine.annotations(
    out_path=("path to write values pickle", ),
    opponent_path=("path to opponent policy; random otherwise", ))


def main(out_path, opponent_path=None):
    """Compute the TTT value function using value iteration."""

    # construct domain
    opponent_domain = specmine.rl.TicTacToeDomain(player=-1)

    if opponent_path is None:
        opponent_policy = specmine.rl.RandomPolicy(opponent_domain)
    else:
        logger.info("loading opponent policy from %s", opponent_path)

        with specmine.openz(opponent_path) as opponent_file:
            opponent_policy = pickle.load(opponent_file)

    domain = specmine.rl.TicTacToeDomain(player=1, opponent=opponent_policy)
import specmine.tools.write_ttt_values

if __name__ == "__main__":
    specmine.script(specmine.tools.write_ttt_values.main)

import cPickle as pickle
import specmine

logger = specmine.get_logger(__name__)

specmine.annotations(
    out_path = ("path to write values pickle",),
    opponent_path = ("path to opponent policy; random otherwise",)
    )
def main(out_path, opponent_path = None):
    """Compute the TTT value function using value iteration."""

    # construct domain
    opponent_domain = specmine.rl.TicTacToeDomain(player = -1)

    if opponent_path is None:
        opponent_policy = specmine.rl.RandomPolicy(opponent_domain)
    else:
        logger.info("loading opponent policy from %s", opponent_path)

        with specmine.openz(opponent_path) as opponent_file:
            opponent_policy = pickle.load(opponent_file)

    domain = specmine.rl.TicTacToeDomain(player = 1, opponent = opponent_policy)

    # compute the value function
import specmine.experiments.ttt_analyze_evs

if __name__ == "__main__":
    specmine.script(specmine.experiments.ttt_analyze_evs.main)

import csv
import specmine

logger = specmine.get_logger(__name__)

specmine.annotations(
    out_path=("path to write output CSV", ),
    number=("number of eigenvectors to compute", "option", None, int),
)


def main(out_path, number=9):
    """Analyze eigenvectors in the TTT domain."""

    B = number
    adict = specmine.tictac.load_adjacency_dict()
    (gameplay_NN, index) = specmine.discovery.adjacency_dict_to_matrix(adict)
    basis_NB = specmine.spectral.laplacian_basis(gameplay_NN, B)
    start = specmine.tictac.BoardState()
    rows = []

    for i in xrange(3):
        for j in xrange(3):
            board = start.make_move(1, i, j)
            n = index[(board, -1)]
import specmine.tools.write_ttt_optimal

if __name__ == "__main__":
    specmine.script(specmine.tools.write_ttt_optimal.main)

import cPickle as pickle
import specmine

logger = specmine.get_logger(__name__)

specmine.annotations(out_path=("path to write policy pickle",), player=("player number", "option", None, int))


def main(out_path, player=-1):
    """Compute the optimal TTT policy for the specified player."""

    # construct domain
    opponent_domain = specmine.rl.TicTacToeDomain(player=-1 * player)
    opponent_policy = specmine.rl.RandomPolicy(opponent_domain)

    domain = specmine.rl.TicTacToeDomain(player=player, opponent=opponent_policy)

    # compute the optimal policy
    policy = {}

    for state in domain.states:
        (board, player_to_move) = state

        if player == player_to_move:
            (move_i, move_j, _) = specmine.tictac.ab_optimal_move(board, player)