-
Notifications
You must be signed in to change notification settings - Fork 5
/
HydroPowerModels.jl
167 lines (139 loc) · 4.99 KB
/
HydroPowerModels.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module HydroPowerModels
using JuMP, PowerModels, SDDP
using JSON
using CSV
using DataFrames
using Reexport: Reexport
mutable struct HydroPowerModel
policygraph::SDDP.PolicyGraph
alldata::Array{Dict{Any,Any}}
params::Dict
end
include("variable.jl")
include("constraint.jl")
include("utilities.jl")
include("IO.jl")
include("simulate.jl")
include("train.jl")
# include("visualization/visualize_data.jl") # Issue: https://github.com/andrewrosemberg/HydroPowerModels.jl/issues/46
include("objective.jl")
include("build_model.jl")
export hydrothermaloperation, create_param, set_active_demand!, flat_dict, signif_dict
Reexport.@reexport using PowerModels, SDDP
"""
hydrothermaloperation(alldata::Array{Dict{Any,Any}}, params::Dict)
Create a hydrothermal power operation model containing the policygraph the system data and the planning parameters.
Required parameters are:
- alldata is a vector of dicts with information of the problem's stages.
- param is a dict containing solution parameters.
"""
function hydrothermaloperation(
alldata::Array{Dict{Any,Any}},
params::Dict;
build_opf_model::Function=HydroPowerModels.build_opf_powermodels,
build_graph::Function=HydroPowerModels.build_graph,
)
# verbose
if !params["verbose"]
PowerModels.silence()
end
# if set silence the solver
# related to https://github.com/JuliaOpt/JuMP.jl/pull/1921
if !params["verbose"]
try
MOI.set(JuMP.backend(Model(params["optimizer"])), MOI.Silent(), true)
catch
@info "Silent() attribute not implemented by the optimizer."
end
end
# Model Definition
graph = build_graph(params)
policygraph = SDDP.PolicyGraph(
graph;
sense=:Min,
optimizer=params["optimizer"],
#optimizer_forward = params["optimizer_forward"],
#optimizer_backward = params["optimizer_backward"],
lower_bound=0.0,
direct_mode=false,
) do sp, t #, isforward
# if set silence the solver
# related to https://github.com/JuliaOpt/JuMP.jl/pull/1921
if !params["verbose"]
try
MOI.set(JuMP.backend(sp), MOI.Silent(), true)
catch
#@info "Silent() attribute not implemented by the optimizer."
end
end
# Extract current data
data = alldata[min(t, size(alldata, 1))]
# gather useful information from data
gatherusefulinfo!(data)
# build eletric grid model using PowerModels
pm = build_opf_model(sp, data, params)
#if isforward # NOT YET IMPLEMENTED
#pm = build_opf_model(data["powersystem"], params["model_constructor_grid_forward"],
#params["post_method"], jump_model=sp, setting = params["setting"])
#else
#pm = build_opf_model(data["powersystem"], params["model_constructor_grid_backward"],
# params["post_method"], jump_model=sp, setting = params["setting"])
#end
# create reference to variables
createvarrefs!(sp, pm)
# save AbstractPowerModel and Data
sp.ext[:pm] = pm
sp.ext[:data] = data
# save lower_bound
sp.ext[:lower_bound] = 0.0
# resevoir variables
variable_volume(sp, data)
# outflow and spillage variables
variable_outflow(sp, data)
variable_spillage(sp, data)
# hydro balance
variable_inflow(sp, data)
rainfall_noises(sp, data, params, cidx(t, data["hydro"]["size_inflow"][1]))
constraint_hydro_balance(sp, data, params)
# hydro_generation
constraint_hydro_generation(sp, data, pm)
# deficit
variable_deficit(sp, data, pm)
constraint_mod_deficit(sp, data, pm)
# costs stage
variable_cost(sp, data)
add_gen_cost(sp, data)
add_spill_cost(sp, data)
add_deficit_cost(sp, data)
# Stage objective
set_objective(sp, data)
# # variable primal start
# JuMP.MathOptInterface.set.(sp,JuMP.MathOptInterface.VariablePrimalStart(), JuMP.all_variables(sp), NaN)
JuMP.MathOptInterface.set.(
sp, JuMP.MathOptInterface.VariablePrimalStart(), sp[:deficit], 0.0
)
JuMP.MathOptInterface.set.(
sp, JuMP.MathOptInterface.VariablePrimalStart(), sp[:inflow], 0.0
)
JuMP.MathOptInterface.set.(
sp, JuMP.MathOptInterface.VariablePrimalStart(), sp[:outflow], 0.0
)
JuMP.MathOptInterface.set.(
sp, JuMP.MathOptInterface.VariablePrimalStart(), sp[:spill], 0.0
)
for r in sp[:reservoir]
JuMP.MathOptInterface.set.(
sp, JuMP.MathOptInterface.VariablePrimalStart(), r.in, 0.0
)
end
for r in sp[:reservoir]
JuMP.MathOptInterface.set.(
sp, JuMP.MathOptInterface.VariablePrimalStart(), r.out, 0.0
)
end
end
# save data
m = HydroPowerModel(policygraph, alldata, params)
return m
end
end