Skip to content

Commit

Permalink
Deleted relevant code of alternative object functions in Ribasim pyth…
Browse files Browse the repository at this point in the history
…on and core (#1199)

Fixes #1191 
deleted all the relevant code of the Linear relative, quadratic
absolute and quadratic relative. Relevant unit tests are deleted or
modified. Updated the allocation page in documentation.
  • Loading branch information
Jingru923 authored Mar 5, 2024
1 parent 1fb4ca8 commit a6c95ce
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 298 deletions.
173 changes: 64 additions & 109 deletions core/src/allocation_init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -399,38 +399,36 @@ function add_variables_absolute_value!(
problem::JuMP.Model,
p::Parameters,
allocation_network_id::Int,
config::Config,
)::Nothing
(; graph, allocation) = p
(; main_network_connections) = allocation
if startswith(config.allocation.objective_type, "linear")
node_ids = graph[].node_ids[allocation_network_id]
node_ids_user_demand = NodeID[]
node_ids_basin = NodeID[]

for node_id in node_ids
type = node_id.type
if type == NodeType.UserDemand
push!(node_ids_user_demand, node_id)
elseif type == NodeType.Basin
push!(node_ids_basin, node_id)
end

node_ids = graph[].node_ids[allocation_network_id]
node_ids_user_demand = NodeID[]
node_ids_basin = NodeID[]

for node_id in node_ids
type = node_id.type
if type == NodeType.UserDemand
push!(node_ids_user_demand, node_id)
elseif type == NodeType.Basin
push!(node_ids_basin, node_id)
end
end

# For the main network, connections to subnetworks are treated as UserDemands
if is_main_network(allocation_network_id)
for connections_subnetwork in main_network_connections
for connection in connections_subnetwork
push!(node_ids_user_demand, connection[2])
end
# For the main network, connections to subnetworks are treated as UserDemands
if is_main_network(allocation_network_id)
for connections_subnetwork in main_network_connections
for connection in connections_subnetwork
push!(node_ids_user_demand, connection[2])
end
end

problem[:F_abs_user_demand] =
JuMP.@variable(problem, F_abs_user_demand[node_id = node_ids_user_demand])
problem[:F_abs_basin] =
JuMP.@variable(problem, F_abs_basin[node_id = node_ids_basin])
end

problem[:F_abs_user_demand] =
JuMP.@variable(problem, F_abs_user_demand[node_id = node_ids_user_demand])
problem[:F_abs_basin] = JuMP.@variable(problem, F_abs_basin[node_id = node_ids_basin])

return nothing
end

Expand Down Expand Up @@ -632,49 +630,30 @@ function add_constraints_absolute_value!(
problem::JuMP.Model,
flow_per_node::Dict{NodeID, JuMP.VariableRef},
F_abs::JuMP.Containers.DenseAxisArray,
objective_type::String,
variable_type::String,
)::Nothing
# Example demand
d = 2.0

node_ids = only(F_abs.axes)

if objective_type == "linear_absolute"
# These constraints together make sure that F_abs_* acts as the absolute
# value F_abs_* = |x| where x = F-d (here for example d = 2)
base_name = "abs_positive_$variable_type"
problem[Symbol(base_name)] = JuMP.@constraint(
problem,
[node_id = node_ids],
F_abs[node_id] >= (flow_per_node[node_id] - d),
base_name = base_name
)
base_name = "abs_negative_$variable_type"
problem[Symbol(base_name)] = JuMP.@constraint(
problem,
[node_id = node_ids],
F_abs[node_id] >= -(flow_per_node[node_id] - d),
base_name = base_name
)
elseif objective_type == "linear_relative"
# These constraints together make sure that F_abs_user_demand acts as the absolute
# value F_abs_user_demand = |x| where x = 1-F/d (here for example d = 2)
base_name = "abs_positive_$variable_type"
problem[Symbol(base_name)] = JuMP.@constraint(
problem,
[node_id = node_ids],
F_abs[node_id] >= (1 - flow_per_node[node_id] / d),
base_name = base_name
)
base_name = "abs_negative_$variable_type"
problem[Symbol(base_name)] = JuMP.@constraint(
problem,
[node_id = node_ids],
F_abs[node_id] >= -(1 - flow_per_node[node_id] / d),
base_name = base_name
)
end
# These constraints together make sure that F_abs_* acts as the absolute
# value F_abs_* = |x| where x = F-d (here for example d = 2)
base_name = "abs_positive_$variable_type"
problem[Symbol(base_name)] = JuMP.@constraint(
problem,
[node_id = node_ids],
F_abs[node_id] >= (flow_per_node[node_id] - d),
base_name = base_name
)
base_name = "abs_negative_$variable_type"
problem[Symbol(base_name)] = JuMP.@constraint(
problem,
[node_id = node_ids],
F_abs[node_id] >= -(flow_per_node[node_id] - d),
base_name = base_name
)

return nothing
end

Expand All @@ -685,51 +664,39 @@ absolute value of the expression comparing flow to a UserDemand to its demand.
function add_constraints_absolute_value_user_demand!(
problem::JuMP.Model,
p::Parameters,
config::Config,
)::Nothing
(; graph) = p

objective_type = config.allocation.objective_type
if startswith(objective_type, "linear")
F = problem[:F]
F_abs_user_demand = problem[:F_abs_user_demand]
F = problem[:F]
F_abs_user_demand = problem[:F_abs_user_demand]

flow_per_node = Dict(
node_id => F[(only(inflow_ids_allocation(graph, node_id)), node_id)] for
node_id in only(F_abs_user_demand.axes)
)
flow_per_node = Dict(
node_id => F[(only(inflow_ids_allocation(graph, node_id)), node_id)] for
node_id in only(F_abs_user_demand.axes)
)

add_constraints_absolute_value!(
problem,
flow_per_node,
F_abs_user_demand,
"user_demand",
)

add_constraints_absolute_value!(
problem,
flow_per_node,
F_abs_user_demand,
objective_type,
"user_demand",
)
end
return nothing
end

"""
Add constraints so that variables F_abs_basin act as the
absolute value of the expression comparing flow to a basin to its demand.
"""
function add_constraints_absolute_value_basin!(problem::JuMP.Model, config::Config)::Nothing
objective_type = config.allocation.objective_type
if startswith(objective_type, "linear")
F_basin_in = problem[:F_basin_in]
F_abs_basin = problem[:F_abs_basin]
flow_per_node =
Dict(node_id => F_basin_in[node_id] for node_id in only(F_abs_basin.axes))
function add_constraints_absolute_value_basin!(problem::JuMP.Model)::Nothing
F_basin_in = problem[:F_basin_in]
F_abs_basin = problem[:F_abs_basin]
flow_per_node =
Dict(node_id => F_basin_in[node_id] for node_id in only(F_abs_basin.axes))

add_constraints_absolute_value!(problem, flow_per_node, F_abs_basin, "basin")

add_constraints_absolute_value!(
problem,
flow_per_node,
F_abs_basin,
objective_type,
"basin",
)
end
return nothing
end

Expand Down Expand Up @@ -806,7 +773,6 @@ end
Construct the allocation problem for the current subnetwork as a JuMP.jl model.
"""
function allocation_problem(
config::Config,
p::Parameters,
capacity::SparseMatrixCSC{Float64, Int},
allocation_network_id::Int,
Expand All @@ -817,15 +783,15 @@ function allocation_problem(
# Add variables to problem
add_variables_flow!(problem, p, allocation_network_id)
add_variables_basin!(problem, p, allocation_network_id)
add_variables_absolute_value!(problem, p, allocation_network_id, config)
add_variables_absolute_value!(problem, p, allocation_network_id)

# Add constraints to problem
add_constraints_capacity!(problem, capacity, p, allocation_network_id)
add_constraints_source!(problem, p, allocation_network_id)
add_constraints_flow_conservation!(problem, p, allocation_network_id)
add_constraints_user_demand_returnflow!(problem, p, allocation_network_id)
add_constraints_absolute_value_user_demand!(problem, p, config)
add_constraints_absolute_value_basin!(problem, config)
add_constraints_absolute_value_user_demand!(problem, p)
add_constraints_absolute_value_basin!(problem)
add_constraints_fractional_flow!(problem, p, allocation_network_id)
add_constraints_basin_flow!(problem)

Expand All @@ -837,7 +803,6 @@ Construct the JuMP.jl problem for allocation.
Inputs
------
config: The model configuration with allocation configuration in config.allocation
p: Ribasim problem parameters
Δt_allocation: The timestep between successive allocation solves
Expand All @@ -846,7 +811,6 @@ Outputs
An AllocationModel object.
"""
function AllocationModel(
config::Config,
allocation_network_id::Int,
p::Parameters,
Δt_allocation::Float64,
Expand All @@ -855,16 +819,7 @@ function AllocationModel(
capacity = allocation_graph(p, allocation_network_id)

# The JuMP.jl allocation problem
problem = allocation_problem(config, p, capacity, allocation_network_id)
if config.allocation.objective_type != "linear_absolute"
error("Type of object function is not supported")
end
problem = allocation_problem(p, capacity, allocation_network_id)

return AllocationModel(
Symbol(config.allocation.objective_type),
allocation_network_id,
capacity,
problem,
Δt_allocation,
)
return AllocationModel(allocation_network_id, capacity, problem, Δt_allocation)
end
Loading

0 comments on commit a6c95ce

Please sign in to comment.