Skip to content

Commit

Permalink
Refactor priority handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Feb 7, 2024
1 parent 821c574 commit e5b7c88
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions core/src/allocation_init.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Find the edges from the main network to a subnetwork."""
function find_subnetwork_connections!(p::Parameters)::Nothing
(; allocation, graph, user) = p
n_priorities = length(user.demand[1])
(; allocation, graph, allocation) = p
n_priorities = length(allocation.priorities)
(; subnetwork_demands, subnetwork_allocateds) = allocation
for node_id in graph[].node_ids[1]
for outflow_id in outflow_ids(graph, node_id)
Expand Down
5 changes: 3 additions & 2 deletions core/src/create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,8 @@ function User(db::DB, config::Config)::User
error("Problems encountered when parsing User static and time node IDs.")
end

# All provided priorities
priorities = sort(unique(union(static.priority, time.priority)))
# All priorities used in the model
priorities = get_all_priorities(db, config)

active = BitVector()
min_level = Float64[]
Expand Down Expand Up @@ -837,6 +837,7 @@ function Parameters(db::DB, config::Config)::Parameters
Int[],
AllocationModel[],
Vector{Tuple{NodeID, NodeID}}[],
get_all_priorities(db, config),
Dict{Tuple{NodeID, NodeID}, Float64}(),
Dict{Tuple{NodeID, NodeID}, Float64}(),
(;
Expand Down
13 changes: 2 additions & 11 deletions core/src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct Allocation
allocation_network_ids::Vector{Int}
allocation_models::Vector{AllocationModel}
main_network_connections::Vector{Vector{Tuple{NodeID, NodeID}}}
priorities::Vector{Int}
subnetwork_demands::Dict{Tuple{NodeID, NodeID}, Vector{Float64}}
subnetwork_allocateds::Dict{Tuple{NodeID, NodeID}, Vector{Float64}}
record::@NamedTuple{
Expand Down Expand Up @@ -436,7 +437,6 @@ struct User <: AbstractParameterNode
allocated::Vector{Vector{Float64}}
return_factor::Vector{Float64}
min_level::Vector{Float64}
priorities::Vector{Int}
record::@NamedTuple{
time::Vector{Float64},
allocation_network_id::Vector{Int},
Expand All @@ -458,16 +458,7 @@ struct User <: AbstractParameterNode
record,
)
if valid_demand(node_id, demand, priorities)
return new(
node_id,
active,
demand,
allocated,
return_factor,
min_level,
priorities,
record,
)
return new(node_id, active, demand, allocated, return_factor, min_level, record)
else
error("Invalid demand")
end
Expand Down
15 changes: 15 additions & 0 deletions core/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1282,3 +1282,18 @@ end
function is_main_network(allocation_network_id::Int)::Bool
return allocation_network_id == 1
end

function get_all_priorities(db::DB, config::Config)::Vector{Int}
priorities = Set{Int}()

# TODO: Is there a way to automatically grab all tables with a priority column?
for type in [
UserStaticV1,
UserTimeV1,
AllocationLevelControlStaticV1,
AllocationLevelControlTimeV1,
]
union!(priorities, load_structvector(db, config, type).priority)
end
return sort(unique(priorities))
end

0 comments on commit e5b7c88

Please sign in to comment.