-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.py
More file actions
42 lines (32 loc) · 1.59 KB
/
Copy pathAgent.py
File metadata and controls
42 lines (32 loc) · 1.59 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
from WarehouseEnv import WarehouseEnv, manhattan_distance
import random
class Agent:
# returns the next operator to be applied - i.e. takes one turn
def run_step(self, env: WarehouseEnv, agent_id, time_limit):
raise NotImplementedError()
# returns list of legal operators and matching list of states reached by applying them
def successors(self, env: WarehouseEnv, robot_id: int):
operators = env.get_legal_operators(robot_id)
children = [env.clone() for _ in operators]
for child, op in zip(children, operators):
child.apply_operator(robot_id, op)
return operators, children
def heuristic(self, env: WarehouseEnv, robot_id: int):
robot = env.get_robot(robot_id)
other_robot = env.get_robot((robot_id + 1) % 2)
return robot.credit - other_robot.credit
# picks random operators from the legal ones
class AgentRandom(Agent):
def run_step(self, env: WarehouseEnv, robot_id, time_limit):
operators, _ = self.successors(env, robot_id)
return random.choice(operators)
class AgentGreedy(Agent):
def run_step(self, env: WarehouseEnv, robot_id, time_limit):
operators = env.get_legal_operators(robot_id)
children = [env.clone() for _ in operators]
for child, op in zip(children, operators):
child.apply_operator(robot_id, op)
children_heuristics = [self.heuristic(child, robot_id) for child in children]
max_heuristic = max(children_heuristics)
index_selected = children_heuristics.index(max_heuristic)
return operators[index_selected]