Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,8 @@ import requests
|
|
4 |
from geopy.geocoders import Nominatim # Valid import
|
5 |
from stable_baselines3 import PPO
|
6 |
from stable_baselines3.common.vec_env import DummyVecEnv
|
7 |
-
import gym
|
8 |
-
from
|
9 |
import numpy as np
|
10 |
from PIL import Image
|
11 |
import gradio as gr
|
@@ -68,7 +68,11 @@ class TrafficSimEnv(gym.Env):
|
|
68 |
self.congestion_level = congestion_level
|
69 |
|
70 |
# Define observation space: [congestion_level, signal_duration]
|
71 |
-
self.observation_space = spaces.Box(
|
|
|
|
|
|
|
|
|
72 |
|
73 |
# Define action space: [increase, decrease, maintain]
|
74 |
self.action_space = spaces.Discrete(3)
|
@@ -129,14 +133,8 @@ class TrafficSimEnv(gym.Env):
|
|
129 |
def optimize_signal_rl(congestion_level):
|
130 |
try:
|
131 |
# Map congestion levels (string to numeric)
|
132 |
-
congestion_map = {
|
133 |
-
|
134 |
-
"Medium": 5,
|
135 |
-
"High": 8
|
136 |
-
}
|
137 |
-
|
138 |
-
if isinstance(congestion_level, str):
|
139 |
-
congestion_level = congestion_map.get(congestion_level, 5) # Default to "Medium" if unrecognized
|
140 |
|
141 |
# Create environment
|
142 |
env = DummyVecEnv([lambda: TrafficSimEnv(congestion_level)])
|
@@ -149,27 +147,29 @@ def optimize_signal_rl(congestion_level):
|
|
149 |
|
150 |
# Reset the environment
|
151 |
obs = env.reset()
|
|
|
152 |
|
153 |
-
# Ensure obs is properly handled
|
154 |
-
if obs.ndim == 0:
|
155 |
obs = np.expand_dims(obs, axis=0) # Convert 0D array to 1D
|
|
|
|
|
156 |
|
157 |
# RL Optimization loop
|
158 |
for _ in range(10):
|
159 |
action, _ = model.predict(obs, deterministic=True)
|
160 |
obs, rewards, dones, infos = env.step(action)
|
|
|
161 |
|
162 |
# Ensure obs is properly handled
|
163 |
-
if obs.ndim == 0:
|
164 |
obs = np.expand_dims(obs, axis=0)
|
|
|
|
|
165 |
|
166 |
-
#
|
167 |
-
|
168 |
-
|
169 |
-
done = dones[0]
|
170 |
-
|
171 |
-
# Debugging logs for better tracking
|
172 |
-
logging.debug(f"Action: {action}, Observation: {obs}, Reward: {reward}, Done: {done}")
|
173 |
|
174 |
if done:
|
175 |
break
|
|
|
4 |
from geopy.geocoders import Nominatim # Valid import
|
5 |
from stable_baselines3 import PPO
|
6 |
from stable_baselines3.common.vec_env import DummyVecEnv
|
7 |
+
import gymnasium as gym
|
8 |
+
from gymnasium import spaces
|
9 |
import numpy as np
|
10 |
from PIL import Image
|
11 |
import gradio as gr
|
|
|
68 |
self.congestion_level = congestion_level
|
69 |
|
70 |
# Define observation space: [congestion_level, signal_duration]
|
71 |
+
self.observation_space = spaces.Box(
|
72 |
+
low=np.array([0, 0], dtype=np.float32),
|
73 |
+
high=np.array([10, 60], dtype=np.float32),
|
74 |
+
dtype=np.float32
|
75 |
+
)
|
76 |
|
77 |
# Define action space: [increase, decrease, maintain]
|
78 |
self.action_space = spaces.Discrete(3)
|
|
|
133 |
def optimize_signal_rl(congestion_level):
|
134 |
try:
|
135 |
# Map congestion levels (string to numeric)
|
136 |
+
congestion_map = {"Low": 2, "Medium": 5, "High": 8}
|
137 |
+
congestion_level = congestion_map.get(congestion_level, 5) if isinstance(congestion_level, str) else congestion_level
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
# Create environment
|
140 |
env = DummyVecEnv([lambda: TrafficSimEnv(congestion_level)])
|
|
|
147 |
|
148 |
# Reset the environment
|
149 |
obs = env.reset()
|
150 |
+
logging.debug(f"Observation after reset: {obs}, type: {type(obs)}, shape: {np.shape(obs)}")
|
151 |
|
152 |
+
# Ensure obs is properly handled
|
153 |
+
if isinstance(obs, np.ndarray) and obs.ndim == 0:
|
154 |
obs = np.expand_dims(obs, axis=0) # Convert 0D array to 1D
|
155 |
+
elif not isinstance(obs, np.ndarray):
|
156 |
+
obs = np.array(obs) # Ensure it's an array
|
157 |
|
158 |
# RL Optimization loop
|
159 |
for _ in range(10):
|
160 |
action, _ = model.predict(obs, deterministic=True)
|
161 |
obs, rewards, dones, infos = env.step(action)
|
162 |
+
logging.debug(f"Step results -> obs: {obs}, rewards: {rewards}, dones: {dones}, infos: {infos}")
|
163 |
|
164 |
# Ensure obs is properly handled
|
165 |
+
if isinstance(obs, np.ndarray) and obs.ndim == 0:
|
166 |
obs = np.expand_dims(obs, axis=0)
|
167 |
+
elif not isinstance(obs, np.ndarray):
|
168 |
+
obs = np.array(obs)
|
169 |
|
170 |
+
obs = obs[0] if obs.ndim > 1 else obs # Flatten to 1D if necessary
|
171 |
+
reward = rewards[0] if isinstance(rewards, list) else rewards
|
172 |
+
done = dones[0] if isinstance(dones, list) else dones
|
|
|
|
|
|
|
|
|
173 |
|
174 |
if done:
|
175 |
break
|