def run(dataset, sensor_class, which_i, coeff_type, k_nn, epochs, folder, T_train, freq): # Create the folder in which the experiment results will be stored. exp_logger = ExperimentLogger(folder, locals()) # Load true load flow. V_org, I_org, P_org, Q_org, current_idx = load_true_data(dataset) T = V_org.shape[1] # Extract the list of which coefficients to estimate. which_i = utils.unpack_which_i(which_i, V_org.shape[0]) # Select the noise std that corresponds to the type of coefficient(s). std_abs = constants.SENSOR_STD_ABS[sensor_class] std_arg = constants.SENSOR_STD_ANG[sensor_class] # Transform voltage phasor measurements into either # (|x|, Re{x}, or Im{x}) based on `coeff_type`. X_org_a, X_org_b = utils.dependent_vars_setup(V_org, coeff_type) # Remove slack bus measurements and create delta matrices. dPQ_org = np.vstack((np.diff(P_org[1:], axis=1), np.diff(Q_org[1:], axis=1))) dX_org_a = np.diff(X_org_a[1:], axis=1) # Add noise to load flow data. V_meas, _, P_meas, Q_meas, _, _, _, _, _, _ = \ simulate_noisy_meas(sensor_class, V_org, I_org, current_idx) # Transform voltage phasor measurements into either # (|x|, Re{x}, or Im{x}) based on `coeff_type`. X_meas_a, X_meas_b = utils.dependent_vars_setup(V_meas, coeff_type) # Remove slack bus measurements and create delta matrices. dPQ_meas = np.vstack((np.diff(P_meas[1:], axis=1), np.diff(Q_meas[1:], axis=1))) dX_meas_a = np.diff(X_meas_a[1:], axis=1) # Load the true sensitivity coefficients of interest. coefficients = load_coefficients(dataset) S_a_true, S_b_true = utils.coefficients_setup(coefficients, coeff_type, which_i) del coefficients # Compute the true voltage magnitude deviations (from the load flow). dV_load_flow = np.diff(np.abs(V_org[1:]), axis=1) # T_plot = 200 t_nn = np.arange(T_train + k_nn, T) # which timesteps the NN estimates. t_plot = np.arange(T_train + k_nn, T, freq) # which timesteps should be plotted. t_nn_plot = np.arange(0, T - T_train - k_nn, freq) # the idx of the nn estimations that should be plotted X_train, X_test = dX_meas_a[:, :T_train], dX_meas_a[:, T_train:] PQ_train, PQ_test = dPQ_meas[:, :T_train], dPQ_meas[:, T_train:] fig_1, axs_1 = plt.subplots(len(which_i), 1, sharex=True, figsize=(10, 2.5*len(which_i))) fig_2, axs_2 = plt.subplots(len(which_i), 1, sharex=True, figsize=(10, 2.5*len(which_i))) for idx, x_i in enumerate(which_i): training_dataset = build_training_dataloader(X_train, PQ_train, x_i, k_nn) in_shape = (dX_meas_a.shape[0] + dPQ_meas.shape[0]) * k_nn hidden_shapes = [128, 128] sc_matrix_shape = (dPQ_meas.shape[0]) model = FeedForward(in_shape, hidden_shapes, sc_matrix_shape, k_nn) train_loss = nn.train(model, training_dataset, lr=1e-3, epochs=epochs, l2=0.) # Use the neural net to estimate the coefficients for the last 12 hours. testing_dataset = build_testing_dataloader(X_test, PQ_test, x_i, k_nn) S_a_nn, dV_nn, dV_nn_true = model.predict(testing_dataset) # Plot estimated coefficient. ax = axs_1[idx] ax.plot(t_plot, S_a_true[x_i][x_i - 1][t_plot], label='True') ax.plot(t_plot, S_a_nn[x_i - 1][t_nn_plot], label='Neural net') ax.legend(loc='upper right') # Plot predicted d|X|. ax = axs_2[idx] ax.plot(t_plot, dV_load_flow[x_i-1][t_plot], label='True') ax.plot(t_plot, dV_nn[t_nn_plot], label='Neural net') ax.legend(loc='upper right') exp_logger.save_figure(fig_1, 'coefficients') exp_logger.save_figure(fig_2, 'dV') plt.show() print('Done!')
for idx in range(6): nn_run += 1 print(f'\nnn run {nn_run}/{nn_runs}...') start_time = time.time() # Extract the correct parameters for this type of neural net. in_shape = in_shapes[idx] out_shape = out_shapes[idx] X_tr = X_matrices_tr[idx] PQ_tr = PQ_matrices_tr[idx] X_val = X_matrices_val[idx] PQ_val = PQ_matrices_val[idx] # Build training and validation sets. train_data = build_training_dataloader(X_tr, PQ_tr, x_i, k_nn) val_data = build_training_dataloader(X_val, PQ_val, x_i, k_nn) # Initialize and train the neural net. model = FeedForward(in_shape, h_shapes, out_shape, k_nn) model, val_loss = nn_utils.train(model, train_data, val_data, lr=1e-3, epochs=nn_epoch_max, l2=0.) # Keep the model if it's the best seen so far. if best_val_loss[idx] is None or val_loss < best_val_loss[idx]: best_val_loss[idx] = val_loss best_models[idx] = model print(f' time: {(time.time() - start_time) / 60:.2} min.') # Save the 6 neural nets. for idx, model in enumerate(best_models):
sc_matrix_shape = 2 * N model_dv_magn = FeedForward(in_shape, hidden_shapes, sc_matrix_shape, k_nn) model_dv_re = FeedForward(in_shape, hidden_shapes, sc_matrix_shape, k_nn) model_dv_im = FeedForward(in_shape, hidden_shapes, sc_matrix_shape, k_nn) in_shape = (3 * N + 1) * k_nn sc_matrix_shape = 2 * N + 1 model_v_magn = FeedForward(in_shape, hidden_shapes, sc_matrix_shape, k_nn) model_v_re = FeedForward(in_shape, hidden_shapes, sc_matrix_shape, k_nn) model_v_im = FeedForward(in_shape, hidden_shapes, sc_matrix_shape, k_nn) models = [model_dv_magn, model_dv_re, model_dv_im, model_v_magn, model_v_re, model_v_im] # Define training data sets. train_dv_magn = build_training_dataloader(dV_meas_magn_tr, dPQ_meas_tr, x_i, k_nn) train_dv_re = build_training_dataloader(dV_meas_re_tr, dPQ_meas_tr, x_i, k_nn) train_dv_im = build_training_dataloader(dV_meas_im_tr, dPQ_meas_tr, x_i, k_nn) train_v_magn = build_training_dataloader(V_meas_magn_tr, PQ_meas_bias_tr, x_i, k_nn) train_v_re = build_training_dataloader(V_meas_re_tr, PQ_meas_bias_tr, x_i, k_nn) train_v_im = build_training_dataloader(V_meas_im_tr, PQ_meas_bias_tr, x_i, k_nn) train_datasets = [train_dv_magn, train_dv_re, train_dv_im, train_v_magn, train_v_re, train_v_im] # Define validation data sets. val_dv_magn = build_testing_dataloader(dV_meas_magn_val, dPQ_meas_val, x_i, k_nn) val_dv_re = build_testing_dataloader(dV_meas_re_val, dPQ_meas_val, x_i, k_nn) val_dv_im = build_testing_dataloader(dV_meas_im_val, dPQ_meas_val, x_i, k_nn)
# Create measurement delta matrices used in Model 1. dV_meas_magn = np.diff(V_meas_magn, axis=1) dPQ_meas = np.diff(PQ_meas, axis=1) # Split training and testing data. dV_train, dPQ_train = dV_meas_magn[:, :T_train], dPQ_meas[:, :T_train] dV_test, dPQ_test = dV_meas_magn[:, T_train:], dPQ_meas[:, T_train:] T_test = T - T_train V_train, PQ_train = V_meas_magn[:, :T_train], PQ_meas_bias[:, :T_train] V_test, PQ_test = V_meas_magn[:, T_train:], PQ_meas_bias[:, T_train:] ####################### TRAIN NEURAL NETWORKS ####################### # Define training and testing data sets. train_dv = build_training_dataloader(dV_train, dPQ_train, x_i, k_nn) test_dv = build_testing_dataloader(dV_test, dPQ_test, x_i, k_nn) train_v = build_training_dataloader(V_train, PQ_train, x_i, k_nn) test_v = build_testing_dataloader(V_test, PQ_test, x_i, k_nn) # models_dv = [] models_v = [] for iter in range(N_nets): print(f'\nTraining model {iter}...') # # Initialize neural nets. # in_shape = 3 * N * k_nn # sc_matrix_shape = 2 * N # model_dv = FeedForward(in_shape, hidden_shapes, sc_matrix_shape, k_nn)