Skip to content

Commit 74c13bd

Browse files
committed
updtd runner to obtain user specified hyperparams
1 parent a9d6409 commit 74c13bd

File tree

4 files changed

+95
-22
lines changed

4 files changed

+95
-22
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Running snake game without Gym Environment
22
To run snake game without gym environment, you can look into folder [multiagent-cooperative env](https://github.com/crazymuse/snakegame-numpy/blob/master/multiagent-cooperative-env/SnakeExample.ipynb). Open jupyter notebook from same and run the sample code.
33

4+
![](preview.png)
5+
46
### Prerequisities
57
* Install [OpenAI Gym](https://github.com/openai/gym) and [Baselines](https://github.com/openai/baselines)
68
* Install IPython and numpy via pip or conda.

multiagent-cooperative-env/SnakeExample.ipynb

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Training the multiAgent Environment"
8+
]
9+
},
310
{
411
"cell_type": "code",
512
"execution_count": 1,
613
"metadata": {},
714
"outputs": [
815
{
9-
"ename": "ModuleNotFoundError",
10-
"evalue": "No module named 'tensorflow'",
11-
"output_type": "error",
12-
"traceback": [
13-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
14-
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
15-
"\u001b[0;32m<ipython-input-1-3cef2640497c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mSnakeTrainer\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtensorflow\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mIPython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisplay\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mclear_output\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtime\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0msleep\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
16-
"\u001b[0;32m~/snakegame-numpy/multiagent-cooperative-env/SnakeTrainer.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mSnakeEnv\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0msnakeEnv\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mtensorflow\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mSnakeModel\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mbaselines\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcommon\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtf_util\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
17-
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'tensorflow'"
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"PLAYING . . . TIME = 29 s\n",
20+
"PLAYER : 0\n",
21+
"|. 0 . . .|\n",
22+
"|. . . x .|\n",
23+
"|. . . . .|\n",
24+
"|. . . . .|\n",
25+
"|. . . . .|\n",
26+
"PLAYER : 1\n",
27+
"|. . . . .|\n",
28+
"|. . . . .|\n",
29+
"|. . . x .|\n",
30+
"|. . . . 0|\n",
31+
"|. . . . .|\n",
32+
"PLAYER : 2\n",
33+
"|. . x . .|\n",
34+
"|. . . . .|\n",
35+
"|. . . . .|\n",
36+
"|. . . . .|\n",
37+
"|. . . . 0|\n"
1838
]
1939
}
2040
],
@@ -25,23 +45,56 @@
2545
"from time import sleep;\n",
2646
"\n",
2747
"runner = SnakeTrainer.SnakeRunner()\n",
28-
"runner.play()\n",
29-
"clear_output()"
48+
"runner.train()"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"metadata": {},
54+
"source": [
55+
"# For Training MultiAgent MultiSnake Cooperative Environment\n",
56+
"In this case you will be able to observe multiple snakes fighting for the same resource in the same environment. This is an interesting case to try out, because they have to learn best stratagy to maximize the reward. Which means, ideally after extensive training, the far snake should pave the way for close by snakes (in order to maximize average reward)."
3057
]
3158
},
3259
{
3360
"cell_type": "code",
34-
"execution_count": null,
61+
"execution_count": 1,
3562
"metadata": {},
36-
"outputs": [],
63+
"outputs": [
64+
{
65+
"name": "stdout",
66+
"output_type": "stream",
67+
"text": [
68+
"PLAYING . . . TIME = 29 s\n",
69+
"PLAYER : 0\n",
70+
"|. . . . x|\n",
71+
"|. . . . 0|\n",
72+
"|. . . . .|\n",
73+
"|. . . 2 .|\n",
74+
"|. 1 . . .|\n",
75+
"PLAYER : 1\n",
76+
"|. . . . .|\n",
77+
"|. . . . .|\n",
78+
"|2 . . . .|\n",
79+
"|1 o . . .|\n",
80+
"|. 0 . x .|\n",
81+
"PLAYER : 2\n",
82+
"|. . . . .|\n",
83+
"|. 2 . . .|\n",
84+
"|. 1 0 . .|\n",
85+
"|. . . . .|\n",
86+
"|. . x . .|\n"
87+
]
88+
}
89+
],
3790
"source": [
38-
"for i in range(50):\n",
39-
" runner.user_step(action=1)\n",
40-
" runner.env.display(verbose=True)\n",
41-
" print (runner.env.get_state())\n",
42-
" sleep(0.5)\n",
43-
" clear_output(wait=True)\n",
44-
" \n"
91+
"import SnakeTrainer;\n",
92+
"import tensorflow as tf;\n",
93+
"from IPython.display import clear_output\n",
94+
"from time import sleep;\n",
95+
"\n",
96+
"runner = SnakeTrainer.SnakeRunner(n_envs=10,n_snakes=3)\n",
97+
"runner.train()\n"
4598
]
4699
},
47100
{
@@ -68,7 +121,7 @@
68121
"name": "python",
69122
"nbconvert_exporter": "python",
70123
"pygments_lexer": "ipython3",
71-
"version": "3.6.4"
124+
"version": "3.5.4"
72125
}
73126
},
74127
"nbformat": 4,

multiagent-cooperative-env/SnakeTrainer.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,24 @@ class SingleSnakeGameHyperParameters:
9090

9191
SAVE_SKIP = 1000 # Save after 1000 iterations
9292
DEFAULT_SAVE_PATH = "./model/model.ckpt"
93+
94+
def update_params(self,n_envs,n_snakes,grid_length,grid_width):
95+
if n_envs != None:
96+
self.N_ENVS=n_envs
97+
if n_snakes != None:
98+
self.N_SNAKES=n_snakes
99+
if grid_length != None:
100+
self.GRID_LENGTH = grid_length
101+
self.GRID_WIDTH = grid_length
102+
if grid_width != None:
103+
self.GRID_WIDTH = grid_width
104+
self.N_BATCH = self.N_STEPS*self.N_ENVS*self.N_SNAKES
105+
self.OBS_SHAPE = (None, self.GRID_LENGTH, self.GRID_WIDTH, 1)
106+
self.BATCH_OBS_SHAPE = (self.N_BATCH,self.GRID_LENGTH,self.GRID_WIDTH,1)
107+
self.STATE_SHAPE = (None,self.N_STATES)
108+
self.BATCH_STATE_SHAPE = (self.N_BATCH,self.N_STATES)
93109

110+
94111
class Model:
95112
def __init__(self,policy,p,has_state):
96113
"""
@@ -191,8 +208,9 @@ class SnakeRunner(object):
191208
"""
192209
This class will take the Model and interface it with Snake Env
193210
"""
194-
def __init__(self):
211+
def __init__(self,n_envs=None,n_snakes=None,grid_length=None,grid_width=None):
195212
self.p = SingleSnakeGameHyperParameters() # Game Parameters;
213+
self.p.update_params(n_envs=n_envs,n_snakes=n_snakes,grid_length=grid_length,grid_width=grid_width)
196214
p = self.p
197215
self.env = snakeEnv.MultiAgentSnakeGame(length = p.GRID_LENGTH,
198216
width = p.GRID_WIDTH,n_envs=self.p.N_ENVS,n_snakes=self.p.N_SNAKES)

preview.png

16.3 KB
Loading

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy