Skip to content

Commit 8c4a6e1

Browse files
committed
BFS
1 parent 5202eb8 commit 8c4a6e1

File tree

1 file changed

+35
-1
lines changed
  • 2022/FA22/intro-ai-series/workshop-1-ai-search-algorithms/src

1 file changed

+35
-1
lines changed

2022/FA22/intro-ai-series/workshop-1-ai-search-algorithms/src/search.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import gym
2+
13
'''
24
Search Algorithms
35
'''
@@ -14,6 +16,38 @@ def dfs(self):
1416
def bfs(self):
1517
print("Algo: Breadth-First Search")
1618
# TODO: Implement BFS
19+
MAPS = {"4x4":["SGFF", "FHFH", "FFFH", "HFFF"],
20+
"8x8":["SFFFFFFF", "FFFFFFFF", "FFFHFFFF", "FFFFFHFF",
21+
"FFFHFFFF", "FHHFFFHF", "FHFFHFHF", "FFFHFFFG"]}
22+
MAP = "4x4" # can be 8x8 or 4x4
23+
24+
ACTIONS = {0: "LEFT", 1: "DOWN", 2: "RIGHT", 3: "UP"}
25+
RENDER_MODE="rgb_array_list"
26+
env = gym.make("FrozenLake-v1", desc=MAPS[MAP], render_mode=RENDER_MODE, is_slippery=False)
27+
env.action_space.seed(42)
28+
observation, info = env.reset(seed=42)
29+
queue = [[]]
30+
while len(queue) > 0 :
31+
acs = queue.pop(0)
32+
for i in range(4):
33+
rew = -1
34+
done = False
35+
obs, info = env.reset(seed=42)
36+
for a in acs:
37+
obs,rew, terminated, truncated, _ = env.step(a)
38+
if terminated or truncated:
39+
done=True
40+
break
41+
if rew > 0.0:
42+
print('Found path by taking following actions: ', acs)
43+
env.close()
44+
return acs
45+
if done:
46+
break
47+
queue.append(acs +[i] )
48+
49+
print("No sol found")
50+
return None
1751

1852

1953
def ucs(self):
@@ -23,4 +57,4 @@ def ucs(self):
2357

2458
def a_star(self):
2559
print("Algo: A* Search")
26-
# TODO: Implement A*
60+
# TODO: Implement A*

0 commit comments

Comments
 (0)