Skip to content

Commit

Permalink
feat: renaming public function from "component" to "get_component"
Browse files Browse the repository at this point in the history
  • Loading branch information
sstroemer committed Oct 11, 2024
1 parent 2fabce2 commit a0e2785
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 73 deletions.
8 changes: 4 additions & 4 deletions src/IESopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function _build_model!(model::JuMP.Model; callbacks::Union{Nothing, Dict})
push!(obj.constants, term)
else
comp, proptype, prop = rsplit(term, "."; limit=3)
field = getproperty(getproperty(component(model, comp), Symbol(proptype)), Symbol(prop))
field = getproperty(getproperty(get_component(model, comp), Symbol(proptype)), Symbol(prop))
if field isa Vector
push!(obj.terms, sum(field))
else
Expand Down Expand Up @@ -674,11 +674,11 @@ function compute_IIS(model::JuMP.Model; filename::String="")
end

"""
function component(model::JuMP.Model, component_name::String)
function get_component(model::JuMP.Model, component_name::String)
Get the component `component_name` from `model`.
"""
function component(model::JuMP.Model, component_name::AbstractString)
function get_component(model::JuMP.Model, component_name::AbstractString)
if !haskey(_iesopt(model).model.components, component_name)
st = stacktrace()
trigger = length(st) > 0 ? st[1] : nothing
Expand Down Expand Up @@ -710,7 +710,7 @@ function _components_tagged(model::JuMP.Model, tags::Vector{String})
end

function extract_result(model::JuMP.Model, component_name::String, field::String; mode::String)
return _result(component(model, component_name), mode, field)[2]
return _result(get_component(model, component_name), mode, field)[2]
end

"""
Expand Down
6 changes: 3 additions & 3 deletions src/core/expression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ function _finalize(e::_Expression)
# Add all `Decision`s to the inner expression.
for (coeff, cname, field) in e.decisions
if field == "value"
var = _value(component(model, cname))
var = _value(get_component(model, cname))
elseif field == "size"
var = _size(component(model, cname))
var = _size(get_component(model, cname))
elseif field == "count"
var = _count(component(model, cname))
var = _count(get_component(model, cname))
else
@critical "Wrong Decision accessor in unnamed expression" coeff decision = cname accessor = field
end
Expand Down
4 changes: 2 additions & 2 deletions src/core/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ function _prepare!(profile::Profile)
model = profile.model

# Extract the carrier from the connected nodes.
if !isnothing(profile.node_from) && (profile.carrier != component(model, profile.node_from).carrier)
if !isnothing(profile.node_from) && (profile.carrier != get_component(model, profile.node_from).carrier)
@critical "Profile <carrier> mismatch" profile = profile.name node_from = profile.node_from
end
if !isnothing(profile.node_to) && (profile.carrier != component(model, profile.node_to).carrier)
if !isnothing(profile.node_to) && (profile.carrier != get_component(model, profile.node_to).carrier)
@critical "Profile <carrier> mismatch" profile = profile.name node_to = profile.node_to
end

Expand Down
2 changes: 1 addition & 1 deletion src/core/virtual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function Base.getproperty(cc::Virtual, field::Symbol)
# See if we may be trying to find a component that is "inside" this Virtual?
cname = "$(getfield(cc, :name)).$field"
model = getfield(cc, :model)
haskey(_iesopt(model).model.components, cname) && return component(model, cname)
haskey(_iesopt(model).model.components, cname) && return get_component(model, cname)

return getfield(cc, field)
catch e
Expand Down
27 changes: 14 additions & 13 deletions src/opt/benders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ function benders(
# Modify Decisions in sub-problem.
@info "[benders] Modifying sub model with fixed Decisions"
for comp_name in benders_data.decisions
component(benders_data.sub, comp_name).mode = :fixed
component(benders_data.sub, comp_name).cost = nothing
component(benders_data.sub, comp_name).fixed_cost = nothing
component(benders_data.sub, comp_name).fixed_value = 0.0
get_component(benders_data.sub, comp_name).mode = :fixed
get_component(benders_data.sub, comp_name).cost = nothing
get_component(benders_data.sub, comp_name).fixed_cost = nothing
get_component(benders_data.sub, comp_name).fixed_value = 0.0
end

# Build sub-problem.
Expand All @@ -168,8 +168,8 @@ function benders(
# JuMP.@variable(benders_data.main, _x >= 0)
# JuMP.@variable(benders_data.sub, _x >= 0)

# JuMP.@constraint(benders_data.main, _c, benders_data.main[:_x] >= component(benders_data.main, "invest1_1").var.value)
# JuMP.@constraint(benders_data.sub, _c, benders_data.sub[:_x] >= component(benders_data.sub, "invest1_1").var.value)
# JuMP.@constraint(benders_data.main, _c, benders_data.main[:_x] >= get_component(benders_data.main, "invest1_1").var.value)
# JuMP.@constraint(benders_data.sub, _c, benders_data.sub[:_x] >= get_component(benders_data.sub, "invest1_1").var.value)

# user_defined = Dict(
# :main => Set([:_x]), :sub => Set([:_x, :_c])
Expand Down Expand Up @@ -280,7 +280,7 @@ function _cb_benders(benders_data::BendersData, cb_data::Any)

# Get the current solution from the main-problem.
current_decisions = Dict(
comp_name => JuMP.callback_value(cb_data, component(benders_data.main, comp_name).var.value) for
comp_name => JuMP.callback_value(cb_data, get_component(benders_data.main, comp_name).var.value) for
comp_name in benders_data.decisions
)
current_user_defined_variables = Dict(
Expand All @@ -289,7 +289,7 @@ function _cb_benders(benders_data::BendersData, cb_data::Any)

# Update the sub-problem.
for (comp_name, value) in current_decisions
JuMP.fix(component(benders_data.sub, comp_name).var.value, value; force=true)
JuMP.fix(get_component(benders_data.sub, comp_name).var.value, value; force=true)
end
for (obj, value) in current_user_defined_variables
JuMP.fix.(benders_data.sub[obj], value; force=true)
Expand Down Expand Up @@ -326,7 +326,8 @@ function _cb_benders(benders_data::BendersData, cb_data::Any)
obj_sub +
sum(
extract_result(benders_data.sub, comp_name, "value"; mode="dual") *
(component(benders_data.main, comp_name).var.value - value) for (comp_name, value) in current_decisions
(get_component(benders_data.main, comp_name).var.value - value) for
(comp_name, value) in current_decisions
) +
user_sum
)
Expand Down Expand Up @@ -358,7 +359,7 @@ function _iterative_benders(benders_data::BendersData; exploration_iterations=0)
if (benders_data.iteration <= exploration_iterations) && isempty(benders_data.user_defined_variables)
# Random values in the beginning to explore.
for comp_name in benders_data.decisions
comp = component(benders_data.main, comp_name)
comp = get_component(benders_data.main, comp_name)
lb = !isnothing(comp.lb) ? comp.lb : -500
ub = !isnothing(comp.ub) ? comp.ub : 500
current_decisions[comp_name] = lb + rand() * (ub - lb)
Expand All @@ -380,7 +381,7 @@ function _iterative_benders(benders_data::BendersData; exploration_iterations=0)

# Update the sub-problem.
for (comp_name, value) in current_decisions
JuMP.fix(component(benders_data.sub, comp_name).var.value, value; force=true)
JuMP.fix(get_component(benders_data.sub, comp_name).var.value, value; force=true)
end
for (obj, value) in current_user_defined_variables
JuMP.fix.(benders_data.sub[obj], value; force=true)
Expand All @@ -397,7 +398,7 @@ function _iterative_benders(benders_data::BendersData; exploration_iterations=0)
exploration_sum = 0.0
if exploration
exploration_dict = Dict(
component(benders_data.main, comp_name).var.value => current_decisions[comp_name] for
get_component(benders_data.main, comp_name).var.value => current_decisions[comp_name] for
comp_name in benders_data.decisions
)
exploration_dict[benders_data.main[]] = 0.0
Expand Down Expand Up @@ -453,7 +454,7 @@ function _iterative_benders(benders_data::BendersData; exploration_iterations=0)
obj_sub +
sum(
extract_result(benders_data.sub, comp_name, "value"; mode="dual") *
(component(benders_data.main, comp_name).var.value - value) for
(get_component(benders_data.main, comp_name).var.value - value) for
(comp_name, value) in current_decisions
) +
user_sum
Expand Down
67 changes: 35 additions & 32 deletions src/opt/sddp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ generate!(
build_cost1=500.0,
build_cost2=5000.0,
)
JuMP.fix.(component(model, "inflow").var.aux_value, _inflow; force=true);
JuMP.fix.(component(model, "demand").var.aux_value, _demand; force=true);
JuMP.fix.(get_component(model, "inflow").var.aux_value, _inflow; force=true);
JuMP.fix.(get_component(model, "demand").var.aux_value, _demand; force=true);
true_cost = []
for t in 1:365
append!(true_cost, [_cost[t * 24 * 4] for _ in 1:(24 * 4)])
Expand All @@ -42,7 +42,7 @@ objs = _iesopt(model).model.objectives
JuMP.@objective(
model,
Min,
sum(component(model, "thermal").var.aux_value[t] * true_cost[t] for t in 1:(8760 * 4)) +
sum(get_component(model, "thermal").var.aux_value[t] * true_cost[t] for t in 1:(8760 * 4)) +
objs["build1_value"].func +
objs["build2_value"].func
);
Expand All @@ -69,19 +69,19 @@ sddp_model = SDDP.LinearPolicyGraph(; stages=365, sense=:Min, lower_bound=0.0, o

JuMP.@constraint(model, x_storage.in <= x_storage_cap.in)
JuMP.@constraint(model, x_storage.out <= x_storage_cap.in)
# JuMP.@constraint(model, component(model, "reservoir").var.state[1] <= x_storage_cap.in)
# JuMP.@constraint(model, get_component(model, "reservoir").var.state[1] <= x_storage_cap.in)

JuMP.@constraint(model, component(model, "reservoir").var.state[1] == x_storage.in)
JuMP.@constraint(model, get_component(model, "reservoir").var.state[1] == x_storage.in)
JuMP.@constraint(
model,
-JuMP.constraint_object(component(model, "reservoir").con.last_state_lb).func == x_storage.out
-JuMP.constraint_object(get_component(model, "reservoir").con.last_state_lb).func == x_storage.out
)

if t == 1
JuMP.@constraint(
model,
x_storage_cap.out ==
x_storage_cap.in + component(model, "build1").var.value + component(model, "build2").var.value
x_storage_cap.in + get_component(model, "build1").var.value + get_component(model, "build2").var.value
)
else
JuMP.@constraint(model, x_storage_cap.out == x_storage_cap.in)
Expand All @@ -93,8 +93,8 @@ sddp_model = SDDP.LinearPolicyGraph(; stages=365, sense=:Min, lower_bound=0.0, o
return
end

JuMP.fix.(component(model, "inflow").var.aux_value, _inflow[((t - 1) * 24 * 4 + 1):(t * 24 * 4)]; force=true)
JuMP.fix.(component(model, "demand").var.aux_value, _demand[((t - 1) * 24 * 4 + 1):(t * 24 * 4)]; force=true)
JuMP.fix.(get_component(model, "inflow").var.aux_value, _inflow[((t - 1) * 24 * 4 + 1):(t * 24 * 4)]; force=true)
JuMP.fix.(get_component(model, "demand").var.aux_value, _demand[((t - 1) * 24 * 4 + 1):(t * 24 * 4)]; force=true)
objs = _iesopt(model).model.objectives

if t == 1
Expand All @@ -119,8 +119,9 @@ simulations = SDDP.simulate(
custom_recorders=Dict{Symbol, Function}(
:build =>
(model::JuMP.Model) ->
JuMP.value(component(model, "build1").var.value) + JuMP.value(component(model, "build2").var.value),
:thermal => (model::JuMP.Model) -> JuMP.value(component(model, "thermal").var.aux_value[1]),
JuMP.value(get_component(model, "build1").var.value) +
JuMP.value(get_component(model, "build2").var.value),
:thermal => (model::JuMP.Model) -> JuMP.value(get_component(model, "thermal").var.aux_value[1]),
),
)

Expand Down Expand Up @@ -162,18 +163,18 @@ sddp_model = SDDP.LinearPolicyGraph(; stages=20, sense=:Min, lower_bound=0.0, op

JuMP.@constraint(model, x_storage.in <= x_storage_cap.in)
JuMP.@constraint(model, x_storage.out <= x_storage_cap.in)
# JuMP.@constraint(model, component(model, "reservoir").var.state[1] <= x_storage_cap.in)
# JuMP.@constraint(model, get_component(model, "reservoir").var.state[1] <= x_storage_cap.in)

JuMP.@constraint(model, component(model, "reservoir").var.state[1] == x_storage.in)
JuMP.@constraint(model, get_component(model, "reservoir").var.state[1] == x_storage.in)
JuMP.@constraint(
model,
-JuMP.constraint_object(component(model, "reservoir").con.last_state_lb).func == x_storage.out
-JuMP.constraint_object(get_component(model, "reservoir").con.last_state_lb).func == x_storage.out
)

JuMP.@constraint(
model,
x_storage_cap.out ==
x_storage_cap.in + component(model, "build1").var.value + component(model, "build2").var.value
x_storage_cap.in + get_component(model, "build1").var.value + get_component(model, "build2").var.value
)

_z = 0
Expand All @@ -182,7 +183,7 @@ sddp_model = SDDP.LinearPolicyGraph(; stages=20, sense=:Min, lower_bound=0.0, op
JuMP.@constraint(model, x_storage.out + _z >= 5)
end

v = JuMP.fix_value(component(model, "inflow").var.aux_value[1])
v = JuMP.fix_value(get_component(model, "inflow").var.aux_value[1])
objs = _iesopt(model).model.objectives

Ω = [1.0]
Expand All @@ -208,8 +209,9 @@ simulations = SDDP.simulate(
custom_recorders=Dict{Symbol, Function}(
:build =>
(model::JuMP.Model) ->
JuMP.value(component(model, "build1").var.value) + JuMP.value(component(model, "build2").var.value),
:thermal => (model::JuMP.Model) -> JuMP.value(component(model, "thermal").var.aux_value[1]),
JuMP.value(get_component(model, "build1").var.value) +
JuMP.value(get_component(model, "build2").var.value),
:thermal => (model::JuMP.Model) -> JuMP.value(get_component(model, "thermal").var.aux_value[1]),
),
)

Expand Down Expand Up @@ -247,18 +249,18 @@ sddp_model = SDDP.LinearPolicyGraph(; stages=20, sense=:Min, lower_bound=0.0, op

JuMP.@constraint(model, x_storage.in <= x_storage_cap.in)
JuMP.@constraint(model, x_storage.out <= x_storage_cap.in)
# JuMP.@constraint(model, component(model, "reservoir").var.state[1] <= x_storage_cap.in)
# JuMP.@constraint(model, get_component(model, "reservoir").var.state[1] <= x_storage_cap.in)

JuMP.@constraint(model, component(model, "reservoir").var.state[1] == x_storage.in)
JuMP.@constraint(model, get_component(model, "reservoir").var.state[1] == x_storage.in)
JuMP.@constraint(
model,
-JuMP.constraint_object(component(model, "reservoir").con.last_state_lb).func == x_storage.out
-JuMP.constraint_object(get_component(model, "reservoir").con.last_state_lb).func == x_storage.out
)

JuMP.@constraint(
model,
x_storage_cap.out ==
x_storage_cap.in + component(model, "build1").var.value + component(model, "build2").var.value
x_storage_cap.in + get_component(model, "build1").var.value + get_component(model, "build2").var.value
)

_z = 0
Expand All @@ -267,7 +269,7 @@ sddp_model = SDDP.LinearPolicyGraph(; stages=20, sense=:Min, lower_bound=0.0, op
JuMP.@constraint(model, x_storage.out + _z >= 5)
end

v = JuMP.fix_value(component(model, "inflow").var.aux_value[1])
v = JuMP.fix_value(get_component(model, "inflow").var.aux_value[1])
objs = _iesopt(model).model.objectives

lower = -convert(Int64, floor(t / 5.0 * 2500))
Expand All @@ -277,7 +279,7 @@ sddp_model = SDDP.LinearPolicyGraph(; stages=20, sense=:Min, lower_bound=0.0, op
P = [1.0 / length(Ω) for i in 1:length(Ω)]

SDDP.parameterize(model, Ω, P) do ω
# JuMP.fix(component(model, "inflow").var.aux_value[1], max(0., v + ω.inflow); force=true)
# JuMP.fix(get_component(model, "inflow").var.aux_value[1], max(0., v + ω.inflow); force=true)
# SDDP.@stageobjective(model, objs["thermal"].func + _z * 1e4)
SDDP.@stageobjective(
model,
Expand All @@ -300,8 +302,9 @@ simulations = SDDP.simulate(
custom_recorders=Dict{Symbol, Function}(
:build =>
(model::JuMP.Model) ->
JuMP.value(component(model, "build1").var.value) + JuMP.value(component(model, "build2").var.value),
:thermal => (model::JuMP.Model) -> JuMP.value(component(model, "thermal").var.aux_value[1]),
JuMP.value(get_component(model, "build1").var.value) +
JuMP.value(get_component(model, "build2").var.value),
:thermal => (model::JuMP.Model) -> JuMP.value(get_component(model, "thermal").var.aux_value[1]),
),
)

Expand Down Expand Up @@ -333,10 +336,10 @@ sddp_model = SDDP.LinearPolicyGraph(; stages=52, sense=:Min, lower_bound=0.0, op
parametric=true,
)

JuMP.@constraint(model, component(model, "reservoir").var.state[1] == x_storage.in)
JuMP.@constraint(model, get_component(model, "reservoir").var.state[1] == x_storage.in)
JuMP.@constraint(
model,
-JuMP.constraint_object(component(model, "reservoir").con.last_state_lb).func == x_storage.out
-JuMP.constraint_object(get_component(model, "reservoir").con.last_state_lb).func == x_storage.out
)

_z = 0
Expand All @@ -345,7 +348,7 @@ sddp_model = SDDP.LinearPolicyGraph(; stages=52, sense=:Min, lower_bound=0.0, op
JuMP.@constraint(model, x_storage.out + _z >= 300)
end

v = JuMP.fix_value(component(model, "inflow").var.aux_value[1])
v = JuMP.fix_value(get_component(model, "inflow").var.aux_value[1])
objs = _iesopt(model).model.objectives

Ω = [
Expand All @@ -358,7 +361,7 @@ sddp_model = SDDP.LinearPolicyGraph(; stages=52, sense=:Min, lower_bound=0.0, op
P /= sum(P)

SDDP.parameterize(model, Ω, P) do ω
JuMP.fix(component(model, "inflow").var.aux_value[1], max(0.0, v + ω.inflow); force=true)
JuMP.fix(get_component(model, "inflow").var.aux_value[1], max(0.0, v + ω.inflow); force=true)
SDDP.@stageobjective(model, ω.fuel_multiplier * objs["thermal"].func + _z * 1e4)
return
end
Expand Down Expand Up @@ -406,8 +409,8 @@ simulations = SDDP.simulate(
50,
[:x_storage];
custom_recorders=Dict{Symbol, Function}(
:thermal => (model::JuMP.Model) -> JuMP.value(component(model, "thermal").var.aux_value[1]),
:spillage => (model::JuMP.Model) -> JuMP.value(component(model, "spill").var.aux_value[1]),
:thermal => (model::JuMP.Model) -> JuMP.value(get_component(model, "thermal").var.aux_value[1]),
:spillage => (model::JuMP.Model) -> JuMP.value(get_component(model, "spill").var.aux_value[1]),
),
)

Expand Down
Loading

0 comments on commit a0e2785

Please sign in to comment.