From 5a07668ab48da23afc070c2681808b86de22a52e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 Jan 2025 19:50:48 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .gitignore | 2 +- headers_ignore_config.json | 116 +++++++++--------- platform_version_required.txt | 2 +- pytest.ini | 2 +- .../models/design_var/design_var.py | 2 +- .../models/design_var/design_var_disc.py | 3 +- .../documentation/design_var.markdown | 6 +- .../documentation/func_manager_disc.markdown | 12 +- .../models/func_manager/func_manager.py | 26 ++-- .../usecase.py | 2 +- .../process.py | 5 +- .../usecase.py | 1 - .../test_sellar_opt_w_func_manager/process.py | 2 + .../test_sellar_opt_w_func_manager/usecase.py | 4 +- .../process.py | 4 +- .../usecase.py | 1 - ...est_14_optim_scenario_with_func_manager.py | 4 +- .../tests/l0_test_44_func_manager.py | 4 +- .../tests/l0_test_62_design_var_in_sellar.py | 6 +- .../tests/l0_test_header.py | 10 +- .../l1_test_01_gradient_design_var_disc.py | 1 - ...1_test_gradient_sellar_optim_subprocess.py | 2 - .../tools/cst_manager/constraint_manager.py | 81 ++++++------ .../tools/cst_manager/fileutils.py | 3 - .../tools/cst_manager/func_manager_common.py | 34 ++--- 25 files changed, 166 insertions(+), 169 deletions(-) diff --git a/.gitignore b/.gitignore index 0938fd8..5c88521 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,4 @@ build/ dist/ /.pytest_cache/ /sostrades_optimization_plugins/tests/__pycache__/ -/sostrades_optimization_plugins/tests/.pytest_cache/ \ No newline at end of file +/sostrades_optimization_plugins/tests/.pytest_cache/ diff --git a/headers_ignore_config.json b/headers_ignore_config.json index 22ce4ec..875a09c 100644 --- a/headers_ignore_config.json +++ b/headers_ignore_config.json @@ -1,59 +1,59 @@ { - "extension_to_ignore": [ - "pkl", - "png", - "jpg", - "csv", - "md", - "markdown", - "avif", - "json", - "in", - "gitignore", - "cfg", - "puml", - "pdf", - "txt", - "ipynb", - "zip", - "rst", - "ini", - "coveragerc", - "yaml", - "bat", - "tex", - "toml" - ], - "files_to_ignore": [ - "LICENSE", - ".readthedocs", - "docs/Makefile", - ".flake8", - ".prettierignore", - "sostrades_optimization_plugins/models/design_var/design_var.py", - "sostrades_optimization_plugins/models/design_var/design_var_disc.py", - "sostrades_optimization_plugins/models/func_manager/func_manager.py", - "sostrades_optimization_plugins/models/func_manager/func_manager_disc.py", - "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var/__init__.py", - "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var/process.py", - "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var/usecase.py", - "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/__init__.py", - "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/process.py", - "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/usecase.py", - "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager_faulty/__init__.py", - "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager_faulty/process.py", - "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager_faulty/usecase.py", - "sostrades_optimization_plugins/tests/l0_test_14_optim_scenario_with_func_manager.py", - "sostrades_optimization_plugins/tests/l0_test_44_func_manager.py", - "sostrades_optimization_plugins/tests/l0_test_62_design_var_in_sellar.py", - "sostrades_optimization_plugins/tests/l1s_test_all_usecases.py", - "sostrades_optimization_plugins/tools/cst_manager/common_config.py", - "sostrades_optimization_plugins/tools/cst_manager/constraint_manager.py", - "sostrades_optimization_plugins/tools/cst_manager/constraint_object.py", - "sostrades_optimization_plugins/tools/cst_manager/database.py", - "sostrades_optimization_plugins/tools/cst_manager/fileutils.py", - "sostrades_optimization_plugins/tools/cst_manager/func_manager_common.py", - "sostrades_optimization_plugins/tools/cst_manager/__init__.py" - ], - "airbus_rev_commit": "d833fdf740389587b876e30f084631430687fa5a" -} \ No newline at end of file + "extension_to_ignore": [ + "pkl", + "png", + "jpg", + "csv", + "md", + "markdown", + "avif", + "json", + "in", + "gitignore", + "cfg", + "puml", + "pdf", + "txt", + "ipynb", + "zip", + "rst", + "ini", + "coveragerc", + "yaml", + "bat", + "tex", + "toml" + ], + "files_to_ignore": [ + "LICENSE", + ".readthedocs", + "docs/Makefile", + ".flake8", + ".prettierignore", + "sostrades_optimization_plugins/models/design_var/design_var.py", + "sostrades_optimization_plugins/models/design_var/design_var_disc.py", + "sostrades_optimization_plugins/models/func_manager/func_manager.py", + "sostrades_optimization_plugins/models/func_manager/func_manager_disc.py", + "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var/__init__.py", + "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var/process.py", + "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var/usecase.py", + "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/__init__.py", + "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/process.py", + "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/usecase.py", + "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager_faulty/__init__.py", + "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager_faulty/process.py", + "sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager_faulty/usecase.py", + "sostrades_optimization_plugins/tests/l0_test_14_optim_scenario_with_func_manager.py", + "sostrades_optimization_plugins/tests/l0_test_44_func_manager.py", + "sostrades_optimization_plugins/tests/l0_test_62_design_var_in_sellar.py", + "sostrades_optimization_plugins/tests/l1s_test_all_usecases.py", + "sostrades_optimization_plugins/tools/cst_manager/common_config.py", + "sostrades_optimization_plugins/tools/cst_manager/constraint_manager.py", + "sostrades_optimization_plugins/tools/cst_manager/constraint_object.py", + "sostrades_optimization_plugins/tools/cst_manager/database.py", + "sostrades_optimization_plugins/tools/cst_manager/fileutils.py", + "sostrades_optimization_plugins/tools/cst_manager/func_manager_common.py", + "sostrades_optimization_plugins/tools/cst_manager/__init__.py" + ], + "airbus_rev_commit": "d833fdf740389587b876e30f084631430687fa5a" +} diff --git a/platform_version_required.txt b/platform_version_required.txt index 8eb3891..d3845ad 100644 --- a/platform_version_required.txt +++ b/platform_version_required.txt @@ -1 +1 @@ -v5.0.0 \ No newline at end of file +v5.0.0 diff --git a/pytest.ini b/pytest.ini index 49b5731..15b2a7c 100644 --- a/pytest.ini +++ b/pytest.ini @@ -2,4 +2,4 @@ python_files = l*_test*.py testpaths = sostrades_optimization_plugins/tests -addopts = --numprocesses=auto \ No newline at end of file +addopts = --numprocesses=auto diff --git a/sostrades_optimization_plugins/models/design_var/design_var.py b/sostrades_optimization_plugins/models/design_var/design_var.py index 9db29a8..a94e768 100644 --- a/sostrades_optimization_plugins/models/design_var/design_var.py +++ b/sostrades_optimization_plugins/models/design_var/design_var.py @@ -145,7 +145,7 @@ def rebuild_input_array_with_activated_elements(self, inputs_dict, elem): return np.array(final_value), np.array(gradient) def update_gradient_with_deactivated_elements_first_value(self, initial_gradient): - ### TO DO generalize it with other deactivated elements + # TO DO generalize it with other deactivated elements result = np.delete(initial_gradient, 0, axis=1) return result diff --git a/sostrades_optimization_plugins/models/design_var/design_var_disc.py b/sostrades_optimization_plugins/models/design_var/design_var_disc.py index 66191a1..bcc598d 100644 --- a/sostrades_optimization_plugins/models/design_var/design_var_disc.py +++ b/sostrades_optimization_plugins/models/design_var/design_var_disc.py @@ -170,9 +170,8 @@ def run(self): # update output dictionary with dspace outputs_dict.update({'design_space_last_ite': dspace_out}) - if self.get_sosdisc_inputs(self.WRITE_XVECT): - #dspace_out.to_csv(f"dspace_ite_{self.iter}.csv", index=False) + # dspace_out.to_csv(f"dspace_ite_{self.iter}.csv", index=False) # write all iterations into a dictionnary self.all_iterations_dict.update({f"iteration {self.iter}": dict_current}) diff --git a/sostrades_optimization_plugins/models/design_var/documentation/design_var.markdown b/sostrades_optimization_plugins/models/design_var/documentation/design_var.markdown index ef5c497..23ad51b 100644 --- a/sostrades_optimization_plugins/models/design_var/documentation/design_var.markdown +++ b/sostrades_optimization_plugins/models/design_var/documentation/design_var.markdown @@ -1,4 +1,4 @@ -# Documentation +# Documentation The design variable discipline handles the variables that are used to drive the optimization. @@ -9,5 +9,5 @@ A b-spline function is a continuous composite function, that creates smooth curv The optimizer has control over the poles, and moves them (on the y-axis) to modify the B-Spline function. Then the value of the design variables for each year are calculated using the B-Spline function. - -![](BSpline_example.PNG) \ No newline at end of file + +![](BSpline_example.PNG) diff --git a/sostrades_optimization_plugins/models/func_manager/documentation/func_manager_disc.markdown b/sostrades_optimization_plugins/models/func_manager/documentation/func_manager_disc.markdown index c89c7a9..420bed3 100644 --- a/sostrades_optimization_plugins/models/func_manager/documentation/func_manager_disc.markdown +++ b/sostrades_optimization_plugins/models/func_manager/documentation/func_manager_disc.markdown @@ -1,11 +1,11 @@ ## Documentation Function manager is a model that aggregates weighted objectives, inequality and equality constraints using smoothing functions. -It takes both dataframes/arrays and floats as objectives/constraints. +It takes both dataframes/arrays and floats as objectives/constraints. ![](scheme_funcmanager.png) -### Scalarize functions +### Scalarize functions Objectives, inequality constraints and equality constraints functions enter the function manager with a numpy array containing a single (objective) or multiple (constraints) values. The first step towards the aggregation of these functions is to merge these numpy array into a single scalar value. @@ -15,8 +15,8 @@ Each type of function is treated individually at first: -objectives: the values are either summed or the smooth_maximum function is applied on value, depending on the 'aggr_type' variable of the function. -equality and inequality constraints: - the array goes through a function 'cst_func_smooth_positive' to scale the values between 0 and +inf, then the smooth_maximum value is returned. - + the array goes through a function 'cst_func_smooth_positive' to scale the values between 0 and +inf, then the smooth_maximum value is returned. + The scalar value of each function is then multiplied by the weight associated with the function #### Function 'smooth\_maximum' @@ -45,7 +45,7 @@ This function loops on the numpy array and apply one of four functions on each e - if self.smooth_log and val.real > self.eps2: res = res0 + 2 * np.log(val) The four parts of this function are designed to ensure a smooth continuous function, with a quadratic increase above 'eps'. -If 'smoothlog' is set to True, the function is capped with a log increase above eps2, to avoid some numeric issues with constraints +If 'smoothlog' is set to True, the function is capped with a log increase above eps2, to avoid some numeric issues with constraints values going haywire. !['Continuous positive smoothing function'](residuals_wo_smoothlog.png) @@ -57,5 +57,3 @@ It is calculated as the sum of the three aggregated functions multiplied by 100. [^1]: [Martins, J. R. R. A., and Nicholas MK Poon. "On structural optimization using constraint aggregation." VI World Congress on Structural and Multidisciplinary Optimization WCSMO6, Rio de Janeiro, Brasil. 2005.](https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.140.3612&rep=rep1&type=pdf) - - diff --git a/sostrades_optimization_plugins/models/func_manager/func_manager.py b/sostrades_optimization_plugins/models/func_manager/func_manager.py index 64198a9..ff80fe1 100644 --- a/sostrades_optimization_plugins/models/func_manager/func_manager.py +++ b/sostrades_optimization_plugins/models/func_manager/func_manager.py @@ -63,7 +63,7 @@ def __init__(self): self.POS_FTYPE = [self.OBJECTIVE, self.INEQ_CONSTRAINT, self.EQ_CONSTRAINT] self.reinit() - #-- We could use it to log? + # -- We could use it to log? def reinit(self): """ @@ -129,13 +129,13 @@ def scalarize_all_functions(self, eps=1e-3, alpha=3): dict_mod_func = {} dict_mod_func[self.FTYPE] = self.functions[tag][self.FTYPE] dict_mod_func[self.AGGR] = self.functions[tag][self.AGGR] - #-- All values are an np array even single values - #-- Weights are applied here to allow sign modification + # -- All values are an np array even single values + # -- Weights are applied here to allow sign modification values = weight * self.functions[tag][self.VALUE] aggr_type = dict_mod_func[self.AGGR] if self.functions[tag][self.FTYPE] == self.OBJECTIVE: - #-- smooth maximum of values return the value if it was a float - #-- return smooth maximum if objective was an array + # -- smooth maximum of values return the value if it was a float + # -- return smooth maximum if objective was an array if aggr_type == self.AGGR_TYPE_SMAX: res = smooth_maximum(values, alpha) elif aggr_type == self.AGGR_TYPE_SUM: @@ -145,7 +145,7 @@ def scalarize_all_functions(self, eps=1e-3, alpha=3): else: raise Exception(f"Unhandled aggr_type {aggr_type}") elif self.functions[tag][self.FTYPE] == self.INEQ_CONSTRAINT: - #-- scale between (0., +inf) and take smooth maximum + # -- scale between (0., +inf) and take smooth maximum if aggr_type == self.INEQ_NEGATIVE_WHEN_SATIFIED: res = keep_positive_only(values) elif aggr_type == self.INEQ_POSITIVE_WHEN_SATIFIED: @@ -189,12 +189,12 @@ def build_aggregated_functions(self, eps=1e-3, alpha=3): elif self.mod_functions[tag][self.FTYPE] == self.EQ_CONSTRAINT: all_mod_eq_cst.append(self.mod_functions[tag]) - #-- Objective aggregation: sum all the objectives + # -- Objective aggregation: sum all the objectives self.aggregated_functions[self.OBJECTIVE] = 0. for obj_dict in all_mod_obj: self.aggregated_functions[self.OBJECTIVE] += obj_dict[self.VALUE] - #-- Inequality constraint aggregation: takes the smooth maximum + # -- Inequality constraint aggregation: takes the smooth maximum ineq_cst_val = [ineq_dict[self.VALUE] for ineq_dict in all_mod_ineq_cst] ineq_cst_val = np.array(ineq_cst_val) if len(ineq_cst_val) > 0: @@ -207,7 +207,7 @@ def build_aggregated_functions(self, eps=1e-3, alpha=3): else: self.aggregated_functions[self.INEQ_CONSTRAINT] = 0. - #-- Equality constraint aggregation: takes the smooth maximum + # -- Equality constraint aggregation: takes the smooth maximum eq_cst_val = [eq_dict[self.VALUE] for eq_dict in all_mod_eq_cst] eq_cst_val = np.array(eq_cst_val) if len(eq_cst_val) > 0: @@ -219,7 +219,7 @@ def build_aggregated_functions(self, eps=1e-3, alpha=3): else: self.aggregated_functions[self.EQ_CONSTRAINT] = 0. - #--- Lagrangian objective calculation: sum the aggregated objective and constraints * 100. + # --- Lagrangian objective calculation: sum the aggregated objective and constraints * 100. self.mod_obj = 0. self.mod_obj += self.aggregated_functions[self.OBJECTIVE] self.mod_obj += self.aggregated_functions[self.INEQ_CONSTRAINT] @@ -279,7 +279,7 @@ def cst_func_eq_lintoquad(self, values, eps=1e-3, tag='cst'): cst_result = np.zeros_like(values) for iii, val in enumerate(values): if val.real > eps: - #if val > eps: quadratic + # if val > eps: quadratic res0 = eps * (np.exp(eps) - 1.) res = res0 + val ** 2 - eps ** 2 elif -eps < val.real < 0: @@ -287,7 +287,7 @@ def cst_func_eq_lintoquad(self, values, eps=1e-3, tag='cst'): res = eps * (np.exp(-val) - 1.) elif val.real < -eps: res0 = eps * (np.exp(eps) - 1.) - res= res0 + (-val) - eps + res = res0 + (-val) - eps else: # if 0 < val < eps: linear res = eps * (np.exp(val) - 1.) @@ -389,7 +389,7 @@ def scale_function(val, val_range): :return: scaled function with 0 corresponding to ideal value and 1 to anti-ideal """ # TODO: consider using a positive interval and adding a maximisation flag in the sake of clarity - return np.array([(val - val_range[0]) / (val_range[1] - val_range[0])]).reshape((-1,)) # NB: funcmanager demands arrays of shape (N, ) + return np.array([(val - val_range[0]) / (val_range[1] - val_range[0])]).reshape((-1,)) # NB: funcmanager demands arrays of shape (N, ) @staticmethod def scale_function_derivative(val_range) -> float: diff --git a/sostrades_optimization_plugins/sos_processes/test/test_sellar_coupling_for_design_var/usecase.py b/sostrades_optimization_plugins/sos_processes/test/test_sellar_coupling_for_design_var/usecase.py index 8a7217a..ed71380 100644 --- a/sostrades_optimization_plugins/sos_processes/test/test_sellar_coupling_for_design_var/usecase.py +++ b/sostrades_optimization_plugins/sos_processes/test/test_sellar_coupling_for_design_var/usecase.py @@ -29,7 +29,7 @@ def setup_usecase(self): disc_dict = {} # Sellar inputs - disc_dict[f'{ns}.{coupling_name}.x'] = pd.DataFrame(data={'index': [0,1,2,3], 'value' : [1., 1., 1., 1.]}) + disc_dict[f'{ns}.{coupling_name}.x'] = pd.DataFrame(data={'index': [0, 1, 2, 3], 'value': [1., 1., 1., 1.]}) disc_dict[f'{ns}.{coupling_name}.y_1'] = 1. disc_dict[f'{ns}.{coupling_name}.y_2'] = 1. disc_dict[f'{ns}.{coupling_name}.z'] = array([5., 2.]) diff --git a/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var_sub/process.py b/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var_sub/process.py index 2bc401f..d111768 100644 --- a/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var_sub/process.py +++ b/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var_sub/process.py @@ -49,8 +49,8 @@ def get_builders(self): } - ns_dict = {'ns_optim': self.ee.study_name , - 'ns_OptimSellar': self.ee.study_name , + ns_dict = {'ns_optim': self.ee.study_name, + 'ns_OptimSellar': self.ee.study_name, 'ns_functions': self.ee.study_name } @@ -61,5 +61,4 @@ def get_builders(self): "SellarCoupling") coupling_builder.set_builder_info('cls_builder', builder_list) - return coupling_builder diff --git a/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var_sub/usecase.py b/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var_sub/usecase.py index 3ddcd0b..52f7ac1 100644 --- a/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var_sub/usecase.py +++ b/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_design_var_sub/usecase.py @@ -70,7 +70,6 @@ def setup_usecase(self): disc_dict[ f'{ns}.{self.coupling_name}.DesignVar.design_var_descriptor'] = design_var_descriptor - # Sellar and design var inputs disc_dict[f'{ns}.x_in'] = array([1., 1., 1., 1.]) disc_dict[f'{ns}.y_1'] = 5. diff --git a/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/process.py b/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/process.py index ad80beb..aee25d0 100644 --- a/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/process.py +++ b/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/process.py @@ -21,6 +21,7 @@ Generate an optimization scenario """ + class ProcessBuilder(BaseProcessBuilder): # ontology information @@ -30,6 +31,7 @@ class ProcessBuilder(BaseProcessBuilder): 'category': '', 'version': '', } + def get_builders(self): ''' default initialisation test diff --git a/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/usecase.py b/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/usecase.py index eb80ac7..dc9e92a 100644 --- a/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/usecase.py +++ b/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager/usecase.py @@ -65,8 +65,8 @@ def setup_usecase(self): # f'{ns}.{self.optim_name}.c_1', f'{ns}.{self.optim_name}.c_2'] disc_dict[f'{ns}.{self.optim_name}.algo_options'] = { - #"maxls": 6, - #"maxcor": 3, + # "maxls": 6, + # "maxcor": 3, "ftol_rel": 1e-15, } diff --git a/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager_faulty/process.py b/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager_faulty/process.py index cac6330..01fb0fc 100644 --- a/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager_faulty/process.py +++ b/sostrades_optimization_plugins/sos_processes/test/test_sellar_opt_w_func_manager_faulty/process.py @@ -21,6 +21,7 @@ Generate an optimization scenario """ + class ProcessBuilder(BaseProcessBuilder): # ontology information @@ -30,6 +31,7 @@ class ProcessBuilder(BaseProcessBuilder): 'category': '', 'version': '', } + def get_builders(self): ''' default initialisation test @@ -50,7 +52,7 @@ def get_builders(self): builder_list = self.create_builder_list(mods_dict, ns_dict=ns_dict) coupling_builder = self.ee.factory.create_builder_coupling("SellarCoupling") coupling_builder.set_builder_info('cls_builder', builder_list) - #coupling_builder.set_builder_info('with_data_io', True) + # coupling_builder.set_builder_info('with_data_io', True) opt_builder = self.ee.factory.create_optim_builder( 'SellarOptimScenario', [coupling_builder]) diff --git a/sostrades_optimization_plugins/sos_processes/test/test_sellar_sub_opt_w_design_var/usecase.py b/sostrades_optimization_plugins/sos_processes/test/test_sellar_sub_opt_w_design_var/usecase.py index c71e57d..dc896cb 100644 --- a/sostrades_optimization_plugins/sos_processes/test/test_sellar_sub_opt_w_design_var/usecase.py +++ b/sostrades_optimization_plugins/sos_processes/test/test_sellar_sub_opt_w_design_var/usecase.py @@ -71,7 +71,6 @@ def setup_usecase(self): f'{ns}.Sellar.DesignVar.design_var_descriptor'] = design_var_descriptor disc_dict[f'{ns}.Sellar.{self.optim_name}.design_space'] = dspace - # Sellar and design var inputs disc_dict[f'{ns}.Sellar.SellarOptimScenario.x_in'] = array([1., 1., 1., 1.]) disc_dict[f'{ns}.Sellar.{self.optim_name}.y_1'] = 5. diff --git a/sostrades_optimization_plugins/tests/l0_test_14_optim_scenario_with_func_manager.py b/sostrades_optimization_plugins/tests/l0_test_14_optim_scenario_with_func_manager.py index 94364c7..92f2634 100644 --- a/sostrades_optimization_plugins/tests/l0_test_14_optim_scenario_with_func_manager.py +++ b/sostrades_optimization_plugins/tests/l0_test_14_optim_scenario_with_func_manager.py @@ -64,10 +64,10 @@ def _test_18_optim_scenario_optim_algo_projected_gradient_func_manager(self): filters = proxy_optim.get_chart_filter_list() graph_list = proxy_optim.get_post_processing_list(filters) for graph in graph_list: - #graph.to_plotly().show() + # graph.to_plotly().show() pass + if '__main__' == __name__: cls = TestSoSOptimScenarioWithFuncManager() cls.setUp() - diff --git a/sostrades_optimization_plugins/tests/l0_test_44_func_manager.py b/sostrades_optimization_plugins/tests/l0_test_44_func_manager.py index eb48d0b..2ef59b2 100644 --- a/sostrades_optimization_plugins/tests/l0_test_44_func_manager.py +++ b/sostrades_optimization_plugins/tests/l0_test_44_func_manager.py @@ -283,7 +283,7 @@ def test_07_jacobian_func_manager_disc(self): # -- ~GUI inputs: selection of functions func_df = pd.DataFrame(columns=['variable', 'ftype', 'weight']) - func_df['variable'] = ['cst0','cst1', 'cst2', 'cst3', + func_df['variable'] = ['cst0', 'cst1', 'cst2', 'cst3', 'eqcst1', 'eqcst2', 'obj1', 'obj2'] func_df['ftype'] = [INEQ_CONSTRAINT, INEQ_CONSTRAINT, INEQ_CONSTRAINT, INEQ_CONSTRAINT, EQ_CONSTRAINT, EQ_CONSTRAINT, OBJECTIVE, OBJECTIVE] @@ -292,7 +292,6 @@ def test_07_jacobian_func_manager_disc(self): func_df['parent'] = 'obj' func_df['namespace'] = '' - values_dict = {} values_dict[prefix + FunctionManagerDisc.FUNC_DF] = func_df @@ -764,7 +763,6 @@ def test_13_jacobian_func_manager_disc_ineq_constraint_negative_when_satisfied(s func_df['parent'] = 'obj' func_df['namespace'] = '' - values_dict = {} values_dict[prefix + FunctionManagerDisc.FUNC_DF] = func_df diff --git a/sostrades_optimization_plugins/tests/l0_test_62_design_var_in_sellar.py b/sostrades_optimization_plugins/tests/l0_test_62_design_var_in_sellar.py index d6a533b..2ac11db 100644 --- a/sostrades_optimization_plugins/tests/l0_test_62_design_var_in_sellar.py +++ b/sostrades_optimization_plugins/tests/l0_test_62_design_var_in_sellar.py @@ -45,7 +45,6 @@ class TestDesignVar(AbstractJacobianUnittest): def analytic_grad_entry(self): return [] - def setUp(self): self.study_name = 'Test' @@ -82,7 +81,6 @@ def setUp(self): self.repo = 'sostrades_optimization_plugins.sos_processes.test' self.proc_name = 'test_sellar_opt_w_design_var' - self.ee = ExecutionEngine(self.study_name) factory = self.ee.factory @@ -134,7 +132,7 @@ def setUp(self): # load and run self.ee.load_study_from_input_dict(values_dict) - self.values_dict=values_dict + self.values_dict = values_dict def test_01_check_execute(self): print("\n Test 1 : check configure and treeview") @@ -165,8 +163,6 @@ def test_derivative(self): f'{self.ns}.{self.sc_name}.z', ] - self.check_jacobian(local_data=self.values_dict, location=dirname(__file__), filename='jacobian_design_var_bspline.pkl', discipline=disc.discipline_wrapp.discipline, step=1e-15, inputs=input_names, outputs=output_names, derr_approx='complex_step') - diff --git a/sostrades_optimization_plugins/tests/l0_test_header.py b/sostrades_optimization_plugins/tests/l0_test_header.py index bc916c2..b0dd87f 100644 --- a/sostrades_optimization_plugins/tests/l0_test_header.py +++ b/sostrades_optimization_plugins/tests/l0_test_header.py @@ -34,18 +34,16 @@ def setUp(self): ''' self.pp = pprint.PrettyPrinter(indent=4, compact=True) - with open(os.path.join(os.path.dirname(__file__),"..","..","headers_ignore_config.json"),"r",encoding="utf-8") as f: + with open(os.path.join(os.path.dirname(__file__), "..", "..", "headers_ignore_config.json"), "r", encoding="utf-8") as f: - headers_ignore_config=json.load(f) + headers_ignore_config = json.load(f) self.extension_to_ignore = headers_ignore_config["extension_to_ignore"] - #Add here the files to ignore + # Add here the files to ignore self.files_to_ignore = headers_ignore_config["files_to_ignore"] - #commit from where to compare added, modeified deleted ... + # commit from where to compare added, modeified deleted ... self.airbus_rev_commit = headers_ignore_config["airbus_rev_commit"] - - def test_Headers(self): ht = HeaderTools() ht.check_headers(self.extension_to_ignore, self.files_to_ignore, self.airbus_rev_commit) diff --git a/sostrades_optimization_plugins/tests/l1_test_01_gradient_design_var_disc.py b/sostrades_optimization_plugins/tests/l1_test_01_gradient_design_var_disc.py index d128f3c..b1e834a 100644 --- a/sostrades_optimization_plugins/tests/l1_test_01_gradient_design_var_disc.py +++ b/sostrades_optimization_plugins/tests/l1_test_01_gradient_design_var_disc.py @@ -33,7 +33,6 @@ class GradiantAssetDiscTestCase(AbstractJacobianUnittest): """ np.random.seed(42) - def analytic_grad_entry(self): return [] diff --git a/sostrades_optimization_plugins/tests/l1_test_gradient_sellar_optim_subprocess.py b/sostrades_optimization_plugins/tests/l1_test_gradient_sellar_optim_subprocess.py index b67e611..39475dd 100644 --- a/sostrades_optimization_plugins/tests/l1_test_gradient_sellar_optim_subprocess.py +++ b/sostrades_optimization_plugins/tests/l1_test_gradient_sellar_optim_subprocess.py @@ -52,7 +52,6 @@ class SellarOptimSubprocessJacobianDiscTest(AbstractJacobianUnittest): - test_11_gradient_subprocess_wo_dvar_fmanager_flatten_local_data : Test Sellar without design var nor func manager """ - def analytic_grad_entry(self): return [] @@ -430,7 +429,6 @@ def test_08_gradient_subprocess_flatten_local_data_deepcopy_high_step(self): inputs=inputs, outputs=outputs) - def execute_sellar(self): """ Function to execute sellar with a newly created exec engine diff --git a/sostrades_optimization_plugins/tools/cst_manager/constraint_manager.py b/sostrades_optimization_plugins/tools/cst_manager/constraint_manager.py index fd68e80..317db3f 100644 --- a/sostrades_optimization_plugins/tools/cst_manager/constraint_manager.py +++ b/sostrades_optimization_plugins/tools/cst_manager/constraint_manager.py @@ -33,6 +33,7 @@ # pylint: disable=no-value-for-parameter + class ConstraintManager: """ Class to manage constraints for mission/vehicle design @@ -128,6 +129,7 @@ def export_to_csv(self, filename): with open(filename, 'w') as file: file.write(strg) + def cst_func_hard_max(values): """ Function which return the max of values @@ -155,9 +157,10 @@ def smooth_maximum(cst, alpha=3): return result + def compute_delta_type(delta, type='abs'): if type == 'abs': - cdelta=np.sqrt(compute_func_with_exp_min(delta ** 2, 1e-15)) + cdelta = np.sqrt(compute_func_with_exp_min(delta ** 2, 1e-15)) elif type == 'hardmax': cdelta = -np.sign(delta) * np.sqrt(compute_func_with_exp_min(delta ** 2, 1e-15)) elif type == 'hardmin': @@ -168,10 +171,11 @@ def compute_delta_type(delta, type='abs'): raise Exception('Unknown type of delta_type') return cdelta + def compute_dcdelta_dvalue(delta, ddelta_dvalue, type='abs'): if type == 'abs': - dcdelta_dvalue = 2 * delta * ddelta_dvalue * compute_dfunc_with_exp_min(delta ** 2, 1e-15)/( - 2*np.sqrt(compute_func_with_exp_min(delta ** 2, 1e-15))) + dcdelta_dvalue = 2 * delta * ddelta_dvalue * compute_dfunc_with_exp_min(delta ** 2, 1e-15) / ( + 2 * np.sqrt(compute_func_with_exp_min(delta ** 2, 1e-15))) elif type == 'hardmax': dcdelta_dvalue = -np.sign(delta) * 2 * delta * ddelta_dvalue * compute_dfunc_with_exp_min(delta ** 2, 1e-15) / ( 2 * np.sqrt(compute_func_with_exp_min(delta ** 2, 1e-15))) @@ -184,23 +188,25 @@ def compute_dcdelta_dvalue(delta, ddelta_dvalue, type='abs'): raise Exception('Unknown type of delta_type') return dcdelta_dvalue + def compute_delta_constraint(value, goal, tolerable_delta=1.0, delta_type='abs', reference_value=1.0, eps=1E-3): # Transform inputs into arrays if needed - length=1 + length = 1 if isinstance(value, list): length = len(value) else: - value=np.array(value) + value = np.array(value) if not isinstance(goal, list): - goal = np.ones(length)*goal + goal = np.ones(length) * goal if not isinstance(tolerable_delta, list): - tolerable_delta = np.ones(length)*tolerable_delta + tolerable_delta = np.ones(length) * tolerable_delta delta = (goal - value) cdelta = compute_delta_type(delta, delta_type) constraint = ((tolerable_delta - cdelta) / reference_value - eps) return constraint + def compute_ddelta_constraint(value, goal, tolerable_delta=1.0, delta_type='abs', reference_value=1.0, eps=1E-3): # Transform inputs into arrays if needed length = 1 @@ -213,7 +219,7 @@ def compute_ddelta_constraint(value, goal, tolerable_delta=1.0, delta_type='abs' if not isinstance(tolerable_delta, list): tolerable_delta = np.ones(length) * tolerable_delta - #First step + # First step delta = (goal - value) ddelta_dvalue = -np.identity(len(value)) ddelta_dgoal = np.identity(len(goal)) @@ -231,60 +237,62 @@ def compute_ddelta_constraint(value, goal, tolerable_delta=1.0, delta_type='abs' return ddelta_constraint_dvalue, ddelta_constraint_dgoal, ddelta_constraint_dtolerable_delta + def delta_constraint_demonstrator(): """ Function to plot an example of the delta constraint and its treatment through the function manager """ tolerable_delta = 1.0 - eps=1E-3 + eps = 1E-3 x = np.linspace(-2.0, 2.0, 100) y = x goal = np.zeros(len(x)) delta = (goal - y) min_valid_x, max_valid_x = x[-1], x[0] for i, val in enumerate(delta): - if np.abs(val)709: + cst_array_limited += np.imag(cst_array) * 1j + if np.amax(abs(cst_array_limited)) * k > 709: raise ValueError('The absolute value of k*max(cst_array) is too high and would cause a floating point error') - result = np.log(np.sum(np.exp(k*cst_array_limited), axis=1))/k + result = np.log(np.sum(np.exp(k * cst_array_limited), axis=1)) / k return result + def get_dsoft_maximum_vect(cst, k=7e2): """ Return derivative of soft maximum """ cst_array = np.array(cst) - cst_array_limited = np.sign(cst_array)*compute_func_with_exp_min(np.abs(cst_array), 1.0E-15/k) + cst_array_limited = np.sign(cst_array) * compute_func_with_exp_min(np.abs(cst_array), 1.0E-15 / k) d_cst_array = np.ones(cst_array.shape) d_cst_array_limited = d_cst_array * \ - compute_dfunc_with_exp_min(np.abs(cst_array), 1.0E-15/k) - d_exp = k*d_cst_array_limited*np.exp(k*cst_array_limited) + compute_dfunc_with_exp_min(np.abs(cst_array), 1.0E-15 / k) + d_exp = k * d_cst_array_limited * np.exp(k * cst_array_limited) d_sum = d_exp - d_log = (1/k) * (d_sum / np.sum(np.exp(k*cst_array_limited), axis=1).reshape(cst_array_limited.shape[0],1)) - d_log = np.where(d_log>1E-20, d_log, 0.0) + d_log = (1 / k) * (d_sum / np.sum(np.exp(k * cst_array_limited), axis=1).reshape(cst_array_limited.shape[0], 1)) + d_log = np.where(d_log > 1E-20, d_log, 0.0) return d_log + def cons_smooth_maximum_vect(cst, alpha=1E16): """ Conservative smooth maximum function. @@ -217,6 +220,7 @@ def cons_smooth_maximum_vect(cst, alpha=1E16): return result + def get_dcons_smooth_dvariable_vect(cst, alpha=1E16): cst_array = np.array(cst) max_exp = 650.0 # max value for exponent input, higher value gives infinity @@ -266,14 +270,14 @@ def get_dcons_smooth_dvariable_vect(cst, alpha=1E16): return grad_value -def pseudo_abs_obj(value: np.ndarray, eps= 1e-2): +def pseudo_abs_obj(value: np.ndarray, eps=1e-2): """compute the pseudo absolute value of x, where x is a 1dimensional array""" return np.array([np.sum(np.sqrt(value ** 2 + eps ** 2) - eps)]) / len(value) def d_pseudo_abs_obj(value, d_value, eps=1e-2): """compute the derivative of the pseudo absolute value of x, where x is a 1dimensional array""" - return d_value.T @ (value / np.sqrt(value ** 2 + eps ** 2)) / len(value) + return d_value.T @ (value / np.sqrt(value ** 2 + eps ** 2)) / len(value) def keep_positive_only(value: np.ndarray): @@ -312,9 +316,9 @@ def keep_negative_only_square(value: np.ndarray): def derivative_d_keep_positive_only_square(value: np.ndarray): """values must be a 1dimensional np array""" - return ((value > 0) * 1) * value * 2/ len(value) + return ((value > 0) * 1) * value * 2 / len(value) def derivative_d_keep_negative_only_square(value: np.ndarray): """values must be a 1dimensional np array""" - return ((value < 0) * 1) * value * 2/ len(value) + return ((value < 0) * 1) * value * 2 / len(value)