Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -146,45 +146,54 @@ def optimize_signal_rl(congestion_level):
|
|
146 |
model.learn(total_timesteps=1000)
|
147 |
|
148 |
# Reset the environment and get the initial observation
|
149 |
-
obs = env.reset()
|
150 |
-
if not isinstance(obs, np.ndarray) or obs.ndim != 2: # Validate obs shape
|
151 |
-
raise ValueError(f"Unexpected observation shape: {obs}, type: {type(obs)}")
|
152 |
-
|
153 |
logging.info(f"Initial observation: {obs}")
|
154 |
|
|
|
|
|
|
|
|
|
155 |
# RL Optimization loop
|
156 |
-
for
|
157 |
-
# Predict action
|
158 |
action, _ = model.predict(obs, deterministic=True)
|
|
|
|
|
159 |
obs, rewards, dones, infos = env.step(action)
|
160 |
-
|
161 |
-
if not isinstance(obs, np.ndarray) or obs.ndim != 2: # Validate obs shape again
|
162 |
-
raise ValueError(f"Unexpected observation after step: {obs}, type: {type(obs)}")
|
163 |
-
|
164 |
-
# Flatten the observation for easier handling
|
165 |
-
obs = obs.flatten()
|
166 |
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
-
|
|
|
170 |
break
|
171 |
|
172 |
-
#
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
|
178 |
return f"Green for {optimal_duration}s, Red for {60 - optimal_duration}s"
|
179 |
|
180 |
except ValueError as ve:
|
181 |
-
logging.error(f"Value error: {ve}")
|
182 |
return "Error: Unexpected values encountered during optimization."
|
183 |
except Exception as e:
|
184 |
logging.error(f"Error optimizing signal with RL: {e}")
|
185 |
return "Error in RL Optimization"
|
186 |
|
187 |
|
|
|
188 |
def process_traffic_image(image):
|
189 |
"""
|
190 |
Orchestrates the traffic analysis workflow.
|
|
|
146 |
model.learn(total_timesteps=1000)
|
147 |
|
148 |
# Reset the environment and get the initial observation
|
149 |
+
obs = env.reset()
|
|
|
|
|
|
|
150 |
logging.info(f"Initial observation: {obs}")
|
151 |
|
152 |
+
# Ensure `obs` is valid
|
153 |
+
if not isinstance(obs, np.ndarray) or obs.ndim != 2:
|
154 |
+
raise ValueError(f"Invalid observation after reset: {obs} (type: {type(obs)}, ndim: {obs.ndim if isinstance(obs, np.ndarray) else 'N/A'})")
|
155 |
+
|
156 |
# RL Optimization loop
|
157 |
+
for step_count in range(10):
|
158 |
+
# Predict action
|
159 |
action, _ = model.predict(obs, deterministic=True)
|
160 |
+
|
161 |
+
# Perform environment step
|
162 |
obs, rewards, dones, infos = env.step(action)
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
+
# Check and log the observation
|
165 |
+
logging.debug(f"Step {step_count}: Obs: {obs}, Rewards: {rewards}, Done: {dones}, Infos: {infos}")
|
166 |
+
|
167 |
+
# Validate `obs` after each step
|
168 |
+
if not isinstance(obs, np.ndarray):
|
169 |
+
raise ValueError(f"Observation is not an array after step {step_count}: {obs}")
|
170 |
+
if obs.ndim == 0: # Handle 0-dimensional observation
|
171 |
+
obs = np.array([obs]) # Convert to at least 1D
|
172 |
+
logging.warning(f"Converted 0D obs to array: {obs}")
|
173 |
+
elif obs.ndim == 1: # Ensure it's 2D for consistency
|
174 |
+
obs = obs.reshape(1, -1)
|
175 |
|
176 |
+
# Stop if the environment signals it's done
|
177 |
+
if dones[0]:
|
178 |
break
|
179 |
|
180 |
+
# Extract the optimal signal duration from the last valid observation
|
181 |
+
obs = obs.flatten() # Ensure it's a 1D array for indexing
|
182 |
+
if len(obs) < 2:
|
183 |
+
raise ValueError(f"Observation does not contain enough elements: {obs}")
|
184 |
+
optimal_duration = int(obs[1])
|
185 |
|
186 |
return f"Green for {optimal_duration}s, Red for {60 - optimal_duration}s"
|
187 |
|
188 |
except ValueError as ve:
|
189 |
+
logging.error(f"Value error during RL optimization: {ve}")
|
190 |
return "Error: Unexpected values encountered during optimization."
|
191 |
except Exception as e:
|
192 |
logging.error(f"Error optimizing signal with RL: {e}")
|
193 |
return "Error in RL Optimization"
|
194 |
|
195 |
|
196 |
+
|
197 |
def process_traffic_image(image):
|
198 |
"""
|
199 |
Orchestrates the traffic analysis workflow.
|