@@ -33,14 +33,19 @@ import Plots
3333
3434# Define the model that will be construct given a set of parameters.
3535
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+ )
3742 # # Creation of the Model and Parameters
3843 model = Model (() -> DiffOpt. diff_optimizer (HiGHS. Optimizer))
3944 set_silent (model)
4045 I = length (g_sup)
4146
4247 # # Variables
43- @variable (model, g[i in 1 : I] >= 0.0 )
48+ @variable (model, g[i in 1 : I] >= 0.0 )
4449 @variable (model, ϕ >= 0.0 )
4550
4651 # # Constraints
@@ -66,15 +71,20 @@ function diff_forward(model::Model, ϵ::Float64 = 1.0)
6671
6772 # # Get the primal solution of the model
6873 vect = MOI. get .(model, MOI. VariablePrimal (), vect_ref)
69-
74+
7075 # # Pass the perturbation to the DiffOpt Framework and set the context to Forward
7176 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+ )
7383 DiffOpt. forward_differentiate! (model)
74-
84+
7585 # # Get the derivative of the model
7686 dvect = MOI. get .(model, DiffOpt. ForwardVariablePrimal (), vect_ref)
77-
87+
7888 # # Return the values as a vector
7989 return [vect; dvect]
8090end
@@ -88,7 +98,7 @@ function diff_reverse(model::Model, ϵ::Float64 = 1.0)
8898 vect = MOI. get .(model, MOI. VariablePrimal (), vect_ref)
8999
90100 # # Set variables needed for the DiffOpt Backward Framework
91- dvect = Array {Float64, 1} (undef, I + 1 )
101+ dvect = Array {Float64,1} (undef, I + 1 )
92102 perturbation = zeros (I + 1 )
93103
94104 # # Loop for each primal variable
@@ -99,12 +109,18 @@ function diff_reverse(model::Model, ϵ::Float64 = 1.0)
99109 DiffOpt. reverse_differentiate! (model)
100110
101111 # # 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+ )
103119 perturbation[i] = 0.0
104120 end
105121
106122 # # Return the values as a vector
107- return [vect;dvect]
123+ return [vect; dvect]
108124end
109125
110126# Initialize of Parameters
@@ -114,8 +130,7 @@ I = length(g_sup)
114130d = 0.0 : 0.1 : 80
115131d_size = length (d)
116132c_g = [1.0 , 3.0 , 5.0 ]
117- c_ϕ = 10.0
118- ;
133+ c_ϕ = 10.0 ;
119134
120135# Generate models for each demand `d`
121136models = generate_model .(d; g_sup = g_sup, c_g = c_g, c_ϕ = c_ϕ);
@@ -128,37 +143,53 @@ result_reverse = diff_reverse.(models);
128143
129144# Organization of results to plot
130145# 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 ));
132147
133148# Populate the data_results array
134149for 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]
137152end
138153
139154# ## Results with Plot graphs
140155# ### Results for the forward context
141156# 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]" ,
145164)
146165
147166# 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 [-]" ,
151174)
152175
153176# ### Results for the reverse context
154177# 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]" ,
158185)
159186
160187# 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 [-]" ,
164195)
0 commit comments