Skip to content

Commit

Permalink
Calibration dump
Browse files Browse the repository at this point in the history
Signed-off-by: Max <mcschrader@crimson.ua.edu>
  • Loading branch information
mschrader15 committed May 4, 2024
1 parent 3e3fd5e commit 2ed77e1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 33 deletions.
12 changes: 11 additions & 1 deletion sumo_pipelines/blocks/simulation_complex/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def __init__(
self._on = True
self.turn_off()

@property
def on(self):
return self._on

def get_e2s(self, all_e2_detector_ids):
self._e2s = list(
filter(lambda x: f"{self.tl}_{self.phase}" in x, all_e2_detector_ids)
Expand Down Expand Up @@ -115,7 +119,10 @@ def update(self, e3_subs, veh_subs, sim_time):
veh_subs[_id][tc.VAR_SPEED] * (self.truck_speed_factor * truck + 1), 0
)
self.accumulated_wtime += max(
(sim_time - self.accumulated_wtime_holder[(_id, truck)])
(
(sim_time - self.accumulated_wtime_holder[(_id, truck)])
* (veh_subs[_id][tc.VAR_SPEED] < 3)
)
* (self.truck_waiting_time_factor * truck + 1),
0,
)
Expand Down Expand Up @@ -251,6 +258,9 @@ def upstream_factor(tl, phase, signal_subs):
for _, phase_holders in lights.values():
for phase in phase_holders.values():
phase.update(e3_subs, veh_subs, sim_time / 1000)

# if not phase.on and phase.phase in (2, 6):

# print(
# f"tl: {_} phase: {phase.phase} speed: {phase.veh_speed_factor} wait: {phase.accumulated_wtime} count: {phase.veh_count}"
# )
Expand Down
1 change: 1 addition & 0 deletions sumo_pipelines/blocks/vehicle_distributions/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def _sample_dist(param: ParamConfig, n: int, seed: int = 42) -> np.array:
# first fast pass
vals = dist.rvs(n, random_state=seed)
if param.bounds:
param.bounds = [float(b) for b in param.bounds]

def get_ob() -> np.array:
return (vals < param.bounds[0]) | (vals > param.bounds[1])
Expand Down
1 change: 1 addition & 0 deletions sumo_pipelines/optimization/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ObjectiveConfig:
n_iterations: int = 1
return_intermediate: bool = field(default=False)
report_config: bool = field(default=False)
fail_safe: Any = field(default=False)
config: Dict[str, Any] = field(default_factory=dict)
additional_returns: Dict[str, Any] = field(default_factory=dict)

Expand Down
62 changes: 43 additions & 19 deletions sumo_pipelines/optimization/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def optimize_me(
local_global_config.Optimization.SearchSpace, config
)

fail_safe = local_global_config.Optimization.ObjectiveFn.fail_safe

empty_config = {}

def _update_mean(new_val_dict) -> dict:
Expand All @@ -63,26 +65,48 @@ def _update_mean(new_val_dict) -> dict:
for _ in range(local_global_config.Optimization.ObjectiveFn.n_iterations):
# execute the pre-processing pipeline
block = get_pipeline_by_name(local_global_config, "Pre-Processing")
if block is not None:
execute_pipe_block(block, local_global_config, random_state=rnd_state)

# execute the target function. Don't actually have to have an objective fn...
if local_global_config.Optimization.ObjectiveFn.function is not None:
res = local_global_config.Optimization.ObjectiveFn.function(
*args,
config=local_global_config,
function_config=local_global_config.Optimization.ObjectiveFn.config,
**kwargs,
)
if local_global_config.Optimization.ObjectiveFn.report_config:
res = _update_mean(
OmegaConf.to_container(
local_global_config.Optimization.ObjectiveFn.config,
resolve=True,
try:
if block is not None:
execute_pipe_block(
block, local_global_config, random_state=rnd_state
)
)
else:
res = _update_mean(res)

# execute the target function. Don't actually have to have an objective fn...
if local_global_config.Optimization.ObjectiveFn.function is not None:
res = local_global_config.Optimization.ObjectiveFn.function(
*args,
config=local_global_config,
function_config=local_global_config.Optimization.ObjectiveFn.config,
**kwargs,
)
if local_global_config.Optimization.ObjectiveFn.report_config:
res = _update_mean(
OmegaConf.to_container(
local_global_config.Optimization.ObjectiveFn.config,
resolve=True,
)
)
else:
res = _update_mean(res)
except Exception as e:
if fail_safe:
if local_global_config.Optimization.ObjectiveFn.report_config:
train.report(
_update_mean(
dict(
zip(
local_global_config.Optimization.ObjectiveFn.config.keys(),
[1e6]
* len(
local_global_config.Optimization.ObjectiveFn.config
),
)
)
)
)
return
else:
raise e

if local_global_config.Optimization.ObjectiveFn.return_intermediate:
train.report(res)
Expand Down
32 changes: 19 additions & 13 deletions sumo_pipelines/utils/nema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,10 @@ def update_coordinate_splits(
add_time = {
_p: rounded_time
for _p, rounded_time in zip(
add_time.keys(), iteround.saferound(add_time.values(), places=1)
add_time.keys(),
iteround.saferound(
add_time.values(), places=1, strategy=iteround.LARGEST
),
)
}

Expand Down Expand Up @@ -413,7 +416,10 @@ def update_coordinate_splits(
add_time = {
_p: rounded_time
for _p, rounded_time in zip(
add_time.keys(), iteround.saferound(add_time.values(), places=1)
add_time.keys(),
iteround.saferound(
add_time.values(), places=1, strategy=iteround.LARGEST
),
)
}

Expand Down Expand Up @@ -659,28 +665,28 @@ def add_params_to_xml(
# test the NEMALight class

tl = NEMALight.from_xml(
"/Users/max/Development/DOE-Project/airport-harper-calibration/simulation/additional/signals/63082002.NEMA.Coordinated.xml",
id="63082002",
programID="63082002_12",
"/Users/max/Development/DOE-Project/airport-harper-calibration/simulation/additional/signals/63082004.NEMA.Coordinated.xml",
id="63082004",
programID="63082004_20",
)

tl.update_offset(22)

tl.update_coordinate_splits(
{
2: 0.76666666,
6: 0.63333333,
2: 0.5,
6: 0.5,
}
)

tl.get_valid_phase_combos()

# tl.update_coordinate_splits(
# {
# 4: 0.2,
# 8: 0.1,
# }
# )
tl.update_coordinate_splits(
{
4: 0.5,
8: 0.5,
}
)

# tl.update_coordinate_splits(
# {
Expand Down

0 comments on commit 2ed77e1

Please sign in to comment.