def _get_state(self): """Get the observation.""" locations = _A(self.eng.get_vehicles_location()) self.image = self.quantize_list(locations) phases = self.eng.get_trafficLight_phases() position_speed_start = self.observation_space.spaces['vehicle'].low observation = {'phase':_A(phases), 'vehicle':self.image} return observation
def __init__(self, V, N=1024): self.N = N self.min_xy = _A([V[:, 0].min(), V[:, 1].min()]) self.max_xy = _A([V[:, 0].max(), V[:, 1].max()])
def __init__(self, config_path=config_path, mode='train'): work_dir = _P(config_path).parents[0] self.mode = mode self.last_action = None self.config = config = json.load(open(config_path, 'rt')) self.thread_num = config.get("thread_num", 4) roadnetFile = _P(config['dir']) / config['roadnetFile'] self.roadnet = roadnet = json.load(roadnetFile.open('rt')) self.intersections = json_normalize(roadnet['intersections']) self.roads = roads = json_normalize(roadnet['roads']) self.intersections = self.intersections[~self.intersections['virtual']] # Filter out virtuals self.lns = [len(row['trafficLight.lightphases']) for i, row in self.intersections.iterrows()] l = list() # Read all points to get bounding box. for i, (p1, p2) in roads.points.iteritems(): l.extend((p1, p2)) l = _D(l) self.min_xy = _A(l['x'].min(), l['y'].min()) self.max_xy = _A(l['x'].max(), l['y'].max()) self.MAT_SIZE = (self.max_xy - self.min_xy).max() self.range_1 = self.MAT_SIZE / (self.max_xy - self.min_xy) self.image = np.zeros((self.MAT_SIZE, self.MAT_SIZE), dtype=np.uint8) self.input_shape = (config['WINDOW_LENGTH'], self.MAT_SIZE, self.MAT_SIZE) num_phases = self.lns[0] self.action_space = gym.spaces.Discrete(num_phases) self.observation_space = gym.spaces.Dict({ 'phase':gym.spaces.Box(low=0, high=1, shape=(1,1), dtype=np.float32), 'vehicle':gym.spaces.Box(low=0, high=14, shape=(1, self.MAT_SIZE, self.MAT_SIZE), dtype=np.float32)}) #self.seed(seed, state) # Store what the agent tried self.curr_episode = -1 self.action_episode_memory = [] self.count_from_last_bump = 0 self.total_num_bumps = 0 self.curr_step = -1 DIR = _P(config['dir']) #archive_dump = _P(config['dir']) / config['archive_dump'] self.num_sim_loops = config.get('num_sim_loops', 500) self.output_dict_path = _P(config['dir']) / "summary.json" roudnet_output = DIR/ config['roadnetLogFile'] replayLogFile = DIR/ config['replayLogFile'] weights_filename = f"dqn_{config['WINDOW_LENGTH']}_{self.MAT_SIZE},{self.MAT_SIZE}_{mode}_weights.h5f" self.weights_filename = work_dir / weights_filename self.checkpoint_weights_filename = work_dir / ('checkpoint_{step}_' + weights_filename) if mode == 'train': self.config['saveReplay'] = False self.work_config_path = work_dir / "generated_train_config.json" else: self.work_config_path = work_dir / "generated_predict_config.json" self.config['saveReplay'] = True try: os.remove(str(roudnet_output)) print("Removed previous log file %s" % roudnet_output) except BaseException as e: print(str(e), roudnet_output) try: os.remove(str(replayLogFile)) print("Removed previous log file %s" % replayLogFile) except BaseException as e: print(str(e), replayLogFile) self.eng = cityflow.Engine(str(self.work_config_path), thread_num=self.thread_num) json.dump(self.config, self.work_config_path.open('wt'))
def preprocess(self, image, mask): im = _A(image, dtype=np.float32) / 255.0 msk = _A(mask, dtype=np.float32) / 255.0 msk = 1.0 - msk im[msk == 0] = 1 return im, msk
def get_screen(self): locations = _A(self.env.eng.get_vehicles_location()) image = self.env.quantize_list(locations).reshape( 1, 1, self.env.MAT_SIZE, self.env.MAT_SIZE) return torch.tensor(image, device=self.device, dtype=torch.float32)