def __init__(self, config): self.config = config self.eng = cityflow.Engine(config['cityflow_config_path'], thread_num=1) self.num_step = config['num_step'] self.lane_phase_info = config['lane_phase_info'] # "intersection_1_1" self.state_size = len(config['lane_phase_info'][ config["intersection_id"]]['start_lane']) + 1 + 4 ## state size self.intersection_id = list(self.lane_phase_info.keys())[0] self.start_lane = self.lane_phase_info[ self.intersection_id]['start_lane'] self.phase_list = self.lane_phase_info[self.intersection_id]["phase"] self.phase_startLane_mapping = self.lane_phase_info[ self.intersection_id]["phase_startLane_mapping"] self.current_phase = self.phase_list[0] self.current_phase_time = 0 self.yellow_time = 5 self.state_store_i = 0 self.time_span = 50 self.state_time_span = 5 self.num_span_1 = 0 self.num_span_2 = 0 self.state_size_single = len(self.start_lane) self.count = np.zeros([8, self.time_span]) self.accum_s = np.zeros([self.state_size_single, self.state_time_span]) self.phase_log = []
def __init__(self, config): self.config = config self.eng = cityflow.Engine(self.config['cityflow_config_file'], thread_num=self.config['thread_num']) self.num_step = self.config['num_step'] self.state_size = self.config['state_size'] self.lane_phase_info = self.config[ 'lane_phase_info'] # "intersection_1_1" self.intersection_id = list(self.lane_phase_info.keys())[0] self.start_lane = self.lane_phase_info[ self.intersection_id]['start_lane'] self.phase_list = self.lane_phase_info[self.intersection_id]["phase"] self.phase_startLane_mapping = self.lane_phase_info[ self.intersection_id]["phase_startLane_mapping"] self.current_phase = self.phase_list[0] self.current_phase_time = 0 self.yellow_time = 5 self.state_store_i = 0 self.time_span = self.config['time_span'] self.state_time_span = self.config['state_time_span'] self.num_span_1 = 0 self.num_span_2 = 0 self.state_size_single = len(self.start_lane) self.phase_log = [] self.count = np.zeros([8, self.time_span]) self.accum_s = np.zeros([self.state_size_single, self.state_time_span]) self.observation_space = Box(-1 * np.ones(9), 100 * np.ones(9)) self.action_space = Discrete(8)
def __init__(self,): self.episode_over = False self.coef = 0.1 # easy version config_path = '/home/CityFlow/conf/.json' roadnet_path = '' self.engine = cityflow.Engine(config_path) self.lane_phase_info_dict = parse_roadnet(roadnet_path) self.intersection_list = self.lane_phase_info_dict.keys() self.n = len(self.intersection_list) # number of intersections self.naction = len(self.lane_phase_info_dict[self.intersection_list[0]]["phase"]) self.obs_dim = len(self.lane_phase_info_dict[self.intersection_list[0]]['start_lane']) + 1 # initialize previous action self.current_phase = {intersection_id:0 for intersection_id in self.intersection_list} # gym like environment self.action_space = [] self.observation_space = [] for agent_id in range(self.n): self.action_space.append(spaces.Discrete(self.naction)) self.observation_space.append(spaces.Box) return
def __init__(self, lane_phase_info, intersection_id='xxx', num_step=1500, thread_num=1, cityflow_config_file='examples/config_1x1.json', replay_data_path='./replay' ): # cityflow_config['rlTrafficLight'] = rl_control # use RL to control the light or not self.eng = cityflow.Engine(cityflow_config_file, thread_num=thread_num) self.num_step = num_step self.state_size = None self.lane_phase_info = lane_phase_info # "intersection_1_1" self.intersection_id = intersection_id self.start_lane = self.lane_phase_info[self.intersection_id]['start_lane'] self.end_lane = self.lane_phase_info[self.intersection_id]['end_lane'] self.phase_list = self.lane_phase_info[self.intersection_id]["phase"] self.phase_startLane_mapping = self.lane_phase_info[self.intersection_id]["phase_startLane_mapping"] self.replay_data_path = replay_data_path self.current_phase = self.phase_list[0] self.current_phase_time = 0 self.yellow_time = 5 self.state_store_i = 0 self.get_state() # set self.state_size self.phase_log = []
def __init__(self, lane_phase_info, intersection_id, num_step=2000, thread_num=1, cityflow_config_file='example/config_1x2.json' ): self.eng = cityflow.Engine(cityflow_config_file, thread_num=thread_num) self.num_step = num_step self.intersection_id = intersection_id # list, [intersection_id, ...] self.state_size = None self.lane_phase_info = lane_phase_info # "intersection_1_1" self.current_phase = {} self.current_phase_time = {} self.start_lane = {} self.end_lane = {} self.phase_list = {} self.phase_startLane_mapping = {} self.intersection_lane_mapping = {} #{id_:[lanes]} for id_ in self.intersection_id: self.start_lane[id_] = self.lane_phase_info[id_]['start_lane'] self.end_lane[id_] = self.lane_phase_info[id_]['end_lane'] self.phase_startLane_mapping[id_] = self.lane_phase_info[id_]["phase_startLane_mapping"] self.phase_list[id_] = self.lane_phase_info[id_]["phase"] self.num_actions = len(self.phase_list[self.intersection_id[0]]) self.current_phase[id_] = self.phase_list[id_][0] self.current_phase_time[id_] = 0 self.get_state() # set self.state_size
def __init__(self, config): # cityflow_config['rlTrafficLight'] = rl_control # use RL to control the light or not self.eng = cityflow.Engine(config['cityflow_config_file'], thread_num=config['thread_num']) self.config = config self.num_step = config['num_step'] self.state_size = len(config['lane_phase_info'][ config["intersection_id"]]['start_lane']) + 1 self.lane_phase_info = config['lane_phase_info'] # "intersection_1_1" self.intersection_id = config["intersection_id"] # self.intersection_id = list(self.lane_phase_info.keys())[0] self.start_lane = self.lane_phase_info[ self.intersection_id]['start_lane'] self.phase_list = self.lane_phase_info[self.intersection_id]["phase"] self.phase_startLane_mapping = self.lane_phase_info[ self.intersection_id]["phase_startLane_mapping"] self.current_phase = self.phase_list[0] self.current_phase_time = 0 self.yellow_time = 5 self.state_store_i = 0 self.phase_log = []
def __init__(self): """ 创建环境,配置道路环境信息。 """ # 使用2x2的“井”字形路网环境 config = "data/2x2/config.json" # 创建CityFlow模拟器 self.eng = cityflow.Engine(config, thread_num=4) # 存储十字路口的当前红绿灯状态 self.cur_phase = { 'intersection_1_2': 0, 'intersection_1_1': 0, 'intersection_2_1': 0, 'intersection_2_2': 0 } # 声明各个十字路口联结的四条道路 self.lanes_dict = { 'intersection_1_2': ["road_2_2_2", "road_1_3_3", "road_0_2_0", "road_1_1_1"], 'intersection_1_1': ["road_1_2_3", "road_0_1_0", "road_1_0_1", "road_2_1_2"], 'intersection_2_1': ["road_1_1_0", "road_2_0_1", "road_3_1_2", "road_2_2_3"], 'intersection_2_2': ["road_2_1_1", "road_3_2_2", "road_2_3_3", "road_1_2_0"] }
def _init_sim(self, seed, gui=False): cityflow_config = { "interval": 1, "seed": seed, "laneChange": False, "dir": self.base_dir + "\\", "roadnetFile": "roadnet_{0}.json".format("4_4"), "flowFile": "anon_4_4_hangzhou_real.json", "rlTrafficLight": True, "saveReplay": True, "roadnetLogFile": "roadnetLogFile.json", "replayLogFile": "replayLogFile.txt" } print("=========================") # print(cityflow_config) with open(os.path.join(self.base_dir, "cityflow.config"), "w") as json_file: json.dump( cityflow_config, json_file) # json.dump() 和 json.load() 来编码和解码JSON数据,用于处理文件。 self.sim = cityflow.Engine(os.path.join(self.base_dir, "cityflow.config"), thread_num=1) self.sim.reset() time.sleep(1)
def static_test(): path = "cityflow_config/{}/config.json".format( "hangzhou_1x1_bc-tyc_18041607_1h") max_time = 360 eng = cityflow.Engine(path, thread_num=1) eng.set_save_replay(True) for t in range(max_time): eng.next_step()
def reset(self): self.eng = engine.Engine(self.path_to_conf_file) self._extract_sim_params() self._load_signal_plan() self._set_signal() n_obs, n_reward, n_info = self._get_description() return n_obs, n_info
def test_save_to_file(self): """ Disk IO test """ engine = cityflow.Engine(config_file=self.config_file, thread_num=4) self.run_steps(engine, self.period) engine.snapshot().dump("save.json") self.run_steps(engine, self.period) record = engine.get_lane_vehicle_count() engine.load_from_file("save.json") self.run_and_check(engine, record) del engine
def _build_eng( config_file_path, thread_num, save_replay: bool, ): try: eng = cityflow.Engine(config_file_path, thread_num) eng.set_save_replay(save_replay) except Exception as ex: raise ex return eng
def __init__(self, scenario_name, steps_per_episode): super(LineEnv, self).__init__() self.scenario_name = scenario_name self.eng = cityflow.Engine("data/" + self.scenario_name + "/config.json", thread_num=12) self.eng.set_save_replay(True) self.steps_per_episode = steps_per_episode self.is_done = False self.reward_range = (-float('inf'), float('inf')) # HARDCODE self.action_space = spaces.Discrete(10) # HARDCODE self.observation_space = spaces.Box(low=0, high=float('inf'), shape=np.array([12]), dtype=np.float32) # HARDCODE
def test_multi_save_to_file(self): """ Disk IO test 2""" engine = cityflow.Engine(config_file=self.config_file, thread_num=4) for i in range(2): self.run_steps(engine, self.period) engine.snapshot().dump("save.json") self.run_steps(engine, self.period) record = self.get_record(engine) for j in range(2): engine.load_from_file("save.json") self.run_and_check(engine, record) del engine
def test_save_and_load_multithread(self): """Single save and single load with multi-threading engine""" engine = cityflow.Engine(config_file=self.config_file, thread_num=4) self.run_steps(engine, self.period) archive = engine.snapshot() self.run_steps(engine, self.period) record0 = engine.get_lane_vehicle_count() engine.load(archive) self.run_and_check(engine, record0) del engine
def simulate(time_values, shouldSave=False): count = 0 totals = {} averages = {} #expecting time _values tp be 2 long print(time_values) time_values = time_values.tolist() print(time_values) time_values = time_values * 20 print("Simulating with:") print(time_values) create_file(time_values) eng = None if (shouldSave): eng = cityflow.Engine("./configs/config.json", thread_num=1) else: eng = cityflow.Engine("./configs/config_no_save.json", thread_num=1) for x in range(STEPS): if (x % 100 == 0): print("step: " + str(x)) eng.next_step() trackTotal(eng.get_lane_waiting_vehicle_count(), totals) count += 1 calculateAverages(totals, averages, count) total_avg_time = calculateTotalAverage(averages) print("calculated total avg time. " + str(total_avg_time)) return total_avg_time
def test_save_and_multi_load(self): """Multiple saves and multiple loads with multi-threading engine""" engine = cityflow.Engine(config_file=self.config_file, thread_num=4) self.run_steps(engine, self.period) archive = engine.snapshot() self.run_steps(engine, self.period) record0 = engine.get_lane_vehicle_count() repeats = 2 for i in range(repeats): engine.load(archive) self.run_and_check(engine, record0) del engine
def __init__(self, config): # if config['data_set_mode'] == 'train': # self.eng = cityflow.Engine("src/config_args.json", thread_num=1) # if config['data_set_mode'] == 'test': # self.eng = cityflow.Engine("src/config_args_test.json", thread_num=1) # if config['data_set_mode'] == 'val': # self.eng = cityflow.Engine("src/config_args_val.json", thread_num=1) path = "src/config_{}_args.json".format(config['scenario']) self.eng = cityflow.Engine(path, thread_num=1) self.config = config self.lane_phase_info = config['lane_phase_info'] self.intersection_id = list(self.lane_phase_info.keys())[0] self.start_lane = self.lane_phase_info[ self.intersection_id]['start_lane'] self.end_lane = self.lane_phase_info[self.intersection_id]['end_lane'] self.phase_list = self.lane_phase_info[self.intersection_id]["phase"] self.phase_startLane_mapping = self.lane_phase_info[ self.intersection_id]["phase_startLane_mapping"] self.current_phase = 0 # from -1 to len(phase_list)-1 self.current_phase_time = 0 self.last_phase = 0 self.yellow_time = 5 self.phase_log = [] self.acyclic = config['acyclic'] self.WAITING = config['waiting_added'] self.DISTANCE = config['distance_added'] self.SPEED = config['speed_added'] self.ALL_VEHICLES_MAX = 40 self.WAITING_MAX = 30 self.MAX_DISTANCE = 300 self.MAX_SPEED = 11 self.last_reward = 0 self.SHAPING_REWARD = 0 self.TEMPORAL_REWARD = 0 self.ADDED_REWARD = 0 self.state_normalizer = Normalizer( len(config['lane_phase_info'][self.intersection_id]['start_lane']), config['norm_tau']) self.reward_normalizer = Normalizer(1, config['norm_tau'])
def __init__(self, config): self.eng = cityflow.Engine(config["cityflow_config_file"], thread_num=config["thread_num"]) # self.eng = config["eng"][0] self.num_step = config["num_step"] self.intersection_id = config[ "intersection_id"] # list, [intersection_id, ...] self.num_agents = len(self.intersection_id) self.state_size = None self.lane_phase_info = config["lane_phase_info"] # "intersection_1_1" self.current_phase = {} self.current_phase_time = {} self.start_lane = {} self.end_lane = {} self.phase_list = {} self.phase_startLane_mapping = {} self.intersection_lane_mapping = {} #{id_:[lanes]} #### dw self.gamma = 0.5 self.R_buffer = [{id_: 0 for id_ in self.intersection_id}, {id_: 0 for id_ in self.intersection_id}] #### dw for id_ in self.intersection_id: self.start_lane[id_] = self.lane_phase_info[id_]['start_lane'] self.end_lane[id_] = self.lane_phase_info[id_]['end_lane'] self.phase_startLane_mapping[id_] = self.lane_phase_info[id_][ "phase_startLane_mapping"] self.phase_list[id_] = self.lane_phase_info[id_]["phase"] self.current_phase[id_] = self.phase_list[id_][0] self.current_phase_time[id_] = 0 self.get_state() # set self.state_size self.num_actions = len(self.phase_list[self.intersection_id[0]]) # self.observation_space = Box(np.ones(0.0*(self.state_size,)), 20.0*np.ones((self.state_size))) # self.action_space = Discrete(self.num_actions) # num of agents self.count = 0 self.done = False self.congestion = False self.reset()
def __init__(self, dir="1x1_config", episode_length=200): self.config_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), dir) self.cityflow = cityflow.Engine(os.path.join(self.config_dir, "config.json"), thread_num=1) self.nA = 2 self.cars = {} self.state = None self.action = None self.red_yellow_green = ['rrrGGGgrrrGGGg', 'GGgrrrrGGgrrrr'] self.episode_length = episode_length self.yellow_length = 3 self._bounding_box = np.array([[0. , 0.], [1020., 1020]]) self.edges = libsumo.edge.getIDList()
def test_multi_save_and_multi_load(self): """ Multiple save and multiple loads with multi-threading engine") """ engine = cityflow.Engine(config_file=self.config_file, thread_num=4) archives, records = [], [] repeats = 5 for i in range(repeats + 1): archives.append(engine.snapshot()) records.append(engine.get_lane_vehicle_count()) self.run_steps(engine, self.period) for i in range(repeats): for j in range(repeats): engine.load(archives[j]) self.run_and_check(engine, records[j + 1]) del engine
def __init__(self, config): config_path = 'data/{}/config.json'.format(args.scenario) self.eng = cityflow.Engine(config_path, threadNum=1) self.config = config self.num_step = config['num_step'] self.lane_phase_info = config['lane_phase_info'] # "intersection_1_1" self.intersection_id = list(self.lane_phase_info.keys())[0] self.start_lane = self.lane_phase_info[ self.intersection_id]['start_lane'] self.phase_list = self.lane_phase_info[self.intersection_id]["phase"] self.phase_startLane_mapping = self.lane_phase_info[ self.intersection_id]["phase_startLane_mapping"] self.current_phase = self.phase_list[0] self.current_phase_time = 0 self.yellow_time = 5 self.phase_log = []
def __init__(self, config): print("init") print("config:", list(config.keys())) self.config = config self.eng = cityflow.Engine(config["cityflow_config_file"], thread_num=config["thread_num"]) # self.eng = config["eng"][0] self.num_step = config["num_step"] self.intersection_id = config["intersection_id"] # list, [intersection_id, ...] self.num_agents = len(self.intersection_id) self.state_size = None self.lane_phase_info = config["lane_phase_info"] # "intersection_1_1" self.congestion_thres = 30 # 30 -> episode length will change self.current_phase = {} self.current_phase_time = {} self.start_lane = {} self.end_lane = {} self.phase_list = {} self.phase_startLane_mapping = {} self.intersection_lane_mapping = {} #{id_:[lanes]} self.action_log = deque(maxlen=100) for id_ in self.intersection_id: self.start_lane[id_] = self.lane_phase_info[id_]['start_lane'] self.end_lane[id_] = self.lane_phase_info[id_]['end_lane'] self.phase_startLane_mapping[id_] = self.lane_phase_info[id_]["phase_startLane_mapping"] self.phase_list[id_] = self.lane_phase_info[id_]["phase"] self.num_actions = len(self.phase_list[self.intersection_id[0]]) self.current_phase[id_] = self.phase_list[id_][np.random.randint(self.num_actions)] self.current_phase_time[id_] = 0 self.get_state() # set self.state_size # self.observation_space = Box(np.ones(0.0*(self.state_size,)), 20.0*np.ones((self.state_size))) # self.action_space = Discrete(self.num_actions) # num of agents self.count = 0 self.done = False self.congestion = False self.reset()
def __init__(self, obs_dim, action_dim, state_dim, INT_id_list): self.obs_dim = obs_dim self.action_dim = action_dim self.state_dim = state_dim self.num_agent = len(INT_id_list) self.INT_id_list = INT_id_list self.step_length = step_length self.yellow_len = yellow_len self.phase_num = phase_num self.max_veh_num = max_veh_num self.CityFlow = cityflow.Engine(config_path, thread_num=16) self.state = {} self.obs = np.zeros((self.obs_dim, self.num_agent)) self.RoadNet_dict = RoadNet_dict self.junction_num = len(self.RoadNet_dict['intersections']) self.ini_steps() self.update_state() self.update_obs()
def __init__(self, config_path, episode_length=200, scale=2): self.cityflowcfg = config_path self.scale = scale self.nA = 2 #background, shift = get_background(self.sumocfg, scale=self.scale) #self.background = background #self.shift = shift #net = sumolib.net.readNet('simulation_files/networks/simple.net.xml') #self._bounding_box = np.array(net.getBBoxXY()) self.cars = {} self.state = None self.action = None self.red_yellow_green = ['rrrGGGgrrrGGGg', 'GGgrrrrGGgrrrr'] self.episode_length = episode_length self.yellow_length = 3 self.env = cityflow.Engine(self.config_path, thread_num=1)
def test_save_and_load(self): """Single save and single load with single threading engine""" engine = cityflow.Engine(config_file=self.config_file, thread_num=1) self.run_steps(engine, self.period) start_time = time.time() archive = engine.snapshot() save_time = time.time() - start_time self.run_steps(engine, self.period) record0 = engine.get_lane_vehicle_count() start_time = time.time() engine.load(archive) load_time = time.time() - start_time self.run_and_check(engine, record0) del engine print("\nsave: %.4fs load: %.4fs" % (save_time, load_time))
def cal_travel_time(dic_sim_setting, plan_file): dic_sim_setting['saveReplay'] = True # Write to file so the engine can open it. # with open('src/config_args2.json', 'w') as outfile: # json.dump(dic_sim_setting, outfile) # if dic_sim_setting['data_set_mode'] == 'train': # with open('src/config_args2.json', 'w') as outfile: # json.dump(dic_sim_setting, outfile) # eng = cityflow.Engine("src/config_args2.json", thread_num=1) # if dic_sim_setting['data_set_mode'] == 'test': # with open('src/config_args2_test.json', 'w') as outfile: # json.dump(dic_sim_setting, outfile) # eng = cityflow.Engine("src/config_args2_test.json", thread_num=1) # if dic_sim_setting['data_set_mode'] == 'val': # with open('src/config_args2_val.json', 'w') as outfile: # json.dump(dic_sim_setting, outfile) # eng = cityflow.Engine("src/config_args2_val.json", thread_num=1) path = "src/config_{}_args2.json".format(dic_sim_setting['scenario']) with open(path, 'w') as outfile: json.dump(dic_sim_setting, outfile) eng = cityflow.Engine(path, thread_num=1) # eng = cityflow.Engine("src/config_args2.json", thread_num=1) plan = pd.read_csv(plan_file, sep="\t", header=0, dtype=int) intersection_id = plan.columns[0] actions = {-1: 0, 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0} tt_list = [] for step in range(dic_sim_setting["num_step"]): phase = int(plan.loc[step]) actions[phase-1] += 1 tt_list.append(eng.get_average_travel_time()) eng.set_tl_phase(intersection_id, phase) eng.next_step() return eng.get_average_travel_time(), actions, tt_list
def reset(self): cityflow_config = { "interval": self.dic_traffic_env_conf["INTERVAL"], "seed": 0, "laneChange": False, "dir": self.path_to_work_directory + "/", "roadnetFile": self.dic_traffic_env_conf["ROADNET_FILE"], "flowFile": self.dic_traffic_env_conf["TRAFFIC_FILE"], "rlTrafficLight": self.dic_traffic_env_conf["RLTRAFFICLIGHT"], "saveReplay": self.dic_traffic_env_conf["SAVEREPLAY"], "roadnetLogFile": "frontend/web/roadnetLogFile.json", "replayLogFile": "frontend/web/replayLogFile.txt" } print("=========================") print(cityflow_config) with open(os.path.join(self.path_to_work_directory, "cityflow.config"), "w") as json_file: json.dump(cityflow_config, json_file) self.eng = engine.Engine(os.path.join(self.path_to_work_directory, "cityflow.config"), thread_num=1) # self.load_roadnet() # self.load_flow() # initialize intersections (grid) self.list_intersection = [Intersection((i+1, j+1), self.dic_traffic_env_conf, self.eng) for i in range(self.dic_traffic_env_conf["NUM_ROW"]) for j in range(self.dic_traffic_env_conf["NUM_COL"])] self.list_inter_log = [[] for i in range(self.dic_traffic_env_conf["NUM_ROW"] * self.dic_traffic_env_conf["NUM_COL"])] # get lanes list self.list_lanes = [] for inter in self.list_intersection: self.list_lanes += inter.list_lanes self.list_lanes = np.unique(self.list_lanes).tolist() # get new measurements for inter in self.list_intersection: inter.update_current_measurements() state = self.get_state() return state
def __init__(self): config = "data/2x2/config.json" self.eng = cityflow.Engine(config, thread_num=4) self.cur_phase = { 'intersection_1_2': 0, 'intersection_1_1': 0, 'intersection_2_1': 0, 'intersection_2_2': 0 } self.lanes_dict = { 'intersection_1_2': ["road_2_2_2", "road_1_3_3", "road_0_2_0", "road_1_1_1"], 'intersection_1_1': ["road_1_2_3", "road_0_1_0", "road_1_0_1", "road_2_1_2"], 'intersection_2_1': ["road_1_1_0", "road_2_0_1", "road_3_1_2", "road_2_2_3"], 'intersection_2_2': ["road_2_1_1", "road_3_2_2", "road_2_3_3", "road_1_2_0"] } return
def __init__(self, config): self.eng = cityflow.Engine(config['cityflow_config_file'], thread_num=config['thread_num']) self.num_step = config['num_step'] self.lane_phase_info = config['lane_phase_info'] # "intersection_1_1" self.intersection_id = list(self.lane_phase_info.keys())[0] self.start_lane = self.lane_phase_info[ self.intersection_id]['start_lane'] self.phase_list = self.lane_phase_info[self.intersection_id]["phase"] self.phase_startLane_mapping = self.lane_phase_info[ self.intersection_id]["phase_startLane_mapping"] self.current_phase = self.phase_list[0] self.current_phase_time = 0 self.phase_log = [] self.step_count = 1 self.congestion_thres = 30