From c3c27c2393d196f9afee65d0a02582c6cbf21af4 Mon Sep 17 00:00:00 2001 From: rodrigomha Date: Thu, 12 Dec 2024 14:06:09 -0800 Subject: [PATCH] formatter vom --- .../common/objective_function/market_bid.jl | 203 +++++++++++------- 1 file changed, 123 insertions(+), 80 deletions(-) diff --git a/src/devices_models/devices/common/objective_function/market_bid.jl b/src/devices_models/devices/common/objective_function/market_bid.jl index d20d16528..04ad299ce 100644 --- a/src/devices_models/devices/common/objective_function/market_bid.jl +++ b/src/devices_models/devices/common/objective_function/market_bid.jl @@ -3,21 +3,24 @@ ################################################## # For Market Bid -function _add_pwl_variables!(container::OptimizationContainer, +function _add_pwl_variables!( + container::OptimizationContainer, ::Type{T}, component_name::String, time_period::Int, - cost_data::PSY.PiecewiseStepData) where {T <: PSY.Component} + cost_data::PSY.PiecewiseStepData, +) where {T <: PSY.Component} var_container = lazy_container_addition!(container, PieceWiseLinearBlockOffer(), T) # length(PiecewiseStepData) gets number of segments, here we want number of points break_points = PSY.get_x_coords(cost_data) pwlvars = Array{JuMP.VariableRef}(undef, length(break_points)) for i in 1:(length(break_points) - 1) pwlvars[i] = - var_container[(component_name, i, time_period)] = - JuMP.@variable(get_jump_model(container), - base_name = "PieceWiseLinearBlockOffer_$(component_name)_{pwl_$(i), $time_period}", - lower_bound = 0.0,) + var_container[(component_name, i, time_period)] = JuMP.@variable( + get_jump_model(container), + base_name = "PieceWiseLinearBlockOffer_$(component_name)_{pwl_$(i), $time_period}", + lower_bound = 0.0, + ) end return pwlvars end @@ -34,24 +37,29 @@ Implement the constraints for PWL Block Offer variables. That is: \\sum_{k\\in\\mathcal{K}} \\delta_{k,t} <= P_{k+1,t}^{max} - P_{k,t}^{max} ``` """ -function _add_pwl_constraint!(container::OptimizationContainer, +function _add_pwl_constraint!( + container::OptimizationContainer, component::T, ::U, break_points::Vector{Float64}, - period::Int) where {T <: PSY.Component, U <: VariableType} + period::Int, +) where {T <: PSY.Component, U <: VariableType} variables = get_variable(container, U(), T) - const_container = lazy_container_addition!(container, + const_container = lazy_container_addition!( + container, PieceWiseLinearBlockOfferConstraint(), T, - axes(variables)...) + axes(variables)..., + ) len_cost_data = length(break_points) - 1 jump_model = get_jump_model(container) pwl_vars = get_variable(container, PieceWiseLinearBlockOffer(), T) name = PSY.get_name(component) - const_container[name, period] = JuMP.@constraint(jump_model, + const_container[name, period] = JuMP.@constraint( + jump_model, variables[name, period] == - sum(pwl_vars[name, ix, period] - for ix in 1:len_cost_data)) + sum(pwl_vars[name, ix, period] for ix in 1:len_cost_data) + ) #= const_upperbound_container = lazy_container_addition!( @@ -64,9 +72,10 @@ function _add_pwl_constraint!(container::OptimizationContainer, # TODO: Parameter for this for ix in 1:len_cost_data - JuMP.@constraint(jump_model, - pwl_vars[name, ix, period] <= - break_points[ix + 1] - break_points[ix]) + JuMP.@constraint( + jump_model, + pwl_vars[name, ix, period] <= break_points[ix + 1] - break_points[ix] + ) end return end @@ -79,32 +88,37 @@ Implement the constraints for PWL Block Offer variables for ORDC. That is: \\sum_{k\\in\\mathcal{K}} \\delta_{k,t} <= P_{k+1,t}^{max} - P_{k,t}^{max} ``` """ -function _add_pwl_constraint!(container::OptimizationContainer, +function _add_pwl_constraint!( + container::OptimizationContainer, component::T, ::U, break_points::Vector{Float64}, sos_status::SOSStatusVariable, - period::Int) where {T <: PSY.ReserveDemandCurve, - U <: ServiceRequirementVariable} + period::Int, +) where {T <: PSY.ReserveDemandCurve, U <: ServiceRequirementVariable} name = PSY.get_name(component) variables = get_variable(container, U(), T, name) - const_container = lazy_container_addition!(container, + const_container = lazy_container_addition!( + container, PieceWiseLinearBlockOfferConstraint(), T, axes(variables)...; - meta = name) + meta = name, + ) len_cost_data = length(break_points) - 1 jump_model = get_jump_model(container) pwl_vars = get_variable(container, PieceWiseLinearBlockOffer(), T) - const_container[name, period] = JuMP.@constraint(jump_model, + const_container[name, period] = JuMP.@constraint( + jump_model, variables[name, period] == - sum(pwl_vars[name, ix, period] - for ix in 1:len_cost_data)) + sum(pwl_vars[name, ix, period] for ix in 1:len_cost_data) + ) for ix in 1:len_cost_data - JuMP.@constraint(jump_model, - pwl_vars[name, ix, period] <= - break_points[ix + 1] - break_points[ix]) + JuMP.@constraint( + jump_model, + pwl_vars[name, ix, period] <= break_points[ix + 1] - break_points[ix] + ) end return end @@ -113,66 +127,77 @@ end ################ PWL Expressions ################# ################################################## -function _get_pwl_cost_expression(container::OptimizationContainer, +function _get_pwl_cost_expression( + container::OptimizationContainer, component::T, time_period::Int, cost_data::PSY.PiecewiseStepData, - multiplier::Float64) where {T <: PSY.Component} + multiplier::Float64, +) where {T <: PSY.Component} name = PSY.get_name(component) pwl_var_container = get_variable(container, PieceWiseLinearBlockOffer(), T) gen_cost = JuMP.AffExpr(0.0) cost_data = PSY.get_y_coords(cost_data) for (i, cost) in enumerate(cost_data) - JuMP.add_to_expression!(gen_cost, - cost * multiplier * - pwl_var_container[(name, i, time_period)]) + JuMP.add_to_expression!( + gen_cost, + cost * multiplier * pwl_var_container[(name, i, time_period)], + ) end return gen_cost end -function _get_pwl_cost_expression(container::OptimizationContainer, +function _get_pwl_cost_expression( + container::OptimizationContainer, component::T, time_period::Int, cost_function::PSY.MarketBidCost, ::PSY.PiecewiseStepData, ::U, - ::V) where {T <: PSY.Component, U <: VariableType, - V <: AbstractDeviceFormulation} + ::V, +) where {T <: PSY.Component, U <: VariableType, V <: AbstractDeviceFormulation} incremental_curve = PSY.get_incremental_offer_curves(cost_function) value_curve = PSY.get_value_curve(incremental_curve) power_units = PSY.get_power_units(incremental_curve) cost_component = PSY.get_function_data(value_curve) base_power = get_base_power(container) device_base_power = PSY.get_base_power(component) - cost_data_normalized = get_piecewise_incrementalcurve_per_system_unit(cost_component, + cost_data_normalized = get_piecewise_incrementalcurve_per_system_unit( + cost_component, power_units, base_power, - device_base_power) + device_base_power, + ) resolution = get_resolution(container) dt = Dates.value(resolution) / MILLISECONDS_IN_HOUR - return _get_pwl_cost_expression(container, + return _get_pwl_cost_expression( + container, component, time_period, cost_data_normalized, - dt) + dt, + ) end """ Get cost expression for StepwiseCostReserve """ -function _get_pwl_cost_expression(container::OptimizationContainer, +function _get_pwl_cost_expression( + container::OptimizationContainer, component::T, time_period::Int, cost_data::PSY.PiecewiseStepData, - multiplier::Float64) where {T <: PSY.ReserveDemandCurve} + multiplier::Float64, +) where {T <: PSY.ReserveDemandCurve} name = PSY.get_name(component) pwl_var_container = get_variable(container, PieceWiseLinearBlockOffer(), T) slopes = PSY.get_y_coords(cost_data) ordc_cost = JuMP.AffExpr(0.0) for i in 1:length(slopes) - JuMP.add_to_expression!(ordc_cost, - slopes[i] * multiplier * - pwl_var_container[(name, i, time_period)]) + JuMP.add_to_expression!( + ordc_cost, + slopes[i] * multiplier * pwl_var_container[(name, i, time_period)], + ) end return ordc_cost end @@ -254,13 +279,14 @@ end Add PWL cost terms for data coming from the MarketBidCost with a fixed incremental offer curve """ -function _add_pwl_term!(container::OptimizationContainer, +function _add_pwl_term!( + container::OptimizationContainer, component::T, cost_function::PSY.MarketBidCost, ::PSY.CostCurve{PSY.PiecewiseIncrementalCurve}, ::U, - ::V) where {T <: PSY.Component, U <: VariableType, - V <: AbstractDeviceFormulation} + ::V, +) where {T <: PSY.Component, U <: VariableType, V <: AbstractDeviceFormulation} name = PSY.get_name(component) incremental_offer_curve = PSY.get_incremental_offer_curves(cost_function) value_curve = PSY.get_value_curve(incremental_offer_curve) @@ -269,10 +295,12 @@ function _add_pwl_term!(container::OptimizationContainer, device_base_power = PSY.get_base_power(component) power_units = PSY.get_power_units(incremental_offer_curve) - data = get_piecewise_incrementalcurve_per_system_unit(cost_component, + data = get_piecewise_incrementalcurve_per_system_unit( + cost_component, power_units, base_power, - device_base_power) + device_base_power, + ) cost_is_convex = PSY.is_convex(data) if !cost_is_convex @@ -285,8 +313,8 @@ function _add_pwl_term!(container::OptimizationContainer, for t in time_steps _add_pwl_variables!(container, T, name, t, data) _add_pwl_constraint!(container, component, U(), break_points, t) - pwl_cost = _get_pwl_cost_expression(container, component, t, cost_function, data, - U(), V()) + pwl_cost = + _get_pwl_cost_expression(container, component, t, cost_function, data, U(), V()) pwl_cost_expressions[t] = pwl_cost end return pwl_cost_expressions @@ -296,12 +324,13 @@ end ########## PWL for StepwiseCostReserve ########## ################################################## -function _add_pwl_term!(container::OptimizationContainer, +function _add_pwl_term!( + container::OptimizationContainer, component::T, cost_data::PSY.CostCurve{PSY.PiecewiseIncrementalCurve}, ::U, - ::V) where {T <: PSY.Component, U <: VariableType, - V <: AbstractServiceFormulation} + ::V, +) where {T <: PSY.Component, U <: VariableType, V <: AbstractServiceFormulation} multiplier = objective_function_multiplier(U(), V()) resolution = get_resolution(container) dt = Dates.value(Dates.Second(resolution)) / SECONDS_IN_HOUR @@ -310,10 +339,12 @@ function _add_pwl_term!(container::OptimizationContainer, power_units = PSY.get_power_units(cost_data) cost_component = PSY.get_function_data(value_curve) device_base_power = PSY.get_base_power(component) - data = get_piecewise_incrementalcurve_per_system_unit(cost_component, + data = get_piecewise_incrementalcurve_per_system_unit( + cost_component, power_units, base_power, - device_base_power) + device_base_power, + ) name = PSY.get_name(component) time_steps = get_time_steps(container) pwl_cost_expressions = Vector{JuMP.AffExpr}(undef, time_steps[end]) @@ -432,19 +463,20 @@ Decremental offers are not accepted for most components, except Storage systems - component_name::String: The component_name of the variable container - cost_function::MarketBidCost : container for market bid cost """ -function _add_variable_cost_to_objective!(container::OptimizationContainer, +function _add_variable_cost_to_objective!( + container::OptimizationContainer, ::T, component::PSY.Component, cost_function::PSY.MarketBidCost, - ::U) where {T <: VariableType, - U <: AbstractDeviceFormulation} + ::U, +) where {T <: VariableType, U <: AbstractDeviceFormulation} component_name = PSY.get_name(component) @debug "Market Bid" _group = LOG_GROUP_COST_FUNCTIONS component_name time_steps = get_time_steps(container) initial_time = get_initial_time(container) incremental_cost_curves = PSY.get_incremental_offer_curves(cost_function) decremental_cost_curves = PSY.get_decremental_offer_curves(cost_function) - if !(isnothing(decremental_cost_curves)) + if isnothing(decremental_cost_curves) error("Component $(component_name) is not allowed to participate as a demand.") end #= @@ -464,12 +496,15 @@ function _add_variable_cost_to_objective!(container::OptimizationContainer, eltype(variable_cost_forecast_values), ) =# - pwl_cost_expressions = _add_pwl_term!(container, - component, - cost_function, - incremental_cost_curves, - T(), - U()) + pwl_cost_expressions = + _add_pwl_term!( + container, + component, + cost_function, + incremental_cost_curves, + T(), + U(), + ) jump_model = get_jump_model(container) for t in time_steps #= @@ -489,11 +524,13 @@ function _add_variable_cost_to_objective!(container::OptimizationContainer, t, ) =# - add_to_expression!(container, + add_to_expression!( + container, ProductionCostExpression, pwl_cost_expressions[t], component, - t) + t, + ) add_to_objective_variant_expression!(container, pwl_cost_expressions[t]) end @@ -507,17 +544,21 @@ function _add_variable_cost_to_objective!(container::OptimizationContainer, return end -function _add_service_bid_cost!(container::OptimizationContainer, +function _add_service_bid_cost!( + container::OptimizationContainer, component::PSY.Component, - service::T) where {T <: PSY.Reserve{<:PSY.ReserveDirection}} + service::T, +) where {T <: PSY.Reserve{<:PSY.ReserveDirection}} time_steps = get_time_steps(container) initial_time = get_initial_time(container) base_power = get_base_power(container) - forecast_data = PSY.get_services_bid(component, + forecast_data = PSY.get_services_bid( + component, PSY.get_operation_cost(component), service; start_time = initial_time, - len = length(time_steps)) + len = length(time_steps), + ) forecast_data_values = PSY.get_cost.(TimeSeries.values(forecast_data)) # Single Price Bid if eltype(forecast_data_values) == Float64 @@ -529,25 +570,27 @@ function _add_service_bid_cost!(container::OptimizationContainer, error("$(eltype(forecast_data_values)) not supported for MarketBidCost") end - reserve_variable = get_variable(container, ActivePowerReserveVariable(), T, - PSY.get_name(service)) + reserve_variable = + get_variable(container, ActivePowerReserveVariable(), T, PSY.get_name(service)) component_name = PSY.get_name(component) for t in time_steps - add_to_objective_invariant_expression!(container, - data_values[t] * base_power * - reserve_variable[component_name, t]) + add_to_objective_invariant_expression!( + container, + data_values[t] * base_power * reserve_variable[component_name, t], + ) end return end function _add_service_bid_cost!(::OptimizationContainer, ::PSY.Component, ::PSY.Service) end -function _add_vom_cost_to_objective!(container::OptimizationContainer, +function _add_vom_cost_to_objective!( + container::OptimizationContainer, ::T, component::PSY.Component, op_cost::PSY.MarketBidCost, - ::U) where {T <: VariableType, - U <: AbstractDeviceFormulation} + ::U, +) where {T <: VariableType, U <: AbstractDeviceFormulation} incremental_cost_curves = PSY.get_incremental_offer_curves(op_cost) decremental_cost_curves = PSY.get_decremental_offer_curves(op_cost) power_units = PSY.get_power_units(incremental_cost_curves)