From 212c3871a00424d50660710116ba02fe5cb9f5fd Mon Sep 17 00:00:00 2001 From: Kathy Jang Date: Fri, 6 Jul 2018 15:53:25 -0700 Subject: [PATCH 1/2] Small fix to baseline --- flow/core/generator.py | 113 +++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/flow/core/generator.py b/flow/core/generator.py index 0dab37e4..8c37fa83 100755 --- a/flow/core/generator.py +++ b/flow/core/generator.py @@ -239,63 +239,64 @@ def generate_cfg(self, net_params, traffic_lights): add.append(E("route", id="route%s" % edge, edges=" ".join(route))) # add (optionally) the traffic light properties to the .add.xml file - if traffic_lights.num_traffic_lights > 0: - if traffic_lights.baseline: - tl_type = str(traffic_lights["tl_type"]) - program_id = str(traffic_lights["program_id"]) - phases = traffic_lights["phases"] - max_gap = str(traffic_lights["max_gap"]) - detector_gap = str(traffic_lights["detector_gap"]) - show_detector = traffic_lights["show_detectors"] - - detectors = {"key": "detector-gap", "value": detector_gap} - gap = {"key": "max-gap", "value": max_gap} - - if show_detector: - show_detector = {"key": "show-detectors", "value": "true"} - else: - show_detector = {"key": "show-detectors", "value": "false"} - - # FIXME(ak): add abstract method - nodes = self.specify_tll(net_params) - tll = [] - for node in nodes: - tll.append({"id": node['id'], "type": tl_type, - "programID": program_id}) - - for elem in tll: - e = E("tlLogic", **elem) - e.append(E("param", **show_detector)) - e.append(E("param", **gap)) - e.append(E("param", **detectors)) - for phase in phases: - e.append(E("phase", **phase)) - add.append(e) - + if traffic_lights.baseline: + defaults = traffic_lights.actuated_default() + tl_type = str(defaults["tl_type"]) + program_id = str(defaults["program_id"]) + max_gap = str(defaults["max_gap"]) + detector_gap = str(defaults["detector_gap"]) + show_detector = defaults["show_detectors"] + phases = defaults["phases"] + + detectors = {"key": "detector-gap", "value": detector_gap} + gap = {"key": "max-gap", "value": max_gap} + + if show_detector: + show_detector = {"key": "show-detectors", "value": "true"} else: - tl_properties = traffic_lights.get_properties() - for node in tl_properties.values(): - # at this point, the generator assumes that traffic lights - # are properly formed. If there are no phases for a static - # traffic light, ignore and use default - if node["type"] == "static" and not node.get("phases"): - continue - - elem = {"id": str(node["id"]), "type": str(node["type"]), - "programID": str(node["programID"])} - if node.get("offset"): - elem["offset"] = str(node.get("offset")) - - e = E("tlLogic", **elem) - for key, value in node.items(): - if key == "phases": - for phase in node.get("phases"): - e.append(E("phase", **phase)) - else: - e.append(E("param", - **{"key": key, "value": str(value)})) - - add.append(e) + show_detector = {"key": "show-detectors", "value": "false"} + + # FIXME(ak): add abstract method + nodes = self.specify_tll(net_params) + tll = [] + for node in nodes: + tll.append({"id": node['id'], "type": tl_type, + "programID": program_id}) + + for elem in tll: + e = E("tlLogic", **elem) + e.append(E("param", **show_detector)) + e.append(E("param", **gap)) + e.append(E("param", **detectors)) + for phase in phases: + e.append(E("phase", **phase)) + add.append(e) + + elif traffic_lights.num_traffic_lights > 0: + + tl_properties = traffic_lights.get_properties() + for node in tl_properties.values(): + # at this point, the generator assumes that traffic lights + # are properly formed. If there are no phases for a static + # traffic light, ignore and use default + if node["type"] == "static" and not node.get("phases"): + continue + + elem = {"id": str(node["id"]), "type": str(node["type"]), + "programID": str(node["programID"])} + if node.get("offset"): + elem["offset"] = str(node.get("offset")) + + e = E("tlLogic", **elem) + for key, value in node.items(): + if key == "phases": + for phase in node.get("phases"): + e.append(E("phase", **phase)) + else: + e.append(E("param", + **{"key": key, "value": str(value)})) + + add.append(e) printxml(add, self.cfg_path + addfn) From debdc47a8e9ff6342d047a6b85fdd19ab956b5c4 Mon Sep 17 00:00:00 2001 From: Kathy Jang Date: Thu, 2 Aug 2018 13:43:57 -0700 Subject: [PATCH 2/2] Fixed if statement logic to avoid Nonetype attribute error --- flow/core/generator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flow/core/generator.py b/flow/core/generator.py index 8c37fa83..97088c24 100755 --- a/flow/core/generator.py +++ b/flow/core/generator.py @@ -239,7 +239,7 @@ def generate_cfg(self, net_params, traffic_lights): add.append(E("route", id="route%s" % edge, edges=" ".join(route))) # add (optionally) the traffic light properties to the .add.xml file - if traffic_lights.baseline: + if traffic_lights and traffic_lights.baseline: defaults = traffic_lights.actuated_default() tl_type = str(defaults["tl_type"]) program_id = str(defaults["program_id"]) @@ -272,7 +272,7 @@ def generate_cfg(self, net_params, traffic_lights): e.append(E("phase", **phase)) add.append(e) - elif traffic_lights.num_traffic_lights > 0: + elif traffic_lights and traffic_lights.num_traffic_lights > 0: tl_properties = traffic_lights.get_properties() for node in tl_properties.values():