@@ -33,14 +33,19 @@ import Plots
33
33
34
34
# Define the model that will be construct given a set of parameters.
35
35
36
- function generate_model (d:: Float64 ; g_sup:: Vector{Float64} , c_g:: Vector{Float64} , c_ϕ:: Float64 )
36
+ function generate_model (
37
+ d:: Float64 ;
38
+ g_sup:: Vector{Float64} ,
39
+ c_g:: Vector{Float64} ,
40
+ c_ϕ:: Float64 ,
41
+ )
37
42
# # Creation of the Model and Parameters
38
43
model = Model (() -> DiffOpt. diff_optimizer (HiGHS. Optimizer))
39
44
set_silent (model)
40
45
I = length (g_sup)
41
46
42
47
# # Variables
43
- @variable (model, g[i in 1 : I] >= 0.0 )
48
+ @variable (model, g[i in 1 : I] >= 0.0 )
44
49
@variable (model, ϕ >= 0.0 )
45
50
46
51
# # Constraints
@@ -66,15 +71,20 @@ function diff_forward(model::Model, ϵ::Float64 = 1.0)
66
71
67
72
# # Get the primal solution of the model
68
73
vect = MOI. get .(model, MOI. VariablePrimal (), vect_ref)
69
-
74
+
70
75
# # Pass the perturbation to the DiffOpt Framework and set the context to Forward
71
76
constraint_equation = convert (MOI. ScalarAffineFunction{Float64}, ϵ)
72
- MOI. set (model, DiffOpt. ForwardConstraintFunction (), model[:demand_constraint ], constraint_equation)
77
+ MOI. set (
78
+ model,
79
+ DiffOpt. ForwardConstraintFunction (),
80
+ model[:demand_constraint ],
81
+ constraint_equation,
82
+ )
73
83
DiffOpt. forward_differentiate! (model)
74
-
84
+
75
85
# # Get the derivative of the model
76
86
dvect = MOI. get .(model, DiffOpt. ForwardVariablePrimal (), vect_ref)
77
-
87
+
78
88
# # Return the values as a vector
79
89
return [vect; dvect]
80
90
end
@@ -88,7 +98,7 @@ function diff_reverse(model::Model, ϵ::Float64 = 1.0)
88
98
vect = MOI. get .(model, MOI. VariablePrimal (), vect_ref)
89
99
90
100
# # Set variables needed for the DiffOpt Backward Framework
91
- dvect = Array {Float64, 1} (undef, I + 1 )
101
+ dvect = Array {Float64,1} (undef, I + 1 )
92
102
perturbation = zeros (I + 1 )
93
103
94
104
# # Loop for each primal variable
@@ -99,12 +109,18 @@ function diff_reverse(model::Model, ϵ::Float64 = 1.0)
99
109
DiffOpt. reverse_differentiate! (model)
100
110
101
111
# # Get the value of the derivative of the model
102
- dvect[i] = JuMP. constant (MOI. get (model, DiffOpt. ReverseConstraintFunction (), model[:demand_constraint ]))
112
+ dvect[i] = JuMP. constant (
113
+ MOI. get (
114
+ model,
115
+ DiffOpt. ReverseConstraintFunction (),
116
+ model[:demand_constraint ],
117
+ ),
118
+ )
103
119
perturbation[i] = 0.0
104
120
end
105
121
106
122
# # Return the values as a vector
107
- return [vect;dvect]
123
+ return [vect; dvect]
108
124
end
109
125
110
126
# Initialize of Parameters
@@ -114,8 +130,7 @@ I = length(g_sup)
114
130
d = 0.0 : 0.1 : 80
115
131
d_size = length (d)
116
132
c_g = [1.0 , 3.0 , 5.0 ]
117
- c_ϕ = 10.0
118
- ;
133
+ c_ϕ = 10.0 ;
119
134
120
135
# Generate models for each demand `d`
121
136
models = generate_model .(d; g_sup = g_sup, c_g = c_g, c_ϕ = c_ϕ);
@@ -128,37 +143,53 @@ result_reverse = diff_reverse.(models);
128
143
129
144
# Organization of results to plot
130
145
# Initialize data_results that will contain every result
131
- data_results = Array {Float64,3} (undef, 2 , d_size, 2 * (I + 1 ));
146
+ data_results = Array {Float64,3} (undef, 2 , d_size, 2 * (I + 1 ));
132
147
133
148
# Populate the data_results array
134
149
for k in 1 : d_size
135
- data_results[1 ,k, :] = result_forward[k]
136
- data_results[2 ,k, :] = result_reverse[k]
150
+ data_results[1 , k, :] = result_forward[k]
151
+ data_results[2 , k, :] = result_reverse[k]
137
152
end
138
153
139
154
# ## Results with Plot graphs
140
155
# ### Results for the forward context
141
156
# Result Primal Values:
142
- Plots. plot (d,data_results[1 ,:,1 : I+ 1 ],
143
- title= " Generation by Demand" ,label= [" Thermal Generation 1" " Thermal Generation 2" " Thermal Generation 3" " Generation Deficit" ],
144
- xlabel= " Demand [unit]" ,ylabel= " Generation [unit]"
157
+ Plots. plot (
158
+ d,
159
+ data_results[1 , :, 1 : I+ 1 ];
160
+ title = " Generation by Demand" ,
161
+ label = [" Thermal Generation 1" " Thermal Generation 2" " Thermal Generation 3" " Generation Deficit" ],
162
+ xlabel = " Demand [unit]" ,
163
+ ylabel = " Generation [unit]" ,
145
164
)
146
165
147
166
# Result Sensitivity Analysis:
148
- Plots. plot (d,data_results[1 ,:,I+ 2 : 2 * (I+ 1 )],
149
- title= " Sensitivity of Generation by Demand" ,label= [" T. Gen. 1 Sensitivity" " T. Gen. 2 Sensitivity" " T. Gen. 3 Sensitivity" " Gen. Deficit Sensitivity" ],
150
- xlabel= " Demand [unit]" ,ylabel= " Sensitivity [-]"
167
+ Plots. plot (
168
+ d,
169
+ data_results[1 , :, I+ 2 : 2 * (I+ 1 )];
170
+ title = " Sensitivity of Generation by Demand" ,
171
+ label = [" T. Gen. 1 Sensitivity" " T. Gen. 2 Sensitivity" " T. Gen. 3 Sensitivity" " Gen. Deficit Sensitivity" ],
172
+ xlabel = " Demand [unit]" ,
173
+ ylabel = " Sensitivity [-]" ,
151
174
)
152
175
153
176
# ### Results for the reverse context
154
177
# Result Primal Values:
155
- Plots. plot (d,data_results[2 ,:,1 : I+ 1 ],
156
- title= " Generation by Demand" ,label= [" Thermal Generation 1" " Thermal Generation 2" " Thermal Generation 3" " Generation Deficit" ],
157
- xlabel= " Demand [unit]" ,ylabel= " Generation [unit]"
178
+ Plots. plot (
179
+ d,
180
+ data_results[2 , :, 1 : I+ 1 ];
181
+ title = " Generation by Demand" ,
182
+ label = [" Thermal Generation 1" " Thermal Generation 2" " Thermal Generation 3" " Generation Deficit" ],
183
+ xlabel = " Demand [unit]" ,
184
+ ylabel = " Generation [unit]" ,
158
185
)
159
186
160
187
# Result Sensitivity Analysis:
161
- Plots. plot (d,data_results[2 ,:,I+ 2 : 2 * (I+ 1 )],
162
- title= " Sensitivity of Generation by Demand" ,label= [" T. Gen. 1 Sensitivity" " T. Gen. 2 Sensitivity" " T. Gen. 3 Sensitivity" " Gen. Deficit Sensitivity" ],
163
- xlabel= " Demand [unit]" ,ylabel= " Sensitivity [-]"
188
+ Plots. plot (
189
+ d,
190
+ data_results[2 , :, I+ 2 : 2 * (I+ 1 )];
191
+ title = " Sensitivity of Generation by Demand" ,
192
+ label = [" T. Gen. 1 Sensitivity" " T. Gen. 2 Sensitivity" " T. Gen. 3 Sensitivity" " Gen. Deficit Sensitivity" ],
193
+ xlabel = " Demand [unit]" ,
194
+ ylabel = " Sensitivity [-]" ,
164
195
)
0 commit comments