/
team_battle_example.py
58 lines (49 loc) · 1.98 KB
/
team_battle_example.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
from abmarl.sim.gridworld.smart import SmartGridWorldSimulation
from abmarl.sim.gridworld.agent import (
GridObservingAgent,
MovingAgent,
AttackingAgent
)
from abmarl.sim.gridworld.actor import MoveActor, BinaryAttackActor
class BattleAgent(GridObservingAgent, MovingAgent, AttackingAgent):
def __init__(self, **kwargs):
super().__init__(
move_range=1,
attack_range=1,
attack_strength=1,
attack_accuracy=1,
view_range=3,
**kwargs
)
class TeamBattleSim(SmartGridWorldSimulation):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Action Components
self.move_actor = MoveActor(**kwargs)
self.attack_actor = BinaryAttackActor(**kwargs)
self.finalize()
def step(self, action_dict, **kwargs):
# Process attacks:
for agent_id, action in action_dict.items():
agent = self.agents[agent_id]
if agent.active:
attack_status, attacked_agents = \
self.attack_actor.process_action(agent, action, **kwargs)
if attack_status: # Attack was attempted
if not attacked_agents: # Attack failed
self.rewards[agent_id] -= 0.1
else:
for attacked_agent in attacked_agents:
if not attacked_agent.active: # Agent has died
self.rewards[attacked_agent.id] -= 1
self.rewards[agent_id] += 1
# Process moves
for agent_id, action in action_dict.items():
agent = self.agents[agent_id]
if agent.active:
move_result = self.move_actor.process_action(agent, action, **kwargs)
if not move_result:
self.rewards[agent.id] -= 0.1
# Entropy penalty
for agent_id in action_dict:
self.rewards[agent_id] -= 0.01