def _update(self): temp_moving_obstacles = [self.main_vessel] for vessel in self.moving_distances: if len(vessel.reachable_vessels): temp_moving_obstacles.append(vessel) self.moving_obstacles = temp_moving_obstacles while len(self.moving_obstacles) < MAX_VESSELS: new_vessel_count = self._vessel_count + 1 vessel = Vessel(self.config, width=self.config["vessel_width"], index=new_vessel_count, vessel_pos=self.main_vessel.position) self.rewarder_dict[vessel.index] = ColavRewarder(vessel) self.moving_obstacles.append(vessel) print(f'vessel {i} has been created') self._vessel_count += 1 for vessel in self.moving_obstacles: other_vessels = [ x for x in self.moving_obstacles if x.index != vessel.index ] vessel.obstacles = np.hstack( [self.static_obstacles, other_vessels]) self.moving_obstacles.sort(key=lambda x: x.index) return self.moving_obstacles
def _generate(self): waypoints1 = np.vstack([[0, 0], [0, 500]]).T path1 = Path(waypoints1) init_pos1 = path1(0) init_angle1 = path1.get_direction(0) init_state1 = np.hstack([init_pos1, init_angle1]) self.main_vessel = Vessel(self.config, init_state=init_state1, init_path=path1, width=2) #self.config["vessel_width"]) self.main_vessel.path = path1 self.rewarder_dict[self.main_vessel.index] = ColavRewarder( self.main_vessel) self.rewarder = self.rewarder_dict[self.main_vessel.index] prog = 0 self.path_prog_hist = np.array([prog]) self.max_path_prog = prog self.moving_obstacles = [self.main_vessel] #Adding moving obstacle waypoints2 = np.vstack([[0, 150], [0, -400]]).T path2 = Path(waypoints2) init_pos2 = path2(0) init_angle2 = path2.get_direction(0) init_state2 = np.hstack([init_pos2, init_angle2]) vessel = Vessel(self.config, init_state=init_state2, init_path=path2, index=1, width=2) #self.config["vessel_width"]) self.rewarder_dict[vessel.index] = ColavRewarder(vessel) self.moving_obstacles.append(vessel) vessel.path = path2 for vessel in self.moving_obstacles: other_vessels = [ x for x in self.moving_obstacles if x.index != vessel.index ] vessel.obstacles = np.hstack([other_vessels]) print('Generated vessels!')
def _generate(self): print('In GENERATE in MA') self.main_vessel = Vessel(self.config, width=self.config["vessel_width"]) self.rewarder_dict[self.main_vessel.index] = ColavRewarder( self.main_vessel) self.rewarder = self.rewarder_dict[self.main_vessel.index] prog = 0 self.path_prog_hist = np.array([prog]) self.max_path_prog = prog print(f'Ownvessel created!') self.moving_obstacles = [self.main_vessel] self.static_obstacles = [] self._vessel_count = 1 #Adding moving obstacles (vessels) curr_vessel_count = self._vessel_count for i in range(curr_vessel_count, curr_vessel_count + MAX_VESSELS - 1): vessel = Vessel(self.config, width=self.config["vessel_width"], index=i, vessel_pos=self.main_vessel.position) self.rewarder_dict[vessel.index] = ColavRewarder(vessel) self.moving_obstacles.append(vessel) print(f'vessel {i} has been created') self._vessel_count += 1 for vessel in self.moving_obstacles: other_vessels = [ x for x in self.moving_obstacles if x.index != vessel.index ] vessel.obstacles = np.hstack( [self.static_obstacles, other_vessels]) #Adding static obstacles for _ in range(8): obstacle = CircularObstacle(*helpers.generate_obstacle( self.rng, self.path, self.vessel, displacement_dist_std=500)) self.static_obstacles.append(obstacle) print('Exiting GENERATE in MA')
def _generate(self): print('In GENERATE in MA') self.main_vessel = Vessel(self.config, width=self.config["vessel_width"]) prog = 0 self.path_prog_hist = np.array([prog]) self.max_path_prog = prog self.rewarder = MultiRewarder(self.main_vessel) print(f'Ownship created!') self.moving_obstacles = [self.main_vessel] self.static_obstacles = [] self.queued_vessels = [] #Adding static obstacles #for _ in range(8): # obstacle = CircularObstacle(*helpers.generate_obstacle(self.rng, self.main_vessel.path, self.main_vessel)) # self.static_obstacles.append(obstacle) self._vessel_count = 1 #Adding moving obstacles (ships) curr_vessel_count = self._vessel_count for i in range(curr_vessel_count, curr_vessel_count + 5): #obst_speed = np.random.random() ship = Vessel(self.config, width=self.config["vessel_width"], index=i, vessel_pos=self.main_vessel.position) self.moving_obstacles.append(ship) print(f'Ship {i} has been created') self._vessel_count += 1 for ship in self.moving_obstacles: other_ships = [ x for x in self.moving_obstacles if x.index != ship.index ] #ship.obstacles.extend(other_ships) ship.obstacles = np.hstack([self.static_obstacles, other_ships]) print('Exiting GENERATE in MA')
def step(self, action: list) -> (np.ndarray, float, bool, dict): """ Steps the environment by one timestep. Returns observation, reward, done, info. Parameters ---------- action : np.ndarray [thrust_input, torque_input]. Returns ------- obs : np.ndarray Observation of the environment after action is performed. reward : double The reward for performing action at his timestep. done : bool If True the episode is ended, due to either a collision or having reached the goal position. info : dict Dictionary with data used for reporting or debugging """ print('IN STEP') if len(self.queued_vessels) == 0: current_vessel = self.main_vessel else: current_vessel = self.queued_vessels.pop(0) current_index = current_vessel.index print(f'Current vessel is ship {current_index}') #[vessel.update_without_agent(self.config["t_step_size"]) for vessel in self.moving_obstacles if vessel.index != current_index] action[0] = (action[0] + 1) / 2 current_vessel.step(action) reward = self.rewarder.calculate(current_vessel) self.cumulative_reward += reward vessel_data = self.main_vessel.req_latest_data() self.collision = vessel_data['collision'] self.reached_goal = vessel_data['reached_goal'] self.progress = vessel_data['progress'] info = {} info['collision'] = self.collision info['reached_goal'] = self.reached_goal info['progress'] = self.progress done = self._isdone() self._save_latest_step() self.moving_obstacles = self.main_vessel.nearby_vessels #Adding moving obstacles (ships) if not self.t_step % 150: #print(f'Time step: {self.t_step}, position of vessel: {self.main_vessel.position}') curr_vessel_count = self._vessel_count for i in range(curr_vessel_count, curr_vessel_count + 5): #obst_speed = np.random.random() ship = Vessel(self.config, width=self.config["vessel_width"], index=i, vessel_pos=self.main_vessel.position) self.moving_obstacles.append(ship) print(f'Ship {i} has been created') self._vessel_count += 1 for ship in self.moving_obstacles: other_ships = [ x for x in self.moving_obstacles if x.index != ship.index ] #ship.obstacles.extend(other_ships) ship.obstacles = np.hstack( [self.static_obstacles, other_ships]) if len(self.queued_vessels) == 0: self.queued_vessels = [ x for x in self.moving_obstacles if x.index != 0 ] next_vessel = self.main_vessel else: next_vessel = self.queued_vessels[0] obs = next_vessel.observe() self.t_step += 1 print('EXITIG STEP') return (obs, reward, done, info)