Skip to content

Commit

Permalink
Move jump objects from Components to Model
Browse files Browse the repository at this point in the history
Slightly reverses conclusions of #2.
  • Loading branch information
coroa committed Jun 19, 2019
1 parent 888000f commit 47c4720
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
11 changes: 5 additions & 6 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ struct SubNetwork{T <: AbstractEnergyModel} <: Component
model::T
class::Symbol
buses::Axis
objects::Dict{Symbol,Any}
end

"Connection points at which energy balance is upheld, has `axis` and `addto!`"
struct Bus{T <: AbstractEnergyModel} <: Component
model::T
class::Symbol
objects::Dict{Symbol,Any}
end

mutable struct EnergyModel{MT <: ModelType, TF <: PM.AbstractPowerFormulation} <: AbstractEnergyModel
Expand All @@ -19,19 +17,20 @@ mutable struct EnergyModel{MT <: ModelType, TF <: PM.AbstractPowerFormulation} <
buses::Dict{Symbol,Bus{EnergyModel{MT,TF}}}
data::Data
jumpmodel::Union{JuMP.AbstractModel,Nothing}
jumpobjects::Dict{Symbol,Dict{Symbol}{Any}}
end


EnergyModel(filename::String; kwargs...) = EnergyModel(load(filename); kwargs...)
EnergyModel(data::Data) = load(EnergyModel{ExpansionModel,PM.DCPlosslessForm}(Dict{Symbol,Device}(), Dict{Symbol,SubNetwork}(), Dict{Symbol,Bus}(), data, nothing))
EnergyModel(data::Data) = load(EnergyModel{ExpansionModel,PM.DCPlosslessForm}(Dict{Symbol,Device}(), Dict{Symbol,SubNetwork}(), Dict{Symbol,Bus}(), data, nothing, Dict{Symbol, Dict{Symbol}{Any}}()))

# TODO which forms are picked should be determined by Data or the Components
function load(m::EnergyModel)
for T in modelcomponents(m.data), class in classes(m.data, T)
if (T <: SubNetwork) || (T <: Bus)
push!(m, T(m, class, Dict{Symbol}{Any}()))
push!(m, T(m, class))
else
push!(m, T{LinearExpansionForm{LinearDispatchForm}}(m, class, Dict{Symbol}{Any}()))
push!(m, T{LinearExpansionForm{LinearDispatchForm}}(m, class))
end

end
Expand Down Expand Up @@ -120,7 +119,7 @@ Base.get(c::Component, attr::Symbol, axes...) = WrappedArray(get(c, attr), axes.

Base.getindex(c::Component, attr::Symbol) = get(c, attr)

getjump(c::Component, attr::Symbol) = get(c.objects, attr, nothing)
getjump(c::Component, attr::Symbol) = get(get!(model(c).jumpobjects, c.class, Dict{Symbol}{Any}()), attr, nothing)
JuMP.getvalue(c::Component, attr::Symbol) = getvalue.(getjump(c, attr))
JuMP.getdual(c::Component, attr::Symbol) = getdual.(getjump(c, attr))
getparam(c::Component, attr::Symbol) = get(model(c).data, c, attr)
Expand Down
1 change: 0 additions & 1 deletion src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ macro adddevice(type, abstype, name, axes, file)
struct $type{DF<:DeviceFormulation} <: $abstype{DF}
model::EnergyModel
class::Symbol
objects::Dict{Symbol,Any}
end
$type{DF}(d::$type) where DF = $type{DF}(d.model, d.class, d.objects)
$type{DF}(d::$type, ::Type{NewDF}) where {DF, NewDF <: DeviceFormulation} = $type{NewDF}(d)
Expand Down
53 changes: 27 additions & 26 deletions src/modelview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,43 @@ A wrapper around any JuMP `model` to prefix variables and constraints with
Every other operation is just handed through to the inner `model`.
"""
struct ModelView{T,S} <: JuMP.AbstractModel
model::T
component::S
jumpmodel::T
model::S
class::Symbol
end

prefix(view::ModelView, name::String) = string(view.component.class, "::", name)
prefix(view::ModelView, name::String) = string(view.class, "::", name)

Base.broadcastable(view::ModelView) = Ref(view)
JuMP.object_dictionary(view::ModelView) = view.component.objects
JuMP.object_dictionary(view::ModelView) = get!(view.model.jumpobjects, view.class, Dict{Symbol}{Any}())

JuMP.variable_type(view::ModelView) = variable_type(view.model)
JuMP.add_variable(view::ModelView, v::JuMP.AbstractVariable, name::String="") = add_variable(view.model, v, prefix(view, name))
JuMP.delete(view::ModelView, variable_ref::VariableRef) = delete(view.model, variable_ref)
JuMP.is_valid(view::ModelView, variable_ref::VariableRef) = is_valid(view.model, variable_ref)
JuMP.num_variables(view::ModelView) = num_variables(view.model)
JuMP.variable_type(view::ModelView) = variable_type(view.jumpmodel)
JuMP.add_variable(view::ModelView, v::JuMP.AbstractVariable, name::String="") = add_variable(view.jumpmodel, v, prefix(view, name))
JuMP.delete(view::ModelView, variable_ref::VariableRef) = delete(view.jumpmodel, variable_ref)
JuMP.is_valid(view::ModelView, variable_ref::VariableRef) = is_valid(view.jumpmodel, variable_ref)
JuMP.num_variables(view::ModelView) = num_variables(view.jumpmodel)

JuMP.constraint_type(view::ModelView) = constraint_type(view.model)
JuMP.add_constraint(view::ModelView, c::JuMP.AbstractConstraint, name::String="") = add_constraint(view.model, c, prefix(view, name))
JuMP.delete(view::ModelView, constraint_ref::ConstraintRef) = delete(view.model, constraint_ref)
JuMP.is_valid(view::ModelView, constraint_ref::ConstraintRef) = is_valid(view.model, constraint_ref)
JuMP.num_constraints(view::ModelView) = num_constraints(view.model)
JuMP.constraint_type(view::ModelView) = constraint_type(view.jumpmodel)
JuMP.add_constraint(view::ModelView, c::JuMP.AbstractConstraint, name::String="") = add_constraint(view.jumpmodel, c, prefix(view, name))
JuMP.delete(view::ModelView, constraint_ref::ConstraintRef) = delete(view.jumpmodel, constraint_ref)
JuMP.is_valid(view::ModelView, constraint_ref::ConstraintRef) = is_valid(view.jumpmodel, constraint_ref)
JuMP.num_constraints(view::ModelView) = num_constraints(view.jumpmodel)

# Objective
JuMP.set_objective(view::ModelView, sense::MOI.OptimizationSense, f::JuMP.AbstractJuMPScalar) = set_objective(view.model, sense, f)
JuMP.set_objective_sense(view::ModelView, sense) = set_objective_sense(view.model, sense)
JuMP.objective_sense(view::ModelView) = objective_sense(view.model)
JuMP.set_objective(view::ModelView, sense::MOI.OptimizationSense, f::JuMP.AbstractJuMPScalar) = set_objective(view.jumpmodel, sense, f)
JuMP.set_objective_sense(view::ModelView, sense) = set_objective_sense(view.jumpmodel, sense)
JuMP.objective_sense(view::ModelView) = objective_sense(view.jumpmodel)

JuMP.objective_function_type(view::ModelView) = objective_function_type(view.model)
JuMP.objective_function(view::ModelView) = objective_function(view.model)
JuMP.objective_function_type(view::ModelView) = objective_function_type(view.jumpmodel)
JuMP.objective_function(view::ModelView) = objective_function(view.jumpmodel)

# Names
JuMP.variable_by_name(view::ModelView, name::String) = variable_by_name(view.model, prefix(view, name))
JuMP.constraint_by_name(view::ModelView, name::String) = constraint_by_name(view.model, prefix(view, name))
JuMP.variable_by_name(view::ModelView, name::String) = variable_by_name(view.jumpmodel, prefix(view, name))
JuMP.constraint_by_name(view::ModelView, name::String) = constraint_by_name(view.jumpmodel, prefix(view, name))

# Show
JuMP.show_backend_summary(io::IO, view::ModelView) = show_backend_summary(io, view.model)
JuMP.show_objective_function_summary(io::IO, view::ModelView) = show_objective_function_sumary(io, view.model)
JuMP.objective_function_string(print_mode, view::ModelView) = objective_function_string(print_mode, view.model)
JuMP.show_constraints_summary(io::IO, view::ModelView) = show_constraints_summary(io, view.model)
JuMP.constraints_string(print_mode, view::ModelView) = constraints_string(print_mode, view.model)
JuMP.show_backend_summary(io::IO, view::ModelView) = show_backend_summary(io, view.jumpmodel)
JuMP.show_objective_function_summary(io::IO, view::ModelView) = show_objective_function_sumary(io, view.jumpmodel)
JuMP.objective_function_string(print_mode, view::ModelView) = objective_function_string(print_mode, view.jumpmodel)
JuMP.show_constraints_summary(io::IO, view::ModelView) = show_constraints_summary(io, view.jumpmodel)
JuMP.constraints_string(print_mode, view::ModelView) = constraints_string(print_mode, view.jumpmodel)

0 comments on commit 47c4720

Please sign in to comment.