diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 05cdaaa..b54c475 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,7 +36,7 @@ jobs: run: | python -m pip install --upgrade pip setuptools pip install -r requirements-tests.txt - pip install -e .[opt] + pip install -e . - name: Test with pytest run: pytest adsg_core --durations=0 diff --git a/.github/workflows/tests_slow.yml b/.github/workflows/tests_slow.yml index 4e59257..be01026 100644 --- a/.github/workflows/tests_slow.yml +++ b/.github/workflows/tests_slow.yml @@ -33,7 +33,7 @@ jobs: run: | python -m pip install --upgrade pip setuptools pip install -r requirements-tests.txt - pip install -e .[opt,nb] + pip install -e .[nb] ipython kernel install --name "python3" --user - name: List dependencies diff --git a/README.md b/README.md index d89fb45..a5f75fc 100644 --- a/README.md +++ b/README.md @@ -46,9 +46,9 @@ conda install numpy scipy~=1.9 pip install adsg-core ``` -Optionally also install optimization algorithms ([SBArchOpt](sbarchopt.readthedocs.io/)): +Ensure optimization algorithms are installed ([SBArchOpt](sbarchopt.readthedocs.io/)): ``` -pip install adsg-core[opt] +pip install sb-arch-opt ``` If you want to interact with the DSG from a [Jupyter notebook](https://jupyter.org/): diff --git a/adsg_core/examples/robust_uav.py b/adsg_core/examples/robust_uav.py new file mode 100644 index 0000000..7cd8ec1 --- /dev/null +++ b/adsg_core/examples/robust_uav.py @@ -0,0 +1,382 @@ +""" +MIT License + +Copyright: (c) 2024, Deutsches Zentrum fuer Luft- und Raumfahrt e.V. +Contact: jasper.bussemaker@dlr.de + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" +import numpy as np +import openturns as ot +import warnings +from typing import * +from adsg_core.graph.adsg_basic import * +from adsg_core.graph.adsg_nodes import * +from adsg_core.optimization.stochastic_evaluator import DSGStochasticEvaluator +from sb_arch_opt.uncertainty import UQMethod, Mean, Margin, PolynomialChaos +from sb_arch_opt.algo.pymoo_interface import plot +from pymoo.optimize import minimize +from sb_arch_opt.algo.arch_sbo import get_arch_sbo_gp + +__all__ = ['RobustUAVStochasticEvaluator', 'UAVOptionNode', 'run_sbo'] + + +GRAVITY = 9.81 +RHO_SL = 1.225 # Sea-level air density [kg/m3] + + +class UAVOptionNode(NamedNode): + """Custom node representing one option of an architectural decision""" + + def __init__(self, decision: str, value): + self.decision = decision + self.value = value + super().__init__(f'{decision}:{value!s}') + + def get_export_title(self) -> str: + return f'{self.decision} = {self.value}' + + +class RobustUAVStochasticEvaluator(DSGStochasticEvaluator): + """ + Robust design of a multirotor UAV, as an example of architecture optimization under uncertainty. + + Design variables (13 total, 9 active in any one architecture): + + | Variable | Type | Active when | + |----------------|-------------|----------------------| + | `mission` | categorical | always | + | `airframe` | categorical | always | + | `n_rotors` | ordinal | always | + | `powertrain` | categorical | always | + | `n_cells` | ordinal | powertrain=electric | + | `cell_chem` | categorical | powertrain=electric | + | `bat_frac` | continuous | powertrain=electric | + | `engine` | categorical | powertrain=hybrid | + | `n_gen` | ordinal | powertrain=hybrid | + | `fuel_frac` | continuous | powertrain=hybrid | + | `rotor_radius` | continuous | always | + | `cruise_speed` | continuous | always | + | `cruise_alt` | continuous | always | + + That gives 120 valid discrete architectures spanning a continuous design space on top. + + Uncertain parameters (few, and themselves hierarchical): + + - `payload`: payload mass [kg], always present + - `headwind`: cruise headwind [m/s], always present + - `drag_factor`: airframe drag scatter [-], always present + - `eta_bat`: battery + motor efficiency [-], only in the electric architecture + - `bsfc`: brake specific fuel consumption [kg/kWh], only in the hybrid architecture + + Metrics: + + - `endurance` [min]: maximized, stochastic, reduced with `Margin(k=k)`, i.e. `mean - k*std` (the sign is + negative because the scalar is applied to the physical samples of a *maximized* quantity) + - `mass` [kg]: minimized, evaluated at the mean payload so it has no scatter of its own + + Ensure the optional dependencies are installed: `pip install sb-arch-opt[arch_sbo]` + """ + + # Mission -> payload scaling [-] and minimum useful cruise speed [m/s] + mission_options = ['survey', 'delivery'] + mission_payload_factor = {'survey': 1., 'delivery': 1.8} + + # Airframe -> structural mass [kg] and drag scaling [-] + airframe_options = ['carbon', 'aluminium'] + airframe_mass = {'carbon': 2.6, 'aluminium': 3.6} + airframe_drag = {'carbon': 1., 'aluminium': 1.08} + + rotor_options = [4, 6, 8] + + # Battery: cells in series scale the usable specific energy, chemistry sets its base level [Wh/kg] + cell_options = [3, 4, 6] + cell_factor = {3: .92, 4: 1., 6: 1.06} + chem_options = ['lipo', 'liion'] + chem_specific_energy = {'lipo': 180., 'liion': 240.} + chem_mass_penalty = {'lipo': 0., 'liion': .4} # Liion needs more protection circuitry [kg] + + # Engine -> installed mass [kg] and fuel consumption scaling [-] + # The turbine is lighter but thirstier: a genuine categorical trade-off + engine_options = ['piston', 'turbine'] + engine_mass = {'piston': 2.4, 'turbine': 1.6} + engine_bsfc_factor = {'piston': 1., 'turbine': 1.35} + + # Generator count -> mass [kg] and combined engine+generator+motor chain efficiency [-] + gen_options = [1, 2] + gen_mass = {1: .8, 2: 1.7} + gen_chain_efficiency = {1: .18, 2: .195} + + avionics_mass = .6 # [kg] + rotor_mass_factor = 1.5 # Rotor + motor mass per rotor, per m of radius [kg/m] + figure_of_merit = .72 # Rotor hover efficiency [-] + energy_mass_ref = 8. # Reference mass the energy fractions apply to [kg] + + def __init__(self, uq_method: UQMethod, k: float = 2., objective: int = None): + """ + :param k: margin factor; the robust endurance is `mean - k*std` + :param objective: 0 for endurance only, 1 for mass only, None for both + """ + self.k = k + self.uq_method = uq_method + + # Uncertain parameters: three always present, two conditional on the selected powertrain. The nodes are + # identities only - the distributions are attached to the graph in get_dsg(). + self.par_payload = InputParameterNode('payload', 2.0) + self.par_headwind = InputParameterNode('headwind', ot.Normal(10., 2.)) + self.par_drag = InputParameterNode('drag_factor', ot.Normal(1., .08)) + self.par_eta_bat = InputParameterNode('eta_bat', ot.Normal(.92, .03)) + self.par_bsfc = InputParameterNode('bsfc', ot.Normal(.42, .075)) + + self.metric_node_map: Dict[str, MetricNode] = {} + self.option_nodes: Dict[str, List[UAVOptionNode]] = {} + + obj_scalar = [Margin(k=self.k), Mean()] + + super().__init__(self.get_dsg(objective=objective), uq_method=uq_method, obj_scalar=obj_scalar) + + def _add_choice(self, dsg: BasicDSG, decision: str, originating_node: DSGNode, values: list, + is_ordinal: bool = False): + """Add a selection choice whose options are UAVOptionNodes carrying the decision value""" + option_nodes = [UAVOptionNode(decision, value) for value in values] + self.option_nodes[decision] = option_nodes + dsg.add_selection_choice(decision, originating_node, option_nodes, is_ordinal=is_ordinal) + return option_nodes + + def get_dsg(self, objective: int = None) -> DSGType: + metric_nodes = [] + if objective is None or objective == 0: + # Maximized, and the metric the uncertainty propagation feeds + self.metric_node_map['endurance'] = MetricNode( + 'endurance', direction=1, type_=MetricType.OBJECTIVE) + metric_nodes.append(self.metric_node_map['endurance']) + + if objective is None or objective == 1: + # Minimized, evaluated at the mean payload so it carries no scatter + self.metric_node_map['mass'] = MetricNode('mass', direction=-1, type_=MetricType.OBJECTIVE) + metric_nodes.append(self.metric_node_map['mass']) + + if len(metric_nodes) == 0: + raise ValueError('No objectives specified!') + + dsg = BasicDSG() + + uav = NamedNode('UAV') + dsg.add_edges([(uav, mn) for mn in metric_nodes]) + + # Always-present uncertain parameters and continuous design variables + dsg.add_edges([ + (uav, self.par_payload), + (uav, self.par_headwind), + (uav, self.par_drag), + (uav, DesignVariableNode('rotor_radius', bounds=(.12, .30))), + (uav, DesignVariableNode('cruise_speed', bounds=(8., 26.))), + (uav, DesignVariableNode('cruise_alt', bounds=(0., 2000.))), + ]) + + # ALWAYS-ACTIVE DISCRETE CHOICES + mission = NamedNode('mission') + airframe = NamedNode('airframe') + rotors = NamedNode('rotors') + dsg.add_edges([(uav, mission), (uav, airframe), (uav, rotors)]) + + self._add_choice(dsg, 'mission', mission, self.mission_options) + self._add_choice(dsg, 'airframe', airframe, self.airframe_options) + self._add_choice(dsg, 'n_rotors', rotors, self.rotor_options, is_ordinal=True) + + # POWERTRAIN: categorical, and the root of the hierarchy + powertrain = NamedNode('powertrain') + dsg.add_edge(uav, powertrain) + electric, hybrid = self._add_choice(dsg, 'powertrain', powertrain, ['electric', 'hybrid']) + + # ELECTRIC BRANCH + self._add_choice(dsg, 'n_cells', electric, self.cell_options, is_ordinal=True) + self._add_choice(dsg, 'cell_chem', electric, self.chem_options) + dsg.add_edges([ + (electric, DesignVariableNode('bat_frac', bounds=(.20, .60))), + (electric, self.par_eta_bat), + ]) + + # HYBRID BRANCH + self._add_choice(dsg, 'engine', hybrid, self.engine_options) + self._add_choice(dsg, 'n_gen', hybrid, self.gen_options, is_ordinal=True) + dsg.add_edges([ + (hybrid, DesignVariableNode('fuel_frac', bounds=(.10, .40))), + (hybrid, self.par_bsfc), + ]) + + dsg = dsg.set_start_nodes({uav}) + + return dsg + + """########################### + ### ARCHITECTURE ANALYSIS ### + ###########################""" + + @staticmethod + def _decision_values(dsg: DSGType) -> Dict[str, Any]: + """Read the selected option of every decision taken in this architecture""" + return {node.decision: node.value for node in dsg.get_nodes_by_type(UAVOptionNode)} + + @staticmethod + def _des_var_values(dsg: DSGType) -> Dict[str, float]: + """Read the values of the continuous design variables active in this architecture""" + return {node.name: value for node, value in dsg.des_var_values.items()} + + def _mass(self, dsg: DSGType, payload: float) -> Tuple[float, float]: + """Total mass [kg] and the energy-carrying mass (battery or fuel) [kg]""" + decisions = self._decision_values(dsg) + des_vars = self._des_var_values(dsg) + + structural = (self.airframe_mass[decisions['airframe']] + self.avionics_mass + + decisions['n_rotors'] * self.rotor_mass_factor * des_vars['rotor_radius']) + + if decisions['powertrain'] == 'electric': + energy_mass = des_vars['bat_frac'] * self.energy_mass_ref + installed = energy_mass + self.chem_mass_penalty[decisions['cell_chem']] + else: + energy_mass = des_vars['fuel_frac'] * self.energy_mass_ref + installed = (energy_mass + self.engine_mass[decisions['engine']] + + self.gen_mass[decisions['n_gen']]) + + effective_payload = payload * self.mission_payload_factor[decisions['mission']] + return structural + installed + effective_payload, energy_mass + + def _power_required(self, dsg: DSGType, mass: float, headwind: float, drag_factor: float) -> float: + """Cruise power [W] from simple momentum theory plus a forward-flight penalty""" + decisions = self._decision_values(dsg) + des_vars = self._des_var_values(dsg) + + # Air density falls off with altitude (ISA troposphere) + rho = RHO_SL * (1. - 2.2558e-5*des_vars['cruise_alt'])**4.2559 + + disc_area = decisions['n_rotors'] * np.pi * des_vars['rotor_radius']**2 + hover_power = (mass*GRAVITY)**1.5 / (np.sqrt(2*rho*disc_area) * self.figure_of_merit) + + airspeed = des_vars['cruise_speed'] + headwind + drag = drag_factor * self.airframe_drag[decisions['airframe']] + return hover_power * (1. + .0016*drag*airspeed**2) + + def _endurance(self, dsg: DSGType, sample: Dict[InputParameterNode, float]) -> float: + """ + Endurance [min] for one realization of the uncertain parameters. + + `sample` maps every parameter node present in this architecture to its value for this realization, so the + branch-local parameters (`eta_bat`, `bsfc`) are only read where they exist. + """ + decisions = self._decision_values(dsg) + mass, energy_mass = self._mass(dsg, sample[self.par_payload]) + power = self._power_required(dsg, mass, sample[self.par_headwind], sample[self.par_drag]) + + if decisions['powertrain'] == 'electric': + # Stored energy [Wh] delivered at the sampled efficiency + specific_energy = (self.chem_specific_energy[decisions['cell_chem']] * + self.cell_factor[decisions['n_cells']]) + energy = specific_energy * energy_mass * sample[self.par_eta_bat] + return 60. * energy / power + + # Hybrid: shaft power follows from the chain efficiency, fuel burn from the sampled BSFC [kg/kWh] + shaft_power = power / self.gen_chain_efficiency[decisions['n_gen']] + bsfc = sample[self.par_bsfc] * self.engine_bsfc_factor[decisions['engine']] + fuel_flow = bsfc * (shaft_power/1000.) # [kg/h] + return 60. * energy_mass / fuel_flow + + """##################### + ### ROBUST EVALUATION ### + #####################""" + + def _evaluate_sample(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, float]: + """Evaluate one architecture for ONE realization of the uncertain parameters""" + results = {} + parameters = dsg.inp_param_values + for metric_node in metric_nodes: + if metric_node.name == 'endurance': + results[metric_node] = self._endurance(dsg, parameters) + + elif metric_node.name == 'mass': + results[metric_node], _ = self._mass(dsg, parameters[self.par_payload]) + + return results + + def evaluate_statistics(self, dsg: DSGType) -> Dict[str, float]: + """Convenience helper returning the raw statistics of one architecture, using this evaluator's UQ method""" + objective_values, _ = self.evaluate(dsg) + + by_name = {objective.name: output for objective, output in zip(self.objectives, objective_values)} + endurance, mass = by_name['endurance'], by_name['mass'] + return { + 'endurance_mean': endurance.mean, + 'endurance_std': endurance.std, + 'endurance_robust': endurance.mean - self.k*endurance.std, + 'mass': mass, + } + + +def run_sbo(uq: UQMethod, n_infill: int = 20, init_size: int = 40, k: float = 2., objective: int = None, + seed: int = None, verbose: bool = True): + """ + Optimize the robust UAV problem with SBArchOpt's Surrogate-Based Optimization (SBO). + """ + + if seed is not None: + np.random.seed(seed) + + evaluator = RobustUAVStochasticEvaluator(uq, k=k, objective=objective) + + problem = evaluator.get_problem(n_parallel=4) + problem.print_stats() + + sbo = get_arch_sbo_gp(problem, init_size=init_size) + result = minimize(problem, sbo, termination=('n_eval', init_size+n_infill), verbose=verbose, + seed=seed, progress=False) + + plot(result.opt.get('F'), + labels=f'SBO ({n_infill+init_size} evaluations)') + + # Print results + opt = result.opt + print('Best f:', opt.get('F')[0]) + print('Best dist:', list(opt.get('f_stochastic')[0])) + print('Best x:', list(opt.get('X')[0])) + + if verbose: + print('\nOptimum found:') + x_opt = np.atleast_2d(result.X) + f_opt = np.atleast_2d(result.F) + for x_i, f_i in zip(x_opt, f_opt): + # Objectives are stored for minimization, so flip the maximized ones back + values = [-f if obj.dir.value > 0 else f for f, obj in zip(f_i, evaluator.objectives)] + described = ', '.join(f'{obj.name}={value:.2f}' for obj, value in zip(evaluator.objectives, values)) + print(f' x={np.array2string(x_i, precision=3)} --> {described}') + + return evaluator, problem, result + + +if __name__ == '__main__': + uq = PolynomialChaos(n_evaluations=70, seed=42, degree=3, n_metamodel_samples=1000) + evaluator = RobustUAVStochasticEvaluator(uq, k=2, objective=None) + x = evaluator.get_random_design_vector() + dsg, _, _ = evaluator.get_graph(x) + result = evaluator.evaluate(dsg) + print(result) + dsg_all = evaluator.get_dsg() + dsg_all.render() + dsg.render() + + run_sbo(uq, n_infill=20, init_size=40, k=3, objective=None, seed=42, verbose=True) \ No newline at end of file diff --git a/adsg_core/graph/adsg.py b/adsg_core/graph/adsg.py index be93f65..dbaa7a3 100644 --- a/adsg_core/graph/adsg.py +++ b/adsg_core/graph/adsg.py @@ -23,6 +23,7 @@ SOFTWARE. """ import numpy as np +import openturns as ot from typing import * import networkx as nx from natsort import natsorted @@ -34,6 +35,7 @@ from adsg_core.graph.incompatibility import * from adsg_core.graph.influence_matrix import * from adsg_core.graph.choice_constraints import * +from sb_arch_opt.uncertainty import EvaluationOutput __all__ = ['DSG', 'EdgeType', 'CDVNode', 'ChoiceConstraint', 'ChoiceConstraintType', 'DSGType', 'ADSG', 'ADSGType'] @@ -49,7 +51,7 @@ class DSG: _taken_single_choices = [] def __init__(self, _graph=None, _influence_matrix=None, _status_array=None, _choice_con_map=None, - _des_var_values=None, _metric_values=None, **_): + _des_var_values=None, _inp_param_values=None, _metric_values=None, **_): self._graph = _graph or self._get_empty_graph() self._choice_constraints: List[ChoiceConstraint] = _choice_con_map or [] self._influence_matrix: Optional[InfluenceMatrix] = _influence_matrix @@ -57,7 +59,8 @@ def __init__(self, _graph=None, _influence_matrix=None, _status_array=None, _cho self._update_connector_grouping_degrees() self._des_var_values: Dict[DesignVariableNode, Union[float, int]] = (_des_var_values or {}).copy() - self._metric_values: Dict[MetricNode, float] = (_metric_values or {}).copy() + self._inp_param_values: Dict[InputParameterNode, Union[ot.DistributionImplementation, float]] = (_inp_param_values or {}).copy() + self._metric_values: Dict[MetricNode, EvaluationOutput] = (_metric_values or {}).copy() @staticmethod def _get_empty_graph(): @@ -185,6 +188,8 @@ def _get_graph_for_export(self): for node in self.des_var_nodes: node.assigned_value = self.des_var_value(node) + for node in self.inp_param_nodes: + node.assigned_value = self.inp_param_value(node) for node in self.metric_nodes: node.assigned_value = self.metric_value(node) @@ -210,9 +215,9 @@ def get_nodes_by_subtype(self, type_): """Get all nodes in the graph that are or inherit from the given type""" return get_nodes_by_subtype(self._graph, type_) - """########################################## - ### DESIGN VARIABLE AND METRICS FUNCTIONS ### - ##########################################""" + """############################################################ + ### DESIGN VARIABLE, INPUT PARAMETERS AND METRICS FUNCTIONS ### + ############################################################""" @property def all_des_var_nodes(self) -> List[DesignVariableNode]: @@ -283,21 +288,41 @@ def des_var_values(self) -> Dict[DesignVariableNode, Union[float, int]]: def reset_des_var_values(self): self._des_var_values = {} + @property + def inp_param_nodes(self) -> List[InputParameterNode]: + return self.get_nodes_by_type(InputParameterNode) + + def set_inp_param_value(self, parameter_node: InputParameterNode, value: Union[ot.DistributionImplementation, float]): + """ + Set the value of the realization of the respective input parameter. + """ + self._inp_param_values[parameter_node] = value + + def inp_param_value(self, parameter_node: InputParameterNode) -> Optional[Union[ot.DistributionImplementation, float]]: + return self._inp_param_values.get(parameter_node) + + @property + def inp_param_values(self) -> Dict[InputParameterNode, Union[ot.DistributionImplementation, float]]: + return self._inp_param_values.copy() + + def reset_inp_param_values(self): + self._inp_param_values = {} + @property def metric_nodes(self) -> List[MetricNode]: return self.get_nodes_by_type(MetricNode) - def set_metric_value(self, metric_node: MetricNode, value: float): + def set_metric_value(self, metric_node: MetricNode, value: EvaluationOutput): """ Set the value of a metric node. """ self._metric_values[metric_node] = value - def metric_value(self, metric_node) -> Optional[float]: + def metric_value(self, metric_node) -> Optional[EvaluationOutput]: return self._metric_values.get(metric_node) @property - def metric_values(self): + def metric_values(self) -> Dict[MetricNode, EvaluationOutput]: return self._metric_values.copy() def reset_metric_values(self): @@ -685,7 +710,7 @@ def get_for_adjusted(self, removed_edges=None, removed_nodes=None, added_edges=N dec_con_map_copy = self._choice_constraints.copy() return self.__class__(_graph=graph_copy, _influence_matrix=self._influence_matrix, _status_array=status_array if status_array is not None else self._status_array, - _choice_con_map=dec_con_map_copy, _des_var_values=self._des_var_values, + _choice_con_map=dec_con_map_copy, _des_var_values=self._des_var_values, _inp_param_values=self._inp_param_values, _metric_values=self._metric_values, **kwargs) def _mod_graph_adjust_kwargs(self, kwargs): @@ -701,7 +726,7 @@ def get_for_kept_edges(self, kept_edges, **kwargs): self._mod_graph_adjust_kwargs(kwargs) return self.__class__(_graph=graph_copy, _influence_matrix=self._influence_matrix, _status_array=self._status_array, _choice_con_map=self._choice_constraints, - _des_var_values=self._des_var_values, _metric_values=self._metric_values, **kwargs) + _des_var_values=self._des_var_values, _inp_param_values=self._inp_param_values, _metric_values=self._metric_values, **kwargs) """######################################### ### INCOMPATIBILITY CONSTRAINT FUNCTIONS ### diff --git a/adsg_core/graph/adsg_nodes.py b/adsg_core/graph/adsg_nodes.py index dd4560a..b041661 100644 --- a/adsg_core/graph/adsg_nodes.py +++ b/adsg_core/graph/adsg_nodes.py @@ -27,13 +27,14 @@ import enum import itertools import numpy as np +import openturns as ot from typing import * import networkx as nx from collections import OrderedDict from adsg_core.graph.graph_edges import * __all__ = ['DSGNode', 'ChoiceNode', 'SelectionChoiceNode', 'ConnectionChoiceNode', 'ConnectorNode', 'NamedNode', - 'ConnectorDegreeGroupingNode', 'DesignVariableNode', 'MetricNode', 'MetricType', 'EdgeType', 'EdgeTuple', + 'ConnectorDegreeGroupingNode', 'DesignVariableNode', 'InputParameterNode', 'MetricNode', 'MetricType', 'EdgeType', 'EdgeTuple', 'NodeExportShape', 'ADSGNode', 'CollectorNode', 'NonSelectionNode'] @@ -56,7 +57,7 @@ class DSGNode: """ def __init__(self, obj_id=None, decision_id=None, option_id=None, src_decision_link_key=None, - tgt_decision_link_key=None, perm_decision_link_key=None, obj_ref=None): + tgt_decision_link_key=None, perm_decision_link_key=None, obj_ref=None): self._obj_id = obj_id self._id = None self.update_node_id() @@ -455,6 +456,37 @@ def str_context(self): def __str__(self): return f'DV[{self.name}]' +class InputParameterNode(DSGNode): + """ + Node representing input parameter that can be either deterministic or stochastic. + """ + + def __init__(self, name, value: Union[ot.DistributionImplementation, float], idx=None, **kwargs): + + self.name = name + self.idx = idx + self.value = value + self.assigned_value = None # Only for export + super(InputParameterNode, self).__init__(**kwargs) + + @property + def is_stochastic(self) -> bool: + """A parameter is stochastic if its value is a distribution; any other value is a fixed number""" + return isinstance(self.value, ot.DistributionImplementation) + + def get_export_title(self) -> str: + if self.assigned_value is not None: + return f'{self.name} = {self.assigned_value}' + return f'{self.name} = {self.value}' + + def get_export_color(self) -> str: + return _INP_OUT_COLOR + + def str_context(self): + return 'INP[%s]' % self.name + + def __str__(self): + return 'INP[%s]' % self.name class MetricType(enum.Flag): NONE = 0 @@ -462,7 +494,6 @@ class MetricType(enum.Flag): CONSTRAINT = enum.auto() OBJ_OR_CON = OBJECTIVE | CONSTRAINT - class MetricNode(DSGNode): """ Node representing a performance metric. In the end, a performance metric can either be used as an @@ -501,10 +532,7 @@ def get_export_title(self) -> str: role_str = ' [↑]' if self.dir > 0 else ' [↓]' if self.assigned_value is not None: - if math.isnan(self.assigned_value): - role_str = f' = NaN'+role_str - else: - role_str = f' = {self.assigned_value:.4g}'+role_str + role_str = f' = {self.assigned_value}'+role_str return self.name+role_str diff --git a/adsg_core/optimization/__init__.py b/adsg_core/optimization/__init__.py index d786273..68747f2 100644 --- a/adsg_core/optimization/__init__.py +++ b/adsg_core/optimization/__init__.py @@ -2,4 +2,6 @@ from .evaluator import * from .dv_output_defs import * from .problem import * +from .stochastic_problem import * +from .stochastic_evaluator import * from .graph_decoder import * diff --git a/adsg_core/optimization/dv_output_defs.py b/adsg_core/optimization/dv_output_defs.py index 9c25824..08b5be8 100644 --- a/adsg_core/optimization/dv_output_defs.py +++ b/adsg_core/optimization/dv_output_defs.py @@ -24,10 +24,11 @@ """ import enum import random +import openturns as ot from typing import * -from adsg_core.graph.adsg_nodes import MetricNode, DesignVariableNode, ChoiceNode +from adsg_core.graph.adsg_nodes import MetricNode, DesignVariableNode, ChoiceNode, InputParameterNode -__all__ = ['DesVar', 'Direction', 'Objective', 'Constraint'] +__all__ = ['DesVar', 'Direction', 'Objective', 'Constraint', 'InpParam'] class DesVar: @@ -134,6 +135,47 @@ def __repr__(self): return str(self) +class InpParam: + """Class representing an input parameter.""" + + def __init__(self, name: str, value: Union[ot.DistributionImplementation, float], node: InputParameterNode = None): + self._name = name + self._value = value + self._node = node + + @classmethod + def from_inp_param_node(cls, inp_param_node: InputParameterNode) -> 'InpParam': + name = inp_param_node.name + if inp_param_node.idx is not None: + name = '%s_%d' % (name, inp_param_node.idx) + value = inp_param_node.value + + return cls(name, value, node=inp_param_node) + + @property + def name(self) -> str: + '''Parameter name''' + return self._name + + @property + def value(self) -> Union[ot.DistributionImplementation, float]: + '''Parameter value''' + return self._value + + @property + def node(self) -> InputParameterNode: + '''Associated input parameter node''' + return self._node + + @property + def is_stochastic(self) -> bool: + '''Whether the input parameter is stochastic''' + return self._node.is_stochastic + + def __str__(self): + return f'INP: {self.name} = {self.value}' + + class Direction(enum.Enum): MIN = -1 MAX = 1 diff --git a/adsg_core/optimization/evaluator.py b/adsg_core/optimization/evaluator.py index 7f97c7d..218fca1 100644 --- a/adsg_core/optimization/evaluator.py +++ b/adsg_core/optimization/evaluator.py @@ -28,6 +28,7 @@ from adsg_core.graph.adsg_nodes import MetricNode from adsg_core.optimization.dv_output_defs import * from adsg_core.optimization.graph_processor import * +from sb_arch_opt.uncertainty import EvaluationOutput __all__ = ['DSGEvaluator', 'ADSGEvaluator'] @@ -49,7 +50,7 @@ def _choose_metric_type(self, objective: Objective, constraint: Constraint) -> U raise RuntimeError(f'Metric {objective.name} can either be an objective or a constraint! ' f'Specify the metric type using node.type = MetricType.x') - def evaluate(self, dsg: DSGType) -> Tuple[List[float], List[float]]: + def evaluate(self, dsg: DSGType) -> Tuple[List[EvaluationOutput], List[EvaluationOutput]]: """ Evaluate a DSG instance. Returns a list of objective values and a list of constraint values. """ @@ -72,7 +73,7 @@ def evaluate(self, dsg: DSGType) -> Tuple[List[float], List[float]]: return objective_values, constraint_values - def _evaluate(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, float]: + def _evaluate(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, EvaluationOutput]: """ Implement this function to provide DSG evaluation. Should return a mapping from metric node to float (NaN is allowed). @@ -80,4 +81,4 @@ def _evaluate(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[Metric raise NotImplementedError -ADSGEvaluator = DSGEvaluator # Backward compatibility +ADSGEvaluator = DSGEvaluator # Backward compatibility \ No newline at end of file diff --git a/adsg_core/optimization/graph_processor.py b/adsg_core/optimization/graph_processor.py index d8aa9c6..6a8e48d 100644 --- a/adsg_core/optimization/graph_processor.py +++ b/adsg_core/optimization/graph_processor.py @@ -23,6 +23,7 @@ SOFTWARE. """ import numpy as np +import warnings from typing import * from adsg_core.graph.adsg_nodes import * from adsg_core.optimization.dv_output_defs import * @@ -34,6 +35,7 @@ from adsg_core.optimization.assign_enc.selector import EncoderSelector from adsg_core.optimization.assign_enc.time_limiter import run_timeout from adsg_core.optimization.assign_enc.assignment_manager import AssignmentManagerBase +from sb_arch_opt.uncertainty import EvaluationOutput, StochasticParameter, StochasticParameterSpace __all__ = ['GraphProcessor', 'MetricType', 'SelChoiceEncoderType'] @@ -190,6 +192,10 @@ def dv_is_conditionally_active(self) -> List[bool]: def fixed_values(self): return self._fixed_values.copy() + @cached_property + def inp_params(self): + return self._get_inp_params() + @cached_property def objectives(self) -> List[Objective]: """The optimization problem objectives""" @@ -379,6 +385,24 @@ def _choice_nodes(self) -> List[ChoiceNode]: def design_variable_nodes(self) -> List[DesignVariableNode]: return self.graph.ordered_choice_nodes(self.graph.des_var_nodes) + @cached_property + def inp_param_nodes(self) -> List[InputParameterNode]: + return sorted(self.graph.get_nodes_by_type(InputParameterNode), key=lambda n: n.name) + + @cached_property + def param_space(self) -> StochasticParameterSpace: + """ + Return a stochastic parameter space corresponding to all the parameters defined during initialization. + Handles both stochastic and deterministic parameters. + """ + parameters = [] + for parameter_node in self.inp_param_nodes: + if parameter_node.is_stochastic: + dist = parameter_node.value + parameters.append(StochasticParameter(parameter_node.name, dist, ref=parameter_node)) + + return StochasticParameterSpace(parameters) + @cached_property def metric_nodes(self) -> List[MetricNode]: return sorted(self.graph.get_nodes_by_type(MetricNode), key=lambda n: n.name) @@ -417,6 +441,14 @@ def _can_be_constraint(metric_node): """A metric can be a constraint if a reference value has been defined.""" return metric_node.dir is not None and metric_node.ref is not None + def _get_inp_params(self) -> List[InpParam]: + inp_params = [] + for inp_param_node in self.inp_param_nodes: + inp_param = InpParam.from_inp_param_node(inp_param_node) + inp_params.append(inp_param) + + return inp_params + def _categorize_metrics(self): objectives = [] constraints = [] diff --git a/adsg_core/optimization/problem.py b/adsg_core/optimization/problem.py index 6cdc172..a3a74c8 100644 --- a/adsg_core/optimization/problem.py +++ b/adsg_core/optimization/problem.py @@ -22,9 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import logging -import warnings import functools +import logging import numpy as np from typing import * from concurrent.futures import wait, ProcessPoolExecutor, ThreadPoolExecutor @@ -32,37 +31,14 @@ from adsg_core.optimization.dv_output_defs import DesVar from adsg_core.optimization.graph_processor import GraphProcessor from adsg_core.optimization.assign_enc.time_limiter import run_timeout +from sb_arch_opt.problem import ArchOptProblemBase +from sb_arch_opt.design_space import ArchDesignSpace +from pymoo.core.variable import Variable, Real, Integer, Choice -try: - from sb_arch_opt.problem import ArchOptProblemBase - from sb_arch_opt.design_space import ArchDesignSpace - from pymoo.core.variable import Variable, Real, Integer, Choice - - from sb_arch_opt.sampling import TrailRepairWarning - warnings.simplefilter("ignore", category=TrailRepairWarning) - - HAS_SB_ARCH_OPT = True - -except ImportError: - HAS_SB_ARCH_OPT = False - - class ArchDesignSpace: - pass - - class ArchOptProblemBase: - pass - -__all__ = ['check_dependency', 'DSGDesignSpace', 'DSGArchOptProblem', 'HAS_SB_ARCH_OPT', - 'ADSGDesignSpace', 'ADSGArchOptProblem'] +__all__ = ['DSGDesignSpace', 'DSGArchOptProblem', 'ADSGDesignSpace', 'ADSGArchOptProblem'] log = logging.getLogger('adsg.opt') - -def check_dependency(): - if not HAS_SB_ARCH_OPT: - raise ImportError('Looks like SBArchOpt is not installed! Run: pip install sb-arch-opt') - - class DSGDesignSpace(ArchDesignSpace): """ SBArchOpt design space implementation for a DSG design problem. @@ -210,7 +186,6 @@ class DSGArchOptProblem(ArchOptProblemBase): """ def __init__(self, evaluator: DSGEvaluator, n_parallel=None, parallel_processes=True): - check_dependency() self.evaluator = evaluator self.n_parallel = n_parallel @@ -223,7 +198,7 @@ def __init__(self, evaluator: DSGEvaluator, n_parallel=None, parallel_processes= super().__init__(design_space, n_obj=n_objs, n_ieq_constr=n_constr) self.obj_is_max = [obj.dir.value > 0 for obj in evaluator.objectives] - self.con_ref = [(con.dir > 0, con.ref) for con in evaluator.constraints] + self.con_ref = [(con.dir.value > 0, con.ref) for con in evaluator.constraints] def _arch_evaluate(self, x: np.ndarray, is_active_out: np.ndarray, f_out: np.ndarray, g_out: np.ndarray, h_out: np.ndarray, *args, **kwargs): @@ -273,4 +248,4 @@ def __repr__(self): ADSGDesignSpace = DSGDesignSpace # Backward compatibility -ADSGArchOptProblem = DSGArchOptProblem # Backward compatibility +ADSGArchOptProblem = DSGArchOptProblem # Backward compatibility \ No newline at end of file diff --git a/adsg_core/optimization/stochastic_evaluator.py b/adsg_core/optimization/stochastic_evaluator.py new file mode 100644 index 0000000..caa0576 --- /dev/null +++ b/adsg_core/optimization/stochastic_evaluator.py @@ -0,0 +1,125 @@ +""" +MIT License + +Copyright: (c) 2024, Deutsches Zentrum fuer Luft- und Raumfahrt e.V. +Contact: jasper.bussemaker@dlr.de + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" +import math +from typing import * +import numpy as np +from adsg_core.graph.adsg import DSGType +from adsg_core.graph.adsg_nodes import MetricNode, InputParameterNode +from adsg_core.optimization.evaluator import DSGEvaluator +from sb_arch_opt.uncertainty import EvaluationOutput, Scalarization, UQMethod + +__all__ = ['DSGStochasticEvaluator', 'StochasticADSGEvaluator'] + + +class DSGStochasticEvaluator(DSGEvaluator): + """ + Base class for implementing an evaluator for stochastic problem that directly evaluates DSG instances. + Override _evaluate_sample to implement the evaluation. + + Inherits `DSGEvaluator` and 'GraphProcessor', so all their functions are also available. + """ + + def __init__(self, + *args, + uq_method: UQMethod, + obj_scalar: Optional[List[Scalarization]] = None, + constr_scalar: Optional[List[Scalarization]] = None, + **kwargs): + + self.uq_method = uq_method + self.obj_scalar = obj_scalar + self.constr_scalar = constr_scalar + + super().__init__(*args, **kwargs) + + def get_problem(self, n_parallel=None, parallel_processes=True): + + from adsg_core.optimization.stochastic_problem import DSGStochasticArchOptProblem + return DSGStochasticArchOptProblem(self, + self.param_space, + self.uq_method, + self.obj_scalar, + self.constr_scalar, + n_parallel=n_parallel, parallel_processes=parallel_processes) + + def _set_param_realizations(self, dsg, param_nodes: List[InputParameterNode], samples: np.ndarray, i_realization: int): + realized = {param.ref: param.sample for param in self.param_space.param_realization(samples, i_realization)} + for node in param_nodes: + # Stochastic parameters get their realization, deterministic ones keep their fixed value + dsg.set_inp_param_value(node, realized.get(node, node.value)) + + def _evaluate(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, EvaluationOutput]: + """ + Implement _evaluate_sample with evaluation code for each realized sample stored on DSG. + """ + # Sample the stochastic parameters + stochastic_samples = self.uq_method.get_samples(self.param_space) + param_nodes = dsg.inp_param_nodes + + n_s = stochastic_samples.shape[0] + n_obj = len(self.objectives) + n_constr = len(self.constraints) + + f_s = np.zeros((n_s, n_obj)) * np.nan + g_s = np.zeros((n_s, n_constr)) * np.nan + + for i in range(n_s): + # Set parameter realizations on the graph + self._set_param_realizations(dsg, param_nodes, stochastic_samples, i) + + # Evaluate architecture for a realized sample + value_map = self._evaluate_sample(dsg, metric_nodes) + + f_s[i, :] = [value_map.get(objective.node, math.nan) for objective in self.objectives] + g_s[i, :] = [value_map.get(constraint.node, math.nan) if constraint.node in metric_nodes else constraint.ref for constraint in self.constraints] + + # Apply UQ method to process the results and return StochasticResult for this DSG instance + outputs = self.uq_method.process_results(np.concatenate([f_s, g_s], axis=1), param_space = self.param_space) + + metric_map = {} + + # After UQ reset the parameter node to store its distribution + for parameter_node in param_nodes: + dsg.set_inp_param_value(parameter_node, parameter_node.value) + + # Return metric map + for i, objective in enumerate(self.objectives): + if objective.node in metric_nodes: + metric_map[objective.node] = outputs[i] + for i, constraint in enumerate(self.constraints): + if constraint.node in metric_nodes and outputs[n_obj+i] is not None: + metric_map[constraint.node] = outputs[n_obj+i] + + return metric_map + + def _evaluate_sample(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, float]: + """ + Implement this function to provide DSG evaluation for ONE realization of the uncertain parameters. + Should return a mapping from metric node to float (NaN is allowed). + """ + raise NotImplementedError + + +StochasticADSGEvaluator = DSGStochasticEvaluator \ No newline at end of file diff --git a/adsg_core/optimization/stochastic_problem.py b/adsg_core/optimization/stochastic_problem.py new file mode 100644 index 0000000..bd2b21c --- /dev/null +++ b/adsg_core/optimization/stochastic_problem.py @@ -0,0 +1,163 @@ +""" +MIT License + +Copyright: (c) 2024, Deutsches Zentrum fuer Luft- und Raumfahrt e.V. +Contact: jasper.bussemaker@dlr.de + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" +import copy +import warnings +import numpy as np +from typing import * +from concurrent.futures import wait, ProcessPoolExecutor, ThreadPoolExecutor +from adsg_core.optimization.stochastic_evaluator import DSGStochasticEvaluator +from adsg_core.optimization.problem import DSGDesignSpace +from sb_arch_opt.uncertainty import Scalarization, StochasticParameterSpace, UQMethod, Margin, Quantile +from sb_arch_opt.stochastic_problem import StochasticArchOptProblem + +__all__ = ['DSGStochasticArchOptProblem', 'ADSGStochasticArchOptProblem'] + + +class DSGStochasticArchOptProblem(StochasticArchOptProblem): + """ + [SBArchOpt](https://sbarchopt.readthedocs.io/) wrapper for a DSG stochastic optimization problem. Note that under the + hood, SBArchOpt uses [pymoo](https://pymoo.org/). + The connection is made between the `StochasticArchOptProblem` class (which specifies all information needed to optimize an + architecture optimization problem), and the `DSGStochasticEvaluator` class, which contains all information for + running a stochastic DSG architecture optimization problem. + + Parallel processing is possible by setting `n_parallel` to a number higher than 1. + By default, assumes parallel processing is done within the thread and therefore starts a multiprocessing pool to + run the parallel evaluations. + + Ensure SBArchOpt is installed with uncertainty package: `pip install sb-arch-opt[uncertainty]` + + Example usage: + + ```python + from pymoo.optimize import minimize + from sb_arch_opt.algo.pymoo_interface import get_nsga2 + + evaluator = ... # Instance of DSGStochasticEvaluator + + algorithm = get_nsga2(pop_size=100) + problem = DSGStochasticArchOptProblem(evaluator, param_space, uq_method) + + result = minimize(problem, algorithm, termination=('n_eval', 500)) + ``` + """ + + def __init__(self, evaluator: DSGStochasticEvaluator, + param_space: StochasticParameterSpace, + uq_method: UQMethod, + obj_scalar: Optional[List[Scalarization]] = None, + constr_scalar: Optional[List[Scalarization]] = None, + n_parallel=None, parallel_processes=True): + + self.evaluator = evaluator + self.n_parallel = n_parallel + self.parallel_processes = parallel_processes + + n_obj = len(evaluator.objectives) + n_constr = len(evaluator.constraints) + + design_space = DSGDesignSpace(evaluator) + + super().__init__(design_space, param_space=param_space, uq_method=uq_method, n_obj=n_obj, n_ieq_constr=n_constr, + obj_scalar=obj_scalar, ieq_constr_scalar=constr_scalar) + + self.obj_is_max = [obj.dir.value > 0 for obj in evaluator.objectives] + self.con_ref = [(con.dir.value > 0, con.ref) for con in evaluator.constraints] + + # Values are negated for maximization only after scalarization, so the scalars themselves follow the direction + self.obj_scalar = [self._orient_scalar(scalar, is_max) + for scalar, is_max in zip(self.obj_scalar, self.obj_is_max)] + self.ieq_constr_scalar = [self._orient_scalar(scalar, is_gte) + for scalar, (is_gte, _) in zip(self.ieq_constr_scalar, self.con_ref)] + + @staticmethod + def _orient_scalar(scalar: Scalarization, is_max: bool) -> Scalarization: + """Get the scalars""" + if isinstance(scalar, Margin) or isinstance(scalar, Quantile): + scalar = copy.copy(scalar) + scalar.direction = 1 if is_max else -1 + return scalar + + def _arch_evaluate(self, x: np.ndarray, is_active_out: np.ndarray, f_out: np.ndarray, g_out: np.ndarray, h_out: np.ndarray, *args, + f_stoch_out: np.ndarray=None, g_stoch_out: np.ndarray=None, h_stoch_out: np.ndarray=None, **kwargs): + """ + Overrides parent _arch_evaluate class to integrate it with DSGStochasticEvaluator, but maintains the same functionality. + """ + # Correct integer design variables + self.design_space.round_x_discrete(x) + + # Generate architectures + is_discrete_mask = self.is_discrete_mask + dsg_instances = [] + for i, xi in enumerate(x): + x_arch = [int(val) if is_discrete_mask[j] else float(val) for j, val in enumerate(xi)] + dsg_instance, x_imputed, is_active_arch = self.evaluator.get_graph(x_arch) + dsg_instances.append(dsg_instance) + x[i, :] = x_imputed + is_active_out[i, :] = is_active_arch + + # Evaluate architectures for each DSG instance + if self.n_parallel is not None and self.n_parallel > 1: + self.evaluator.uq_method.get_samples(self.evaluator.param_space) # Get samples, so that same seed is used for parallel execution + executor_class = ProcessPoolExecutor if self.parallel_processes else ThreadPoolExecutor + with executor_class(max_workers=self.n_parallel) as executor: + futures = [executor.submit(self.evaluator.evaluate, dsg) for dsg in dsg_instances] + + wait(futures) + results = [fut.result() for fut in futures] + + else: + results = [self.evaluator.evaluate(dsg) for dsg in dsg_instances] + + # Process results + for i, (obj_outputs, con_outputs) in enumerate(results): + + for j, obj_output in enumerate(obj_outputs): + obj_scalar = self.obj_scalar[j] + val = obj_scalar.scalarize(obj_output) if not isinstance(obj_output, float) else obj_output + + f_stoch_out[i, j] = obj_output + f_out[i, j] = -val if self.obj_is_max[j] else val + + for j, con_output in enumerate(con_outputs): + con_scalar = self.ieq_constr_scalar[j] + val = con_scalar.scalarize(con_output) if not isinstance(con_output, float) else con_output + + g_stoch_out[i, j] = con_output + g_out[i, j] = (val-self.con_ref[j][1])*(-1 if self.con_ref[j][0] else 1) + + def _print_extra_stats(self): + super()._print_extra_stats() + self.get_discrete_rates(show=True) + self.evaluator.print_stats() + + def get_n_batch_evaluate(self) -> Optional[int]: + return self.n_parallel + + def __repr__(self): + return f'{self.__class__.__name__}({self.evaluator!r})' + + +ADSGStochasticArchOptProblem = DSGStochasticArchOptProblem # Backward compatibility diff --git a/adsg_core/tests/test_optimization_bridge.py b/adsg_core/tests/test_optimization_bridge.py index 51bf805..c23aba8 100644 --- a/adsg_core/tests/test_optimization_bridge.py +++ b/adsg_core/tests/test_optimization_bridge.py @@ -4,20 +4,11 @@ from adsg_core.examples.gnc import * from adsg_core.examples.apollo import * from adsg_core.optimization.problem import * +from sb_arch_opt.algo.pymoo_interface import get_nsga2 +from pymoo.optimize import minimize -if HAS_SB_ARCH_OPT: - from sb_arch_opt.algo.pymoo_interface import get_nsga2 - from pymoo.optimize import minimize -check_dependency = lambda: pytest.mark.skipif(not HAS_SB_ARCH_OPT, reason='SBArchOpt not installed') - - -@pytest.mark.skipif(int(os.getenv('RUN_SLOW_TESTS', 0)) != 1, reason='Set RUN_SLOW_TESTS=1 to run slow tests') -def test_slow_tests(): - assert HAS_SB_ARCH_OPT - - -@check_dependency() +@pytest.mark.skipif(os.getenv('RUN_SLOW_TESTS', '0') != '1', reason='Set RUN_SLOW_TESTS=1 to run slow tests') def test_gnc(): gnc = GNCEvaluator() problem = gnc.get_problem() @@ -53,7 +44,6 @@ def test_gnc(): @pytest.mark.skipif(int(os.getenv('RUN_SLOW_TESTS', 0)) != 1, reason='Set RUN_SLOW_TESTS=1 to run slow tests') -@check_dependency() def test_gnc_parallel(): gnc = GNCEvaluator() problem = gnc.get_problem(n_parallel=10, parallel_processes=False) @@ -64,7 +54,6 @@ def test_gnc_parallel(): @pytest.mark.skipif(int(os.getenv('RUN_SLOW_TESTS', 0)) != 1, reason='Set RUN_SLOW_TESTS=1 to run slow tests') -@check_dependency() def test_apollo(): problem = ApolloEvaluator().get_problem() diff --git a/adsg_core/tests/test_robust_optimization.py b/adsg_core/tests/test_robust_optimization.py new file mode 100644 index 0000000..643ca29 --- /dev/null +++ b/adsg_core/tests/test_robust_optimization.py @@ -0,0 +1,471 @@ +import pytest +import numpy as np +import openturns as ot +from typing import * +from adsg_core.graph.adsg_basic import * +from adsg_core.graph.adsg_nodes import * +from adsg_core.optimization.stochastic_evaluator import * +from adsg_core.optimization.graph_processor import * +from sb_arch_opt.uncertainty import MonteCarlo, PolynomialChaos, StochasticOutput, Mean, Margin, Quantile, UQMethod + + +def _dsg_with_parameters(n, par_nodes): + dsg = BasicDSG() + dsg.add_edges([(n[0], par_node) for par_node in par_nodes]) + return dsg.set_start_nodes({n[0]}) + + +def _evaluator(dsg, n_evaluations=5, seed=42) -> 'DSGStochasticEvaluator': + class _Stochastic_Evaluator(DSGStochasticEvaluator): + def _evaluate_sample(self, dsg_instance, metric_nodes): + raise NotImplementedError + + return _Stochastic_Evaluator(dsg, uq_method=MonteCarlo(n_evaluations, seed=seed)) + + +def _dsg_with_branch_parameters(n, common, only_a, only_b): + dsg = BasicDSG() + dsg.add_edges([(n[0], common), (n[1], only_a), (n[2], only_b)]) + dsg.add_selection_choice('C1', n[0], [n[1], n[2]]) + return dsg.set_start_nodes({n[0]}) + + +class BeamStochasticEvaluator(DSGStochasticEvaluator): + """Pick a material and a thickness for a beam under an uncertain load. + + material: steel (stiff, heavy) or alu; t: continuous thickness. The stiffness parameter is branch-local, so + an instance never carries all parameters. + """ + + stiffness = {'steel': 210., 'alu': 70.} + density = {'steel': 7.8, 'alu': 2.7} + + def __init__(self, uq_method=None, stress_ref=None, **kwargs): + self.seen_loads = [] + + self.par_load = InputParameterNode('load', ot.Normal(100., 20.)) + self.par_rho = InputParameterNode('rho_factor', 1.5) # deterministic + self.par_e = {'steel': InputParameterNode('E_steel', ot.Normal(210., 10.)), + 'alu': InputParameterNode('E_alu', ot.Normal(70., 10.))} + + self.mass_node = MetricNode('mass', direction=-1, type_=MetricType.OBJECTIVE) + self.deflection_node = MetricNode('deflection', direction=-1, type_=MetricType.OBJECTIVE) + self.capacity_node = MetricNode('capacity', direction=1, type_=MetricType.OBJECTIVE) # maximized + + self.stress_ref = stress_ref + self.stress_node = None + if stress_ref is not None: + self.stress_node = MetricNode('stress', direction=-1, ref=stress_ref, type_=MetricType.CONSTRAINT) + + self.material_nodes = {} + kwargs.setdefault('uq_method', uq_method if uq_method is not None else MonteCarlo(20, seed=42)) + super().__init__(self._build_dsg(), **kwargs) + + def _build_dsg(self): + dsg = BasicDSG() + beam = NamedNode('beam') + dsg.add_edges([(beam, self.mass_node), (beam, self.deflection_node), (beam, self.capacity_node), + (beam, self.par_load), (beam, self.par_rho), + (beam, DesignVariableNode('t', bounds=(1., 5.)))]) + + for name in ['steel', 'alu']: + material_node = NamedNode(name) + self.material_nodes[name] = material_node + dsg.add_edge(material_node, self.par_e[name]) + + if self.stress_node is not None: + dsg.add_edge(beam, self.stress_node) + + dsg.add_selection_choice('material', beam, [self.material_nodes['steel'], self.material_nodes['alu']]) + return dsg.set_start_nodes({beam}) + + def _material(self, dsg: DSGType) -> str: + for name, material_node in self.material_nodes.items(): + if material_node in dsg.graph.nodes: + return name + raise RuntimeError('No material selected!') + + def _thickness(self, dsg: DSGType) -> float: + for des_var_node, value in dsg.des_var_values.items(): + if des_var_node.name == 't': + return value + raise RuntimeError('No thickness!') + + def _evaluate_sample(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, float]: + material = self._material(dsg) + thickness = self._thickness(dsg) + load = dsg.inp_param_value(self.par_load) + e_modulus = dsg.inp_param_value(self.par_e[material]) + rho_factor = dsg.inp_param_value(self.par_rho) + self.seen_loads.append(load) + + values = {} + + for metric_node in metric_nodes: + if metric_node.name == 'mass': + values[metric_node] = self.density[material]*thickness*rho_factor + elif metric_node.name == 'deflection': + values[metric_node] = load / (e_modulus*thickness**3) + elif metric_node.name == 'capacity': + values[metric_node] = e_modulus*thickness**2 / load + elif metric_node.name == 'stress': + values[metric_node] = load / thickness**2 + + return values + + +@pytest.fixture +def beam(): + return BeamStochasticEvaluator() + + +@pytest.fixture +def constrained_beam(): + return BeamStochasticEvaluator(stress_ref=60.) + + +def test_input_parameter_node(n): + stochastic = InputParameterNode('E', ot.Normal(10., 2.)) + deterministic = InputParameterNode('rho', 1.225) + + assert stochastic.name == 'E' + assert stochastic.idx is None + assert stochastic.is_stochastic + assert not deterministic.is_stochastic + assert deterministic.value == 1.225 + + assert str(stochastic) == 'INP[E]' + assert stochastic.str_context() == 'INP[E]' + + # Only a distribution makes a parameter stochastic; any other value is a fixed number + assert InputParameterNode('h', ot.Uniform(0., 1.)).is_stochastic + assert not InputParameterNode('n_blades', 3).is_stochastic + assert repr(stochastic) + assert stochastic.get_export_color() + assert 'E = ' in stochastic.get_export_title() + + # Nodes are identity-based, so two parameters with the same name are still different nodes + assert InputParameterNode('E', ot.Normal(10., 2.)) != stochastic + assert len({stochastic, InputParameterNode('E', ot.Normal(10., 2.))}) == 2 + + +def test_set_get_input_parameter_value(n): + par_a = InputParameterNode('A', ot.Normal(0., 1.)) + par_b = InputParameterNode('B', 2.5) + dsg = _dsg_with_parameters(n, [par_a, par_b]) + + assert dsg.feasible + assert set(dsg.inp_param_nodes) == {par_a, par_b} + assert dsg.inp_param_value(par_a) is None # nothing assigned on the graph yet + + dist = ot.Normal(5., 1.) + dsg.set_inp_param_value(par_a, dist) + assert dsg.inp_param_value(par_a) is dist + + values = dsg.inp_param_values + values[par_a] = 99. + assert dsg.inp_param_value(par_a) is dist # the mapping is a copy + + assert dsg.copy().inp_param_value(par_a) is dist # and survives derivation + + dsg.reset_inp_param_values() + assert dsg.inp_param_values == {} + + +def test_parameter_values_isolated_between_instances(n): + par_a = InputParameterNode('A', ot.Normal(0., 1.)) + dsg = BasicDSG() + dsg.add_edges([(n[0], par_a)]) + dsg.add_selection_choice('C1', n[0], [n[1], n[2]]) + processor = GraphProcessor(dsg.set_start_nodes({n[0]})) + + graph_a, _, _ = processor.get_graph([0]) + graph_b, _, _ = processor.get_graph([1]) + graph_a.set_inp_param_value(par_a, 1.) + graph_b.set_inp_param_value(par_a, 2.) + + assert graph_a.inp_param_value(par_a) == 1. + assert graph_b.inp_param_value(par_a) == 2. + assert processor.graph.inp_param_value(par_a) is None # template untouched + + +def test_parameters_are_not_design_variables(n): + par_a = InputParameterNode('A', ot.Normal(0., 1.)) + dv_node = DesignVariableNode('DV', bounds=(0., 1.)) + + dsg = BasicDSG() + dsg.add_edges([(n[0], par_a), (n[0], dv_node)]) + dsg.add_selection_choice('C1', n[0], [n[1], n[2]]) + processor = GraphProcessor(dsg.set_start_nodes({n[0]})) + + assert [dv.name for dv in processor.des_vars] == ['C1', 'DV'] + assert all(dv.node is not par_a for dv in processor.des_vars) + assert par_a not in processor.design_variable_nodes + + +def test_param_space_holds_only_stochastic_parameters(n): + stochastic = InputParameterNode('u', ot.Normal(0., 1.)) + deterministic = InputParameterNode('rho', 1.225) + processor = GraphProcessor(_dsg_with_parameters(n, [stochastic, deterministic])) + + assert [node.name for node in processor.inp_param_nodes] == ['rho', 'u'] # sorted by name + assert processor.param_space.parameter_names == ['u'] + assert processor.param_space.n_parameters == 1 + assert processor.param_space.joint_dist.getDimension() == 1 + + +def test_param_space_uses_the_distributions_from_the_graph(n): + # The distribution stored on the node is what the parameter space hands to the UQ method + normal = InputParameterNode('u', ot.Normal(10., 2.)) + uniform = InputParameterNode('h', ot.Uniform(4., 10.)) + fixed = InputParameterNode('n_blades', 3) # not a distribution, so it never reaches the space + processor = GraphProcessor(_dsg_with_parameters(n, [normal, uniform, fixed])) + + assert processor.param_space.parameter_names == ['h', 'u'] + assert [parameter.dist for parameter in processor.param_space._parameters] == [uniform.value, normal.value] + + marginals = {name: processor.param_space.joint_dist.getMarginal(i) + for i, name in enumerate(processor.param_space.parameter_names)} + + assert marginals['u'].getMean()[0] == pytest.approx(10.) + assert marginals['u'].getStandardDeviation()[0] == pytest.approx(2.) + assert marginals['h'].getRange().getLowerBound()[0] == pytest.approx(4.) + assert marginals['h'].getRange().getUpperBound()[0] == pytest.approx(10.) + + +def test_evaluate_restores_parameter_values(beam): + dsg, _, _ = beam.get_graph([0, 3.]) + beam.evaluate(dsg) + + # The loop writes realizations onto the instance; afterwards the nodes carry their own value again + for node in dsg.inp_param_nodes: + assert dsg.inp_param_value(node) is node.value + assert dsg.inp_param_value(beam.par_rho) == 1.5 + + +def test_evaluate_stores_a_stochastic_output_per_metric(beam): + dsg, _, _ = beam.get_graph([0, 3.]) + objective_values, constraint_values = beam.evaluate(dsg) + + assert constraint_values == [] + assert len(objective_values) == 3 + # The deflection scatters with the load, so it has a fitted distribution; the mass does not depend on any + # stochastic parameter, so it is constant over the realizations and comes back as the value itself + deflection, mass = dsg.metric_value(beam.deflection_node), dsg.metric_value(beam.mass_node) + assert isinstance(deflection, StochasticOutput) + assert isinstance(mass, float) + + # Values are physical: no sign conventions are applied by the evaluator + assert mass == pytest.approx(7.8*3.*1.5) + assert deflection.quantile(.01) > 0. + assert deflection.std > 0. + + +def test_evaluate_pairs_each_metric_with_its_own_output(constrained_beam): + dsg, _, _ = constrained_beam.get_graph([0, 3.]) + objective_values, constraint_values = constrained_beam.evaluate(dsg) + by_name = {objective.name: value for objective, value in zip(constrained_beam.objectives, objective_values)} + + e_steel, load, thickness = 210., 100., 3. + assert by_name['mass'] == pytest.approx(7.8*thickness*1.5) # deterministic: a plain value + assert by_name['capacity'].mean == pytest.approx(e_steel*thickness**2 / load, rel=.1) + assert by_name['deflection'].mean == pytest.approx(load / (e_steel*thickness**3), rel=.1) + assert constraint_values[0].mean == pytest.approx(load / thickness**2, rel=.1) + + assert dsg.metric_value(constrained_beam.mass_node) == pytest.approx(by_name['mass']) + for node, name in [(constrained_beam.capacity_node, 'capacity'), (constrained_beam.deflection_node, 'deflection')]: + assert dsg.metric_value(node).mean == pytest.approx(by_name[name].mean) + assert dsg.metric_value(constrained_beam.stress_node).mean == pytest.approx(constraint_values[0].mean) + + +def test_evaluated_instances_keep_their_own_outputs(beam): + steel, _, _ = beam.get_graph([0, 3.]) + alu, _, _ = beam.get_graph([1, 3.]) + beam.evaluate(steel) + beam.evaluate(alu) + + means = [dsg.metric_value(beam.deflection_node).mean for dsg in (steel, alu)] + assert means[0] != means[1] + assert means[0] < means[1] # steel is stiffer, so it deflects less + + +def test_export_handles_stochastic_outputs(beam): + dsg, _, _ = beam.get_graph([0, 3.]) + beam.evaluate(dsg) + dsg._get_graph_for_export() + + title = beam.deflection_node.get_export_title() + assert 'mean =' in title and 'sigma =' in title + + +def test_problem_shape_and_parameter_space(constrained_beam): + problem = constrained_beam.get_problem() + + assert problem.n_obj == len(constrained_beam.objectives) == 3 + assert problem.n_ieq_constr == len(constrained_beam.constraints) == 1 + assert problem.n_var == len(constrained_beam.des_vars) + assert problem.param_space.parameter_names == constrained_beam.param_space.parameter_names + assert problem.param_space.parameter_names == ['E_alu', 'E_steel', 'load'] # rho_factor is deterministic + assert repr(problem) + + +def test_problem_applies_the_optimizer_conventions(constrained_beam): + # Regression: obj_is_max and con_ref were computed but never applied, so a maximized objective was minimized + problem = constrained_beam.get_problem() + x = np.array([[0, 3.]]) + out = problem.evaluate(x, return_as_dictionary=True) + + i_capacity = [objective.name for objective in constrained_beam.objectives].index('capacity') + i_mass = [objective.name for objective in constrained_beam.objectives].index('mass') + capacity = out['f_stochastic'][0, i_capacity] + + # capacity is maximized, so it is stored negated; mass is minimized and stored as-is + assert out['F'][0, i_capacity] < 0. + assert capacity.mean > 0. + assert out['F'][0, i_capacity] == pytest.approx(-Mean().scalarize(capacity)) + assert out['F'][0, i_mass] == pytest.approx(7.8*3.*1.5) + + # the constraint is 'stress <= 60', so g = stress - 60 + assert out['G'][0, 0] == pytest.approx(Mean().scalarize(out['g_stochastic'][0, 0]) - 60.) + + +def test_problem_evaluation_and_statistics(beam): + problem = beam.get_problem() + out = problem.evaluate(np.array([[0, 2.], [1, 4.]]), return_as_dictionary=True) + + assert out['F'].shape == (2, 3) + assert np.all(np.isfinite(out['F'])) + + assert out['f_stochastic'].shape == (2, 3) + + # Realizations really reach the model + deflection = out['f_stochastic'][0, 1] + assert isinstance(deflection, StochasticOutput) + assert deflection.std > 0. + assert out['F'][0, 1] == pytest.approx(Mean().scalarize(deflection)) + + +def test_problem_scalars_take_effect(): + evaluator = BeamStochasticEvaluator(stress_ref=60., + obj_scalar=[Mean(), Margin(k=2.), Mean()], + constr_scalar=[Quantile(q=.9)]) + problem = evaluator.get_problem() + out = problem.evaluate(np.array([[0, 3.]]), return_as_dictionary=True) + deflection, stress = out['f_stochastic'][0, 1], out['g_stochastic'][0, 0] + + # objectives are ordered by name: capacity, deflection, mass + assert out['F'][0, 1] == pytest.approx(Margin(k=2.).scalarize(deflection)) + assert out['F'][0, 1] > deflection.mean + + # Regression: the constraint was scalarized with the objective's scalar + assert out['G'][0, 0] == pytest.approx(Quantile(q=.9).scalarize(stress) - 60.) + assert out['G'][0, 0] != pytest.approx(Mean().scalarize(stress) - 60.) + + +@pytest.mark.parametrize('scalar', [Margin(k=2.), Quantile(q=.9)]) +def test_scalars_penalize_spread_of_maximized_metrics(scalar): + given = dict(scalar.__dict__) + evaluator = BeamStochasticEvaluator(stress_ref=60., obj_scalar=[scalar, Mean(), Mean()], constr_scalar=[scalar]) + problem = evaluator.get_problem() + out = problem.evaluate(np.array([[0, 3.]]), return_as_dictionary=True) + capacity = out['f_stochastic'][0, 0] + + # capacity is maximized, so its value is negated: the unfavorable side of its distribution is below the mean + assert -out['F'][0, 0] < capacity.mean + # stress must stay below its reference, so the unfavorable side is above the mean + assert out['G'][0, 0] + 60. > out['g_stochastic'][0, 0].mean + + assert scalar.__dict__ == given # the given scalar itself is not modified + + +def test_problem_uses_common_random_numbers_and_one_graph_per_point(beam): + problem = beam.get_problem() + n_calls, original = [0], beam.get_graph + + def _counting(*args, **kwargs): + n_calls[0] += 1 + return original(*args, **kwargs) + + beam.get_graph = _counting + problem.evaluate(np.array([[0, 2.], [0, 2.]]), return_as_dictionary=True) + + assert n_calls[0] == 2 # the architecture does not depend on the realization + first, second = beam.seen_loads[:20], beam.seen_loads[20:] + assert first == second # both identical design points saw the same realizations + + +@pytest.mark.parametrize('parallel_processes', [True, False]) +def test_problem_evaluates_in_parallel(parallel_processes): + x = np.array([[0, 2.], [1, 4.], [0, 3.], [1, 2.5]]) + f_serial = BeamStochasticEvaluator().get_problem().evaluate(x, return_as_dictionary=True)['F'] + + problem = BeamStochasticEvaluator().get_problem(n_parallel=2, parallel_processes=parallel_processes) + out = problem.evaluate(x, return_as_dictionary=True) + + assert problem.get_n_batch_evaluate() == 2 + assert np.all(np.isfinite(out['F'])) + assert out['F'] == pytest.approx(f_serial) # see the same realizations as the serial loop + + +def test_problem_with_polynomial_chaos(): + evaluator = BeamStochasticEvaluator(uq_method=PolynomialChaos(40, seed=42, degree=2)) + out = evaluator.get_problem().evaluate(np.array([[0, 2.]]), return_as_dictionary=True) + + assert np.all(np.isfinite(out['F'])) + + # Statistics come from the fitted expansion rather than from the 40 expensive evaluations, and the expansion + # itself is carried on the output, so Sobol indices are available downstream + chaos_result = out['f_stochastic'][0, 1].method_results + assert isinstance(chaos_result, ot.FunctionalChaosResult) + assert ot.FunctionalChaosSobolIndices(chaos_result).getSobolTotalIndex(0) >= 0. + + +def test_uav_example(): + from adsg_core.examples.robust_uav import RobustUAVStochasticEvaluator + + evaluator = RobustUAVStochasticEvaluator(MonteCarlo(25, seed=42), k=2.) + assert set(evaluator.param_space.parameter_names) == {'bsfc', 'drag_factor', 'eta_bat', 'headwind'} + assert not evaluator.par_payload.is_stochastic + + problem = evaluator.get_problem() + assert problem.n_obj == 2 + + x = np.array([evaluator.get_random_design_vector() for _ in range(4)], dtype=float) + out = problem.evaluate(x, return_as_dictionary=True) + + assert out['F'].shape == (4, 2) + assert np.all(np.isfinite(out['F'])) + + # endurance is maximized, so it is stored negated while the graph keeps the physical value + assert np.all(out['F'][:, 0] < 0.) + endurance = out['f_stochastic'][0, 0] + assert endurance.mean > 0. + assert endurance.std > 0. + + # The mass of the UAV does not depend on any of the uncertain parameters, so it is a plain value + mass = out['f_stochastic'][0, 1] + assert isinstance(mass, float) + assert out['F'][0, 1] == pytest.approx(mass) + + +def test_uav_example_statistics_helper(): + from adsg_core.examples.robust_uav import RobustUAVStochasticEvaluator + + evaluator = RobustUAVStochasticEvaluator(MonteCarlo(25, seed=1), k=2.) + dsg, _, _ = evaluator.get_graph(evaluator.get_random_design_vector()) + statistics = evaluator.evaluate_statistics(dsg) + + assert statistics['endurance_mean'] > 0. + assert statistics['endurance_std'] > 0. + assert statistics['endurance_robust'] < statistics['endurance_mean'] + assert statistics['mass'] > 0. + +def test_in_parallel_processes(): + x = np.array([[1, 2.], [1, 2.], [0, 2.], [0, 2.]]) + + problem = BeamStochasticEvaluator(uq_method=MonteCarlo(25, seed=42)) \ + .get_problem(n_parallel=3, parallel_processes=True) + f_parallel = problem.evaluate(x, return_as_dictionary=True)['F'] + + # Each worker evaluates the expansion on the same input sample, so identical design points agree + assert f_parallel[0] == pytest.approx(f_parallel[1]) diff --git a/docs/api_dsg.md b/docs/api_dsg.md index 8aa1f5f..7b0424f 100644 --- a/docs/api_dsg.md +++ b/docs/api_dsg.md @@ -45,6 +45,9 @@ ::: adsg_core.graph.adsg_nodes.DesignVariableNode handler: python +::: adsg_core.graph.adsg_nodes.InputParameterNode + handler: python + ::: adsg_core.graph.adsg_nodes.MetricNode handler: python diff --git a/docs/api_opt.md b/docs/api_opt.md index 0b49810..c38e525 100644 --- a/docs/api_opt.md +++ b/docs/api_opt.md @@ -11,6 +11,14 @@ - _evaluate - get_problem +::: adsg_core.optimization.stochastic_evaluator.DSGStochasticEvaluator + handler: python + options: + members: + - evaluate + - _evaluate + - get_problem + ::: adsg_core.optimization.dv_output_defs.DesVar handler: python @@ -20,8 +28,14 @@ ::: adsg_core.optimization.dv_output_defs.Constraint handler: python +::: adsg_core.optimization.dv_output_defs.InpParam + handler: python + ::: adsg_core.optimization.problem.DSGDesignSpace handler: python ::: adsg_core.optimization.problem.DSGArchOptProblem handler: python + +::: adsg_core.optimization.stochastic_problem.DSGStochasticArchOptProblem + handler: python diff --git a/docs/example_gnc.ipynb b/docs/example_gnc.ipynb index 66711c4..4a5bd2a 100644 --- a/docs/example_gnc.ipynb +++ b/docs/example_gnc.ipynb @@ -59,6 +59,12 @@ "start_time": "2024-01-27T08:54:13.133896200Z" }, "collapsed": false, + "execution": { + "iopub.execute_input": "2026-09-25T10:12:57.954459Z", + "iopub.status.busy": "2026-09-25T10:12:57.954459Z", + "iopub.status.idle": "2026-09-25T10:12:59.031158Z", + "shell.execute_reply": "2026-09-25T10:12:59.031158Z" + }, "jupyter": { "outputs_hidden": false } @@ -99,6 +105,12 @@ "start_time": "2024-01-27T08:54:13.144816600Z" }, "collapsed": false, + "execution": { + "iopub.execute_input": "2026-09-25T10:12:59.033173Z", + "iopub.status.busy": "2026-09-25T10:12:59.033173Z", + "iopub.status.idle": "2026-09-25T10:12:59.045207Z", + "shell.execute_reply": "2026-09-25T10:12:59.045207Z" + }, "jupyter": { "outputs_hidden": false } @@ -107,7 +119,7 @@ { "data": { "text/html": [ - "
\n", + "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", + "\n" + ], + "text/plain": [ + "