-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
167 lines (133 loc) · 5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import random
import numpy as np
from pydantic import BaseModel
from openai import OpenAI
class GridEnvironment:
def __init__(self, size, goal_position):
self.size = size
self.goal_position = goal_position
self.agent_position = [0, 0] # Agent starts at top-left corner
self.hostile_positions = []
def reset(self):
self.agent_position = [0, 0]
return np.array(self.agent_position)
def step(self, action):
if action == 0 and self.agent_position[0] > 0: # UP
self.agent_position[0] -= 1
elif action == 1 and self.agent_position[0] < self.size - 1: # DOWN
self.agent_position[0] += 1
elif action == 2 and self.agent_position[1] > 0: # LEFT
self.agent_position[1] -= 1
elif action == 3 and self.agent_position[1] < self.size - 1: # RIGHT
self.agent_position[1] += 1
def is_done(self):
return self.agent_position == self.goal_position
def render(self):
for i in range(self.size):
for j in range(self.size):
if [i, j] == self.agent_position:
print("A", end=" ") # Agent's position
elif [i, j] == self.goal_position:
print("G", end=" ") # Goal position
else:
print(".", end=" ")
print()
class HostileEnvironment(GridEnvironment):
def __init__(self, size, goal_position, hostile_positions):
super().__init__(size, goal_position)
self.hostile_positions = hostile_positions
def reset(self):
self.agent_position = [0, 0]
return np.array(self.agent_position)
def step(self, action):
super().step(action)
if self.agent_position in self.hostile_positions:
self.agent_position = [0, 0]
def render(self):
for i in range(self.size):
for j in range(self.size):
if [i, j] == self.agent_position:
print("A", end=" ") # Agent's position
elif [i, j] == self.goal_position:
print("G", end=" ") # Goal position
elif [i, j] in self.hostile_positions:
print("H", end=" ") # Hostile position
else:
print(".", end=" ")
print()
class DeterministicAgent:
def __init__(self, env):
self.env = env
def choose_action(self):
if self.env.agent_position[0] < self.env.goal_position[0]:
return 1
elif self.env.agent_position[0] > self.env.goal_position[0]:
return 0
elif self.env.agent_position[1] < self.env.goal_position[1]:
return 3
elif self.env.agent_position[1] > self.env.goal_position[1]:
return 2
class RandomAgent:
def __init__(self, env):
self.env = env
def choose_action(self):
return random.randint(0, 3)
class ActionResponse(BaseModel):
action: int
class OpenAIAgent:
def __init__(self, env):
self.env = env
self.client = OpenAI()
def choose_action(self):
prompt = (
"You are an agent and your goal is to reach the target position. "
f"The grid size is {self.env.size}x{self.env.size}. "
"The grid has hostile positions that you need to avoid. "
"You will be given your current position and the target position. "
"You need to choose the best action to move closer to the target. "
"Actions are: 0 (UP), 1 (DOWN), 2 (LEFT), 3 (RIGHT)."
)
completion = self.client.beta.chat.completions.parse(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": prompt},
{
"role": "user",
"content": (
f"current position: {self.env.agent_position},"
f"target position: {self.env.goal_position}. "
f"hostile positions: {self.env.hostile_positions}. "
),
},
],
response_format=ActionResponse,
)
response: ActionResponse | None = completion.choices[0].message.parsed
if response:
return response.action
else:
return 0
def execute(agent):
num_of_tries = 0
while not agent.env.is_done() and num_of_tries < 100:
action = agent.choose_action()
agent.env.step(action)
agent.env.render()
num_of_tries += 1
return num_of_tries
def main():
env = GridEnvironment(size=4, goal_position=[3, 3])
env = HostileEnvironment(
size=4, goal_position=[3, 3], hostile_positions=[[3, 0], [2, 1]]
)
agent = RandomAgent(env)
agent = OpenAIAgent(env)
agent = DeterministicAgent(env)
num_of_tries = execute(agent)
if agent.env.is_done():
print(f"done in {num_of_tries} steps")
env.render()
else:
print("failed!!")
if __name__ == "__main__":
main()