Skip to content

Commit

Permalink
Merge branch 'main' into document-teamcity-pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
Jingru923 committed Apr 15, 2024
2 parents 1733d18 + 799a2d5 commit a47b5e4
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 296 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/core_testmodels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
with:
cache-compiled: "true"
cache-registries: "true"
- uses: prefix-dev/[email protected].1
- uses: prefix-dev/[email protected].2
with:
pixi-version: "latest"
- name: Prepare pixi
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/core_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
with:
cache-compiled: "true"
cache-registries: "true"
- uses: prefix-dev/[email protected].1
- uses: prefix-dev/[email protected].2
with:
pixi-version: "latest"
- name: Prepare pixi
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
with:
cache-compiled: "true"
cache-registries: "true"
- uses: prefix-dev/[email protected].1
- uses: prefix-dev/[email protected].2
with:
pixi-version: "latest"
- name: Prepare pixi
Expand All @@ -41,7 +41,7 @@ jobs:

- name: Publish Quarto Project
if: github.ref == 'refs/heads/main'
uses: peaceiris/actions-gh-pages@v3
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs/_site
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/julia_auto_update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4
with:
ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
- uses: prefix-dev/[email protected].1
- uses: prefix-dev/[email protected].2
with:
pixi-version: "latest"
- name: Update Julia manifest file
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
continue-on-error: true
steps:
- uses: actions/checkout@v4
- uses: prefix-dev/[email protected].1
- uses: prefix-dev/[email protected].2
with:
pixi-version: "latest"
- name: Prepare pixi
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pixi_auto_update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4
with:
ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
- uses: prefix-dev/[email protected].1
- uses: prefix-dev/[email protected].2
with:
pixi-version: "latest"
cache: false
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python_codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
continue-on-error: true
steps:
- uses: actions/checkout@v4
- uses: prefix-dev/[email protected].1
- uses: prefix-dev/[email protected].2
with:
pixi-version: "latest"
- name: Prepare pixi
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- py310
steps:
- uses: actions/checkout@v4
- uses: prefix-dev/[email protected].1
- uses: prefix-dev/[email protected].2
with:
pixi-version: "latest"
- name: Prepare pixi
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/qgis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- windows-latest
steps:
- uses: actions/checkout@v4
- uses: prefix-dev/[email protected].1
- uses: prefix-dev/[email protected].2
with:
pixi-version: "latest"
- name: Prepare pixi
Expand Down
6 changes: 3 additions & 3 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.10.0"
manifest_format = "2.0"
project_hash = "22708fb864a341d70b001d75765852e92f1f5399"
project_hash = "122f7ec8a573942606ded1bed87e60f2356f603a"

[[deps.ADTypes]]
git-tree-sha1 = "016833eb52ba2d6bea9fcb50ca295980e728ee24"
Expand Down Expand Up @@ -401,9 +401,9 @@ version = "6.149.0"

[[deps.DiffEqCallbacks]]
deps = ["DataStructures", "DiffEqBase", "ForwardDiff", "Functors", "LinearAlgebra", "Markdown", "NonlinearSolve", "Parameters", "RecipesBase", "RecursiveArrayTools", "SciMLBase", "StaticArraysCore"]
git-tree-sha1 = "a731383bbafb87d496fb5e66f60c40e4a5f8f726"
git-tree-sha1 = "173d6a313878635b6c764163f639ac2d5f201fdc"
uuid = "459566f4-90b8-5000-8ac3-15dfb0a30def"
version = "3.4.0"
version = "3.6.0"

[deps.DiffEqCallbacks.weakdeps]
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Expand Down
2 changes: 1 addition & 1 deletion core/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ DataInterpolations = "4.4"
DataStructures = "0.18"
Dates = "<0.0.1,1"
Dictionaries = "0.3.25, 0.4"
DiffEqCallbacks = "=3.4.0"
DiffEqCallbacks = "3.6"
Documenter = "0.27,1"
EnumX = "1.0"
FiniteDiff = "2.21"
Expand Down
158 changes: 2 additions & 156 deletions core/src/allocation_init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ function find_subnetwork_connections!(p::Parameters)::Nothing
(; subnetwork_demands, subnetwork_allocateds) = allocation
for node_id in graph[].node_ids[1]
for outflow_id in outflow_ids(graph, node_id)
if graph[outflow_id].allocation_network_id != 1
if (graph[outflow_id].allocation_network_id != 1) |
(graph[node_id, outflow_id].allocation_network_id_source == 1)
main_network_source_edges =
get_main_network_connections(p, graph[outflow_id].allocation_network_id)
edge = (node_id, outflow_id)
Expand Down Expand Up @@ -405,55 +406,6 @@ function add_variables_flow_buffer!(
return nothing
end

"""
Certain allocation distribution types use absolute values in the objective function.
Since most optimization packages do not support the absolute value function directly,
New variables are introduced that act as the absolute value of an expression by
posing the appropriate constraints.
"""
function add_variables_absolute_value!(
problem::JuMP.Model,
p::Parameters,
allocation_network_id::Int32,
)::Nothing
(; graph, allocation) = p
(; main_network_connections) = allocation

node_ids = graph[].node_ids[allocation_network_id]
node_ids_user_demand = NodeID[]
node_ids_level_demand = NodeID[]
node_ids_flow_demand = 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_level_demand, node_id)
elseif has_external_demand(graph, node_id, :flow_demand)[1]
push!(node_ids_flow_demand, 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
end
end

problem[:F_abs_user_demand] =
JuMP.@variable(problem, F_abs_user_demand[node_id = node_ids_user_demand])
problem[:F_abs_level_demand] =
JuMP.@variable(problem, F_abs_level_demand[node_id = node_ids_level_demand])
problem[:F_abs_flow_demand] =
JuMP.@variable(problem, F_abs_flow_demand[node_id = node_ids_flow_demand])

return nothing
end

"""
Add the flow capacity constraints to the allocation problem.
Only finite capacities get a constraint.
Expand All @@ -468,7 +420,6 @@ function add_constraints_capacity!(
p::Parameters,
allocation_network_id::Int32,
)::Nothing
(; graph) = p
main_network_source_edges = get_main_network_connections(p, allocation_network_id)
F = problem[:F]
edge_ids_finite_capacity = Tuple{NodeID, NodeID}[]
Expand Down Expand Up @@ -692,106 +643,6 @@ function add_constraints_conservation_basin!(
return nothing
end

"""
Minimizing |expr| can be achieved by introducing a new variable expr_abs
and posing the following constraints:
expr_abs >= expr
expr_abs >= -expr
"""
function add_constraints_absolute_value!(
problem::JuMP.Model,
flow_per_node::Dict{NodeID, JuMP.VariableRef},
F_abs::JuMP.Containers.DenseAxisArray,
variable_type::String,
)::Nothing
# Example demand
d = 2.0

node_ids = only(F_abs.axes)

# 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

"""
Add constraints so that variables F_abs_user_demand act as the
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,
)::Nothing
(; graph) = p

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)
)

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

return nothing
end

"""
Add constraints so that variables F_abs_level_demand act as the
absolute value of the expression comparing flow to a basin to its demand.
"""
function add_constraints_absolute_value_level_demand!(problem::JuMP.Model)::Nothing
F_basin_in = problem[:F_basin_in]
F_abs_level_demand = problem[:F_abs_level_demand]
flow_per_node =
Dict(node_id => F_basin_in[node_id] for node_id in only(F_abs_level_demand.axes))

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

return nothing
end

"""
Add constraints so that variables F_abs_flow_demand act as the
absolute value of the expression comparing flow to a flow buffer to the flow demand.
"""
function add_constraints_absolute_value_flow_demand!(problem::JuMP.Model)::Nothing
F_flow_buffer_in = problem[:F_flow_buffer_in]
F_abs_flow_demand = problem[:F_abs_flow_demand]
flow_per_node = Dict(
node_id => F_flow_buffer_in[node_id] for node_id in only(F_abs_flow_demand.axes)
)

add_constraints_absolute_value!(
problem,
flow_per_node,
F_abs_flow_demand,
"flow_demand",
)
return nothing
end

"""
Add the fractional flow constraints to the allocation problem.
The constraint indices are allocation edges over a fractional flow node.
Expand Down Expand Up @@ -921,18 +772,13 @@ 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)
add_variables_flow_buffer!(problem, p, allocation_network_id)

# Add constraints to problem
add_constraints_conservation_basin!(problem, p, allocation_network_id)
add_constraints_conservation_flow_demand!(problem, p, allocation_network_id)
add_constraints_conservation_subnetwork!(problem, p, allocation_network_id)

add_constraints_absolute_value_user_demand!(problem, p)
add_constraints_absolute_value_flow_demand!(problem)
add_constraints_absolute_value_level_demand!(problem)

add_constraints_capacity!(problem, capacity, p, allocation_network_id)
add_constraints_source!(problem, p, allocation_network_id)
add_constraints_user_source!(problem, p, allocation_network_id)
Expand Down
Loading

0 comments on commit a47b5e4

Please sign in to comment.