Coder Social home page Coder Social logo

tictactoe-rl's Introduction

Tic Tac Toe AI Agent

AI agent trained to play tic tac toe using reinforcement learning (q learning). Environment (env.py) creates a tic tac toe game with 2 users using object oriented programming. We then define the q table as a dictionary with key - value pairs being the states - q values pairs. Then, using the q learning algorithm, we train the agent for 50,000 episodes and track the rewards.

Training rewards (moving average every 100 episodes)

Reward

Agent goals

We need a player class which represents our agent, and the player is able to:

  • Choose actions based on current estimation of the states
  • Record all the states of the game
  • Update states-value estimation after each game
  • Save and load the q table

Parameters

Training = 50000 
epsilon = 0.9
DISCOUNT = 0.95
EPS_DECAY = 0.9998
WIN_REWARD = 100
LOSE_REWARD = -200
TIE_REWARD = 50
CHANCE_PENALTY = -1
SHOW_EVERY = 100
LEARNING_RATE = 0.1

Q Table Initialisation

Q Table is a dictionary with all possible states as the keys and q values for each possible action as a list.

if q_table is None:
    q_table = {}
    strings = ['X--------', 'XO-------', 'XXO------', 'XXOO-----', 'XXXOO----', 'XXXOOO---', 'XXXXOOO--', 'XXXXOOOO-', 'XXXXXOOOO']
    states = ['---------']
    for i in strings:
        p = permutations(i)
        for j in list(p):
            stringA = ''.join(j)
            if stringA not in states:
                states.append(stringA)
    print('Number of states:',len(states))
    values = []
    for i in states:
        value = list(np.random.uniform(-5,0,size=9))
        for index,j in enumerate(i):
            if j == 'X' or j == 'O':
                value[index] = None
        values.append(value)

    for i in range(len(states)):
        q_table[states[i]] = values[i]


    with open('QTable.pickle', 'wb') as handle:
        pickle.dump(q_table, handle, protocol=pickle.HIGHEST_PROTOCOL)

Game object

Defined in Board.py, contains class methods for taking chances, printing current state and deciding the winner or if it is a tie.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.