Giter VIP home page Giter VIP logo

bomberai's Introduction

BomberAI

AI in games project. Bomberman game written in python using pygame.

Setup

  1. Create virtual env with your favourite tool
  2. Activate it
  3. To install required dependencies (based on your system) run
python install.py

Run

Run GUI:

python menu.py

Bots

Random

This bot picks random path, drop the bomb and run away to the safe place.

DFS bot

This bot picks shortest path to the destroyable object using DFS algorithm, drop the bomb and run away to the safe place.

Q-learning based bot

This bot picks the best move based on the Q table

Deep Q-learning based bot

This bot picks the best move using neural network

Training bots

Q-learning

1. Write reward function

def get_reward(player_alive: bool, action: Action, is_move_possible: bool,
                suicide: bool, kills: int, destroyed_boxes: int) -> float:
    ...
    return reward

Parameters

  • player_alive: bool
    True if the bot is alive, otherwise False
  • action: Action
    The action chosen by bot in given moment
  • is_move_possible: bool
    True if action is possible, otherwise False
  • suicide: bool
    True if the bot killed himself with his bomb, otherwise False
  • kills: int
    How many enemies the bot's bombs killed in given moment
  • destroyed_boxes: int
    How many boxes the bot's bombs destroyed in given moment

2. Init model

model = Model()

3. Load Qtable (optional) - use to finetune model or continue training from save

model.load(path: str)

Parameters

  • path: str
    Path to file with qtable

4. Compile model

model.compile(get_reward: Callable, state_type: str,state_range: int,
                min_enemy_dist: int, learning_rate=0.1, discount=0.98,
                epsilon=0.1, de=0.01, gamma=0.9, n_past_states=10)

Parameters

  • get_reward: Callable
    reward function
  • state_type: str
    Type of state used by bot. Possible 'full', 'circle', 'square', 'cross'
  • state_range: int
    Radius of the bot surrounding shape
  • min_enemy_dist: int
    Minimum distance from enemies included in state
  • learning_rate: float
    Learning rate parameter
  • discount: float
    Discount factor
  • epsilon : float
    Exploration chance
  • de : float
    Parameter to decreasing epsilon at the end of the epoch
  • gamma : float
    Factor to update past states (gamma ^ t[i]) * reward, where i in [0..n_past_states]. The earlier the state, the less it improves.
  • n_past_states: int
    How many past states to update

5. Set game

model.set_game(grid: np.ndarray[int], en1_alg: Algorithm, en2_alg: Algorithm,
                en3_alg: Algorithm, box_density: int | Tuple[int, int] = 5,
                shuffle_positions: bool = True, max_playing_time=120)

Parameters

  • grid: np.ndarray[int]
    A map of the maze for players. Ground = 0, unbreakable wall = 1,
  • en1_alg: Algorithm
    Algorithm of the first enemy
  • en2_alg: Algorithm
    Algorithm of the second enemy
  • en3_alg: Algorithm
    Algorithm of the third enemy
  • box_density: int | Tuple[int, int]
    How densely the boxes are to be arranged on the map
  • shuffle_positions: bool = True
    Whether to shuffle players' positions
  • max_playing_time: int = 120
    Maximum time for gameplay

6. Fit

model.fit(epochs: int = 10, episodes: int = 1000, start_epoch: int = 0,
            show_game: bool = False, path_to_save: str = 'qtable.csv',
            log_file: str = 'log.csv')

Parameters

  • epochs: int = 10
    The number of epochs to train the model.
  • episodes: int = 1000
    The number of episodes to run.
  • start_epoch: int = 0
    The starting epoch for training.
  • show_game: bool = False
    Whether to display the game during training.
  • path_to_save: str = 'qtable.csv'
    The path to save the Q-table.
  • log_file: str = 'log.csv'
    The path to save the log file.

Deep-Q network based bot

In progress...

Credits

Sprites: https://opengameart.org/content/bomb-party-the-complete-set

Game mechanics based on: https://github.com/Forestf90/Bomberman

bomberai's People

Contributors

maciekpajak avatar warzkos avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

happywht

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.