Git Product home page Git Product logo

tetrislfd's Introduction

Tetris using Learning from Demonstrations

This porject presents the classic game of Tetris built using PyGame whcich makes use of anm agente to learn trough human data how to play this mytical game.

Usage

main.py for playing Tetris manually.

main_imitation.py for training and data collection Imitation learning.

The different modes of use can be applied by modifying the following functions:

if __name__ == "__main__":
    train() # Trains and validate the model using data acquired playing
    model.save_weights() # Save the model weigths
    model.load_weights('_10k_01_nat1') # Load the model weigths
    main() # Shows a model trained or allow the user to play and generate new data
    generate_data(400) #Generates sinthetic data using a natural selection algorithm

If the process you wanna do is take data, you must select in main_imitation.py.

def main(manual=1): # Manual=1(user data acquisition)

dfdfdfdfdfdfdfdfddffdfdf3232343242

Environment

To edit Tetris behaviour change contructor params.

env = Tetris({
  'reduced_shapes': True,
  'reduced_grid': True
})

Where reduced_shapes control the type of pieces used in the learning process and reduced_grid control de type of grid.

Enviorment follows regular OpenAI standard.

from enviorment.tetris import Tetris

from Imitation.agent import imitation_agent
from nat_selection.model import Model
from dqn.agent import DQN

env = Tetris() 

# Agent can either be custom.
 agent = imitation_agent(env)


total_score = 0
state, reward, done, info = env.reset()

while not done:

  # for random action
  action = env.action_sample 
  
  # for agent action
  # Be aware that different agents have different methods of getting the next action.
  action = agent.policy(state)
  
  state, reward, done, info = env.step(action)
  total_score += reward
  
  env.render()

State is a three dimensional array with 2 layers representing the placed blocks and the ones you are controlling. Blocks are 1 and blank cells are 0.

Loading pretrained models can be done like this.

from enviorment.tetris import Tetris

from Imitation.agent import imitation_agent
from nat_selection.model import Model
from dqn.agent import DQN

env = Tetris() 

# Imitation
agent = imitation_agent(env)
agent.load_weights('_10k_01_nat1')
# Where the argument is the suffix after "weights" in ./Imitation/weights

Install

For installation, enter the directory

cd TetrisLfd/

and install al the dependencies:

pip install -r requirements.txt

and Pytorch

tetrislfd's People

Contributors

jlmadsen avatar martorb avatar boundlesscalm avatar mmm4707 avatar adrianprados avatar

Stargazers

 avatar

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.