def validPerturbatorInternalresults(): model_name = "BouncingBall" std_run_results = simu_run_info.SimulationResults(StringIO(bb_std_run_str), "BouncingBall", "/path/to/exe", "output") # Prepare "e" parameter run e_perturbed_param_info = simu_run_info.PerturbedParameterInfo( "e", 0.7, 0.735) e_run_results = simu_run_info.SimulationResults( StringIO(bb_e_perturbed_str), "BouncingBall", "/path/to/exe", "output") # Prepare "g" parameter run g_perturbed_param_info = simu_run_info.PerturbedParameterInfo( "g", 9.81, 10.3005) g_run_results = simu_run_info.SimulationResults( StringIO(bb_g_perturbed_str), "BouncingBall", "/path/to/exe", "output") return e_perturbed_param_info, e_run_results, g_perturbed_param_info, g_run_results, model_name, std_run_results
def runsPerturbedParameters(self, communicator=None): # Get the cartesian product of all possible values cart_prod_dict = dict_product(self.sweep_values_per_param) # Iterate all the combinations instantiating the "PerturbedParameterInfo" objects perturbed_parameters_infos = [] cart_prod_dict = list(cart_prod_dict) if communicator is not None: communicator.set_total_progress_messages(len(cart_prod_dict) + 1) for vals_comb in cart_prod_dict: if communicator is not None: communicator.update_completed(1) run_perturbed_params = [] for param_name in vals_comb: param_default_val = self.params_defaults[param_name] param_perturbed_val = vals_comb[param_name] perturbed_param_info = simu_run_info.PerturbedParameterInfo(param_name, param_default_val, param_perturbed_val) run_perturbed_params.append(perturbed_param_info) # Before adding this run, check if all params have been perturbed and it's not the std run param_is_default_list = [p.default_val == p.new_val for p in run_perturbed_params] if not all(param_is_default_list): # If at least one parameter has been perturbed, add it to the list perturbed_parameters_infos.append(run_perturbed_params) return perturbed_parameters_infos
def sweepResultsExample(self): # Generate dataframe df_std_run = pandas.read_csv(StringIO(bb_std_run_str), index_col=0) model_name = "BouncingBall" std_run = simu_run_info.SimulationResults(StringIO(bb_std_run_str), model_name, "/path/to/exe", "") # Simulate perturbations by multiplying variables perturbed_runs = [] for i in range(1, 9): df_perturbed_i = df_std_run.copy() df_perturbed_i["v"] = df_perturbed_i.apply(lambda row: row["v"] * (1 + i / 8), axis=1) run_output_name = "run_{0}.csv".format(i) run_output_path = os.path.join(self._temp_dir, run_output_name) df_perturbed_i.to_csv(run_output_path) # Pretend that e is always changed to 1 and g is swept in each run swept_param_info = simu_run_info.PerturbedParameterInfo("g", 0, i) swept_params_info_list = [swept_param_info] # The executable can be anything as we assume it has already been ran run_executable = "/path/to/exe" # The following object is for one one. We have to save sweep information alongside run information, so # for now both infos are saved together. In the future, they must be saved separately and one must # reference the other std_output = "" simu_results = simu_run_info.SimulationResults( run_output_path, model_name, run_executable, std_output) sweep_iteration_results = sweep.SweepIterationResults( simu_results, swept_params_info_list) perturbed_runs.append(sweep_iteration_results) sweep_params_swept = ["g"] sweep_params_fixed = [simu_run_info.PerturbedParameterInfo("e", 0, 1)] # The following object is the sweep in general. That is, for all runs sweep_specs = sweep.ParametersSweepResults(model_name, sweep_params_swept, sweep_params_fixed, std_run, perturbed_runs) # Var to analyze var_name = "h" # Returns sweep example objects return sweep_specs, var_name
def perturbationInfoForFixedParams(params_defaults, fixed_params_raw): fixed_params = [] # Iterate all the combinations instantiating the "PerturbedParameterInfo" objects for fixed_param_pert_dict in fixed_params_raw: #Disaggregate fixed param perturbation info param_name = fixed_param_pert_dict["name"] param_perturbed_val = fixed_param_pert_dict["value"] # Get default val param_default_val = params_defaults[param_name] # Initialize perturbation info object perturbed_param_info = simu_run_info.PerturbedParameterInfo(param_name, param_default_val, param_perturbed_val) fixed_params.append(perturbed_param_info) return fixed_params
def runSimulations(self, dest_folder_path): # Make folder for runs runs_folder_name = "runs" runs_folder_path = os.path.join(dest_folder_path, runs_folder_name) files_aux.makeFolderWithPath(runs_folder_path) # Run STD run std_run_name = "std_run.csv" std_run_path = os.path.join(runs_folder_path, std_run_name) flags = "-noEventEmit" std_run_results = self.compiled_model.simulate(std_run_path, optional_flags=flags) # Make dir for perturbed runs perturbed_runs_folder_name = "perturbed" perturbed_runs_folder_path = os.path.join(runs_folder_path, perturbed_runs_folder_name) files_aux.makeFolderWithPath(perturbed_runs_folder_path) # Run the simulations for each parameter perturbed in isolation runs_per_parameter = {} i = 0 for param_name in self.values_per_param: # Get param info for its run param_default_val = self.params_defaults[param_name] param_perturbed_val = self.values_per_param[param_name] # Perturb the parameter self.compiled_model.setParameterStartValue(param_name, param_perturbed_val) # Run the simulation simu_csv_name = "run_{0}.csv".format(i) simu_csv_path = os.path.join(perturbed_runs_folder_path, simu_csv_name) flags = "-noEventEmit" simu_results = self.compiled_model.simulate(simu_csv_path, optional_flags=flags) # Return the parameter to its original value self.compiled_model.setParameterStartValue(param_name, param_default_val) # Save the simulation results for this perturbed parameter perturbed_param_info = simu_run_info.PerturbedParameterInfo( param_name, param_default_val, param_perturbed_val) iter_results = OneParameterPerturbedResults( simu_results, perturbed_param_info) runs_per_parameter[param_name] = iter_results i = i + 1 # Prepare the results instance isolated_perturbations_results = IsolatedPerturbationsResults( self.model_name, std_run_results, runs_per_parameter) return isolated_perturbations_results