""" import sys sys.path.append('lib/') import numpy as np from matplotlib.backends.backend_pdf import PdfPages from python_utils import PickleTool from controller_evaluation import _simulate_scenarios from cdu_comparision_plots import _cdu_cl_comparision_plots # Simulate all the scenarios. (plants, controllers) = _simulate_scenarios(plant_name='cdu', controller_name='satdlqr', Nsim=2880) # Now make the plots here. cdu_parameters = PickleTool.load(filename='cdu_parameters.pickle', type='read') scenarios = cdu_parameters['online_test_scenarios'] plant_parameters = cdu_parameters['cdu_plant_parameters'] mpc_parameters = PickleTool.load(filename='cdu_mpc.pickle', type='read') (mpc_plants, mpc_controllers) = (mpc_parameters['plants'], mpc_parameters['controllers']) figures = _cdu_cl_comparision_plots(scenarios, mpc_plants, mpc_controllers, plants, controllers, 'satK', plant_parameters, (0, 2880)) # Plot. # Create the Pdf figure object and save.
# [depends] cstrs_parameters.pickle # [depends] %LIB%/python_utils.py # [makes] pickle """ Script to generate offline data for training the neural net. """ import sys sys.path.append('lib/') from python_utils import PickleTool # Load data and do an online simulation using the linear MPC controller. task_number = int(sys.argv[1]) - 1 cstrs_parameters = PickleTool.load(filename='cstrs_parameters.pickle', type='read') cstrs_parameters['offline_simulator'].generate_data( task_number=task_number, data_filename='cstrs_offline_data.h5py', stdout_filename=str(task_number) + '-cstrs_offline_data.txt')
def main(): """ Run these commands when this script is called.""" # Get the parameters and the plant object which has the closed-loop data # for the two controllers. (cdu_parameters, cdu_mpc, cdu_us, cdu_satdlqr, cdu_short_horizon, cdu_train, cdu_neural_network) = _load_data_for_plots(plant_name='cdu') # Optimal/NN plants/controllers. mpc_plants = cdu_mpc['plants'] mpc_controllers = cdu_mpc['controllers'] us_controllers = cdu_us['controllers'] satdlqr_controllers = cdu_satdlqr['controllers'] short_horizon_controllers = cdu_short_horizon['controllers'] nn_plants = cdu_neural_network['plants'] nn_controllers = cdu_neural_network['controllers'] performance_loss = cdu_neural_network['performance_loss'] # Number of architectures, NNs, scenarios. num_architectures = len(cdu_train['regulator_dims']) num_nns_per_architecture = len(cdu_train['num_samples']) num_scenarios = len(cdu_parameters['online_test_scenarios']) # Create lists to store the figures and the NN metrics. nn_labels = ['NN-3-1664', 'NN-3-1792', 'NN-3-1920', 'NN-3-2048'] cl_figures = [] scenarios = cdu_parameters['online_test_scenarios'] (best_nn_plants, best_nn_controllers) = _get_best_nn_plant_controllers(nn_plants, nn_controllers, performance_loss, num_architectures, num_nns_per_architecture, num_scenarios) for arch in range(num_architectures): # Get the NN plant amd controller object. fast_plants = [best_nn_plants.pop(0) for _ in range(num_scenarios)] fast_controllers = [best_nn_controllers.pop(0) for _ in range(num_scenarios)] # First make plots of the closed-loop trajectories. cl_figures += _cdu_cl_comparision_plots(scenarios, mpc_plants, mpc_controllers, fast_plants, fast_controllers, nn_labels[arch], cdu_parameters['cdu_plant_parameters'], (60, 1440)) # Make the plots for the closed loop metrics of NNs with # increasing data. performance_loss = cdu_neural_network['performance_loss'][:-1, ...] num_samples = cdu_train['num_samples'] cl_metric_figures = plot_nn_vs_num_samples_performance(performance_loss, num_samples, nn_labels, figure_size_metrics, right_frac=0.9, legend_title_location=(0.6, 0.5), ylabel_xcoordinate=-0.1, yaxis_log=False) # Plot the average stage cost in time. time = np.asarray(cdu_mpc['plants'][0].t[0:-1]).squeeze() nn_controllers = PickleTool.load(filename='cdu_neural_network.pickle', type='read')['controllers'] # Slight modification to not plot the last architecture. nn_plants = nn_plants[:(num_architectures-1)*len(num_samples)] nn_controllers = nn_controllers[:(num_architectures-1)*len(num_samples)] nn_labels = nn_labels[:-1] num_architectures -= 1 cl_ct_figures = plot_cl_performance_and_comp_times(time, mpc_controllers, us_controllers, satdlqr_controllers, short_horizon_controllers, nn_plants, nn_controllers, performance_loss, num_architectures, num_nns_per_architecture, cl_xlabel = 'Time (min)', cl_legend_labels = ['MPC', 'satK'] + nn_labels, cl_right_frac = 0.9, ct_right_frac = 0.9, ct_legend_labels = ['MPC'] + nn_labels, figure_size=figure_size_metrics, fig_cl_title_location=(0.35, 0.55), fig_ct_title_location='center', cl_us=False, cl_short_horizon=False, cl_yaxis_log=False, ylabel_xcoordinate=-0.1, num_bins=2000) # Stuff all the figures into one. figures = cl_figures + cl_metric_figures + cl_ct_figures with PdfPages('cdu_comparision_plots.pdf', 'w') as pdf_file: for fig in figures: pdf_file.savefig(fig)