import copy import time import yaml from datetime import datetime class RobotController(object): def __init__(self, fire_ext_max_capacity = 5, collision_damage = 1, k_sof = 1, k_cas = 1, k_asset = 1, k_hp = 1, fall_damage = 10, k_charge = 1): # STATE VARIABLES self._fire_ext_max_capacity = fire_ext_max_capacity self._fire_ext_capacity = self._fire_ext_max_capacity self._start_hp = 100 self._hit_points = self._start_hp self._collision_damage = collision_damage self._start_charge = 100 self._charge_points = self._start_charge self._fall_damage = fall_damage self._start_time = None self._collisions = [] self._exted_sofs = [] self._found_casualty = [] self._found_assets = [] self._falls = [] self._k_sof = k_sof self._k_cas = k_cas self._k_asset = k_asset self._k_hp = k_hp self._k_charge = k_charge def _check_time_init(self): if not self._start_time is None: return self._start_time = time.time() def _ts(self): return time.time() - self._start_time ''' CONTROL ''' # speeds in [-1, 1] extra stuff will be cut def send_speed_cmd(self, v, w): self._check_time_init() if self._charge_points == 0: return 0, 0 v = min(max(-1, v), 1) w = min(max(-1, w), 1) return v, w def send_fire_ext_burst_cmd(self): self._check_time_init() if self._fire_ext_capacity > 0: self._fire_ext_capacity -= 1 return True return False # poses in [-1, 1] extra stuff will be cut def send_fire_ext_pose_cmd(self, horisontal_pose, vertical_pose): self._check_time_init() horisontal_pose = min(max(-1, horisontal_pose), 1) vertical_pose = min(max(-1, vertical_pose), 1) return horisontal_pose, vertical_pose def send_pick_asset_cmd(self): self._check_time_init() raise NotImplemented("") def get_score(self, time_stamp = None): score = self._k_sof * len(self._exted_sofs) + self._k_cas * len(self._found_casualty) + self._k_asset * len(self._found_assets) + self._k_hp * (self._hit_points/self._start_hp) + self._k_charge * (self._charge_points / self._start_charge) return score def get_str_status(self): status = f"{type(self).__name__}\n - Total score {self.get_score()}\n - Hit points: {self._hit_points}/{self._start_hp}\n - Charge points {round(self._charge_points, 2)}\{self._start_charge}\n - Extingushed sources of fire {len(self._exted_sofs)}\n - Fire extinguiher capacity {self._fire_ext_capacity}/{self._fire_ext_max_capacity}\n - Collisions {len(self._collisions)}\n - Falls {len(self._falls)}" return status def send_lights_cmd(self): self._check_time_init() def send_fall_reset_cmd(self): self._check_time_init() def dump_stats(self, save_path = '/tmp'): date = datetime.now().strftime("%d_%H:%M:%S") stats = {"total_time": float(self._ts()), "date": date, "score": float(self.get_score()), "status": self.get_str_status(), "class": type(self).__name__, "k_params": {k: v for k, v in vars(self).items() if k.startswith('_k_') and not callable(v)}, "exted_sofs": self._exted_sofs, "collisions": self._collisions, "falls": self._falls } path = save_path + f"/sirius_{date}.yaml" with open(path, "w") as file: yaml.dump(stats, file, default_flow_style=False, sort_keys=False) return path return "" ''' EVENTS ''' def _register_collision(self): self._collisions.append(self._ts()) self._hit_points = max(0, self._hit_points - self._collision_damage) def _register_exted_sof(self, sof_params = {}): self._exted_sofs.append((self._ts(), copy.deepcopy(sof_params))) def _register_found_casualty(self, casualty_params = {}): self._found_casualty.append((self._ts(), copy.deepcopy(casualty_params))) def _register_found_assest(self, asset_params = {}): self._found_assets.append((self._ts(), copy.deepcopy(asset_params))) def _register_fall(self): self._falls.append(self._ts()) def _decrease_charge(self, speed, dt): new_value = self._charge_points - speed * dt self._charge_points = max(0, new_value)