Skip to content

Commit

Permalink
make model build work
Browse files Browse the repository at this point in the history
  • Loading branch information
jd-lara committed Sep 16, 2024
1 parent f93528c commit 4e455f4
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 37 deletions.
11 changes: 11 additions & 0 deletions src/PowerSystemsInvestments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const PNM = PowerNetworkMatrices

### Exports ###
export InvestmentModel
export InvestmentModelTemplate
export TransportModel

### Capital Model
export DiscountedCashFlow
export AggregateOperatingCost
export RepresentativePeriods

export SingleRegionBalanceModel

## Variables ##
export BuildCapacity
Expand Down Expand Up @@ -131,6 +140,8 @@ include("base/multi_optimization_container.jl")
include("investment_model/investment_model_store.jl")
include("investment_model/investment_model.jl")

include("model_build/SingleInstanceSolve.jl")

include("utils/printing.jl")
include("utils/jump_utils.jl")
include("technology_models/technologies/common/add_variable.jl")
Expand Down
19 changes: 8 additions & 11 deletions src/base/investment_model_template.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ abstract type AbstractInvestmentModelTemplate end
mutable struct InvestmentModelTemplate <: AbstractInvestmentModelTemplate
capital_model::CapitalCostModel
operation_model::OperationCostModel
transport_model::TransportModel{<:AbstractTransportModel}
feasibility_model::FeasibilityModel
transport_model::TransportModel{<:AbstractTransportAggregation}
technology_models::Dict # Type to be refined later

function InvestmentModelTemplate(
capital_model::CapitalCostModel,
operation_model::OperationCostModel,
transport_model::TransportModel{T}
) where {T <: AbstractTransportModel}
new(
capital_model,
operation_model,
transport_model,
Dict()
)
feasibility_model::FeasibilityModel,
transport_model::TransportModel{T},
) where {T <: AbstractTransportAggregation}
new(capital_model, operation_model, feasibility_model, transport_model, Dict())
end
end

Expand All @@ -28,7 +25,7 @@ function Base.isempty(template::InvestmentModelTemplate)
end
end

InvestmentModelTemplate(::Type{T}) where {T <: AbstractTransportModel} =
InvestmentModelTemplate(::Type{T}) where {T <: AbstractTransportAggregation} =
InvestmentModelTemplate(TransportModel(T))
InvestmentModelTemplate() = InvestmentModelTemplate(SingleRegionPowerModel)

Expand All @@ -42,7 +39,7 @@ Sets the network model in a template.
"""
function set_transport_model!(
template::InvestmentModelTemplate,
model::TransportModel{<:AbstractTransportModel},
model::TransportModel{<:AbstractTransportAggregation},
)
template.transport_model = model
return
Expand Down
2 changes: 1 addition & 1 deletion src/base/multi_optimization_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ end

function init_optimization_container!(
container::MultiOptimizationContainer,
transport_model::TransportModel{<:AbstractTransportModel},
transport_model::TransportModel{<:AbstractTransportAggregation},
portfolio::PSIP.Portfolio,
)
PSY.set_units_base_system!(portfolio, "NATURAL_UNITS")
Expand Down
1 change: 0 additions & 1 deletion src/base/settings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ function Settings(
calculate_conflict,
portfolio_to_file,
deserialize_initial_conditions,
export_pwl_vars,
store_variable_names,
check_numerical_bounds,
ext,
Expand Down
4 changes: 2 additions & 2 deletions src/base/single_optimization_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,10 @@ function add_to_objective_investment_expression!(
return
end

function build_impl!(
function build_model!(
container::SingleOptimizationContainer,
template::InvestmentModelTemplate,
sys::PSIP.Portfolio,
port::PSIP.Portfolio,
)
transmission = get_network_formulation(template)
transmission_model = get_network_model(template)
Expand Down
1 change: 1 addition & 0 deletions src/base/solution_algorithms.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
abstract type SolutionAlgorithm end

# Implementation of the build and solve algorithm is done in the respective folders.
struct SingleInstanceSolve <: SolutionAlgorithm end
16 changes: 9 additions & 7 deletions src/base/transport_model.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
abstract type AbstractTransportModel end
abstract type AbstractTransportAggregation end

struct SingleRegionBalanceModel <: AbstractTransportModel end
struct MultiRegionBalanceModel <: AbstractTransportModel end
struct NodalBalanceModel <: AbstractTransportModel end
struct SingleRegionBalanceModel <: AbstractTransportAggregation end
struct MultiRegionBalanceModel <: AbstractTransportAggregation end
struct NodalBalanceModel <: AbstractTransportAggregation end

mutable struct TransportModel{T <: AbstractTransportModel}
mutable struct TransportModel{T <: AbstractTransportAggregation}
use_slacks::Bool
function TransportModel(::Type{T}; use_slacks=false) where {T <: AbstractTransportModel}
_check_pm_formulation(T)
function TransportModel(
::Type{T};
use_slacks=false,
) where {T <: AbstractTransportAggregation}
new{T}(use_slacks)
end
end
Expand Down
75 changes: 60 additions & 15 deletions src/investment_model/investment_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,27 @@ end

function InvestmentModel(
template::AbstractInvestmentModelTemplate,
::Type{SingleInstanceSolve},
M::Type{SingleInstanceSolve},
portfolio::PSIP.Portfolio,
settings::Settings,
jump_model::Union{Nothing, JuMP.Model}=nothing;
name=nothing,
)
if name === nothing
name = nameof(M)
elseif name isa String
name = Symbol(name)
end
internal = ISOPT.ModelInternal(
SingleOptimizationContainer(portfolio, settings, jump_model, PSY.Deterministic),
)
internal = ISOPT.ModelInternal(SingleOptimizationContainer(settings, jump_model))

model = InvestmentModel(
name,
model = InvestmentModel{M}(
:CEM,
template,
portfolio,
internal,
InvestmentsModelStore(),
InvestmentModelStore(),
Dict{String, Any}(),
)
#PSI.validate_time_series!(model)
return model
end

function InvestmentModel(
template::AbstractInvestmentModelTemplate,
alg::Type{SingleInstanceSolve},
portfolio::PSIP.Portfolio,
jump_model::Union{Nothing, JuMP.Model}=nothing;
name=nothing,
Expand Down Expand Up @@ -69,5 +61,58 @@ function InvestmentModel(
check_numerical_bounds=check_numerical_bounds,
store_variable_names=store_variable_names,
)
return InvestmentModel(template, sys, settings, jump_model; name=name)
return InvestmentModel(template, alg, portfolio, settings, jump_model; name=name)
end

function build_impl!(::InvestmentModel{T}) where {T}
error("Build not implemented for $T")
return
end

"""
Build the Invesment Model.
# Arguments
- `model::InvestmentModel{<:SolutionAlgorithm}`: InvestmentModel object
- `output_dir::String`: Output directory for results
- `console_level = Logging.Error`:
- `file_level = Logging.Info`:
- `disable_timer_outputs = false` : Enable/Disable timing outputs
"""
function build!(
model::InvestmentModel{<:SolutionAlgorithm};
output_dir::String,
console_level=Logging.Error,
file_level=Logging.Info,
disable_timer_outputs=false,
)
mkpath(output_dir)
set_output_dir!(model, output_dir)
set_console_level!(model, console_level)
set_file_level!(model, file_level)
TimerOutputs.reset_timer!(BUILD_PROBLEMS_TIMER)
disable_timer_outputs && TimerOutputs.disable_timer!(BUILD_PROBLEMS_TIMER)
file_mode = "w"

logger = IS.configure_logging(get_internal(model), PROBLEM_LOG_FILENAME, file_mode)
try
Logging.with_logger(logger) do
try
TimerOutputs.@timeit BUILD_PROBLEMS_TIMER "Problem $(get_name(model))" begin
_build!(model)
end
set_status!(model, ModelBuildStatus.BUILT)
@info "\n$(BUILD_PROBLEMS_TIMER)\n"
catch e
set_status!(model, ModelBuildStatus.FAILED)
bt = catch_backtrace()
@error "InvestmentModel Build Failed" exception = e, bt
end
end
finally
unregister_recorders!(model)
close(logger)
end
return get_status(model)
end
8 changes: 8 additions & 0 deletions src/model_build/SingleInstanceSolve.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function build_impl!(model::InvestmentModel{SingleInstanceSolve})
build_model!(
get_optimization_container(model),
get_template(model),
get_porfolio_container(model),
)
return
end
6 changes: 6 additions & 0 deletions src/operation/operation_model.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
abstract type OperationCostModel end

struct AggregateOperatingCost <: OperationCostModel end

struct ClusteredRepresentativeDays <: OperationCostModel
min_consequetive_days::Int
clutering_parameter::Int
storage_time_aggregation::Int
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ function construct_technologies!(
container::SingleOptimizationContainer,
p::PSIP.Portfolio,
::ArgumentConstructStage,
model::CapitalCostModel,
technology_model::TechnologyModel{T, B, C},
# network_model::NetworkModel{<:PM.AbstractActivePowerModel},
) where {T <: PSIP.DemandRequirement, B <: StaticLoadInvestment, C <: BasicDispatch}
Expand Down

0 comments on commit 4e455f4

Please sign in to comment.