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

Variable Cost Refactor Part 2: PowerSimulations #332

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ end

const CONSTANT = Float64
const LIM_TOL = 1e-6
const XY_COORDS = @NamedTuple{x::Float64, y::Float64}
72 changes: 65 additions & 7 deletions src/function_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,49 @@ representation of cost functions where the points store quantities (x, y), such
The curve starts at the first point given, not the origin.

# Arguments
- `points::Vector{Tuple{Float64, Float64}}`: the points (x, y) that define the function
- `points::Vector{XY_COORDS}`: the points that define the function
"""
struct PiecewiseLinearPointData <: FunctionData
points::Vector{Tuple{Float64, Float64}}
points::Vector{XY_COORDS}

function PiecewiseLinearPointData(points)
function PiecewiseLinearPointData(points::Vector{<:NamedTuple{(:x, :y)}})
_validate_piecewise_x(first.(points))
return new(points)
end
end

function PiecewiseLinearPointData(points::Vector{<:NamedTuple})
throw(
ArgumentError(
"If constructing PiecewiseLinearPointData with NamedTuples, points must have type Vector{<:NamedTuple{(:x, :y)}}; got $(typeof(points))",
),
)
end

function _convert_to_xy_coords(point)
# Need to be able to handle dicts for deserialization
if point isa AbstractDict
(keys(point) == Set(["x", "y"])) && return (x = point["x"], y = point["y"])
throw(
ArgumentError(
"If constructing PiecewiseLinearPointData with dictionaries, keys must be [\"x\", \"y\"]; got $(collect(keys(point)))",
),
)
end
return NamedTuple{(:x, :y)}(point)
end

function PiecewiseLinearPointData(points::Vector)
PiecewiseLinearPointData(_convert_to_xy_coords.(points))
end

"Get the points that define the piecewise data"
get_points(data::PiecewiseLinearPointData) = data.points

"Get the x-coordinates of the points that define the piecewise data"
get_x_coords(data::PiecewiseLinearPointData) = first.(get_points(data))

function _get_slopes(vc::Vector{NTuple{2, Float64}})
function _get_slopes(vc::Vector{XY_COORDS})
slopes = Vector{Float64}(undef, length(vc) - 1)
(prev_x, prev_y) = vc[1]
for (i, (comp_x, comp_y)) in enumerate(vc[2:end])
Expand Down Expand Up @@ -138,13 +163,13 @@ get_y0(data::PiecewiseLinearSlopeData) = data.y0
function get_points(data::PiecewiseLinearSlopeData)
slopes = get_slopes(data)
x_coords = get_x_coords(data)
points = Vector{Tuple{Float64, Float64}}(undef, length(x_coords))
points = Vector{XY_COORDS}(undef, length(x_coords))
running_y = get_y0(data)
points[1] = (x_coords[1], running_y)
points[1] = (x = x_coords[1], y = running_y)
for (i, (prev_slope, this_x, dx)) in
enumerate(zip(slopes, x_coords[2:end], get_x_lengths(data)))
running_y += prev_slope * dx
points[i + 1] = (this_x, running_y)
points[i + 1] = (x = this_x, y = running_y)
end
return points
end
Expand Down Expand Up @@ -220,3 +245,36 @@ deserialize(T::Type{<:FunctionData}, val::Dict) = deserialize_struct(T, val)

deserialize(::Type{FunctionData}, val::Dict) =
throw(ArgumentError("FunctionData is abstract, must specify a concrete subtype"))

# FunctionData support fetching "raw data" to support cases where we might want to store
# their data in a different container in its most purely numerical form, such as in
# PowerSimulations.

"""
Get a bare numerical representation of the data represented by the FunctionData
"""
function get_raw_data end
get_raw_data(fd::LinearFunctionData) = get_proportional_term(fd)
get_raw_data(fd::QuadraticFunctionData) =
(get_quadratic_term(fd), get_proportional_term(fd), get_constant_term(fd))
get_raw_data(fd::PolynomialFunctionData) =
sort([(degree, coeff) for (degree, coeff) in get_coefficients(fd)]; by = first)
get_raw_data(fd::PiecewiseLinearPointData) = Tuple.(get_points(fd))
function get_raw_data(fd::PiecewiseLinearSlopeData)
x_coords = get_x_coords(fd)
return vcat((x_coords[1], get_y0(fd)), collect(zip(x_coords[2:end], get_slopes(fd))))
end

"""
Get from a subtype of FunctionData the type of data its get_raw_data method returns
"""
function get_raw_data_type end
get_raw_data_type(::Union{LinearFunctionData, Type{LinearFunctionData}}) = Float64
get_raw_data_type(::Union{QuadraticFunctionData, Type{QuadraticFunctionData}}) =
NTuple{3, Float64}
get_raw_data_type(::Union{PolynomialFunctionData, Type{PolynomialFunctionData}}) =
Vector{Tuple{Int, Float64}}
get_raw_data_type(::Union{PiecewiseLinearPointData, Type{PiecewiseLinearPointData}}) =
Vector{Tuple{Float64, Float64}}
get_raw_data_type(::Union{PiecewiseLinearSlopeData, Type{PiecewiseLinearSlopeData}}) =
Vector{Tuple{Float64, Float64}}
32 changes: 29 additions & 3 deletions test/test_function_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ get_test_function_data() = [
@testset "Test FunctionData validation" begin
@test_throws ArgumentError IS.PiecewiseLinearPointData([(2, 1), (1, 1)])
@test_throws ArgumentError IS.PiecewiseLinearSlopeData([2, 1], 1, [1])

@test IS.PiecewiseLinearPointData([(x = 1, y = 1)]) isa Any # Test absence of error
@test IS.PiecewiseLinearPointData([Dict("x" => 1, "y" => 1)]) isa Any
@test_throws ArgumentError IS.PiecewiseLinearPointData([(y = 1, x = 1)])
@test_throws ArgumentError IS.PiecewiseLinearPointData([Dict("x" => 1)])
end

@testset "Test FunctionData trivial getters" begin
Expand All @@ -26,7 +31,7 @@ end
@test coeffs[0] === 3.0 && coeffs[1] === 1.0 && coeffs[3] === 4.0

yd = IS.PiecewiseLinearPointData([(1, 1), (3, 5)])
@test IS.get_points(yd) == [(1, 1), (3, 5)]
@test IS.get_points(yd) == [(x = 1, y = 1), (x = 3, y = 5)]

dd = IS.PiecewiseLinearSlopeData([1, 3, 5], 2, [3, 6])
@test IS.get_x_coords(dd) == [1, 3, 5]
Expand All @@ -38,7 +43,7 @@ end
@test length(IS.PiecewiseLinearPointData([(0, 0), (1, 1), (1.1, 2)])) == 2
@test length(IS.PiecewiseLinearSlopeData([1, 1.1, 1.2], 1, [1.1, 10])) == 2

@test IS.PiecewiseLinearPointData([(0, 0), (1, 1), (1.1, 2)])[2] == (1, 1)
@test IS.PiecewiseLinearPointData([(0, 0), (1, 1), (1.1, 2)])[2] == (x = 1, y = 1)
@test IS.get_x_coords(IS.PiecewiseLinearPointData([(0, 0), (1, 1), (1.1, 2)])) ==
[0, 1, 1.1]

Expand All @@ -56,6 +61,8 @@ end
IS.get_slopes(IS.PiecewiseLinearPointData([(0, 0), (1, 1), (1.1, 2)])),
[1, 10])

@test IS.get_points(IS.PiecewiseLinearSlopeData([1, 3, 5], 1, [2.5, 10])) isa
Vector{IS.XY_COORDS}
@test isapprox(
collect.(IS.get_points(IS.PiecewiseLinearSlopeData([1, 3, 5], 1, [2.5, 10]))),
collect.([(1, 1), (3, 6), (5, 26)]),
Expand Down Expand Up @@ -84,7 +91,7 @@ end
pointwise = IS.PiecewiseLinearPointData(collect(zip(rand_x, rand_y)))
slopewise = IS.PiecewiseLinearSlopeData(
IS.get_x_coords(pointwise),
first(IS.get_points(pointwise))[2],
first(IS.get_points(pointwise)).y,
IS.get_slopes(pointwise))
pointwise_2 = IS.PiecewiseLinearPointData(IS.get_points(slopewise))
@test isapprox(
Expand All @@ -103,3 +110,22 @@ end
end
end
end

@testset "Test FunctionData raw data" begin
raw_data_answers = [
5.0,
(2.0, 3.0, 4.0),
[(0, 3.0), (1, 1.0), (3, 4.0)],
[(1.0, 1.0), (3.0, 5.0), (5.0, 10.0)],
[(1.0, 1.0), (3.0, 2.0), (5.0, 2.5)],
]
for (fd, answer) in zip(get_test_function_data(), raw_data_answers)
@test IS.get_raw_data_type(fd) == typeof(answer)
end
for (fd, answer) in zip(get_test_function_data(), raw_data_answers)
@test IS.get_raw_data_type(typeof(fd)) == typeof(answer)
end
for (fd, answer) in zip(get_test_function_data(), raw_data_answers)
@test IS.get_raw_data(fd) == answer
end
end
Loading