Skip to content

Commit 76cfb1d

Browse files
Merge pull request #1861 from ayush-09/game
SpaceInvaders Agent
2 parents 6d69e58 + 525bda4 commit 76cfb1d

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

SpaceInvader-Agent/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Reinforcement Learning with SpaceInvaders-v0
2+
3+
This repository contains code for implementing reinforcement learning using the SpaceInvaders-v0 environment from the OpenAI Gym.
4+
5+
## Prerequisites
6+
7+
To run this code, you need the following dependencies:
8+
9+
- Python 3.x
10+
- Gym: `pip install gym`
11+
- TensorFlow: `pip install tensorflow`
12+
- Keras-RL2: `pip install keras-rl2`
13+
14+
## Getting Started
15+
16+
1. Clone the repository: `git clone https://github.com/your_username/your_repository.git`
17+
2. Navigate to the cloned repository: `cd your_repository`
18+
19+
## Running the Code
20+
21+
1. Open the Python script `space_invaders_rl.py`.
22+
2. Configure the number of episodes and other parameters as needed.
23+
3. Run the script: `python space_invaders_rl.py`.
24+
25+
## Understanding the Code
26+
27+
The code performs the following steps:
28+
29+
1. Imports the necessary libraries and initializes the SpaceInvaders-v0 environment.
30+
2. Runs a specified number of episodes, where each episode represents a game.
31+
3. Resets the environment for each episode and plays the game until completion.
32+
4. Renders the environment to visualize the game.
33+
5. Uses a random policy to select actions.
34+
6. Accumulates the score and prints the episode number and score.
35+
7. Closes the environment after all episodes have been completed.
36+
8. Builds a convolutional neural network model using Keras.
37+
9. Implements the DQN agent using the Keras-RL2 library.
38+
10. Compiles the agent with the Adam optimizer.
39+
11. Trains the agent on the SpaceInvaders-v0 environment.
40+
12. Tests the trained agent on a few episodes and calculates the average score.
41+
13. Saves the trained weights of the DQN agent.
42+
14. Loads the saved weights of the DQN agent.
43+
44+
45+
## Acknowledgments
46+
47+
- [OpenAI Gym](https://gym.openai.com/)
48+
- [Keras-RL2](https://github.com/wau/keras-rl2)
49+
50+
Feel free to modify and adapt this code according to your needs.
51+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Thu Jun 3 13:06:20 2021
4+
@author: Ayush
5+
"""
6+
7+
import gym
8+
env = gym.make("SpaceInvaders-v0")
9+
10+
episodes=10
11+
12+
for episode in range(1,episodes):
13+
state = env.reset()
14+
done = False
15+
score=0
16+
17+
while not done:
18+
env.render()
19+
state, reward,done, info = env.step(env.action_space.sample())
20+
score += reward
21+
print('Episode: {}\nScore: {}'.format(episode, score))
22+
env.close()
23+
24+
# Import Neural Network Packages
25+
from tensorflow.keras.models import Sequential
26+
from tensorflow.keras.layers import Dense, Flatten, Conv2D
27+
from tensorflow.keras.optimizers import Adam
28+
29+
def build_model(height,width,channels,actions):
30+
model = Sequential()
31+
model.add(Conv2D(32,(8,8),strides=(4,4), activation='relu', input_shape=(3, height,width,channels)))
32+
model.add(Conv2D(64,(4,4),strides=(2,2), activation='relu'))
33+
model.add(Conv2D(64,(4,4),strides=(2,2), activation='relu'))
34+
model.add(Flatten())
35+
model.add(Dense(512, activation= 'relu'))
36+
model.add(Dense(256,activation='relu'))
37+
model.add(Dense(64,activation='relu'))
38+
model.add(Dense(actions,activation='linear'))
39+
return model
40+
41+
height,width,channels = env.observation_space.shape
42+
actions = env.action_space.n
43+
44+
model = build_model(height,width,channels,actions)
45+
46+
47+
#Importing keras-rl2 reinforcement learning functions
48+
from rl.agents import DQNAgent
49+
from rl.memory import SequentialMemory
50+
from rl.policy import LinearAnnealedPolicy, EpsGreedyQPolicy
51+
52+
def build_agent(model,actions):
53+
policy = LinearAnnealedPolicy(EpsGreedyQPolicy(), attr='eps', value_max=1., value_min=.1, value_test=.2, nb_steps=10000)
54+
memory = SequentialMemory(limit=2000, window_length=3)
55+
dqn = DQNAgent(model=model, memory=memory, policy=policy, enable_dueling_network=True, dueling_type='avg', nb_actions=actions, nb_steps_warmup=1000)
56+
return dqn
57+
58+
dqn = build_agent(model, actions)
59+
60+
61+
dqn.compile(Adam(lr=0.001))
62+
63+
dqn.fit(env,nb_steps=40000, visualize=True, verbose=1)
64+
65+
import numpy as np
66+
scores = dqn.test(env,nb_episodes=10,visualize=True)
67+
print(np.mean(scores.history['episode_reward']))
68+
69+
dqn.save_weights('models/dqn.h5f')
70+
dqn.load_weights('models/dqn.h5f')

SpaceInvader-Agent/dqn.h5f.index

779 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)