Git Product home page Git Product logo

ai_dynamic_environment's Introduction

AI_Dynamic_Environment

Setup

Once you have cloned the repository onto your machine, simply move or copy the environment.py file into the directory where your jupyter notebook is being run. From there, you can simply add from environment import truly_dynamic_environment, replay to gain access to these two functions.

Assumptions that I made about the agents

I made a couple of assumptions about the agents that would be playing together in order for this to work smoothly. The first is that your agent accepts the current board state as the first argument formatted as a 2d numpy array. The second assumption that I made about the agents is that they return a python dictionary where one of the keys is "move" and the value for that key is an integer value representing the column that the agent wishes to place a piece in (0 indexed). For example, my agents return a dictionary that looks something like this(as long as the move key exists, this environment should work):

{
    "move":5,
    "value":0.563
}

Usage

Truly_dynamic_environment

The function header for truly_dynamic_environment is as follows: truly_dynamic_environment(players,size=(6,7),visual=False,board=None) Where the players is a python dictionary containing information about the algorithms that will be playing against each other. Size is the board size, defaulted to 6x7. Visual is a boolean attribute that allows you to view the agents play eachother in realtime. note: the visual attribute will only work in jupyter notebook, not a regular python file. And board is the board to start from, setting this value to None will generate a fresh board for the agents to play on.

Players dictionary

The players dictionary should be formatted as follows note that the first agent in the array should ALWAYS be playing 1 and the second should ALWAYS be playing -1 for proper functionality:

players = [
    {
        "algo":<player_1_agent_function>,
        "player":1,
        "args":{<a dictionary of the other arguments to pass to the function>}
    },
    {
        "algo":<player_2_agent_function>,
        "player":-1,
        "args":{<a dictionary of the other arguments to pass to the function>}
    },
    
]

For example, to play my alpha beta pruning agent against my monte carlo agent, I would create players as follows:

players = [
    {
        "algo":a_b_cutoff_search,
        "player":1,
        "args":{
            "verbose":False,
            "cutoff":7,
            "player":1,
            "eval_func":monte_carlo_threaded_eval
        }
    },
    {
        "algo":pmcs,
        "player":-1,
        "args":{
            "N":3600,
            "verbose":False,
            "player":-1,
            "playout_func":random_player
        }
    },
    
]

Where the function headers for my alpha beta cutoff and pure monte carlo look like a_b_cutoff_search(board,cutoff=None,player=1,verbose=False,eval_func=HelperFunctions.evaluate_board) and pmcs(board,N=50,player=1,verbose=False,playout_func=random_player) respectively.

Return types

The dynamic environment function returns 3 objects. The first is a python dictionary formatted as follows:

result = {
    "algo_info": {
        "<Algo_1_name>: {
            "time": <array of the time it took the algo to move per turn>
        },
        "<Algo_2_name> : {
            "time": <array of the time it took the algo to move per turn>
        }
    },
    "turns_taken":<total number of turns taken by both agents>,
    "winner": <1 or -1 depending on which algo won the game>
} 

The second is the final board where one of the agents won and the third is a list containing all board states that the agents got into.

Replay

This function simply allows the user to view the game played by the agents and accepts a list of all board states and optionally, a sleep value. The sleep value is how long the funciton waits before showing you the next move that an agent made and is defaulted to 1. note that, similar to the visual attribute in the environment, this function is designed to only work in Jupyter Notebook

Using it all together

An example of how to use this environment can be seen below:

from environment import truly_dynamic_environment,replay
playerz = [
    {
        "algo":random_player,
        "player":1,
        "args":{}
    },
    {
        "algo":random_player,
        "player":-1,
        "args":{}
    }
]

result,final_board,all_boards = truly_dynamic_environment(playerz,size=(6,7),visual=True)

replay(all_boards)

ai_dynamic_environment's People

Contributors

alexshock66 avatar

Stargazers

Michael Hahsler avatar

Watchers

 avatar

Forkers

mhahsler

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.