Skip to content

Commit cd75fdb

Browse files
committed
add test.py
1 parent 413b354 commit cd75fdb

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

FA22/intro-ai-series/workshop-1-ai-search-algorithms/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ intro-ai-series
100100
You can write something up or use the marketing description.
101101
-->
102102

103+
## 3.1 Environment Setup
104+
105+
```
106+
conda env create -f environment.yaml
107+
conda activate ai
108+
```
109+
103110
Workshop "Intro to AI: Search Algorithms" consists of <!-- however many you choose --> components:
104111
- [Notebook](<!-- Local Path to Notebook -->) with completed code and explanations.
105112
- [Summary Graphic](<!-- Local Path to Summary Graphic -->) to summarize key points of the workshop.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: ai
2+
channels:
3+
- defaults
4+
dependencies:
5+
- python=3.8
6+
- pip
7+
- pip:
8+
- termcolor
9+
- gym
10+
- gym[box2d]
11+
- moviepy
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import gym
2+
from gym.utils.save_video import save_video
3+
from time import sleep
4+
from search import *
5+
6+
'''
7+
Key:
8+
S = Start
9+
F = Frozen
10+
H = Hole
11+
G = Goal
12+
'''
13+
14+
MAPS = {"4x4":["SFFF", "FHFH", "FFFH", "HFFG"],
15+
"8x8":["SFFFFFFF", "FFFFFFFF", "FFFHFFFF", "FFFFFHFF",
16+
"FFFHFFFF", "FHHFFFHF", "FHFFHFHF", "FFFHFFFG"]}
17+
MAP = "8x8" # can be 8x8 or 4x4
18+
19+
USE_DETERMINISTIC=True # runs a deterministic path which results in Success
20+
TRAJECTORY = {"4x4": [1, 1, 2, 2, 1, 2],
21+
"8x8": [2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1]}
22+
23+
ACTIONS = {0: "LEFT", 1: "DOWN", 2: "RIGHT", 3: "UP"}
24+
RENDER_MODE="rgb_array_list"
25+
26+
27+
def main():
28+
env = gym.make("FrozenLake-v1", desc=MAPS[MAP], render_mode=RENDER_MODE, is_slippery=False)
29+
env.action_space.seed(42)
30+
observation, info = env.reset(seed=42)
31+
32+
step_starting_index = 0
33+
episode_index = 0
34+
35+
if USE_DETERMINISTIC:
36+
for step, action in enumerate(TRAJECTORY[MAP]):
37+
observation, reward, terminated, truncated, info = env.step(action) # deterministic action
38+
39+
print("REWARD:", reward, "| ACTION:", ACTIONS[action])
40+
sleep(0.01) # for spacing out rendering of frames
41+
42+
if terminated or truncated:
43+
save_video(
44+
env.render(),
45+
"videos",
46+
fps=env.metadata["render_fps"],
47+
step_starting_index=step_starting_index,
48+
episode_index=episode_index,
49+
name_prefix=MAP
50+
)
51+
step_starting_index = step + 1
52+
episode_index += 1
53+
observation, info = env.reset()
54+
env.close()
55+
56+
else:
57+
for step in range(1000):
58+
action = env.action_space.sample() # randomly sampled action
59+
observation, reward, terminated, truncated, info = env.step(action)
60+
61+
print("REWARD:", reward, "| ACTION:", ACTIONS[action])
62+
sleep(0.01) # for spacing out rendering of frames
63+
64+
if terminated or truncated:
65+
save_video(
66+
env.render(),
67+
"videos",
68+
fps=env.metadata["render_fps"],
69+
step_starting_index=step_starting_index,
70+
episode_index=episode_index,
71+
name_prefix=MAP
72+
)
73+
step_starting_index = step + 1
74+
episode_index += 1
75+
observation, info = env.reset()
76+
env.close()
77+
78+
if __name__ == "__main__":
79+
main()
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)