From ba43cdf9ae520cd0cf17eacfecc4e6e298abc2e1 Mon Sep 17 00:00:00 2001 From: bial_fr Date: Tue, 15 Sep 2026 10:11:06 +0200 Subject: [PATCH 1/5] Robust optimization v 1.0 --- adsg_core/examples/robust_uav.py | 405 +++++++++++++++ adsg_core/graph/adsg.py | 70 ++- adsg_core/graph/adsg_nodes.py | 73 ++- adsg_core/optimization/evaluator.py | 27 +- adsg_core/optimization/graph_processor.py | 66 ++- adsg_core/optimization/problem.py | 4 +- .../optimization/stochastic_evaluator.py | 167 ++++++ adsg_core/optimization/stochastic_problem.py | 173 +++++++ adsg_core/tests/test_robust_optimization.py | 481 ++++++++++++++++++ 9 files changed, 1440 insertions(+), 26 deletions(-) create mode 100644 adsg_core/examples/robust_uav.py create mode 100644 adsg_core/optimization/stochastic_evaluator.py create mode 100644 adsg_core/optimization/stochastic_problem.py create mode 100644 adsg_core/tests/test_robust_optimization.py diff --git a/adsg_core/examples/robust_uav.py b/adsg_core/examples/robust_uav.py new file mode 100644 index 0000000..b6fa717 --- /dev/null +++ b/adsg_core/examples/robust_uav.py @@ -0,0 +1,405 @@ +""" +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 +from typing import * + +from adsg_core.graph.adsg import DSGType +from adsg_core.graph.adsg_basic import * +from adsg_core.graph.adsg_nodes import * +from adsg_core.optimization.stochastic_evaluator import StochasticDSGEvaluator +from sb_arch_opt.uncertainty import MonteCarlo, UQMethod, Scalarization, Mean, Margin, PolynomialChaos +from sb_arch_opt.algo.pymoo_interface import plot + +__all__ = ['RobustUAVEvaluator', '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 RobustUAVEvaluator(StochasticDSGEvaluator): + """ + Robust design of a multirotor UAV, as an example of architecture optimization under uncertainty. + + The design space mixes categorical, ordinal and continuous variables, and is *hierarchical*: which variables + exist at all depends on the architectural choices made higher up. Performance depends on a handful of + uncertain parameters, so every design point the optimizer visits is assessed by propagating those parameters + through the model instead of by a single deterministic run. + + 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 + + Note the parameter *nodes* carry no value: the distribution is set on the graph, so that a derived instance + exposes exactly the parameters its architecture has (`dsg.stochastic_space`). + + The hybrid architecture reaches a higher *mean* endurance, but its fuel consumption is far more uncertain + (~18% coefficient of variation, against ~3% for the electric efficiency). Increasing the margin factor `k` + therefore shifts the preferred architecture towards electric: the robust optimum is not the deterministic + optimum, which is the whole point of the example. + + 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.Uniform(4., 10.)) + 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, direction=1), 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.input_parameter_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.mean, + } + + +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). + + SBO is the right tool here: every design point costs a full uncertainty propagation, so the number of + evaluations must be kept low. The surrogate is built over the mixed-discrete hierarchical design space, + which SBArchOpt handles natively through the DSGDesignSpace exposed by `get_problem()`. + + Ensure the optional dependencies are installed: `pip install sb-arch-opt[arch_sbo]` + """ + from pymoo.optimize import minimize + from sb_arch_opt.algo.arch_sbo import get_arch_sbo_gp + + if seed is not None: + np.random.seed(seed) + + evaluator = RobustUAVEvaluator(uq, k=k, objective=objective) + + # One seeded draw of the uncertain parameters is reused for every design point (common random numbers), so + # that design points are comparable to each other and the surrogate sees a smooth response + 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 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=100, seed=42, degree=3, n_metamodel_samples=10000) + evaluator = RobustUAVEvaluator(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..d6dad11 100644 --- a/adsg_core/graph/adsg.py +++ b/adsg_core/graph/adsg.py @@ -22,7 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +import logging import numpy as np +import openturns as ot +import warnings from typing import * import networkx as nx from natsort import natsorted @@ -35,8 +38,28 @@ from adsg_core.graph.influence_matrix import * from adsg_core.graph.choice_constraints import * -__all__ = ['DSG', 'EdgeType', 'CDVNode', 'ChoiceConstraint', 'ChoiceConstraintType', 'DSGType', 'ADSG', 'ADSGType'] +try: + from sb_arch_opt.stochastic_problem import StochasticOutput + 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 StochasticOutput: + pass + +__all__ = ['DSG', 'EdgeType', 'CDVNode', 'ChoiceConstraint', 'ChoiceConstraintType', 'DSGType', 'ADSG', 'ADSGType', 'HAS_SB_ARCH_OPT', 'check_dependency'] + +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 DSG: """ @@ -49,7 +72,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, _input_parameter_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 +80,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._input_parameter_values: Dict[InputParameterNode, Union[ot.DistributionImplementation, float]] = (_input_parameter_values or {}).copy() + self._metric_values: Dict[MetricNode, Union[StochasticOutput, float]] = (_metric_values or {}).copy() @staticmethod def _get_empty_graph(): @@ -185,6 +209,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.input_parameter_nodes: + node.assigned_value = self.input_parameter_value(node) for node in self.metric_nodes: node.assigned_value = self.metric_value(node) @@ -210,9 +236,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,26 +309,48 @@ def des_var_values(self) -> Dict[DesignVariableNode, Union[float, int]]: def reset_des_var_values(self): self._des_var_values = {} + @property + def input_parameter_nodes(self) -> List[InputParameterNode]: + return self.get_nodes_by_type(InputParameterNode) + + def set_input_parameter_value(self, parameter_node: InputParameterNode, value: Union[ot.DistributionImplementation, float]): + """ + Set the value of the realization of the respective input parameter. + """ + self._input_parameter_values[parameter_node] = value + + def input_parameter_value(self, parameter_node: InputParameterNode) -> Optional[Union[ot.DistributionImplementation, float]]: + return self._input_parameter_values.get(parameter_node) + + @property + def input_parameter_values(self) -> Dict[InputParameterNode, Union[ot.DistributionImplementation, float]]: + return self._input_parameter_values.copy() + + def reset_input_parameter_values(self): + self._input_parameter_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: Union[StochasticOutput, float]): """ 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[Union[StochasticOutput, float]]: return self._metric_values.get(metric_node) @property - def metric_values(self): + def metric_values(self) -> Dict[MetricNode, Union[StochasticOutput, float]]: return self._metric_values.copy() def reset_metric_values(self): self._metric_values = {} + + """################################ ### CHOICE CONSTRAINT FUNCTIONS ### ################################""" @@ -685,7 +733,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, _input_parameter_values=self._input_parameter_values, _metric_values=self._metric_values, **kwargs) def _mod_graph_adjust_kwargs(self, kwargs): @@ -701,7 +749,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, _input_parameter_values=self._input_parameter_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 2f5ed0c..c934094 100644 --- a/adsg_core/graph/adsg_nodes.py +++ b/adsg_core/graph/adsg_nodes.py @@ -22,21 +22,46 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +import logging import math import copy import enum import itertools +import warnings + 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', - 'NodeExportShape', 'ADSGNode', 'CollectorNode', 'NonSelectionNode'] + +try: + from sb_arch_opt.uncertainty import StochasticOutput + + 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 StochasticOutput: + pass + +__all__ = ['check_dependency', 'DSGNode', 'ChoiceNode', 'SelectionChoiceNode', 'ConnectionChoiceNode', 'ConnectorNode', 'NamedNode', + 'ConnectorDegreeGroupingNode', 'DesignVariableNode', 'InputParameterNode', 'MetricNode', 'MetricType', 'EdgeType', 'EdgeTuple', + 'NodeExportShape', 'ADSGNode', 'CollectorNode', 'NonSelectionNode', 'HAS_SB_ARCH_OPT'] + +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 NodeExportShape(enum.Enum): CIRCLE = 0 ROUNDED_RECT = 1 @@ -56,7 +81,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() @@ -440,13 +465,46 @@ 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): + + self.name = name + self.idx = idx + self.value = value + self.assigned_value = None # Only for export + super(InputParameterNode, self).__init__() + + @property + def is_stochastic(self) -> bool: + if isinstance(self.value, ot.DistributionImplementation): + return True + else: + return False + + 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 f'PARAM.{self.name}' + + def __str__(self): + return f'PARAM[{self.name}]' + class MetricType(enum.Flag): NONE = 0 OBJECTIVE = enum.auto() 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 @@ -482,10 +540,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/evaluator.py b/adsg_core/optimization/evaluator.py index 7f97c7d..cada51e 100644 --- a/adsg_core/optimization/evaluator.py +++ b/adsg_core/optimization/evaluator.py @@ -22,14 +22,35 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +import logging import math +import warnings from typing import * from adsg_core.graph.adsg import DSGType from adsg_core.graph.adsg_nodes import MetricNode from adsg_core.optimization.dv_output_defs import * from adsg_core.optimization.graph_processor import * -__all__ = ['DSGEvaluator', 'ADSGEvaluator'] + +try: + from sb_arch_opt.uncertainty import StochasticOutput + + 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 StochasticOutput: + pass + +__all__ = ['DSGEvaluator', 'ADSGEvaluator', 'check_dependency', 'HAS_SB_ARCH_OPT'] + +log = logging.getLogger('adsg.opt') + + class DSGEvaluator(GraphProcessor): @@ -49,7 +70,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[Union[StochasticOutput, float]], List[Union[StochasticOutput, float]]]: """ Evaluate a DSG instance. Returns a list of objective values and a list of constraint values. """ @@ -80,4 +101,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..c315bfa 100644 --- a/adsg_core/optimization/graph_processor.py +++ b/adsg_core/optimization/graph_processor.py @@ -22,7 +22,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +import logging + import numpy as np +import openturns as ot +import warnings from typing import * from adsg_core.graph.adsg_nodes import * from adsg_core.optimization.dv_output_defs import * @@ -35,8 +39,35 @@ from adsg_core.optimization.assign_enc.time_limiter import run_timeout from adsg_core.optimization.assign_enc.assignment_manager import AssignmentManagerBase -__all__ = ['GraphProcessor', 'MetricType', 'SelChoiceEncoderType'] +try: + from sb_arch_opt.uncertainty import StochasticParameterSpace, StochasticParameter, StochasticOutput + + 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 StochasticParameterSpace: + pass + + class StochasticParameter: + pass + + class StochasticOutput: + pass + +__all__ = ['GraphProcessor', 'MetricType', 'SelChoiceEncoderType', 'HAS_SB_ARCH_OPT', 'check_dependency'] + +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') def catch_memory_overflow(func): def wrapper(obj: 'GraphProcessor', *args, **kwargs): @@ -379,6 +410,39 @@ 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 input_parameter_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.input_parameter_nodes: + if parameter_node.is_stochastic: + parameters.append(StochasticParameter(parameter_node.name, parameter_node.value)) + return StochasticParameterSpace(parameters) + + def param_realization(self, samples: np.ndarray, i_realization: int) -> Dict[InputParameterNode, float]: + """ + Return a dictionary of InputParameterNode with its associated sample realization. + """ + dictionary = {} + stochastic_realization = self.param_space.param_realization(samples, i_realization) + name_list = {param.name: param for param in stochastic_realization} + for parameter_node in self.input_parameter_nodes: + param = name_list.get(parameter_node.name) + if param is None: + # If deterministic use fixed value stored on the node + dictionary[parameter_node] = parameter_node.value + else: + # If stochastic use sample realization that was computed with UQ method + dictionary[parameter_node] = param.sample + return dictionary + @cached_property def metric_nodes(self) -> List[MetricNode]: return sorted(self.graph.get_nodes_by_type(MetricNode), key=lambda n: n.name) diff --git a/adsg_core/optimization/problem.py b/adsg_core/optimization/problem.py index 6cdc172..ca2fd69 100644 --- a/adsg_core/optimization/problem.py +++ b/adsg_core/optimization/problem.py @@ -223,7 +223,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 +273,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..a6acb52 --- /dev/null +++ b/adsg_core/optimization/stochastic_evaluator.py @@ -0,0 +1,167 @@ +""" +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 logging +import math +from typing import * +import warnings + +import numpy as np + +from adsg_core import DSGType, DSGEvaluator +from adsg_core.graph.adsg_nodes import MetricNode +from adsg_core.optimization.graph_processor import * + +__all__ = ['StochasticDSGEvaluator', 'StochasticADSGEvaluator', 'HAS_SB_ARCH_OPT', 'check_dependency'] +try: + from sb_arch_opt.stochastic_problem import StochasticArchOptProblem + from sb_arch_opt.uncertainty import Scalarization, StochasticResults, UQMethod, StochasticParameterSpace, StochasticOutput + 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 Scalarization: + pass + + class StochasticResults: + pass + + class UQMethod: + pass + + class StochasticParameterSpace: + pass + + class StochasticOutput: + pass + +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 StochasticDSGEvaluator(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: List[Scalarization] = None, + constr_scalar: 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 _evaluate(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, StochasticOutput]: + """ + Implement this function to provide stochastic DSG evaluation . + Override this function if external UQ tool is linked. + + 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) + parameter_nodes = dsg.input_parameter_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): + # Create a dictionary that associates parameters with its realization + sample_values = self.param_realization(stochastic_samples, i) + + if sample_values is None: + raise ValueError(f"No sample values available for realization {i}") + + # Set parameter realization or use its deterministic value on the DSG instance + for parameter in parameter_nodes: + value = sample_values[parameter] + dsg.set_input_parameter_value(parameter, value) + + # 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 + result = 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 parameter_nodes: + dsg.set_input_parameter_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] = result.outputs[i] + for i, constraint in enumerate(self.constraints): + if constraint.node in metric_nodes: + metric_map[constraint.node] = result.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 = StochasticDSGEvaluator \ 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..1e8ff07 --- /dev/null +++ b/adsg_core/optimization/stochastic_problem.py @@ -0,0 +1,173 @@ +""" +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 logging +import warnings +import numpy as np +from typing import * +from concurrent.futures import wait, ProcessPoolExecutor, ThreadPoolExecutor + +from adsg_core.optimization.stochastic_evaluator import StochasticDSGEvaluator +from adsg_core.optimization.problem import DSGDesignSpace + + +try: + from sb_arch_opt.stochastic_problem import StochasticArchOptProblem + from sb_arch_opt.uncertainty import * + 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 StochasticArchOptProblem: + pass + +__all__ = ['check_dependency', 'DSGStochasticArchOptProblem', 'HAS_SB_ARCH_OPT', 'ADSGStochasticArchOptProblem'] + +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 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 `StochasticDSGEvaluator` 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: `pip install sb-arch-opt` + + Example usage: + + ```python + from pymoo.optimize import minimize + from sb_arch_opt.algo.pymoo_interface import get_nsga2 + + evaluator = ... # Instance of StochasticDSGEvaluator + + algorithm = get_nsga2(pop_size=100) + problem = DSGStochasticArchOptProblem(evaluator, uq_method) + + result = minimize(problem, algorithm, termination=('n_eval', 500)) + ``` + """ + + def __init__(self, evaluator: StochasticDSGEvaluator, + param_space: StochasticParameterSpace, + uq_method: UQMethod, + obj_scalar: List[Scalarization] = None, + constr_scalar: List[Scalarization] = None, + n_parallel=None, parallel_processes=True): + check_dependency() + + 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] + + + 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): + """ + Overrides parent _arch_evaluate class to integrate it with StochasticDSGEvaluator, 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: + 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] + + self.stochastic_results = [] + + # Process results + for i, (obj_values, con_values) in enumerate(results): + self.stochastic_results.append(StochasticResults(obj_values+con_values)) + + # Reduce the sampled responses of each design point to the values the optimizer sees + obj_scalars = [output.reduce(self.obj_scalar[j]) for j, output in enumerate(obj_values)] + con_scalars = [output.reduce(self.ieq_constr_scalar[j]) for j, output in enumerate(con_values)] + + # Correct directions of objectives to represent minimization + f_out[i, :] = [-val if self.obj_is_max[j] else val for j, val in enumerate(obj_scalars)] + + # Correct directions and offset constraints to represent g(x) <= 0 + g_out[i, :] = [(val-self.con_ref[j][1])*(-1 if self.con_ref[j][0] else 1) + for j, val in enumerate(con_scalars)] + + def _print_extra_stats(self): + 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_robust_optimization.py b/adsg_core/tests/test_robust_optimization.py new file mode 100644 index 0000000..b1d9feb --- /dev/null +++ b/adsg_core/tests/test_robust_optimization.py @@ -0,0 +1,481 @@ +import math +import pytest +import numpy as np +import openturns as ot +from typing import * +from adsg_core.graph.adsg import DSGType +from adsg_core.graph.adsg_basic import * +from adsg_core.graph.adsg_nodes import * +from adsg_core.optimization.evaluator import * +from adsg_core.optimization.stochastic_evaluator import * +from adsg_core.optimization.graph_processor import * +from sb_arch_opt.uncertainty import (MonteCarlo, PolynomialChaos, StochasticOutput, StochasticResults, + Mean, Margin, Quantile) + + +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 _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 BeamEvaluator(StochasticDSGEvaluator): + """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.input_parameter_value(self.par_load) + e_modulus = dsg.input_parameter_value(self.par_e[material]) + rho_factor = dsg.input_parameter_value(self.par_rho) + self.seen_loads.append(load) + + values = { + self.mass_node: self.density[material]*thickness*rho_factor, + self.deflection_node: load / (e_modulus*thickness**3), + self.capacity_node: e_modulus*thickness**2 / load, + } + if self.stress_node is not None: + values[self.stress_node] = load / thickness**2 + return values + + +@pytest.fixture +def beam(): + return BeamEvaluator() + + +@pytest.fixture +def constrained_beam(): + return BeamEvaluator(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) == 'PARAM[E]' + assert stochastic.str_context() == 'PARAM.E' + 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.input_parameter_nodes) == {par_a, par_b} + assert dsg.input_parameter_value(par_a) is None # nothing assigned on the graph yet + + dist = ot.Normal(5., 1.) + dsg.set_input_parameter_value(par_a, dist) + assert dsg.input_parameter_value(par_a) is dist + + values = dsg.input_parameter_values + values[par_a] = 99. + assert dsg.input_parameter_value(par_a) is dist # the mapping is a copy + + assert dsg.copy().input_parameter_value(par_a) is dist # and survives derivation + + dsg.reset_input_parameter_values() + assert dsg.input_parameter_values == {} + + +def test_parameter_node_conditional_existence(n): + common = InputParameterNode('common', ot.Normal(0., 1.)) + only_a = InputParameterNode('only_a', ot.Normal(1., 1.)) + only_b = InputParameterNode('only_b', ot.Normal(2., 1.)) + processor = GraphProcessor(_dsg_with_branch_parameters(n, common, only_a, only_b)) + + assert len(processor.des_vars) == 1 + assert processor.param_space.n_parameters == 3 # the union, from the template graph + + seen = set() + for opt_idx in range(2): + graph, _, _ = processor.get_graph([opt_idx]) + par_nodes = set(graph.input_parameter_nodes) + + assert common in par_nodes + assert len(par_nodes) == 2 + assert (only_b not in par_nodes) if only_a in par_nodes else (only_b in par_nodes) + seen |= par_nodes + + assert seen == {common, only_a, only_b} + + +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_input_parameter_value(par_a, 1.) + graph_b.set_input_parameter_value(par_a, 2.) + + assert graph_a.input_parameter_value(par_a) == 1. + assert graph_b.input_parameter_value(par_a) == 2. + assert processor.graph.input_parameter_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.input_parameter_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_realization_is_keyed_by_node(n): + # The realization covers every input parameter node, stochastic or not: a deterministic one contributes its + # own value, so the evaluation always finds a number for every parameter it can reach on the graph + stochastic = InputParameterNode('u', ot.Normal(10., 2.)) + deterministic = InputParameterNode('rho', 1.225) + processor = GraphProcessor(_dsg_with_parameters(n, [stochastic, deterministic])) + + samples = MonteCarlo(5, seed=42).get_samples(processor.param_space) + + seen = [] + for i in range(5): + realization = processor.param_realization(samples, i) + + assert set(realization) == {stochastic, deterministic} + assert realization[deterministic] == 1.225 + assert realization[stochastic] == pytest.approx(samples[i, 0]) + assert all(isinstance(value, float) for value in realization.values()) + seen.append(realization[stochastic]) + + assert len(set(seen)) == 5 # a different realization each time + + +def test_param_realization_covers_branch_local_parameters(n): + # A parameter that only exists in one branch still has a column in the space, so every instance's nodes + # resolve against the same realization + common = InputParameterNode('common', ot.Normal(0., 1.)) + only_a = InputParameterNode('only_a', ot.Normal(1., 1.)) + only_b = InputParameterNode('only_b', ot.Normal(2., 1.)) + processor = GraphProcessor(_dsg_with_branch_parameters(n, common, only_a, only_b)) + + samples = MonteCarlo(5, seed=42).get_samples(processor.param_space) + realization = processor.param_realization(samples, 0) + + assert set(realization) == {common, only_a, only_b} + assert all(isinstance(value, float) for value in realization.values()) + + +def test_evaluator_constructs(beam): + # Regression: DSGEvaluator.__init__ was declared without self, so super() raised for every evaluator + assert isinstance(beam, DSGEvaluator) + assert isinstance(DSGEvaluator(beam.graph), DSGEvaluator) + assert [objective.name for objective in beam.objectives] == ['capacity', 'deflection', 'mass'] + + +def test_evaluate_uses_one_realization_per_sample(beam): + dsg, _, _ = beam.get_graph([0, 3.]) + beam.evaluate(dsg) + + assert len(beam.seen_loads) == 20 # once per sample + assert len(set(beam.seen_loads)) == 20 # and a different realization each time + assert np.std(beam.seen_loads) > 0. + i_load = beam.param_space.parameter_names.index('load') + assert np.allclose(sorted(beam.seen_loads), + sorted(beam.uq_method.get_samples(beam.param_space)[:, i_load])) + + +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.input_parameter_nodes: + assert dsg.input_parameter_value(node) is node.value + assert dsg.input_parameter_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 + for metric_node in dsg.metric_nodes: + value = dsg.metric_value(metric_node) + assert isinstance(value, StochasticOutput) + assert len(value.to_numpy()) == 20 + + # Values are physical: no sign conventions are applied by the evaluator + assert dsg.metric_value(beam.mass_node).mean == pytest.approx(7.8*3.*1.5) + assert np.all(dsg.metric_value(beam.deflection_node).to_numpy() > 0.) + + # The deflection scatters with the load, the mass does not + assert dsg.metric_value(beam.deflection_node).std > 0. + assert dsg.metric_value(beam.mass_node).std == pytest.approx(0.) + + +def test_evaluate_pairs_each_metric_with_its_own_output(constrained_beam): + # Regression: outputs were assigned by position over the instance's own metric_nodes, which is graph-ordered + # (mass, deflection, capacity, stress) rather than objectives-by-name-then-constraints (capacity, deflection, + # mass, stress). Every value below is checked against what the model computes, not against what evaluate() + # returned: both come from the same mapping, so a mispairing would corrupt them consistently. + 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'].mean == pytest.approx(7.8*thickness*1.5) + 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) + + for node, name in [(constrained_beam.mass_node, 'mass'), (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') + result = out['stochastic'][0] + + # capacity is maximized, so it is stored negated; mass is minimized and stored as-is + assert out['F'][0, i_capacity] < 0. + assert result.outputs[i_capacity].mean > 0. + assert out['F'][0, i_capacity] == pytest.approx(-result.outputs[i_capacity].reduce(Mean())) + 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(result.outputs[3].reduce(Mean()) - 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 len(out['stochastic']) == 2 + for result in out['stochastic']: + assert isinstance(result, StochasticResults) + assert len(result.outputs) == 3 + assert len(result.outputs[0].to_numpy()) == 20 + + # Realizations really reach the model + deflection = out['stochastic'][0].outputs[1] + assert len(set(deflection.to_numpy().tolist())) == 20 + assert deflection.std > 0. + + +def test_problem_scalars_take_effect(): + evaluator = BeamEvaluator(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) + result = out['stochastic'][0] + + # objectives are ordered by name: capacity, deflection, mass + assert out['F'][0, 1] == pytest.approx(result.outputs[1].reduce(Margin(k=2.))) + assert out['F'][0, 1] > result.outputs[1].mean + assert out['G'][0, 0] == pytest.approx(result.outputs[3].reduce(Quantile(q=.9)) - 60.) + + +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 + + +def test_problem_with_polynomial_chaos(): + evaluator = BeamEvaluator(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'])) + result = out['stochastic'][0] + + # Statistics come from the cheap metamodel rather than the 40 expensive evaluations + assert len(result.outputs[0].to_numpy()) == evaluator.uq_method.n_metamodel_samples + + # Note the fitted expansions are not carried through: DSGEvaluator.evaluate returns two lists of outputs, so + # DSGStochasticArchOptProblem rebuilds StochasticResults without the method result (no Sobol indices here) + assert result.method_result is None + + +def test_uav_example(): + from adsg_core.examples.robust_uav import RobustUAVEvaluator + + evaluator = RobustUAVEvaluator(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['stochastic'][0].outputs[0] + assert endurance.mean > 0. + assert endurance.std > 0. + + +def test_uav_example_statistics_helper(): + from adsg_core.examples.robust_uav import RobustUAVEvaluator + + evaluator = RobustUAVEvaluator(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. \ No newline at end of file From 2ce6a7d8c6420e8c1c22e8da628bc25548de461a Mon Sep 17 00:00:00 2001 From: bial_fr Date: Thu, 24 Sep 2026 14:37:43 +0200 Subject: [PATCH 2/5] Robust optimization 1.1 --- adsg_core/examples/robust_uav.py | 47 +- adsg_core/graph/adsg.py | 65 +- adsg_core/graph/adsg_nodes.py | 43 +- adsg_core/optimization/__init__.py | 2 + adsg_core/optimization/dv_output_defs.py | 46 +- adsg_core/optimization/evaluator.py | 28 +- adsg_core/optimization/graph_processor.py | 65 +- .../optimization/stochastic_evaluator.py | 91 +- adsg_core/optimization/stochastic_problem.py | 69 +- adsg_core/tests/test_robust_optimization.py | 267 ++-- adsg_core/uncertainty.py | 94 ++ docs/api_dsg.md | 3 + docs/api_opt.md | 17 + docs/guide.ipynb | 1183 ++++++++++++++--- 14 files changed, 1388 insertions(+), 632 deletions(-) create mode 100644 adsg_core/uncertainty.py diff --git a/adsg_core/examples/robust_uav.py b/adsg_core/examples/robust_uav.py index b6fa717..eddb681 100644 --- a/adsg_core/examples/robust_uav.py +++ b/adsg_core/examples/robust_uav.py @@ -25,15 +25,13 @@ import numpy as np import openturns as ot from typing import * - -from adsg_core.graph.adsg import DSGType from adsg_core.graph.adsg_basic import * from adsg_core.graph.adsg_nodes import * -from adsg_core.optimization.stochastic_evaluator import StochasticDSGEvaluator -from sb_arch_opt.uncertainty import MonteCarlo, UQMethod, Scalarization, Mean, Margin, PolynomialChaos +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 -__all__ = ['RobustUAVEvaluator', 'UAVOptionNode', 'run_sbo'] +__all__ = ['RobustUAVStochasticEvaluator', 'UAVOptionNode', 'run_sbo'] GRAVITY = 9.81 RHO_SL = 1.225 # Sea-level air density [kg/m3] @@ -51,15 +49,10 @@ def get_export_title(self) -> str: return f'{self.decision} = {self.value}' -class RobustUAVEvaluator(StochasticDSGEvaluator): +class RobustUAVStochasticEvaluator(DSGStochasticEvaluator): """ Robust design of a multirotor UAV, as an example of architecture optimization under uncertainty. - The design space mixes categorical, ordinal and continuous variables, and is *hierarchical*: which variables - exist at all depends on the architectural choices made higher up. Performance depends on a handful of - uncertain parameters, so every design point the optimizer visits is assessed by propagating those parameters - through the model instead of by a single deterministic run. - Design variables (13 total, 9 active in any one architecture): | Variable | Type | Active when | @@ -88,14 +81,6 @@ class RobustUAVEvaluator(StochasticDSGEvaluator): - `eta_bat`: battery + motor efficiency [-], only in the electric architecture - `bsfc`: brake specific fuel consumption [kg/kWh], only in the hybrid architecture - Note the parameter *nodes* carry no value: the distribution is set on the graph, so that a derived instance - exposes exactly the parameters its architecture has (`dsg.stochastic_space`). - - The hybrid architecture reaches a higher *mean* endurance, but its fuel consumption is far more uncertain - (~18% coefficient of variation, against ~3% for the electric efficiency). Increasing the margin factor `k` - therefore shifts the preferred architecture towards electric: the robust optimum is not the deterministic - optimum, which is the whole point of the example. - Metrics: - `endurance` [min]: maximized, stochastic, reduced with `Margin(k=-k)`, i.e. `mean - k*std` (the sign is @@ -150,7 +135,7 @@ def __init__(self, uq_method: UQMethod, k: float = 2., objective: int = None): # 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.Uniform(4., 10.)) + 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)) @@ -315,7 +300,7 @@ def _endurance(self, dsg: DSGType, sample: Dict[InputParameterNode, float]) -> f 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.input_parameter_values + parameters = dsg.inp_param_values for metric_node in metric_nodes: if metric_node.name == 'endurance': results[metric_node] = self._endurance(dsg, parameters) @@ -335,7 +320,7 @@ def evaluate_statistics(self, dsg: DSGType) -> Dict[str, float]: 'endurance_mean': endurance.mean, 'endurance_std': endurance.std, 'endurance_robust': endurance.mean - self.k*endurance.std, - 'mass': mass.mean, + 'mass': mass, } @@ -343,12 +328,6 @@ def run_sbo(uq: UQMethod, n_infill: int = 20, init_size: int = 40, k: float = 2. seed: int = None, verbose: bool = True): """ Optimize the robust UAV problem with SBArchOpt's Surrogate-Based Optimization (SBO). - - SBO is the right tool here: every design point costs a full uncertainty propagation, so the number of - evaluations must be kept low. The surrogate is built over the mixed-discrete hierarchical design space, - which SBArchOpt handles natively through the DSGDesignSpace exposed by `get_problem()`. - - Ensure the optional dependencies are installed: `pip install sb-arch-opt[arch_sbo]` """ from pymoo.optimize import minimize from sb_arch_opt.algo.arch_sbo import get_arch_sbo_gp @@ -356,12 +335,9 @@ def run_sbo(uq: UQMethod, n_infill: int = 20, init_size: int = 40, k: float = 2. if seed is not None: np.random.seed(seed) - evaluator = RobustUAVEvaluator(uq, k=k, objective=objective) + evaluator = RobustUAVStochasticEvaluator(uq, k=k, objective=objective) - # One seeded draw of the uncertain parameters is reused for every design point (common random numbers), so - # that design points are comparable to each other and the surrogate sees a smooth response problem = evaluator.get_problem(n_parallel=4) - problem.print_stats() sbo = get_arch_sbo_gp(problem, init_size=init_size) @@ -374,6 +350,7 @@ def run_sbo(uq: UQMethod, n_infill: int = 20, init_size: int = 40, k: float = 2. # 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: @@ -389,10 +366,9 @@ def run_sbo(uq: UQMethod, n_infill: int = 20, init_size: int = 40, k: float = 2. return evaluator, problem, result - if __name__ == '__main__': - uq = PolynomialChaos(n_evaluations=100, seed=42, degree=3, n_metamodel_samples=10000) - evaluator = RobustUAVEvaluator(uq, k=2, objective=None) + 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) @@ -401,5 +377,4 @@ def run_sbo(uq: UQMethod, n_infill: int = 20, init_size: int = 40, k: float = 2. 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 d6dad11..307967b 100644 --- a/adsg_core/graph/adsg.py +++ b/adsg_core/graph/adsg.py @@ -22,10 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import logging import numpy as np import openturns as ot -import warnings from typing import * import networkx as nx from natsort import natsorted @@ -37,29 +35,10 @@ from adsg_core.graph.incompatibility import * from adsg_core.graph.influence_matrix import * from adsg_core.graph.choice_constraints import * +from adsg_core.uncertainty import EvaluationOutput -try: - from sb_arch_opt.stochastic_problem import StochasticOutput +__all__ = ['DSG', 'EdgeType', 'CDVNode', 'ChoiceConstraint', 'ChoiceConstraintType', 'DSGType', 'ADSG', 'ADSGType'] - 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 StochasticOutput: - pass - -__all__ = ['DSG', 'EdgeType', 'CDVNode', 'ChoiceConstraint', 'ChoiceConstraintType', 'DSGType', 'ADSG', 'ADSGType', 'HAS_SB_ARCH_OPT', 'check_dependency'] - -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 DSG: """ @@ -72,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, _input_parameter_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 @@ -80,8 +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._input_parameter_values: Dict[InputParameterNode, Union[ot.DistributionImplementation, float]] = (_input_parameter_values or {}).copy() - self._metric_values: Dict[MetricNode, Union[StochasticOutput, 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(): @@ -209,8 +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.input_parameter_nodes: - node.assigned_value = self.input_parameter_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) @@ -310,47 +289,45 @@ def reset_des_var_values(self): self._des_var_values = {} @property - def input_parameter_nodes(self) -> List[InputParameterNode]: + def inp_param_nodes(self) -> List[InputParameterNode]: return self.get_nodes_by_type(InputParameterNode) - def set_input_parameter_value(self, parameter_node: InputParameterNode, value: Union[ot.DistributionImplementation, float]): + 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._input_parameter_values[parameter_node] = value + self._inp_param_values[parameter_node] = value - def input_parameter_value(self, parameter_node: InputParameterNode) -> Optional[Union[ot.DistributionImplementation, float]]: - return self._input_parameter_values.get(parameter_node) + def inp_param_value(self, parameter_node: InputParameterNode) -> Optional[Union[ot.DistributionImplementation, float]]: + return self._inp_param_values.get(parameter_node) @property - def input_parameter_values(self) -> Dict[InputParameterNode, Union[ot.DistributionImplementation, float]]: - return self._input_parameter_values.copy() + def inp_param_values(self) -> Dict[InputParameterNode, Union[ot.DistributionImplementation, float]]: + return self._inp_param_values.copy() - def reset_input_parameter_values(self): - self._input_parameter_values = {} + 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: Union[StochasticOutput, 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[Union[StochasticOutput, float]]: + def metric_value(self, metric_node) -> Optional[EvaluationOutput]: return self._metric_values.get(metric_node) @property - def metric_values(self) -> Dict[MetricNode, Union[StochasticOutput, float]]: + def metric_values(self) -> Dict[MetricNode, EvaluationOutput]: return self._metric_values.copy() def reset_metric_values(self): self._metric_values = {} - - """################################ ### CHOICE CONSTRAINT FUNCTIONS ### ################################""" @@ -733,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, _input_parameter_values=self._input_parameter_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): @@ -749,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, _input_parameter_values=self._input_parameter_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 c934094..fac16ed 100644 --- a/adsg_core/graph/adsg_nodes.py +++ b/adsg_core/graph/adsg_nodes.py @@ -22,13 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import logging import math import copy import enum import itertools -import warnings - import numpy as np import openturns as ot from typing import * @@ -36,31 +33,10 @@ from collections import OrderedDict from adsg_core.graph.graph_edges import * - -try: - from sb_arch_opt.uncertainty import StochasticOutput - - 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 StochasticOutput: - pass - -__all__ = ['check_dependency', 'DSGNode', 'ChoiceNode', 'SelectionChoiceNode', 'ConnectionChoiceNode', 'ConnectorNode', 'NamedNode', +__all__ = ['DSGNode', 'ChoiceNode', 'SelectionChoiceNode', 'ConnectionChoiceNode', 'ConnectorNode', 'NamedNode', 'ConnectorDegreeGroupingNode', 'DesignVariableNode', 'InputParameterNode', 'MetricNode', 'MetricType', 'EdgeType', 'EdgeTuple', - 'NodeExportShape', 'ADSGNode', 'CollectorNode', 'NonSelectionNode', 'HAS_SB_ARCH_OPT'] - -log = logging.getLogger('adsg.opt') - + 'NodeExportShape', 'ADSGNode', 'CollectorNode', 'NonSelectionNode'] -def check_dependency(): - if not HAS_SB_ARCH_OPT: - raise ImportError('Looks like SBArchOpt is not installed! Run: pip install sb-arch-opt') class NodeExportShape(enum.Enum): CIRCLE = 0 @@ -464,26 +440,23 @@ 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): + 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__() + super(InputParameterNode, self).__init__(**kwargs) @property def is_stochastic(self) -> bool: - if isinstance(self.value, ot.DistributionImplementation): - return True - else: - return False + """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: @@ -494,10 +467,10 @@ def get_export_color(self) -> str: return _INP_OUT_COLOR def str_context(self): - return f'PARAM.{self.name}' + return 'INP[%s]' % self.name def __str__(self): - return f'PARAM[{self.name}]' + return 'INP[%s]' % self.name class MetricType(enum.Flag): NONE = 0 diff --git a/adsg_core/optimization/__init__.py b/adsg_core/optimization/__init__.py index 2ada60c..3ee2db7 100644 --- a/adsg_core/optimization/__init__.py +++ b/adsg_core/optimization/__init__.py @@ -2,3 +2,5 @@ from .evaluator import * from .dv_output_defs import * from .problem import * +from .stochastic_problem import * +from .stochastic_evaluator 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 cada51e..3a4481e 100644 --- a/adsg_core/optimization/evaluator.py +++ b/adsg_core/optimization/evaluator.py @@ -22,35 +22,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import logging import math -import warnings from typing import * from adsg_core.graph.adsg import DSGType from adsg_core.graph.adsg_nodes import MetricNode from adsg_core.optimization.dv_output_defs import * from adsg_core.optimization.graph_processor import * +from adsg_core.uncertainty import EvaluationOutput - -try: - from sb_arch_opt.uncertainty import StochasticOutput - - 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 StochasticOutput: - pass - -__all__ = ['DSGEvaluator', 'ADSGEvaluator', 'check_dependency', 'HAS_SB_ARCH_OPT'] - -log = logging.getLogger('adsg.opt') - - +__all__ = ['DSGEvaluator', 'ADSGEvaluator'] class DSGEvaluator(GraphProcessor): @@ -70,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[Union[StochasticOutput, float]], List[Union[StochasticOutput, 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. """ @@ -93,7 +73,7 @@ def evaluate(self, dsg: DSGType) -> Tuple[List[Union[StochasticOutput, 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). diff --git a/adsg_core/optimization/graph_processor.py b/adsg_core/optimization/graph_processor.py index c315bfa..56f17ae 100644 --- a/adsg_core/optimization/graph_processor.py +++ b/adsg_core/optimization/graph_processor.py @@ -23,10 +23,8 @@ SOFTWARE. """ import logging - import numpy as np import openturns as ot -import warnings from typing import * from adsg_core.graph.adsg_nodes import * from adsg_core.optimization.dv_output_defs import * @@ -38,37 +36,13 @@ 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 - - -try: - from sb_arch_opt.uncertainty import StochasticParameterSpace, StochasticParameter, StochasticOutput - - 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 StochasticParameterSpace: - pass - - class StochasticParameter: - pass - - class StochasticOutput: - pass +from adsg_core.uncertainty import HAS_SB_ARCH_OPT, check_dependency, EvaluationOutput, StochasticParameter, StochasticParameterSpace __all__ = ['GraphProcessor', 'MetricType', 'SelChoiceEncoderType', 'HAS_SB_ARCH_OPT', 'check_dependency'] 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') - def catch_memory_overflow(func): def wrapper(obj: 'GraphProcessor', *args, **kwargs): try: @@ -221,6 +195,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""" @@ -411,7 +389,7 @@ def design_variable_nodes(self) -> List[DesignVariableNode]: return self.graph.ordered_choice_nodes(self.graph.des_var_nodes) @cached_property - def input_parameter_nodes(self) -> List[InputParameterNode]: + def inp_param_nodes(self) -> List[InputParameterNode]: return sorted(self.graph.get_nodes_by_type(InputParameterNode), key=lambda n: n.name) @cached_property @@ -421,27 +399,12 @@ def param_space(self) -> StochasticParameterSpace: Handles both stochastic and deterministic parameters. """ parameters = [] - for parameter_node in self.input_parameter_nodes: + for parameter_node in self.inp_param_nodes: if parameter_node.is_stochastic: - parameters.append(StochasticParameter(parameter_node.name, parameter_node.value)) - return StochasticParameterSpace(parameters) + dist = parameter_node.value + parameters.append(StochasticParameter(parameter_node.name, dist, ref=parameter_node)) - def param_realization(self, samples: np.ndarray, i_realization: int) -> Dict[InputParameterNode, float]: - """ - Return a dictionary of InputParameterNode with its associated sample realization. - """ - dictionary = {} - stochastic_realization = self.param_space.param_realization(samples, i_realization) - name_list = {param.name: param for param in stochastic_realization} - for parameter_node in self.input_parameter_nodes: - param = name_list.get(parameter_node.name) - if param is None: - # If deterministic use fixed value stored on the node - dictionary[parameter_node] = parameter_node.value - else: - # If stochastic use sample realization that was computed with UQ method - dictionary[parameter_node] = param.sample - return dictionary + return StochasticParameterSpace(parameters) @cached_property def metric_nodes(self) -> List[MetricNode]: @@ -481,6 +444,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/stochastic_evaluator.py b/adsg_core/optimization/stochastic_evaluator.py index a6acb52..7a41ebe 100644 --- a/adsg_core/optimization/stochastic_evaluator.py +++ b/adsg_core/optimization/stochastic_evaluator.py @@ -26,49 +26,19 @@ import math from typing import * import warnings - import numpy as np - -from adsg_core import DSGType, DSGEvaluator -from adsg_core.graph.adsg_nodes import MetricNode +from adsg_core.graph.adsg import DSGType +from adsg_core.graph.adsg_nodes import MetricNode, InputParameterNode +from adsg_core.optimization.evaluator import DSGEvaluator from adsg_core.optimization.graph_processor import * +from adsg_core.uncertainty import HAS_SB_ARCH_OPT, check_dependency, EvaluationOutput, Scalarization, UQMethod -__all__ = ['StochasticDSGEvaluator', 'StochasticADSGEvaluator', 'HAS_SB_ARCH_OPT', 'check_dependency'] -try: - from sb_arch_opt.stochastic_problem import StochasticArchOptProblem - from sb_arch_opt.uncertainty import Scalarization, StochasticResults, UQMethod, StochasticParameterSpace, StochasticOutput - 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 Scalarization: - pass - - class StochasticResults: - pass - - class UQMethod: - pass - - class StochasticParameterSpace: - pass - - class StochasticOutput: - pass +__all__ = ['DSGStochasticEvaluator', 'StochasticADSGEvaluator', 'HAS_SB_ARCH_OPT', 'check_dependency'] 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 StochasticDSGEvaluator(DSGEvaluator): +class DSGStochasticEvaluator(DSGEvaluator): """ Base class for implementing an evaluator for stochastic problem that directly evaluates DSG instances. Override _evaluate_sample to implement the evaluation. @@ -79,15 +49,14 @@ class StochasticDSGEvaluator(DSGEvaluator): def __init__(self, *args, uq_method: UQMethod, - obj_scalar: List[Scalarization] = None, - constr_scalar: List[Scalarization] = None, + 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): @@ -100,17 +69,29 @@ def get_problem(self, n_parallel=None, parallel_processes=True): self.constr_scalar, n_parallel=n_parallel, parallel_processes=parallel_processes) - - def _evaluate(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, StochasticOutput]: + def _param_realization(self, param_nodes: List[InputParameterNode], samples: np.ndarray, i_realization: int) -> Dict[InputParameterNode, float]: + dictionary = {} + stochastic_realization = self.param_space.param_realization(samples, i_realization) + name_list = {param.ref: param for param in stochastic_realization} + for param in self.inp_params: + if param.node in param_nodes: + stoch_param = name_list.get(param.node) + if stoch_param is None: + # If deterministic use fixed value stored on the node + dictionary[param.node] = param.node.value + else: + # If stochastic use sample realization that was computed with UQ method + dictionary[param.node] = stoch_param.sample + + return dictionary + + def _evaluate(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, EvaluationOutput]: """ - Implement this function to provide stochastic DSG evaluation . - Override this function if external UQ tool is linked. - 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) - parameter_nodes = dsg.input_parameter_nodes + param_nodes = dsg.inp_param_nodes n_s = stochastic_samples.shape[0] n_obj = len(self.objectives) @@ -121,15 +102,15 @@ def _evaluate(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[Metric for i in range(n_s): # Create a dictionary that associates parameters with its realization - sample_values = self.param_realization(stochastic_samples, i) + sample_values = self._param_realization(param_nodes, stochastic_samples, i) if sample_values is None: raise ValueError(f"No sample values available for realization {i}") # Set parameter realization or use its deterministic value on the DSG instance - for parameter in parameter_nodes: + for parameter in param_nodes: value = sample_values[parameter] - dsg.set_input_parameter_value(parameter, value) + dsg.set_inp_param_value(parameter, value) # Evaluate architecture for a realized sample value_map = self._evaluate_sample(dsg, metric_nodes) @@ -138,21 +119,21 @@ def _evaluate(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[Metric 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 - result = self.uq_method.process_results(np.concatenate([f_s, g_s], axis=1), param_space = self.param_space) + 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 parameter_nodes: - dsg.set_input_parameter_value(parameter_node, parameter_node.value) + 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] = result.outputs[i] + metric_map[objective.node] = outputs[i] for i, constraint in enumerate(self.constraints): - if constraint.node in metric_nodes: - metric_map[constraint.node] = result.outputs[n_obj+i] + 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 @@ -164,4 +145,4 @@ def _evaluate_sample(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict """ raise NotImplementedError -StochasticADSGEvaluator = StochasticDSGEvaluator \ No newline at end of file +StochasticADSGEvaluator = DSGStochasticEvaluator \ No newline at end of file diff --git a/adsg_core/optimization/stochastic_problem.py b/adsg_core/optimization/stochastic_problem.py index 1e8ff07..047509e 100644 --- a/adsg_core/optimization/stochastic_problem.py +++ b/adsg_core/optimization/stochastic_problem.py @@ -23,47 +23,25 @@ SOFTWARE. """ import logging -import warnings import numpy as np from typing import * from concurrent.futures import wait, ProcessPoolExecutor, ThreadPoolExecutor -from adsg_core.optimization.stochastic_evaluator import StochasticDSGEvaluator +from adsg_core.optimization.stochastic_evaluator import DSGStochasticEvaluator from adsg_core.optimization.problem import DSGDesignSpace - - -try: - from sb_arch_opt.stochastic_problem import StochasticArchOptProblem - from sb_arch_opt.uncertainty import * - 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 StochasticArchOptProblem: - pass +from adsg_core.uncertainty import HAS_SB_ARCH_OPT, check_dependency, Scalarization, StochasticArchOptProblem, StochasticParameterSpace, UQMethod __all__ = ['check_dependency', 'DSGStochasticArchOptProblem', 'HAS_SB_ARCH_OPT', 'ADSGStochasticArchOptProblem'] 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 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 `StochasticDSGEvaluator` class, which contains all information for + 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. @@ -78,7 +56,7 @@ class DSGStochasticArchOptProblem(StochasticArchOptProblem): from pymoo.optimize import minimize from sb_arch_opt.algo.pymoo_interface import get_nsga2 - evaluator = ... # Instance of StochasticDSGEvaluator + evaluator = ... # Instance of DSGStochasticEvaluator algorithm = get_nsga2(pop_size=100) problem = DSGStochasticArchOptProblem(evaluator, uq_method) @@ -87,11 +65,11 @@ class DSGStochasticArchOptProblem(StochasticArchOptProblem): ``` """ - def __init__(self, evaluator: StochasticDSGEvaluator, + def __init__(self, evaluator: DSGStochasticEvaluator, param_space: StochasticParameterSpace, uq_method: UQMethod, - obj_scalar: List[Scalarization] = None, - constr_scalar: List[Scalarization] = None, + obj_scalar: Optional[List[Scalarization]] = None, + constr_scalar: Optional[List[Scalarization]] = None, n_parallel=None, parallel_processes=True): check_dependency() @@ -104,18 +82,16 @@ def __init__(self, evaluator: StochasticDSGEvaluator, 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] - - 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): + 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 StochasticDSGEvaluator, but maintains the same functionality. + 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) @@ -142,24 +118,25 @@ def _arch_evaluate(self, x: np.ndarray, is_active_out: np.ndarray, f_out: np.nda else: results = [self.evaluator.evaluate(dsg) for dsg in dsg_instances] - self.stochastic_results = [] - # Process results - for i, (obj_values, con_values) in enumerate(results): - self.stochastic_results.append(StochasticResults(obj_values+con_values)) + 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 - # Reduce the sampled responses of each design point to the values the optimizer sees - obj_scalars = [output.reduce(self.obj_scalar[j]) for j, output in enumerate(obj_values)] - con_scalars = [output.reduce(self.ieq_constr_scalar[j]) for j, output in enumerate(con_values)] + f_stoch_out[i, j] = obj_output + f_out[i, j] = -val if self.obj_is_max[j] else val - # Correct directions of objectives to represent minimization - f_out[i, :] = [-val if self.obj_is_max[j] else val for j, val in enumerate(obj_scalars)] + 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 - # Correct directions and offset constraints to represent g(x) <= 0 - g_out[i, :] = [(val-self.con_ref[j][1])*(-1 if self.con_ref[j][0] else 1) - for j, val in enumerate(con_scalars)] + 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() diff --git a/adsg_core/tests/test_robust_optimization.py b/adsg_core/tests/test_robust_optimization.py index b1d9feb..51704f9 100644 --- a/adsg_core/tests/test_robust_optimization.py +++ b/adsg_core/tests/test_robust_optimization.py @@ -1,15 +1,12 @@ -import math import pytest import numpy as np import openturns as ot from typing import * -from adsg_core.graph.adsg import DSGType from adsg_core.graph.adsg_basic import * from adsg_core.graph.adsg_nodes import * -from adsg_core.optimization.evaluator import * from adsg_core.optimization.stochastic_evaluator import * from adsg_core.optimization.graph_processor import * -from sb_arch_opt.uncertainty import (MonteCarlo, PolynomialChaos, StochasticOutput, StochasticResults, +from sb_arch_opt.uncertainty import (MonteCarlo, PolynomialChaos, StochasticOutput, Mean, Margin, Quantile) @@ -19,6 +16,14 @@ def _dsg_with_parameters(n, 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): + return {} + + 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)]) @@ -26,7 +31,7 @@ def _dsg_with_branch_parameters(n, common, only_a, only_b): return dsg.set_start_nodes({n[0]}) -class BeamEvaluator(StochasticDSGEvaluator): +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 @@ -90,29 +95,34 @@ def _thickness(self, dsg: DSGType) -> float: def _evaluate_sample(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, float]: material = self._material(dsg) thickness = self._thickness(dsg) - load = dsg.input_parameter_value(self.par_load) - e_modulus = dsg.input_parameter_value(self.par_e[material]) - rho_factor = dsg.input_parameter_value(self.par_rho) + 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 = { - self.mass_node: self.density[material]*thickness*rho_factor, - self.deflection_node: load / (e_modulus*thickness**3), - self.capacity_node: e_modulus*thickness**2 / load, - } - if self.stress_node is not None: - values[self.stress_node] = load / thickness**2 + 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 BeamEvaluator() + return BeamStochasticEvaluator() @pytest.fixture def constrained_beam(): - return BeamEvaluator(stress_ref=60.) + return BeamStochasticEvaluator(stress_ref=60.) def test_input_parameter_node(n): @@ -125,8 +135,12 @@ def test_input_parameter_node(n): assert not deterministic.is_stochastic assert deterministic.value == 1.225 - assert str(stochastic) == 'PARAM[E]' - assert stochastic.str_context() == 'PARAM.E' + 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() @@ -142,43 +156,21 @@ def test_set_get_input_parameter_value(n): dsg = _dsg_with_parameters(n, [par_a, par_b]) assert dsg.feasible - assert set(dsg.input_parameter_nodes) == {par_a, par_b} - assert dsg.input_parameter_value(par_a) is None # nothing assigned on the graph yet + 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_input_parameter_value(par_a, dist) - assert dsg.input_parameter_value(par_a) is dist + dsg.set_inp_param_value(par_a, dist) + assert dsg.inp_param_value(par_a) is dist - values = dsg.input_parameter_values + values = dsg.inp_param_values values[par_a] = 99. - assert dsg.input_parameter_value(par_a) is dist # the mapping is a copy - - assert dsg.copy().input_parameter_value(par_a) is dist # and survives derivation - - dsg.reset_input_parameter_values() - assert dsg.input_parameter_values == {} - + assert dsg.inp_param_value(par_a) is dist # the mapping is a copy -def test_parameter_node_conditional_existence(n): - common = InputParameterNode('common', ot.Normal(0., 1.)) - only_a = InputParameterNode('only_a', ot.Normal(1., 1.)) - only_b = InputParameterNode('only_b', ot.Normal(2., 1.)) - processor = GraphProcessor(_dsg_with_branch_parameters(n, common, only_a, only_b)) - - assert len(processor.des_vars) == 1 - assert processor.param_space.n_parameters == 3 # the union, from the template graph - - seen = set() - for opt_idx in range(2): - graph, _, _ = processor.get_graph([opt_idx]) - par_nodes = set(graph.input_parameter_nodes) + assert dsg.copy().inp_param_value(par_a) is dist # and survives derivation - assert common in par_nodes - assert len(par_nodes) == 2 - assert (only_b not in par_nodes) if only_a in par_nodes else (only_b in par_nodes) - seen |= par_nodes - - assert seen == {common, only_a, only_b} + dsg.reset_inp_param_values() + assert dsg.inp_param_values == {} def test_parameter_values_isolated_between_instances(n): @@ -190,12 +182,12 @@ def test_parameter_values_isolated_between_instances(n): graph_a, _, _ = processor.get_graph([0]) graph_b, _, _ = processor.get_graph([1]) - graph_a.set_input_parameter_value(par_a, 1.) - graph_b.set_input_parameter_value(par_a, 2.) + graph_a.set_inp_param_value(par_a, 1.) + graph_b.set_inp_param_value(par_a, 2.) - assert graph_a.input_parameter_value(par_a) == 1. - assert graph_b.input_parameter_value(par_a) == 2. - assert processor.graph.input_parameter_value(par_a) is None # template untouched + 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): @@ -217,26 +209,45 @@ def test_param_space_holds_only_stochastic_parameters(n): deterministic = InputParameterNode('rho', 1.225) processor = GraphProcessor(_dsg_with_parameters(n, [stochastic, deterministic])) - assert [node.name for node in processor.input_parameter_nodes] == ['rho', 'u'] # sorted by name + 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_param_realization_is_keyed_by_node(n): # The realization covers every input parameter node, stochastic or not: a deterministic one contributes its # own value, so the evaluation always finds a number for every parameter it can reach on the graph stochastic = InputParameterNode('u', ot.Normal(10., 2.)) deterministic = InputParameterNode('rho', 1.225) - processor = GraphProcessor(_dsg_with_parameters(n, [stochastic, deterministic])) + evaluator = _evaluator(_dsg_with_parameters(n, [stochastic, deterministic])) - samples = MonteCarlo(5, seed=42).get_samples(processor.param_space) + samples = MonteCarlo(5, seed=42).get_samples(evaluator.param_space) + param_nodes = [stochastic, deterministic] seen = [] for i in range(5): - realization = processor.param_realization(samples, i) + realization = evaluator._param_realization(param_nodes, samples, i) - assert set(realization) == {stochastic, deterministic} assert realization[deterministic] == 1.225 assert realization[stochastic] == pytest.approx(samples[i, 0]) assert all(isinstance(value, float) for value in realization.values()) @@ -251,32 +262,18 @@ def test_param_realization_covers_branch_local_parameters(n): common = InputParameterNode('common', ot.Normal(0., 1.)) only_a = InputParameterNode('only_a', ot.Normal(1., 1.)) only_b = InputParameterNode('only_b', ot.Normal(2., 1.)) - processor = GraphProcessor(_dsg_with_branch_parameters(n, common, only_a, only_b)) + evaluator = _evaluator(_dsg_with_branch_parameters(n, common, only_a, only_b)) - samples = MonteCarlo(5, seed=42).get_samples(processor.param_space) - realization = processor.param_realization(samples, 0) + samples = MonteCarlo(5, seed=42).get_samples(evaluator.param_space) + realization = evaluator._param_realization([common, only_a, only_b], samples, 0) assert set(realization) == {common, only_a, only_b} assert all(isinstance(value, float) for value in realization.values()) - -def test_evaluator_constructs(beam): - # Regression: DSGEvaluator.__init__ was declared without self, so super() raised for every evaluator - assert isinstance(beam, DSGEvaluator) - assert isinstance(DSGEvaluator(beam.graph), DSGEvaluator) - assert [objective.name for objective in beam.objectives] == ['capacity', 'deflection', 'mass'] - - -def test_evaluate_uses_one_realization_per_sample(beam): - dsg, _, _ = beam.get_graph([0, 3.]) - beam.evaluate(dsg) - - assert len(beam.seen_loads) == 20 # once per sample - assert len(set(beam.seen_loads)) == 20 # and a different realization each time - assert np.std(beam.seen_loads) > 0. - i_load = beam.param_space.parameter_names.index('load') - assert np.allclose(sorted(beam.seen_loads), - sorted(beam.uq_method.get_samples(beam.param_space)[:, i_load])) + # Only the nodes this instance carries are resolved, and they take the same columns of the same design + instance_only = evaluator._param_realization([common, only_a], samples, 0) + assert set(instance_only) == {common, only_a} + assert instance_only[common] == realization[common] def test_evaluate_restores_parameter_values(beam): @@ -284,9 +281,9 @@ def test_evaluate_restores_parameter_values(beam): beam.evaluate(dsg) # The loop writes realizations onto the instance; afterwards the nodes carry their own value again - for node in dsg.input_parameter_nodes: - assert dsg.input_parameter_value(node) is node.value - assert dsg.input_parameter_value(beam.par_rho) == 1.5 + 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): @@ -295,18 +292,16 @@ def test_evaluate_stores_a_stochastic_output_per_metric(beam): assert constraint_values == [] assert len(objective_values) == 3 - for metric_node in dsg.metric_nodes: - value = dsg.metric_value(metric_node) - assert isinstance(value, StochasticOutput) - assert len(value.to_numpy()) == 20 + # 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 dsg.metric_value(beam.mass_node).mean == pytest.approx(7.8*3.*1.5) - assert np.all(dsg.metric_value(beam.deflection_node).to_numpy() > 0.) - - # The deflection scatters with the load, the mass does not - assert dsg.metric_value(beam.deflection_node).std > 0. - assert dsg.metric_value(beam.mass_node).std == pytest.approx(0.) + 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): @@ -319,13 +314,13 @@ def test_evaluate_pairs_each_metric_with_its_own_output(constrained_beam): 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'].mean == pytest.approx(7.8*thickness*1.5) + 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) - for node, name in [(constrained_beam.mass_node, 'mass'), (constrained_beam.capacity_node, 'capacity'), - (constrained_beam.deflection_node, 'deflection')]: + 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) @@ -369,16 +364,16 @@ def test_problem_applies_the_optimizer_conventions(constrained_beam): i_capacity = [objective.name for objective in constrained_beam.objectives].index('capacity') i_mass = [objective.name for objective in constrained_beam.objectives].index('mass') - result = out['stochastic'][0] + 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 result.outputs[i_capacity].mean > 0. - assert out['F'][0, i_capacity] == pytest.approx(-result.outputs[i_capacity].reduce(Mean())) + 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(result.outputs[3].reduce(Mean()) - 60.) + assert out['G'][0, 0] == pytest.approx(Mean().scalarize(out['g_stochastic'][0, 0]) - 60.) def test_problem_evaluation_and_statistics(beam): @@ -388,30 +383,30 @@ def test_problem_evaluation_and_statistics(beam): assert out['F'].shape == (2, 3) assert np.all(np.isfinite(out['F'])) - assert len(out['stochastic']) == 2 - for result in out['stochastic']: - assert isinstance(result, StochasticResults) - assert len(result.outputs) == 3 - assert len(result.outputs[0].to_numpy()) == 20 + assert out['f_stochastic'].shape == (2, 3) # Realizations really reach the model - deflection = out['stochastic'][0].outputs[1] - assert len(set(deflection.to_numpy().tolist())) == 20 + 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 = BeamEvaluator(stress_ref=60., - obj_scalar=[Mean(), Margin(k=2.), Mean()], - constr_scalar=[Quantile(q=.9)]) + 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) - result = out['stochastic'][0] + 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(result.outputs[1].reduce(Margin(k=2.))) - assert out['F'][0, 1] > result.outputs[1].mean - assert out['G'][0, 0] == pytest.approx(result.outputs[3].reduce(Quantile(q=.9)) - 60.) + 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.) def test_problem_uses_common_random_numbers_and_one_graph_per_point(beam): @@ -430,25 +425,36 @@ def _counting(*args, **kwargs): 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) # workers see the same realizations as the serial loop + + def test_problem_with_polynomial_chaos(): - evaluator = BeamEvaluator(uq_method=PolynomialChaos(40, seed=42, degree=2)) + 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'])) - result = out['stochastic'][0] - # Statistics come from the cheap metamodel rather than the 40 expensive evaluations - assert len(result.outputs[0].to_numpy()) == evaluator.uq_method.n_metamodel_samples - - # Note the fitted expansions are not carried through: DSGEvaluator.evaluate returns two lists of outputs, so - # DSGStochasticArchOptProblem rebuilds StochasticResults without the method result (no Sobol indices here) - assert result.method_result is None + # 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 RobustUAVEvaluator + from adsg_core.examples.robust_uav import RobustUAVStochasticEvaluator - evaluator = RobustUAVEvaluator(MonteCarlo(25, seed=42), k=2.) + 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 @@ -463,15 +469,20 @@ def test_uav_example(): # endurance is maximized, so it is stored negated while the graph keeps the physical value assert np.all(out['F'][:, 0] < 0.) - endurance = out['stochastic'][0].outputs[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 RobustUAVEvaluator + from adsg_core.examples.robust_uav import RobustUAVStochasticEvaluator - evaluator = RobustUAVEvaluator(MonteCarlo(25, seed=1), k=2.) + evaluator = RobustUAVStochasticEvaluator(MonteCarlo(25, seed=1), k=2.) dsg, _, _ = evaluator.get_graph(evaluator.get_random_design_vector()) statistics = evaluator.evaluate_statistics(dsg) diff --git a/adsg_core/uncertainty.py b/adsg_core/uncertainty.py new file mode 100644 index 0000000..9826d8c --- /dev/null +++ b/adsg_core/uncertainty.py @@ -0,0 +1,94 @@ +""" +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 warnings +from typing import * + +__all__ = ['HAS_SB_ARCH_OPT', 'check_dependency', 'EvaluationOutput', 'Scalarization', 'StochasticArchOptProblem', + 'StochasticOutput', 'StochasticParameter', 'StochasticParameterSpace', 'UQMethod'] + +try: + from sb_arch_opt.stochastic_problem import StochasticArchOptProblem + from sb_arch_opt.uncertainty import (Scalarization, StochasticOutput, StochasticParameter, StochasticParameterSpace, + UQMethod, MonteCarlo, PolynomialChaos, Mean, Margin) + from sb_arch_opt.sampling import TrailRepairWarning + + warnings.simplefilter('ignore', category=TrailRepairWarning) + + HAS_SB_ARCH_OPT = True + + EvaluationOutput = Union[StochasticOutput, float] + """Output either distribution or numeric value.""" + +except ImportError: + HAS_SB_ARCH_OPT = False + + + class StochasticArchOptProblem: + pass + + + class Scalarization: + pass + + + class StochasticOutput: + pass + + + class StochasticParameter: + pass + + + class StochasticParameterSpace: + pass + + + class UQMethod: + pass + + + class MonteCarlo: + pass + + + class PolynomialChaos: + pass + + + class Mean: + pass + + + class Margin: + pass + + + EvaluationOutput = Union[StochasticOutput, float] + """Output either distribution or numeric value.""" + + +def check_dependency(): + if not HAS_SB_ARCH_OPT: + raise ImportError('Looks like SBArchOpt is not installed! Run: pip install sb-arch-opt[uncertainty]') \ No newline at end of file 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..a3947e4 100644 --- a/docs/api_opt.md +++ b/docs/api_opt.md @@ -1,5 +1,8 @@ # DSG Optimization API Reference +::: adsg_core.uncertainty.EvaluationOutput + handler: python + ::: adsg_core.optimization.graph_processor.GraphProcessor handler: python @@ -11,6 +14,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 +31,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/guide.ipynb b/docs/guide.ipynb index a3ff105..ec68d68 100644 --- a/docs/guide.ipynb +++ b/docs/guide.ipynb @@ -28,12 +28,23 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2026-09-24T08:50:08.493589700Z", + "start_time": "2026-09-24T08:50:06.910553100Z" + }, + "execution": { + "iopub.execute_input": "2026-09-24T12:00:22.590356Z", + "iopub.status.busy": "2026-09-24T12:00:22.589359Z", + "iopub.status.idle": "2026-09-24T12:00:24.178111Z", + "shell.execute_reply": "2026-09-24T12:00:24.178111Z" + } + }, "outputs": [ { "data": { "text/html": [ - "
\n", + "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Design variables:\n", + " DV: C1 [2 opts]\n", + " DV: DV1 [3 opts]\n", + " DV: DV2 [3 opts, ord]\n", + "Input parameters:\n", + " INP: INP1 = Normal(mu = 10, sigma = 1)\n", + " INP: INP2 = 2.0\n", + "Objectives:\n", + " OBJ: Objective [min]\n", + "Constraints:\n", + " CON: Constraint [>= 2.00]\n", + "\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import openturns as ot\n", + "from adsg_core import BasicDSG, NamedNode, DesignVariableNode, \\\n", + " MetricNode, MetricType, GraphProcessor, InputParameterNode\n", + "\n", + "# Create the DSG and some nodes\n", + "dsg = BasicDSG()\n", + "n = [NamedNode(f'N{i}') for i in range(10)]\n", + "\n", + "# Define a selection choice\n", + "dsg.add_selection_choice('C1', n[0], [n[1], n[2]])\n", + "\n", + "# Define a conditional additional design variables (discrete)\n", + "dsg.add_edge(n[1], DesignVariableNode('DV1', options=['Red', 'Green', 'Blue']))\n", + "dsg.add_edge(n[1], InputParameterNode('INP1', value=ot.Normal(10., 1.)))\n", + "dsg.add_edge(n[2], DesignVariableNode('DV2', options=['A', 'B', 'C'], is_ordinal=True))\n", + "dsg.add_edge(n[2], InputParameterNode('INP2', value=2.))\n", + "\n", + "# Define a generic metric node\n", + "dsg.add_edge(n[0], MetricNode('Objective', direction=-1,\n", + " type_=MetricType.OBJECTIVE))\n", + "\n", + "# Define a constraint (can be conditional)\n", + "dsg.add_edge(n[2], MetricNode('Constraint', direction=1, ref=2,\n", + " type_=MetricType.CONSTRAINT))\n", + "\n", + "# Set start nodes and render\n", + "dsg = dsg.set_start_nodes({n[0]})\n", + "dsg.render()\n", + "\n", + "# Check their roles in an optimization problem\n", + "processor = GraphProcessor(dsg)\n", + "nl = '\\n'\n", + "print(f'Design variables:{nl}{nl.join(\" \"+str(dv) for dv in processor.des_vars)}')\n", + "print(f'Input parameters:{nl}{nl.join(\" \"+str(inp) for inp in processor.inp_params)}')\n", + "print(f'Objectives:{nl}{nl.join(\" \"+str(obj) for obj in processor.objectives)}')\n", + "print(f'Constraints:{nl}{nl.join(\" \"+str(con) for con in processor.constraints)}')\n", + "print('')\n", + "\n", + "dsg.render_legend(['NODE_START', 'EDGE_DERIVE', 'CHOICE_SEL', 'METRICS'])" + ] + }, { "cell_type": "markdown", "metadata": { @@ -1977,13 +2253,19 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "metadata": { "ExecuteTime": { - "end_time": "2024-01-26T14:04:58.736933500Z", - "start_time": "2024-01-26T14:04:58.682159Z" + "end_time": "2026-09-24T08:50:10.022139Z", + "start_time": "2026-09-24T08:50:10.001628700Z" }, "collapsed": false, + "execution": { + "iopub.execute_input": "2026-09-24T12:00:25.626934Z", + "iopub.status.busy": "2026-09-24T12:00:25.626934Z", + "iopub.status.idle": "2026-09-24T12:00:25.632932Z", + "shell.execute_reply": "2026-09-24T12:00:25.632932Z" + }, "jupyter": { "outputs_hidden": false } @@ -1992,7 +2274,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", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "