-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathagent.py
More file actions
73 lines (60 loc) · 2.54 KB
/
agent.py
File metadata and controls
73 lines (60 loc) · 2.54 KB
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
# structures and helper function definitions were generated by ChatGPT
# changes and implementation made by Jade Neeley
# additional comments, cleanup, and structure by Nina Rubanovich and Gavin Boley
# Define the 2D grid
GRID_ROWS = 4
GRID_COLS = 5
# Define the actions
ACTIONS = ['Left', 'Right', 'Up', 'Down', 'Suck']
# Define the action costs
ACTION_COSTS = {
'Left': 1.0,
'Right': 0.9,
'Up': 0.8,
'Down': 0.7,
'Suck': 0.6
}
# Define the problem instances
PROBLEM_INSTANCES = [
{
'initial_location': (2, 2),
'dirty_squares': [(1, 2), (2, 4), (3, 5)]
},
{
'initial_location': (3, 2),
'dirty_squares': [(1, 2), (2, 1), (2, 4), (3, 3)]
}
]
# Define helper functions
class VacuumWorldProblem:
def __init__(self, initial_location, dirty_squares):
# Create the Vacuum World initial state
self.initial_location = initial_location
self.dirty_squares = set(dirty_squares)
self.current_location = initial_location
def is_goal_state(self):
return not self.dirty_squares # If any dirty squares exist, goal state not met
def get_successors(self): # Generate potential states for successor actions.
successors = []
for action in ACTIONS:
new_state = self.apply_action(action)
if new_state != self:
successors.append((action, new_state))
return successors
def apply_action(self, action): # Apply the specified action to the current state, creates new iteration
if action == 'Left':
new_location = (self.current_location[0], max(1, self.current_location[1] - 1))
elif action == 'Right':
new_location = (self.current_location[0], min(GRID_COLS, self.current_location[1] + 1))
elif action == 'Up':
new_location = (max(1, self.current_location[0] - 1), self.current_location[1])
elif action == 'Down':
new_location = (min(GRID_ROWS, self.current_location[0] + 1), self.current_location[1])
else:
new_location = self.current_location
if new_location == self.current_location: # No impact on state, nothing changed, return same state
return self
new_dirty_squares = self.dirty_squares.copy()
if new_location in new_dirty_squares: # Agent has successfully cleaned a dirty square by moving to it
new_dirty_squares.remove(new_location) # Therefore removes the location of the dirty square
return VacuumWorldProblem(new_location, new_dirty_squares) # New State