-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregular_opf_opt.jl
176 lines (148 loc) · 5.55 KB
/
regular_opf_opt.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
168
169
170
171
172
173
174
175
176
using JuMP
using HiGHS
using Gurobi
using Flux
using BSON
using Plots
using PowerModels
using HDF5
using JLD2
# network_type = "base_case"
network_type = "sole_gen"
#network_type = "high_risk"
#nn_model = BSON.load("wildfire_trained_model_$network_type.bson")
eng = PowerModels.parse_file("case5_risk_$network_type.m")
objective=[]
load_shed_units = []
wildfire_risk = []
line_1 = []
line_2 = []
line_3 = []
line_4 = []
line_5 = []
line_6 = []
# Define alpha parameter
alpha = (0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2)
if network_type == "sole_gen"
alpha = (0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9)
end
for j in alpha
print("This is $j\n \n")
pd = []
qd = []
for i in 1:5
push!(pd, eng["load"]["$i"]["pd"])
push!(qd, eng["load"]["$i"]["qd"])
end
nominal_values = append!(pd, qd)
# Define Big M vector
if network_type == "base_case"
u = fill(2.9504, 100)
l = fill(-1.8339, 100)
elseif network_type == "sole_gen"
u = fill(2.7504, 100)
l = fill(-1.863, 100)
elseif network_type == "high_risk"
u = fill(2.974, 100)
l = fill(-1.81, 100)
end
loads = []
for i in keys(eng["load"])
push!(loads, eng["load"][i]["pd"])
end
D_p = sum(loads)
risk = []
for i in 1:6
push!(risk, eng["branch"]["$i"]["power_risk"])
end
total_risk = sum(risk)
# W_1 = nn_model[:model][1].weight
# W_2 = nn_model[:model][2].weight
# B_1 = nn_model[:model][1].bias
# B_2 = nn_model[:model][2].bias
#Optimization problem
model = Model(Gurobi.Optimizer)
@variable(model, line_status[1:length(eng["branch"])], Bin)
# @variable(model, x2[1:length(nn_model[:model][1].bias)])
@variable(model, x3 >= 0)
# @variable(model, z2[1:length(nn_model[:model][1].bias)], Bin)
input_vector = append!(nominal_values, line_status)
#The load shed must be equal to the output of the nueral network
#@constraint(model, x3 == W_2 * (x2) + B_2)
#@expression(model, without_bias, W_1 * input_vector)
## ReLu constraints
# for i in 1:100
# @constraint(model, x2[i] >= 0)
# end
# for i in 1:100
# @constraint(model, x2[i] >= without_bias[i] + B_1[i])
# end
# for i in 1:100
# @constraint(model, x2[i] <= u[i] * z2[i])
# end
# for i in 1:100
# @constraint(model, x2[i] <= without_bias[i] + B_1[i] - l[i] * (1 - z2[i]))
# end
# Load shed is non negative
# @constraint(model, x3 >= 0)
# Load shed is less than or equal to the total load
for i in 1:6
#print(i)
eng["branch"]["$i"]["br_status"] = line_status[i]
end
# Use the line statuses as a variable in the AC OPF equations
#PowerModels.constraint_switch_on_off!(eng, "branch", "br_status")
pm = instantiate_model(eng, DCPPowerModel, PowerModels.build_opf)
result = optimize_model!(pm, optimizer=Ipopt.Optimizer)
@constraint(model, x3 <= sum(eng["load"]["$i"]["pd"] for i in 1:length(eng["load"])) - sum(result["solution"]["gen"]["$i"]["pg"] for i in 1:length(eng["gen"])))
@constraint( model, x3 <= j*D_p)
display(j)
# # ---Objective function
# @objective(model,
# Min,
# (j * x3[1])/D_p
# +
# ((1 - j) /total_risk)* sum(risk[i] * line_status[i] for i in 1:6)
# )
#---Objective function
@objective(model, Min, sum(risk[i] * line_status[i] for i in 1:6)/total_risk
)
#--- Solve the model
optimize!(model)
push!(objective, JuMP.objective_value(model))
push!(load_shed_units, JuMP.value.(x3)/D_p)
line_risk = sum(risk[i] * JuMP.value.(line_status)[i] for i in 1:6)
push!(wildfire_risk, line_risk/total_risk)
push!(line_1, JuMP.value.(line_status)[1])
push!(line_2, JuMP.value.(line_status)[2])
push!(line_3, JuMP.value.(line_status)[3])
push!(line_4, JuMP.value.(line_status)[4])
push!(line_5, JuMP.value.(line_status)[5])
push!(line_6, JuMP.value.(line_status)[6])
# Check the status of the optimization
if termination_status(model) == MOI.OPTIMAL
println("Optimal solution found.")
println("Objective value: ", JuMP.objective_value(model))
println("load_shed units: ", JuMP.value.(x3))
println("line_status units: ", JuMP.value.(line_status))
#println("Binary Variables: ", JuMP.value.(z2))
else
println("Optimization problem failed to find an optimal solution.")
end
end
alpha = range(0.01, 0.2, length=20)
if network_type == "sole_gen"
alpha = range(0.1, 1, length=9)
end
load_shed_units_combined = vcat(load_shed_units...)
plot(load_shed_units_combined, wildfire_risk, title = "Total Risk VS Total Load Shed for $network_type", xlabel = "% of load shed", ylabel = "Total Wildfire risk", label="Total Risk")
plot(alpha, line_1, title = "Variation of line status with Load Shed", xlabel = "% of load shed", ylabel = "Line status (1=closed & 0=open)", label="Line 1")
plot!(alpha, line_2, label="Line 2")
plot!(alpha, line_3, label="Line 3")
plot!(alpha, line_4, label="Line 4")
plot!(alpha, line_5, label="Line 5")
plot!(alpha, line_6, label="Line 6")
#create a dictionary to store the results
results = Dict("objective" => objective, "load_shed_units" => load_shed_units, "wildfire_risk" => wildfire_risk, "line_1" => line_1, "line_2" => line_2, "line_3" => line_3, "line_4" => line_4, "line_5" => line_5, "line_6" => line_6, "alpha" => alpha)
#save the results
save("nn_opt_results_$network_type.jld2", "nn_opt_results_$network_type", results)