Skip to content

Commit 2dc709c

Browse files
update code
1 parent 47e7b83 commit 2dc709c

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ examples/unitcommitment/app/*
2424
*.log
2525
*.wandb
2626
*latest-run
27+
*.html

examples/powermodels/visualize.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ cossim(x,y) = dot(x,y) / (norm(x)*norm(y))
99
##############
1010
# Parameters
1111
##############
12+
network_formulation = "ACPPowerModel"
1213
case_name = "pglib_opf_case300_ieee"
1314
path_dataset = joinpath(dirname(@__FILE__), "data")
1415
case_file_path = joinpath(path_dataset, case_name)
1516
case_file_path_train = joinpath(case_file_path, "input", "train")
1617
case_file_path_test = joinpath(case_file_path, "input", "test")
18+
case_file_path_output = joinpath(case_file_path, "output", string(network_formulation))
1719

1820
##############
1921
# Load Data
@@ -28,14 +30,22 @@ file_train = [
2830
file_test = [
2931
joinpath(case_file_path_test, file) for file in iter_files_test if occursin("input", file)
3032
]
33+
iter_files_out = readdir(joinpath(case_file_path_output))
34+
iter_files_out = filter(x -> occursin("arrow", x), iter_files_out)
35+
file_outs = [
36+
joinpath(case_file_path_output, file) for file in iter_files_out if occursin("output", file)
37+
]
3138

3239
# Load input and output data tables
3340
input_table_train = Arrow.Table(file_train)
3441
input_table_test = Arrow.Table(file_test)
42+
output_table = Arrow.Table(file_outs)
3543

3644
# Convert to dataframes
3745
input_data_train = DataFrame(input_table_train)
3846
input_data_test = DataFrame(input_table_test)
47+
output_data = DataFrame(output_table)
48+
input_data = vcat(input_data_train, input_data_test[!, Not(:in_train_convex_hull)])
3949

4050
##############
4151
# Plots
@@ -58,6 +68,7 @@ function total_load_vector(input_data; is_test=false)
5868
return df
5969
end
6070

71+
######### Plot Load Vectors #########
6172
load_vector_train = total_load_vector(input_data_train)
6273
load_vector_test = total_load_vector(input_data_test; is_test=true)
6374

@@ -79,4 +90,56 @@ p1 = plot(scatter(theta_train, norm_sim_train, proj = :polar, label="Train", col
7990
p2 = plot(scatter(theta_test, norm_sim_test, proj = :polar, label="Test", color=:orange));
8091
plot(p1, p2; title="Load Similarity", legend=:bottomright, layout = l)
8192

93+
######### Plot Objective Function #########
94+
# Plot objective function for a single direction
95+
96+
# Select two points in the extremes
97+
load_vector = total_load_vector(input_data)
98+
99+
nominal_loads = Vector(load_vector[1, Not(:id)])
100+
norm_nominal_loads = norm(nominal_loads)
101+
102+
load_vector.norm_loads = [norm(Vector(load_vector[i, Not(:id)])) for i in 1:size(load_vector, 1)] ./ norm_nominal_loads
103+
# join input and output data
104+
joined_data = innerjoin(load_vector, output_data[!, [:id, :operational_cost]], on=:id)
105+
106+
# get k extreme points
107+
using L2O
108+
using Gurobi
109+
function maxk(a, k)
110+
b = partialsortperm(a, 1:k, rev=true)
111+
return collect(zip(b, a[b]))
112+
end
113+
function mink(a, k)
114+
b = partialsortperm(a, 1:k, rev=false)
115+
return collect(zip(b, a[b]))
116+
end
117+
k = 10
118+
idx_maxs = maxk(joined_data.norm_loads, k)
119+
idx_mins = mink(joined_data.norm_loads, k)
120+
121+
# Objective function
122+
instances_in_convex_hulls = Array{DataFrame}(undef, k)
123+
for i in 1:k
124+
@info "Processing instance $i"
125+
instance_load_max = joined_data[idx_maxs[i][1]:idx_maxs[i][1], :]
126+
instance_load_min = joined_data[idx_mins[i][1]:idx_mins[i][1], :]
82127

128+
# find instances between the two points (i.e. in their convex hull)
129+
in_convex_hull = inconvexhull(
130+
Matrix(vcat(instance_load_max,
131+
instance_load_min)[!, Not([:id, :norm_loads, :operational_cost])]), Matrix(joined_data[!, Not([:id, :norm_loads, :operational_cost])]),
132+
Gurobi.Optimizer, silent=false, tol=0.1 # close to convex hull
133+
)
134+
135+
instances_in_convex_hulls[i] = sort(joined_data[in_convex_hull, [:operational_cost, :norm_loads]], :norm_loads)
136+
end
137+
138+
# Plot
139+
plotly()
140+
141+
plt = plot(color=:blue, legend=false, xlabel="Total Load (%)", ylabel="Operational Cost", title="AC OPF - IEEE300");
142+
for i in 1:k
143+
plot!(instances_in_convex_hulls[i][!, :norm_loads] * 100, instances_in_convex_hulls[i][!, :operational_cost], label="Direction $i", color=:red, alpha=0.4)
144+
end
145+
plt

src/inconvexhull.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
Check if new points are inside the convex hull of the given points. Solves a linear programming problem to check if the points are inside the convex hull.
66
"""
7-
function inconvexhull(training_set::Matrix{Float64}, test_set::Matrix{Float64}, solver; silent=true)
7+
function inconvexhull(training_set::Matrix{Float64}, test_set::Matrix{Float64}, solver; silent=true, tol=1e-4)
88
# Get the number of points and dimensions
99
n, d = size(training_set)
1010
m, d_ = size(test_set)
@@ -44,7 +44,7 @@ function inconvexhull(training_set::Matrix{Float64}, test_set::Matrix{Float64},
4444
optimize!(model)
4545

4646
# return if the points are inside the convex hull
47-
in_convex_hull[i] = isapprox(JuMP.objective_value(model), 0.0)
47+
in_convex_hull[i] = JuMP.objective_value(model) <= tol
4848
end
4949

5050
return in_convex_hull

0 commit comments

Comments
 (0)