-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrmaics.py
More file actions
83 lines (64 loc) · 2.44 KB
/
Copy pathrmaics.py
File metadata and controls
83 lines (64 loc) · 2.44 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
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
# RoboMaster AI Challenge Simulator (RMAICS)
import math
from kernal import kernal
import random
class rmaics(object):
def __init__(self, agent_num, render=True):
self.game = kernal(car_num=agent_num, render=render)
self.g_map = self.game.get_map()
self.memory = []
self.prev_x = 0
self.prev_y = 0
self.prev_collision = 0
def reset(self):
self.state = self.game.reset()
# state, object
self.obs = self.get_observation(self.state)
return self.obs
def step(self, actions):
state = self.game.step(actions)
obs = self.get_observation(state)
rewards = self.get_reward(state)
self.memory.append([self.obs, actions, rewards])
self.state = state
return obs, rewards, state.done, None
def get_observation(self, state):
# print(state.agents)
observation = {
'time_remaining': state.time,
'agent_states': state.agents,
'competition_info': state.compet,
'is_done': state.done,
'visible_agents': state.detect,
'visible_enemies': state.vision
}
return observation
def euclidean_distance(self, x1, y1, x2, y2):
return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
def normalize(self, value):
return (value )
def get_reward(self, state):
reward = 0 # Initialize reward
# Penalize collisions
for agent in state.agents:
temp_collision = agent[12] + agent[13]
if abs(self.prev_collision - temp_collision) > 0:
reward -= 10 # Penalty for collisions
print("Collision")
self.prev_collision = temp_collision
position_x = self.game.cars[0][1]
position_y = self.game.cars[0][2]
if self.prev_x != 0 and self.prev_y != 0:
reward += self.euclidean_distance(position_x, position_y, self.prev_x, self.prev_y)
self.prev_x = position_x
self.prev_y = position_y
# Reward for reaching a bonus area
if state.compet[0][0] == 1 or state.compet[1][0] == 1:
reward += 10
reward -= 0.05
return reward
def play(self):
self.game.play()
def save_record(self, file):
self.game.save_record(file)