Skip to content

Commit 13ad671

Browse files
committed
Implement behavior interface and multiple behavior classes for agent actions
1 parent 9db14e2 commit 13ad671

18 files changed

+282
-91
lines changed

src/behaviors/bhv_block.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
from pyrusgeom.geom_2d import *
55
from pyrusgeom.soccer_math import *
66
from service_pb2 import *
7+
from src.interfaces.IBehavior import IBehavior
78

8-
class Bhv_Block:
9+
10+
class Bhv_Block(IBehavior):
911
"""
1012
Bhv_Block is a behavior class that determines whether an agent should block the ball based on the predicted future positions of the ball and the players.
1113
Methods

src/behaviors/bhv_kick_planner.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from src.interfaces.IBehavior import IBehavior
2+
from src.interfaces.IAgent import IAgent
3+
from pyrusgeom.soccer_math import *
4+
from pyrusgeom.geom_2d import *
5+
from service_pb2 import *
6+
7+
8+
class BhvKickPlanner(IBehavior):
9+
"""
10+
Decision maker class for an agent with the ball.
11+
12+
Methods
13+
-------
14+
__init__():
15+
Initializes the WithBallDecisionMaker instance.
16+
17+
execute(agent: IAgent):
18+
Makes a decision for the agent when it has the ball.
19+
20+
_get_helios_offensive_planner():
21+
Returns an instance of HeliosOffensivePlanner.
22+
23+
_get_planner_evaluation(agent: IAgent):
24+
Returns an instance of PlannerEvaluation.
25+
26+
_get_planner_evaluation_effector(agent: IAgent):
27+
Returns an instance of PlannerEvaluationEffector.
28+
29+
_get_opponent_effector(agent: IAgent):
30+
Determines the opponent effector based on the agent's world model.
31+
"""
32+
33+
def __init__(self):
34+
pass
35+
36+
def execute(self, agent: IAgent):
37+
agent.logger.debug("--- WithBallDecisionMaker ---")
38+
from src.sample_player_agent import SamplePlayerAgent # Local import to avoid circular import
39+
assert isinstance(agent, SamplePlayerAgent)
40+
41+
agent.add_action(
42+
PlayerAction(helios_offensive_planner=self._get_helios_offensive_planner(agent))
43+
)
44+
45+
def _get_helios_offensive_planner(self, agent):
46+
""" Summary
47+
In this function you can create an instance of HeliosOffensivePlanner and set its attributes.
48+
The HeliosOffensivePlanner is a message that ask proxy to create a tree and find the best chain of actions and execute the first action of the chain.
49+
50+
Returns:
51+
_type_: HeliosOffensivePlanner
52+
"""
53+
res = HeliosOffensivePlanner(evaluation=self._get_planner_evaluation(agent))
54+
res.lead_pass = True
55+
res.direct_pass = True
56+
res.through_pass = True
57+
res.simple_pass = True
58+
res.short_dribble = True
59+
res.long_dribble = True
60+
res.simple_shoot = True
61+
res.simple_dribble = True
62+
res.cross = True
63+
64+
return res
65+
66+
def _get_planner_evaluation(self, agent: IAgent):
67+
return PlannerEvaluation(
68+
effectors=self._get_planner_evaluation_effector(agent),
69+
)
70+
71+
def _get_planner_evaluation_effector(self, agent: IAgent):
72+
return PlannerEvaluationEffector(
73+
opponent_effector=self._get_opponent_effector(agent),
74+
)
75+
76+
def _get_opponent_effector(self, agent: IAgent):
77+
wm = agent.wm
78+
79+
if wm.ball.position.x > 30:
80+
negetive_effect_by_distance = [-5, -4, -3, -2, -1]
81+
else:
82+
negetive_effect_by_distance = [-30, -25, -20, -15, -10, -4, -3, -2, -1]
83+
84+
return OpponentEffector(
85+
negetive_effect_by_distance=negetive_effect_by_distance,
86+
negetive_effect_by_distance_based_on_first_layer=False,
87+
)

src/behaviors/bhv_penalty.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
from src.interfaces.IAgent import IAgent
3+
from src.utils.convertor import Convertor
4+
from pyrusgeom.geom_2d import *
5+
from pyrusgeom.soccer_math import *
6+
from service_pb2 import *
7+
from src.interfaces.IBehavior import IBehavior
8+
9+
10+
class BhvPenalty(IBehavior):
11+
def __init__(self):
12+
pass
13+
14+
def execute(self, agent: IAgent) -> bool:
15+
agent.logger.debug("BhvPenalty.execute")
16+
from src.sample_player_agent import SamplePlayerAgent # Local import to avoid circular import
17+
assert isinstance(agent, SamplePlayerAgent)
18+
agent.add_action(PlayerAction(helios_penalty=HeliosPenalty()))

src/behaviors/bhv_setplay.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
from src.interfaces.IAgent import IAgent
3+
from src.utils.convertor import Convertor
4+
from pyrusgeom.geom_2d import *
5+
from pyrusgeom.soccer_math import *
6+
from service_pb2 import *
7+
from src.interfaces.IBehavior import IBehavior
8+
9+
10+
class BhvSetPlay(IBehavior):
11+
def __init__(self):
12+
pass
13+
14+
def execute(self, agent: IAgent) -> bool:
15+
agent.logger.debug("BhvSetPlay.execute")
16+
from src.sample_player_agent import SamplePlayerAgent # Local import to avoid circular import
17+
assert isinstance(agent, SamplePlayerAgent)
18+
agent.add_action(PlayerAction(helios_set_play=HeliosSetPlay()))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from src.interfaces.IBehavior import IBehavior
2+
from src.interfaces.IAgent import IAgent
3+
from pyrusgeom.soccer_math import *
4+
from pyrusgeom.geom_2d import *
5+
from service_pb2 import *
6+
7+
8+
class BhvStarterKickPlanner(IBehavior):
9+
def __init__(self):
10+
pass
11+
12+
def execute(self, agent: IAgent):
13+
agent.logger.debug("BhvStarterKickPlanner.execute")
14+
from src.sample_player_agent import SamplePlayerAgent # Local import to avoid circular import
15+
assert isinstance(agent, SamplePlayerAgent)
16+
raise NotImplementedError("BhvStarterKickPlanner.execute not implemented")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
from src.interfaces.IAgent import IAgent
3+
from src.utils.convertor import Convertor
4+
from pyrusgeom.geom_2d import *
5+
from pyrusgeom.soccer_math import *
6+
from service_pb2 import *
7+
from src.interfaces.IBehavior import IBehavior
8+
9+
10+
class BhvStarterPenalty(IBehavior):
11+
def __init__(self):
12+
pass
13+
14+
def execute(self, agent: IAgent) -> bool:
15+
agent.logger.debug("BhvStarterPenalty.execute")
16+
from src.sample_player_agent import SamplePlayerAgent # Local import to avoid circular import
17+
assert isinstance(agent, SamplePlayerAgent)
18+
raise NotImplementedError("BhvStarterPenalty.execute not implemented")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
from src.interfaces.IAgent import IAgent
3+
from src.utils.convertor import Convertor
4+
from pyrusgeom.geom_2d import *
5+
from pyrusgeom.soccer_math import *
6+
from service_pb2 import *
7+
from src.interfaces.IBehavior import IBehavior
8+
9+
10+
class BhvStarterSetPlay(IBehavior):
11+
def __init__(self):
12+
pass
13+
14+
def execute(self, agent: IAgent) -> bool:
15+
agent.logger.debug("BhvSetPlay.execute")
16+
from src.sample_player_agent import SamplePlayerAgent # Local import to avoid circular import
17+
assert isinstance(agent, SamplePlayerAgent)
18+
raise NotImplementedError("BhvStarterSetPlay.execute not implemented")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
from src.interfaces.IAgent import IAgent
3+
from src.utils.convertor import Convertor
4+
from pyrusgeom.geom_2d import *
5+
from pyrusgeom.soccer_math import *
6+
from service_pb2 import *
7+
from src.interfaces.IBehavior import IBehavior
8+
9+
10+
class BhvStarterTackle(IBehavior):
11+
def __init__(self):
12+
pass
13+
14+
def execute(self, agent: IAgent) -> bool:
15+
agent.logger.debug("BhvStarterTackle.execute")
16+
from src.sample_player_agent import SamplePlayerAgent # Local import to avoid circular import
17+
assert isinstance(agent, SamplePlayerAgent)
18+
raise NotImplementedError("BhvStarterTackle.execute not implemented")

src/behaviors/bhv_tackle.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
from src.interfaces.IAgent import IAgent
3+
from src.utils.convertor import Convertor
4+
from pyrusgeom.geom_2d import *
5+
from pyrusgeom.soccer_math import *
6+
from service_pb2 import *
7+
from src.interfaces.IBehavior import IBehavior
8+
9+
10+
class BhvTackle(IBehavior):
11+
def __init__(self):
12+
pass
13+
14+
def execute(self, agent: IAgent) -> bool:
15+
agent.logger.debug("BhvTackle.execute")
16+
from src.sample_player_agent import SamplePlayerAgent # Local import to avoid circular import
17+
assert isinstance(agent, SamplePlayerAgent)
18+
agent.add_action(PlayerAction(helios_basic_tackle=HeliosBasicTackle(min_prob=0.8, body_thr=100.0)))

src/decision_makers/kick_decision_maker.py

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,17 @@
33
from pyrusgeom.soccer_math import *
44
from pyrusgeom.geom_2d import *
55
from service_pb2 import *
6+
from src.behaviors.bhv_kick_planner import BhvKickPlanner
7+
from src.behaviors.bhv_starter_kick_planner import BhvStarterKickPlanner
68

79

810
class KickDecisionMaker(IDecisionMaker):
9-
"""
10-
Decision maker class for an agent with the ball.
11-
12-
Methods
13-
-------
14-
__init__():
15-
Initializes the WithBallDecisionMaker instance.
16-
17-
make_decision(agent: IAgent):
18-
Makes a decision for the agent when it has the ball.
19-
20-
_get_helios_offensive_planner():
21-
Returns an instance of HeliosOffensivePlanner.
22-
23-
_get_planner_evaluation(agent: IAgent):
24-
Returns an instance of PlannerEvaluation.
25-
26-
_get_planner_evaluation_effector(agent: IAgent):
27-
Returns an instance of PlannerEvaluationEffector.
28-
29-
_get_opponent_effector(agent: IAgent):
30-
Determines the opponent effector based on the agent's world model.
31-
"""
32-
3311
def __init__(self):
34-
pass
12+
self.bhv_kick_planner = BhvKickPlanner()
13+
# self.bhv_kick_planner = BhvStarterKickPlanner()
3514

3615
def make_decision(self, agent: IAgent):
3716
agent.logger.debug("--- WithBallDecisionMaker ---")
38-
39-
agent.add_action(
40-
PlayerAction(helios_offensive_planner=self._get_helios_offensive_planner(agent))
41-
)
42-
43-
def _get_helios_offensive_planner(self, agent):
44-
""" Summary
45-
In this function you can create an instance of HeliosOffensivePlanner and set its attributes.
46-
The HeliosOffensivePlanner is a message that ask proxy to create a tree and find the best chain of actions and execute the first action of the chain.
47-
48-
Returns:
49-
_type_: HeliosOffensivePlanner
50-
"""
51-
res = HeliosOffensivePlanner(evaluation=self._get_planner_evaluation(agent))
52-
res.lead_pass = True
53-
res.direct_pass = True
54-
res.through_pass = True
55-
res.simple_pass = True
56-
res.short_dribble = True
57-
res.long_dribble = True
58-
res.simple_shoot = True
59-
res.simple_dribble = True
60-
res.cross = True
61-
62-
return res
63-
64-
def _get_planner_evaluation(self, agent: IAgent):
65-
return PlannerEvaluation(
66-
effectors=self._get_planner_evaluation_effector(agent),
67-
)
68-
69-
def _get_planner_evaluation_effector(self, agent: IAgent):
70-
return PlannerEvaluationEffector(
71-
opponent_effector=self._get_opponent_effector(agent),
72-
)
73-
74-
def _get_opponent_effector(self, agent: IAgent):
75-
wm = agent.wm
76-
77-
if wm.ball.position.x > 30:
78-
negetive_effect_by_distance = [-5, -4, -3, -2, -1]
79-
else:
80-
negetive_effect_by_distance = [-30, -25, -20, -15, -10, -4, -3, -2, -1]
81-
82-
return OpponentEffector(
83-
negetive_effect_by_distance=negetive_effect_by_distance,
84-
negetive_effect_by_distance_based_on_first_layer=False,
85-
)
17+
from src.sample_player_agent import SamplePlayerAgent # Local import to avoid circular import
18+
assert isinstance(agent, SamplePlayerAgent)
19+
self.bhv_kick_planner.execute(agent)

0 commit comments

Comments
 (0)