Spaces:
Running on Zero
Running on Zero
File size: 4,595 Bytes
f234112 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | class ConflictGraph:
"""
Represents conflicts between traffic movements.
Each movement is a node.
An edge between two movements means:
those two movements cannot receive green
at the same time.
"""
def __init__(self, topology):
self.topology = topology
# movement_id -> set of conflicting movement_ids
self.conflicts = {}
# Initialize every movement
for movement_id in topology.movements:
self.conflicts[movement_id] = set()
def add_conflict(self, movement_a, movement_b):
"""
Add a two-way conflict between two movements.
"""
if movement_a not in self.topology.movements:
raise ValueError(
f"Unknown movement: {movement_a}"
)
if movement_b not in self.topology.movements:
raise ValueError(
f"Unknown movement: {movement_b}"
)
if movement_a == movement_b:
raise ValueError(
"A movement cannot conflict with itself"
)
self.conflicts[movement_a].add(movement_b)
self.conflicts[movement_b].add(movement_a)
def get_conflicts(self, movement_id):
"""
Return all movements that conflict
with the given movement.
"""
if movement_id not in self.conflicts:
raise ValueError(
f"Unknown movement: {movement_id}"
)
return self.conflicts[movement_id]
def are_conflicting(self, movement_a, movement_b):
"""
Check whether two movements conflict.
"""
if movement_a not in self.conflicts:
raise ValueError(
f"Unknown movement: {movement_a}"
)
if movement_b not in self.conflicts:
raise ValueError(
f"Unknown movement: {movement_b}"
)
return movement_b in self.conflicts[movement_a]
def validate(self):
"""
Validate the conflict graph.
"""
for movement_id, conflicts in self.conflicts.items():
for conflicting_movement in conflicts:
if conflicting_movement not in self.conflicts:
raise ValueError(
f"Conflict references unknown movement: "
f"{conflicting_movement}"
)
# Conflict must be symmetric
if movement_id not in self.conflicts[
conflicting_movement
]:
raise ValueError(
f"Conflict is not symmetric: "
f"{movement_id} <-> "
f"{conflicting_movement}"
)
return True
def to_dict(self):
"""
Convert conflict graph into a dictionary.
"""
return {
movement_id: sorted(list(conflicts))
for movement_id, conflicts in self.conflicts.items()
}
if __name__ == "__main__":
# Import our topology module
from topology import JunctionTopology
# -------------------------------------------------
# Create example topology
# -------------------------------------------------
topology = JunctionTopology("J1")
topology.add_road("A", lanes=2)
topology.add_road("B", lanes=2)
topology.add_road("C", lanes=1)
topology.add_movement(
"A_to_B",
"A",
"B",
lanes=2
)
topology.add_movement(
"A_to_C",
"A",
"C",
lanes=1
)
topology.add_movement(
"B_to_A",
"B",
"A",
lanes=2
)
topology.add_movement(
"C_to_A",
"C",
"A",
lanes=1
)
# -------------------------------------------------
# Create conflict graph
# -------------------------------------------------
graph = ConflictGraph(topology)
# Example conflict relationships.
#
# These are explicitly supplied for the test.
# We are NOT assuming that road names determine
# physical conflicts.
graph.add_conflict(
"A_to_B",
"C_to_A"
)
graph.add_conflict(
"A_to_C",
"B_to_A"
)
# -------------------------------------------------
# Validate
# -------------------------------------------------
topology.validate()
graph.validate()
print("Topology is valid.")
print("Conflict graph is valid.")
print()
print(graph.to_dict()) |