Skip to content

Commit

Permalink
Format and fix SDDiP
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Aug 30, 2021
1 parent 1e12aec commit e22fea2
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 97 deletions.
83 changes: 42 additions & 41 deletions papers/entropic/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ function _build_model(data::Dict)
sp,
sum(
data["deficit_obj"][i] * sum(df[i, :]) +
sum(data["thermal_obj"][i][k] * g[i, k] for k in K(i))
for i in I
sum(data["thermal_obj"][i][k] * g[i, k] for k in K(i)) for
i in I
)
)
@constraint(
sp,
[i in I],
q[i] + sum(g[i, k] for k in K(i)) + sum(df[i, :]) + sum(ex[:, i]) -
sum(ex[i, :]) == data["demand"][month][i] / 1_000
q[i] + sum(g[i, k] for k in K(i)) + sum(df[i, :]) + sum(ex[:, i]) - sum(ex[i, :]) ==
data["demand"][month][i] / 1_000
)
@constraint(
sp,
Expand Down Expand Up @@ -134,17 +134,17 @@ function _train_model(model::SDDP.PolicyGraph, gamma::Float64)
sampling_scheme = psr_sampling_scheme,
)
stage_objectives = [
round(simulations[i][t][:stage_objective]; digits = 1)
for i = 1:length(simulations)
for t = 1:length(simulations[i])
round(simulations[i][t][:stage_objective]; digits = 1) for
i in 1:length(simulations) for t in 1:length(simulations[i])
]
stage_objectives = reshape(
stage_objectives,
length(simulations[1]),
length(simulations),
)
stage_objectives =
reshape(stage_objectives, length(simulations[1]), length(simulations))
open("stage_objectives_$(gamma).csv", "w") do io
DelimitedFiles.writedlm(io, collect(transpose(stage_objectives)), ',')
return DelimitedFiles.writedlm(
io,
collect(transpose(stage_objectives)),
',',
)
end
return
end
Expand All @@ -160,7 +160,7 @@ function _plot_cumulative_density(
Plots.plot()
line_styles = [:solid, :dash, :dot, :dashdot]
for (i, f) in enumerate(filenames)
X = sum(DelimitedFiles.readdlm(f, ','); dims=2)[:]
X = sum(DelimitedFiles.readdlm(f, ','); dims = 2)[:]
x = parse(Float64, match(r"stage\_objectives\_(.+)\.csv", f)[1])
x = if x 0.0
"0"
Expand All @@ -173,7 +173,7 @@ function _plot_cumulative_density(
X,
label = "\$\\gamma = $(x)\$",
style = line_styles[i],
color=:black,
color = :black,
)
end
p = Plots.plot!(
Expand All @@ -186,7 +186,6 @@ function _plot_cumulative_density(
return p
end


function _plot_objectives(filename::String)
quantiles = [0.0, 0.01, 0.05, 0.5, 0.95, 0.99, 1.0]
matrix = DelimitedFiles.readdlm(filename, ',')
Expand All @@ -205,31 +204,31 @@ function _plot_objectives(filename::String)
end
Plots.plot(
A[:, 4],
ribbon=(A[:, 4] .- A[:, 1], A[:, 7] .- A[:, 4]),
color=:black,
fillalpha=0.2,
ribbon = (A[:, 4] .- A[:, 1], A[:, 7] .- A[:, 4]),
color = :black,
fillalpha = 0.2,
legend = false,
title = "\$\\gamma = $(x)\$",
size = (300, 400),
)
Plots.plot!(
A[:, 4],
ribbon=(A[:, 4] .- A[:, 2], A[:, 6] .- A[:, 4]),
color=:black,
fillalpha=0.2,
ribbon = (A[:, 4] .- A[:, 2], A[:, 6] .- A[:, 4]),
color = :black,
fillalpha = 0.2,
)
Plots.plot!(
A[:, 4],
ribbon=(A[:, 4] .- A[:, 3], A[:, 5] .- A[:, 4]),
color=:black,
fillalpha=0.2,
ribbon = (A[:, 4] .- A[:, 3], A[:, 5] .- A[:, 4]),
color = :black,
fillalpha = 0.2,
)
Plots.plot!(
return Plots.plot!(
A[:, 4],
color=:black,
color = :black,
xlabel = "Stages",
ylabel = "Stage objective (\$)",
ylim = (0, 4e4)
ylim = (0, 4e4),
)
end

Expand All @@ -251,24 +250,26 @@ function _plot_objectives(
end

function _print_help()
println("""
usage: julia [-p N] [--project=.] model.jl [--gamma=<value>] [--help] [--plot]
return println(
"""
usage: julia [-p N] [--project=.] model.jl [--gamma=<value>] [--help] [--plot]
Solve the hydro-thermal scheduling problem with the Entropic risk measure
parameterized with γ = <value>.
Solve the hydro-thermal scheduling problem with the Entropic risk measure
parameterized with γ = <value>.
Use `-p N` to run the SDDP solver in parallel mode over `N` processors.
Use `-p N` to run the SDDP solver in parallel mode over `N` processors.
Use `--project=.` to reproduce the example in the paper using the provided
`Manifest.toml` and `Project.toml` files.
Use `--project=.` to reproduce the example in the paper using the provided
`Manifest.toml` and `Project.toml` files.
Examples:
Examples:
julia model.jl --gamma=0.5
julia -p 4 --project=. model.jl --gamma=0.5
julia -p 4 --project=. model.jl --gamma=5.0
julia --project=. model.jl --plot
""")
julia model.jl --gamma=0.5
julia -p 4 --project=. model.jl --gamma=0.5
julia -p 4 --project=. model.jl --gamma=5.0
julia --project=. model.jl --plot
""",
)
end

function main(args)
Expand Down
Loading

0 comments on commit e22fea2

Please sign in to comment.