-
Notifications
You must be signed in to change notification settings - Fork 5
/
perfomance_evaluator.py
176 lines (133 loc) · 5.48 KB
/
perfomance_evaluator.py
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
import torch
from datasets.PowerFlowData import PowerFlowData
from networks.MPN import MPN, MPN_simplenet, MaskEmbdMultiMPN
from utils.custom_loss_functions import Masked_L2_loss
import time
from utils.argument_parser import argument_parser
from pygsp import graphs
import numpy as np
from networks.MLP import MLP
from networks.GCN import GCN
from collaborative_filtering import tikhonov_regularizer, collaborative_filtering_testing
"""
This script is used to evaluate the performance of various models on the power flow problem.
Models:
- MPN
- Tikhonov Regularizer
- MLP
- GCN
- Newton-Raphson method
"""
cases = ['case14', 'case118', 'case6470rte']
# cases = ['case6470rte']
for case in cases:
case_name = case.split("case")[1]
print(f'\n\nCase {case_name} is being evaluated...')
# Load testing data
testset = PowerFlowData(root="./data/", case=case_name+"v2",
split=[.5, .2, .3], task='test')
sample_number = 10
if sample_number > len(testset):
sample_number = len(testset)
print(f'Number of samples: {sample_number}')
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
eval_loss_fn = Masked_L2_loss(regularize=False)
# Load MPN model
model_path = "./models/testing/mpn_" + case_name + ".pt"
MPN_model = MaskEmbdMultiMPN(
nfeature_dim=6,
efeature_dim=5,
output_dim=6,
hidden_dim=129,
n_gnn_layers=4,
K=3,
dropout_rate=0.2
).to(device)
_to_load = torch.load(model_path, map_location=device)
MPN_model.load_state_dict(_to_load['model_state_dict'])
MPN_model.eval()
# Get loss of MPN model and execution time
timer_MPN = 0
loss_MPN = 0
for i, sample in enumerate(testset[:sample_number]):
time_start_gnn = time.time()
result = MPN_model(sample.to(device))
time_end_gnn = time.time()
loss_MPN += eval_loss_fn(result, sample.y.to(device),
sample.x[:, 10:].to(device)).item()
timer_MPN += time_end_gnn - time_start_gnn
print(f'Loss of MPN model: {loss_MPN/sample_number}')
print(f'Execution time of MPN model: {timer_MPN/sample_number}')
###### MLP ##########################
# Load MLP model
model_path = "./models/testing/mlp_" + case_name + ".pt"
num_inputs = testset[0].x.shape[0] * testset[0].x.shape[1]
num_outputs = testset[0].y.shape[0] * testset[0].y.shape[1]
model_MLP = MLP(num_inputs, num_outputs, 128, 3, 0.2).to(device)
_to_load = torch.load(model_path, map_location=device)
model_MLP.load_state_dict(_to_load['model_state_dict'])
model_MLP.eval()
# Get loss of MLP model and execution time
timer_MLP = 0
loss_MLP = 0
for i, sample in enumerate(testset[:sample_number]):
time_start = time.time()
result = model_MLP(sample.to(device))
time_end = time.time()
loss_MLP += eval_loss_fn(result, sample.y.to(device),
sample.x[:, 10:].to(device)).item()
timer_MLP += time_end - time_start
print(f'Loss of MLP model: {loss_MLP/sample_number}')
print(f'Execution time of MLP model: {timer_MLP/sample_number}')
###### GCN ##########################
# Load GCN model
model_path = "./models/testing/gcn_" + case_name + ".pt"
GCN_model = GCN(input_dim=16,
output_dim=6,
hidden_dim=129).to(device)
_to_load = torch.load(model_path, map_location=device)
GCN_model.load_state_dict(_to_load['model_state_dict'])
GCN_model.eval()
# Get loss of GCN model and execution time
timer_GCN = 0
loss_GCN = 0
for i, sample in enumerate(testset[:sample_number]):
time_start = time.time()
result = GCN_model(sample.to(device))
time_end = time.time()
loss_GCN += eval_loss_fn(result, sample.y.to(device),
sample.x[:, 10:].to(device)).item()
timer_GCN += time_end - time_start
print(f'Loss of GCN model: {loss_GCN/sample_number}')
print(f'Execution time of GCN model: {timer_GCN/sample_number}')
continue
###### Tikhonov Regularizer ##########################
# Load adjacency matrix from file
file_path = "./data/raw/case" + str(case_name)+"v2" + '_adjacency_matrix.npy'
adjacency_matrix = np.load(file_path)
# print(adjacency_matrix.shape)
num_of_nodes = adjacency_matrix.shape[0]
# print(f'Number of nodes: {num_of_nodes}')
# create graph from adjacency matrix
G = graphs.Graph(adjacency_matrix)
# get incidence matrix
G.compute_differential_operator()
B = G.D.toarray()
# print(f'B: {B.shape}')
# get laplacian matrix
L = G.L.toarray()
# print(f'Laplacian: {L.shape}')
timer_regularizer = 0
loss_MPN = 0
for i, sample in enumerate(testset[:sample_number]):
time_start = time.time()
result = tikhonov_regularizer(
1.25, L, sample.x[:, 4:8], sample.x[:, 10:].to(device))
# result = collaborative_filtering_testing(sample.x[:,4:8], sample.x[:, 10:14], B,sample.y[:,:4],4)
time_end = time.time()
loss_MPN += eval_loss_fn(result.to(device),
sample.y[:, :4].to(device), sample.x[:, 10:14].to(device)).item()
timer_regularizer += time_end - time_start
print(f'Loss of Tikhonov Regularizer: {loss_MPN/sample_number}')
print(
f'Execution time of Tikhonov Regularizer: {timer_regularizer/sample_number}')