Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial refactor of cost functions #1071

Merged
merged 50 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
5818ed1
delete old cost data
jd-lara Mar 7, 2024
23c6e0d
add ProductionVariableCosts
jd-lara Mar 8, 2024
f7c1aab
add ThermalGenerationCost
jd-lara Mar 8, 2024
4f1a34a
delete old exports
jd-lara Mar 8, 2024
1fc21fb
delete outdated setter
jd-lara Mar 8, 2024
a39cff7
partially update the structs
jd-lara Mar 8, 2024
771b98b
add new cost functions
jd-lara Mar 8, 2024
779f537
format run
jd-lara Mar 8, 2024
f8c28fe
some updates
jd-lara Mar 8, 2024
6a267a6
Fix typos; minor refactors for legibility
GabrielKS Mar 21, 2024
6d19398
Add LoadCost
GabrielKS Mar 21, 2024
6ed554b
Add cost function includes
GabrielKS Mar 21, 2024
ddd5d08
Eliminate TwoPartCost in generated structs
GabrielKS Mar 21, 2024
2d434dd
Refine usage of `LoadCost`
GabrielKS Mar 22, 2024
3032e2d
Fix serialization, typos
GabrielKS Mar 22, 2024
cdfd673
Fix `OperationalCost` constructors, test
GabrielKS Mar 22, 2024
0156734
Make `OperationalCost` structs take the right kind of `ProductionVari…
GabrielKS Mar 25, 2024
7af370b
Remove `PolynomialFunctionData`
GabrielKS Mar 27, 2024
d7ff445
Accommodate `FunctionData` refactors
GabrielKS Mar 29, 2024
44e5f1a
Add `ValueCurve` for input-output, incremental, average rate curves
GabrielKS Mar 29, 2024
d9f1e1d
`ValueCurves`: add documentation, support serialization
GabrielKS Mar 29, 2024
652d09c
Redefine `CostCurve`, `FuelCurve` in terms of `ValueCurve`
GabrielKS Mar 31, 2024
d63dae6
Add some `zero` methods
GabrielKS Mar 31, 2024
31b848a
Standardize `OperationalCost` naming
GabrielKS Mar 31, 2024
f0fdd47
Make `test_cost_functions` pass
GabrielKS Mar 31, 2024
d055875
Export new cost structs
GabrielKS Apr 1, 2024
2d82421
Refine cost struct constructors
GabrielKS Apr 1, 2024
a292ca0
Make further use of new cost structs throughout PSY
GabrielKS Apr 1, 2024
3db189b
Fix cost struct parameterized type specifications
GabrielKS Apr 2, 2024
ccd9145
Revert `power_system_table_data` math, update accessors in test
GabrielKS Apr 2, 2024
187bdc0
Move `operational_cost.jl`
GabrielKS Apr 2, 2024
b824da4
Fine-tune exports
GabrielKS Apr 2, 2024
b114f59
Fix operational cost field types
GabrielKS Apr 2, 2024
5923544
Remove more references to old cost structs
GabrielKS Apr 3, 2024
28220f1
Add x-axis units in `CostCurve` and `FuelCurve`
GabrielKS Apr 3, 2024
dd537cb
Update Project.toml
jd-lara Mar 18, 2024
982861b
Add rough draft of cost function aliases and documentation
GabrielKS Apr 15, 2024
9346083
Add operational cost conversion logic
GabrielKS Apr 16, 2024
a25c586
Fine-tune cost structs
GabrielKS Apr 16, 2024
a49da75
Add cost aliases docs and tests
GabrielKS Apr 16, 2024
259b6cb
Rename `PiecewiseSlopeCurve`, revert ill-advised constructor refactor
GabrielKS Apr 17, 2024
0afc7bc
Save `MarketBidCost` refactor progress before HDF5 transformations work
GabrielKS Apr 19, 2024
bd1cfeb
Implement HDF serialization properly for `ValueCurve`s
GabrielKS Apr 20, 2024
d758391
Wrap up high-level cost structs in time series for MarketBidCost
GabrielKS Apr 20, 2024
b26d639
Run formatter
GabrielKS Apr 20, 2024
e8193dd
Update test for new cost structs
GabrielKS Apr 22, 2024
f524e21
Rename `MarketBidCost.ancillary_services` -> `ancillary_service_offers
GabrielKS Apr 22, 2024
234936e
Use cost function aliases
GabrielKS Apr 22, 2024
b0f4f60
Fix typo
GabrielKS Apr 27, 2024
39c87fb
Merge branch 'psy4' into jdgk/cost_functions
GabrielKS Apr 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PowerSystems"
uuid = "bcd98974-b02a-5e2f-9ee0-a103f5c450dd"
authors = ["Jose Daniel Lara", "Daniel Thom", "Dheepak Krishnamurthy", "Clayton Barrows", "Sourabh Dalvi"]
version = "3.2.2"
version = "3.3.0"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
16 changes: 16 additions & 0 deletions docs/src/modeler_guide/cost_functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# PowerSystems Cost Functions

PowerSystems.jl provides an extensive type hierarchy to explicitly express relationships between power production and cost. This lets the modeler represent cost functions as linear, quadratic, or piecewise input-output curves, potentially piecewise marginal heat rates, average heat rates, and more, as best fits the input data.

To represent a cost for a particular [`Component`](@ref), the modeler first chooses one of the variable cost representations in the table below. Then, they wrap this [`ProductionVariableCost`](@ref) in either a [`CostCurve`](@ref) to indicate a cost in currency or in a [`FuelCurve`](@ref) to indicate a cost per unit of fuel plus a fuel cost. Finally, the user creates a domain-specific [`OperationalCost`](@ref) that contains this variable cost as well as other costs that may exist in that domain, such as a fixed cost that is always incurred when the unit is on. For instance, we may have `RenewableGenerationCost(CostCurve(TODO), 0.0)` to represent the cost of a renewable unit that produces at TODO, or `ThermalGenerationCost(; variable = FuelCurve(TODO), fixed = TODO, start_up = TODO, shut_down = TODO)` to represent the cost of a thermal unit that produces at TODO. Below, we give the options for `ProductionVariableCost`s. Information on what domain-specific cost must be provided for a given component type can be found in that component type's documentation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the TODOs get completed?


## Variable Cost Representations
For more details, see the documentation page for each type.
| Type alias | Description | Constructor parameters | Example |
| --- | --- | --- | --- |
| `LinearCurve` | Linear input-output curve with zero no-load cost (constant average rate) | Average/marginal rate | `LinearCurve(3.0)` |
| `LinearCurve` | Linear input-output curve with nonzero no-load cost (constant marginal rate) | Marginal rate, cost at zero production | `LinearCurve(3.0, 5.0)` |
| `QuadraticCurve` | Quadratic input-output curve, may have nonzero no-load cost | Quadratic, proportional, and constant terms of input-output curve | `QuadraticCurve(1.0, 1.0, 18.0)` |
| `PiecewisePointCurve` | Piecewise linear curve specified by cost values at production points | Vector of (production, cost) pairs | `PiecewisePointCurve([(1.0, 20.0), (2.0, 24.0), (3.0, 30.0)])` |
| `PiecewiseIncrementalCurve` | Piecewise linear curve specified by marginal rates (slopes) between production points, may have nonzero initial value | Cost at minimum production point, vector of $n$ production points, vector of $n-1$ marginal rates/slopes of the curve segments between the points | `PiecewiseIncrementalCurve(20., [1.0, 2.0, 3.0], [4.0, 6.0])` |
| `PiecewiseAverageCurve` | Piecewise linear curve specified by average rates between production points, may have nonzero initial value | Cost at minimum production point, vector of $n$ production points, vector of average rates at the $n-1$ latter points | `PiecewiseAverageCurve(20., [1.0, 2.0, 3.0], [12.0, 10.0])` |
10 changes: 5 additions & 5 deletions docs/src/modeler_guide/market_bid_cost.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ generator = ThermalStandard(
time_limits = (up = 0.015, down = 0.015),
ramp_limits = (up = 5.0, down = 3.0),
operation_cost = MarketBidCost(
no_load = 0.0,
no_load_cost = 0.0,
start_up = (hot = 0.0, warm = 0.0, cold = 0.0),
shut_down = 0.0,
),
Expand Down Expand Up @@ -79,8 +79,8 @@ the `Dict` with the data as follows:

### Step 3a: Adding Energy Bid TimeSeriesData to the device

To add energy market bids time-series to the `MarketBidCost`, the use of `set_variable_cost!`
is recommended. The arguments for `set_variable_cost!` are:
To add energy market bids time-series to the `MarketBidCost`, use `set_variable_cost!`. The
arguments for `set_variable_cost!` are:

- `sys::System`: PowerSystem System
- `component::StaticInjection`: Static injection device
Expand All @@ -93,8 +93,8 @@ set_variable_cost!(sys, generator, time_series_data)

### Step 3b: Adding Service Bid TimeSeriesData to the device

Similar to adding energy market bids, for adding bids for ancillary services the use of
`set_service_bid!` is recommended.
Similar to adding energy market bids, for adding bids for ancillary services, use
`set_service_bid!`.

```@repl market_bid_cost
service = VariableReserve{ReserveUp}("example_reserve", true, 0.6, 2.0)
Expand Down
6 changes: 3 additions & 3 deletions docs/src/modeler_guide/time_series.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

## Introduction

The bulk of the data in many power system models is time series data, in order to
organize the data the potential inherent complexity, `PowerSystems.jl` has a set of definitions
to enable consistent modeling.
The bulk of the data in many power system models is time series data. Given the potential
inherent complexity, `PowerSystems.jl` has a set of definitions to organize this data and
enable consistent modeling.

- **Resolution**: The period of time between each discrete value in the data, all resolutions
are represented using `Dates.Period` types. For instance, a Day-ahead market data set usually
Expand Down
2 changes: 1 addition & 1 deletion docs/src/quick_start_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ new_renewable = RenewableDispatch(
prime_mover_type = PrimeMovers.WT,
reactive_power_limits = (min = 0.0, max = 0.0),
base_power = 100.0,
operation_cost = TwoPartCost(22.0, 0.0),
operation_cost = RenewableGenerationCost(CostCurve(InputOutputCurve(LinearFunctionData(22.0)))),
power_factor = 1.0
)

Expand Down
50 changes: 33 additions & 17 deletions src/PowerSystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ export PhaseShiftingTransformer
export FunctionData
export LinearFunctionData
export QuadraticFunctionData
export PolynomialFunctionData
export PiecewiseLinearPointData
export PiecewiseLinearSlopeData
export PiecewiseLinearData
export PiecewiseStepData
export get_proportional_term
export get_quadratic_term
export get_constant_term
export get_coefficients
export get_slopes
export get_x_lengths
export is_convex
export get_points
export get_x_coords
export get_y0
export get_y_coords

export ThreePartCost
export TwoPartCost
export MultiStartCost
export MarketBidCost
export StorageManagementCost
export ValueCurve, InputOutputCurve, IncrementalCurve, AverageRateCurve
export LinearCurve, QuadraticCurve
export PiecewisePointCurve, PiecewiseIncrementalCurve, PiecewiseAverageCurve
export ProductionVariableCost, CostCurve, FuelCurve
export OperationalCost, MarketBidCost, LoadCost, StorageCost
export HydroGenerationCost, RenewableGenerationCost, ThermalGenerationCost
export get_function_data, get_initial_input, get_value_curve, get_power_units, get_fuel_cost

export Generator
export HydroGen
Expand Down Expand Up @@ -337,6 +337,12 @@ export get_data
export iterate_components
export get_time_series_multiple
export get_variable_cost
export get_no_load_cost
export get_start_up
export get_shut_down
export get_incremental_offer_curves
export get_decremental_offer_curves
export get_ancillary_services
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export get_ancillary_services
export get_ancillary_service_offers

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

export get_services_bid
export set_variable_cost!
export set_service_bid!
Expand Down Expand Up @@ -448,7 +454,7 @@ import Logging
import Dates
import TimeSeries
import DataFrames
import DataStructures: OrderedDict
import DataStructures: OrderedDict, SortedDict
import JSON3
import CSV
import YAML
Expand Down Expand Up @@ -535,19 +541,18 @@ import InfrastructureSystems:
FunctionData,
LinearFunctionData,
QuadraticFunctionData,
PolynomialFunctionData,
PiecewiseLinearPointData,
PiecewiseLinearSlopeData,
PiecewiseLinearData,
PiecewiseStepData,
get_proportional_term,
get_quadratic_term,
get_constant_term,
get_coefficients,
get_slopes,
running_sum,
get_x_lengths,
is_convex,
get_points, # TODO possible rename to disambiguate from geographical information
get_x_coords,
get_y0, # TODO reevaluate whether this should be exported
get_y_coords,
get_raw_data,
get_raw_data_type

Expand Down Expand Up @@ -590,7 +595,6 @@ include("models/static_injection_subsystem.jl")
# PowerSystems models
include("models/topological_elements.jl")
include("models/branches.jl")
include("models/operational_cost.jl")
#include("models/network.jl")

# Static types
Expand All @@ -603,6 +607,18 @@ include("models/dynamic_generator_components.jl")
include("models/dynamic_inverter_components.jl")
include("models/OuterControl.jl")

# Costs
include("models/cost_functions/ValueCurves.jl")
include("models/cost_functions/cost_aliases.jl")
include("models/cost_functions/variable_cost.jl")
include("models/cost_functions/operational_cost.jl")
include("models/cost_functions/MarketBidCost.jl")
include("models/cost_functions/HydroGenerationCost.jl")
include("models/cost_functions/LoadCost.jl")
include("models/cost_functions/RenewableGenerationCost.jl")
include("models/cost_functions/StorageCost.jl")
include("models/cost_functions/ThermalGenerationCost.jl")

# Include all auto-generated structs.
include("models/generated/includes.jl")
include("models/regulation_device.jl")
Expand Down
24 changes: 22 additions & 2 deletions src/data_format_conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,43 @@ function _convert_cost(points::Vector)
@assert all(length.(points) .== 2)
@assert all([all(typeof.(point) .<: Real) for point in points])
# NOTE: old representation stored points as (y, x); new representation is (x, y)
return PiecewiseLinearPointData([(x, y) for (y, x) in points])
return PiecewiseLinearData([(x, y) for (y, x) in points])
end

# _convert_op_cost: take a component type, an old operational cost type, and old operational
# cost data; and create the proper new operational cost struct. Some of these cost structs
# no longer exist, so we dispatch instead on symbols.
_convert_op_cost(::Val{:ThermalStandard}, ::Val{:ThreePartCost}, op_cost::Dict) =
ThermalGenerationCost(
CostCurve(InputOutputCurve(op_cost["variable"])),
op_cost["fixed"],
op_cost["start_up"],
op_cost["shut_down"],
)

# TODO implement remaining _convert_op_cost methods
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to happen?


function _convert_data!(
raw::Dict{String, Any},
::Val{Symbol("3.0.0")},
::Val{Symbol("4.0.0")},
)
for component in vcat(raw["data"]["components"], raw["data"]["masked_components"])
# Convert costs: all old cost structs are in fields named `operation_cost`
if haskey(component, "operation_cost")
op_cost = component["operation_cost"]
# Step 1: insert a FunctionData
if op_cost["__metadata__"]["type"] in COST_CONTAINERS &&
haskey(op_cost["variable"], "cost")
old_cost = op_cost["variable"]["cost"]
new_cost = IS.serialize(_convert_cost(old_cost))
new_cost = _convert_cost(old_cost)
op_cost["variable"] = new_cost
end
# Step 2: convert TwoPartCost/ThreePartCost to new domain-specific cost structs
comp_type = Val{Symbol(component["__metadata__"]["type"])}()
op_cost_type = Val{Symbol(op_cost["__metadata__"]["type"])}()
new_op_cost = IS.serialize(_convert_op_cost(comp_type, op_cost_type, op_cost))
component["operation_cost"] = new_op_cost
end
end
end
Expand Down
1 change: 1 addition & 0 deletions src/definitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const StartUpShutDown = NamedTuple{(:startup, :shutdown), Tuple{Float64, Float64
const FromTo = NamedTuple{(:from, :to), Tuple{Float64, Float64}}
# Exception to CamelCase convention for aliases due to confusssing reading of FromToToFrom
const FromTo_ToFrom = NamedTuple{(:from_to, :to_from), Tuple{Float64, Float64}}
const StartUpStages = NamedTuple{(:hot, :warm, :cold), NTuple{3, Float64}}

"From http://www.pserc.cornell.edu/matpower/MATPOWER-manual.pdf Table B-4"

Expand Down
Loading
Loading