You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This bug is inconsistent. Every fresh Julia session on my machine always trigger this bug. Re-running without restarting Julia sometimes didn't trigger the bug.
ProblemTemplate.branches can contain branch model keys using namespaced symbols such as:
One additional complication is that this behavior appears inconsistent. In some Julia sessions or call paths, the same build_problem_base_uc() function produced the expected keys, such as :MonitoredLine and :Line. In other runs, especially after restart/reload, it produced Symbol("PowerSystems.MonitoredLine") and Symbol("PowerSystems.Line"). This made the issue harder to diagnose because the template display looked correct in both cases, while the underlying dictionary keys differed.
The display table still prints MonitoredLine and Line, so the template looks correct visually, but the internal dictionary keys are not compatible with the later lookup path.
To Reproduce
This is the relevant UC template builder:
using HydroPowerSimulations
using StorageSystemsSimulations
using PowerModels
using PowerSystems
using PowerSimulations
functionbuild_problem_base_uc(; network_model = NFAPowerModel)
template_uc =ProblemTemplate()
set_device_model!(template_uc, MonitoredLine, StaticBranchBounds)
set_device_model!(template_uc, Line, StaticBranch)
set_device_model!(template_uc, ThermalStandard, ThermalStandardUnitCommitment)
set_device_model!(template_uc, RenewableDispatch, RenewableFullDispatch)
set_device_model!(template_uc, RenewableNonDispatch, FixedOutput)
set_device_model!(template_uc, PowerLoad, StaticPowerLoad)
storage_model =DeviceModel(
EnergyReservoirStorage,
StorageDispatchWithReserves;
attributes =Dict(
"reservation"=>true,
"energy_target"=>false,
"cycling_limits"=>false,
"regularization"=>false,
),
use_slacks =false,
)
set_device_model!(template_uc, storage_model)
set_network_model!(
template_uc,
NetworkModel(network_model; use_slacks =true),
)
return template_uc
end
template.branches keys:Symbol("PowerSystems.Line")
Symbol("PowerSystems.MonitoredLine")
has :MonitoredLine=false
has Symbol("PowerSystems.MonitoredLine") =true
For a system containing MonitoredLine components, the model then fails during build:
A workaround is to normalize the branch keys before returning the template:
function_device_type_from_model(::DeviceModel{D}) where {D}
return D
endfunctionnormalize_template_branch_keys!(template::ProblemTemplate)
bad_keys = [
k for k inkeys(template.branches)
ifoccursin(".", String(k))
]
ifisempty(bad_keys)
return template
end@warn"Detected namespaced branch model keys in template.branches; normalizing before returning template" bad_keys
fixed =Dict{Symbol, DeviceModel{<:Branch}}()
for (old_key, model) in template.branches
device_type =_device_type_from_model(model)
new_key =nameof(device_type)
fixed[new_key] = model
endempty!(template.branches)
for (k, v) in fixed
template.branches[k] = v
end@warn"Normalized template.branches keys" normalized_keys =collect(keys(template.branches))
return template
end
Calling this before returning the template fixes the issue:
template.branches keys:Symbol("PowerSystems.Line")
Symbol("PowerSystems.MonitoredLine")
has :MonitoredLine=false
has Symbol("PowerSystems.MonitoredLine") =true
The system being solved contains MonitoredLine components and no Line components:
Line count =0
MonitoredLine count =17
The issue is easy to miss because the printed ProblemTemplate table displays:
Branch Models
MonitoredLine | StaticBranchBounds
Line | StaticBranch
even when the underlying dictionary keys are namespaced and incompatible with the later internal lookup.
A robust internal fix may be to derive branch dictionary keys from the device type using:
nameof(device_type)
rather than any string representation that may include the module prefix.
Another possible fix would be to make the DecisionModel build/solve path tolerant of both key conventions. For example, when looking up a branch model, PowerSimulations could accept either :MonitoredLine or Symbol("PowerSystems.MonitoredLine") for the same device type. This would make the downstream build path robust even if a template contains namespaced symbols. However, normalizing the keys at template construction time may still be cleaner, because it keeps one canonical internal representation.
Describe the bug
Warning
This bug is inconsistent. Every fresh Julia session on my machine always trigger this bug. Re-running without restarting Julia sometimes didn't trigger the bug.
ProblemTemplate.branchescan contain branch model keys using namespaced symbols such as:instead of the plain symbols expected later by PowerSimulations:
This causes
DecisionModelbuild failure for systems containingMonitoredLinecomponents, because PowerSimulations later looks up:branch_models[:MonitoredLine]but the dictionary contains:
The resulting error is:
This appears to happen when using the 3-argument form of
set_device_model!for branch models:Warning
One additional complication is that this behavior appears inconsistent. In some Julia sessions or call paths, the same
build_problem_base_uc()function produced the expected keys, such as:MonitoredLineand:Line. In other runs, especially after restart/reload, it producedSymbol("PowerSystems.MonitoredLine")andSymbol("PowerSystems.Line"). This made the issue harder to diagnose because the template display looked correct in both cases, while the underlying dictionary keys differed.The display table still prints
MonitoredLineandLine, so the template looks correct visually, but the internal dictionary keys are not compatible with the later lookup path.To Reproduce
This is the relevant UC template builder:
Then inspect the branch keys:
Observed output:
For a system containing
MonitoredLinecomponents, the model then fails during build:Error:
A workaround is to normalize the branch keys before returning the template:
Calling this before returning the template fixes the issue:
After normalization:
The model then proceeds past the previous
KeyError.Expected behavior
set_device_model!should store branch models under stable, non-namespaced keys matching the internal lookup convention used later by PowerSimulations:or the later lookup path should use the same key convention as the setter.
The following should not result in keys like
Symbol("PowerSystems.MonitoredLine"):Screenshots
Not applicable. The relevant debug output is:
and the build failure is:
Additional context
Environment where this was observed:
The system being solved contains
MonitoredLinecomponents and noLinecomponents:The issue is easy to miss because the printed
ProblemTemplatetable displays:even when the underlying dictionary keys are namespaced and incompatible with the later internal lookup.
A robust internal fix may be to derive branch dictionary keys from the device type using:
nameof(device_type)rather than any string representation that may include the module prefix.
Another possible fix would be to make the
DecisionModelbuild/solve path tolerant of both key conventions. For example, when looking up a branch model, PowerSimulations could accept either:MonitoredLineorSymbol("PowerSystems.MonitoredLine")for the same device type. This would make the downstream build path robust even if a template contains namespaced symbols. However, normalizing the keys at template construction time may still be cleaner, because it keeps one canonical internal representation.