-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
83 lines (60 loc) · 2.83 KB
/
helper.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
class anav: # The class encapsulating the environment
'''
Actions [0 : Stand, 1 : Up, 2 : Right, 3 : Down, 4 : Left]
These are the representing no.s for the mentioned actions
'''
def __init__(self, length=30, width=30, goalPositions=[15,29]):
# The player start at random locations
self.pA=[np.random.randint(30),np.random.randint(30)]
self.pO=[6,8]
self.h = length # Length of the Football Pitch
self.w = width # Width of the Football Pitch
self.goalPositions = np.array(goalPositions) # This means that the middle 4 positions at the right and left are the goals
self.reward = 0 # Initially the reward is 0
self.observation=np.random.rand(6,)
self.done = bool(0) # This stores whether the game needs to be restart with new position (in the case of a goal)
def reset(self):
self.done = bool(0)
self.reward = 0
self.pA = np.array([np.random.randint(self.h), np.random.randint(self.h)])
self.pA = np.array([15,0])
#self.pO=[6,8]
self.x_traj,self.y_traj=[],[]
self.xo_traj,self.yo_traj=[],[]
return np.array((*self.pA,(15-self.pA[0]),(15-self.pA[1]),self.theta,*self.pO)).astype(np.float32)
def step(self, action):
if self.done == bool(1):
self.reset()
self.move(first, action) # We chose the first player at random
if self.done == bool(1):
return self.observation, self.reward, self.done
if not done:
self.current_player_num = (self.current_player_num + 1) % 2
return self.observation,self.reward, self.done,{}
def move(self, player, action):
opponent = 1 - player
newPosition = self.pA + self.actionToMove(action)
if self.ballOwner is player and self.isInGoal(*newPosition) >= 0:
self.done = bool(1)
return 1 - self.isInGoal(*newPosition)
# If it's in the board
elif self.isInBoard(*newPosition):
self.positions[player] = newPosition
if(self.ballOwner!=0):
self.reward=-1
return -1
def isInBoard(self, x, y):
if(x<0 or x>(30)):
return 0
if(y<0 or y>(30)):
return 0
return 1
#def choosePlayer(self):
# return np.random.randint(0, 2)
def render(self,mode="human"):
plt.cla()
plt.arrow(x_start, y_start, np.cos(theta_start),
np.sin(theta_start), color='r', width=0.1)
plt.arrow(x_goal, y_goal, np.cos(theta_goal),
np.sin(theta_goal), color='g', width=0.1)
plot_vehicle(x, y, theta, x_traj, y_traj)