Fzina commited on
Commit
12eccb4
·
verified ·
1 Parent(s): c241619

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -10
app.py CHANGED
@@ -128,7 +128,6 @@ class TrafficSimEnv(gym.Env):
128
  # Prior to commit had a lot of errors regarding expected output errors
129
  def optimize_signal_rl(congestion_level):
130
  try:
131
-
132
  # Map congestion levels (string to numeric)
133
  congestion_map = {
134
  "Low": 2,
@@ -139,7 +138,6 @@ def optimize_signal_rl(congestion_level):
139
  if isinstance(congestion_level, str):
140
  congestion_level = congestion_map.get(congestion_level, 5) # Default to "Medium" if unrecognized
141
 
142
-
143
  # Create environment
144
  env = DummyVecEnv([lambda: TrafficSimEnv(congestion_level)])
145
 
@@ -150,25 +148,25 @@ def optimize_signal_rl(congestion_level):
150
  model.learn(total_timesteps=1000)
151
 
152
  # Reset the environment
153
- obs, info = env.reset()
154
- obs = obs[0] # Extract first observation from batch
155
 
 
156
  for _ in range(10):
157
  action, _ = model.predict(obs, deterministic=True)
158
- obs, reward, done, truncated, info = env.step(action)
159
 
160
- # Unpack single observations (adjust for DummyVecEnv behavior)
161
  obs = obs[0]
162
- reward = reward[0]
163
- done = done[0]
164
- truncated = truncated[0]
165
 
166
  # Debugging logs for better tracking
167
  logging.debug(f"Action: {action}, Observation: {obs}, Reward: {reward}, Done: {done}")
168
 
169
- if done or truncated:
170
  break
171
 
 
172
  optimal_duration = int(obs[1]) if len(obs) > 1 else 30
173
  return f"Green for {optimal_duration}s, Red for {60 - optimal_duration}s"
174
 
 
128
  # Prior to commit had a lot of errors regarding expected output errors
129
  def optimize_signal_rl(congestion_level):
130
  try:
 
131
  # Map congestion levels (string to numeric)
132
  congestion_map = {
133
  "Low": 2,
 
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)])
143
 
 
148
  model.learn(total_timesteps=1000)
149
 
150
  # Reset the environment
151
+ obs = env.reset() # Returns a single value (batch of observations)
 
152
 
153
+ # RL Optimization loop
154
  for _ in range(10):
155
  action, _ = model.predict(obs, deterministic=True)
156
+ obs, rewards, dones, infos = env.step(action)
157
 
158
+ # Unpack single observation and reward (batch size of 1)
159
  obs = obs[0]
160
+ reward = rewards[0]
161
+ done = dones[0]
 
162
 
163
  # Debugging logs for better tracking
164
  logging.debug(f"Action: {action}, Observation: {obs}, Reward: {reward}, Done: {done}")
165
 
166
+ if done:
167
  break
168
 
169
+ # Get optimal duration
170
  optimal_duration = int(obs[1]) if len(obs) > 1 else 30
171
  return f"Green for {optimal_duration}s, Red for {60 - optimal_duration}s"
172