Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: |
python -m pip install --upgrade pip setuptools
pip install -r requirements-tests.txt
pip install -e .[opt]
pip install -e .

- name: Test with pytest
run: pytest adsg_core --durations=0
2 changes: 1 addition & 1 deletion .github/workflows/tests_slow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
run: |
python -m pip install --upgrade pip setuptools
pip install -r requirements-tests.txt
pip install -e .[opt,nb]
pip install -e .[nb]
ipython kernel install --name "python3" --user

- name: List dependencies
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ conda install numpy scipy~=1.9
pip install adsg-core
```

Optionally also install optimization algorithms ([SBArchOpt](sbarchopt.readthedocs.io/)):
Ensure optimization algorithms are installed ([SBArchOpt](sbarchopt.readthedocs.io/)):
```
pip install adsg-core[opt]
pip install sb-arch-opt
```

If you want to interact with the DSG from a [Jupyter notebook](https://jupyter.org/):
Expand Down
382 changes: 382 additions & 0 deletions adsg_core/examples/robust_uav.py

Large diffs are not rendered by default.

45 changes: 35 additions & 10 deletions adsg_core/graph/adsg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
SOFTWARE.
"""
import numpy as np
import openturns as ot
from typing import *
import networkx as nx
from natsort import natsorted
Expand All @@ -34,6 +35,7 @@
from adsg_core.graph.incompatibility import *
from adsg_core.graph.influence_matrix import *
from adsg_core.graph.choice_constraints import *
from sb_arch_opt.uncertainty import EvaluationOutput

__all__ = ['DSG', 'EdgeType', 'CDVNode', 'ChoiceConstraint', 'ChoiceConstraintType', 'DSGType', 'ADSG', 'ADSGType']

Expand All @@ -49,15 +51,16 @@ class DSG:
_taken_single_choices = []

def __init__(self, _graph=None, _influence_matrix=None, _status_array=None, _choice_con_map=None,
_des_var_values=None, _metric_values=None, **_):
_des_var_values=None, _inp_param_values=None, _metric_values=None, **_):
self._graph = _graph or self._get_empty_graph()
self._choice_constraints: List[ChoiceConstraint] = _choice_con_map or []
self._influence_matrix: Optional[InfluenceMatrix] = _influence_matrix
self._status_array: Optional[np.ndarray] = _status_array

self._update_connector_grouping_degrees()
self._des_var_values: Dict[DesignVariableNode, Union[float, int]] = (_des_var_values or {}).copy()
self._metric_values: Dict[MetricNode, float] = (_metric_values or {}).copy()
self._inp_param_values: Dict[InputParameterNode, Union[ot.DistributionImplementation, float]] = (_inp_param_values or {}).copy()
self._metric_values: Dict[MetricNode, EvaluationOutput] = (_metric_values or {}).copy()

@staticmethod
def _get_empty_graph():
Expand Down Expand Up @@ -185,6 +188,8 @@ def _get_graph_for_export(self):

for node in self.des_var_nodes:
node.assigned_value = self.des_var_value(node)
for node in self.inp_param_nodes:
node.assigned_value = self.inp_param_value(node)
for node in self.metric_nodes:
node.assigned_value = self.metric_value(node)

Expand All @@ -210,9 +215,9 @@ def get_nodes_by_subtype(self, type_):
"""Get all nodes in the graph that are or inherit from the given type"""
return get_nodes_by_subtype(self._graph, type_)

"""##########################################
### DESIGN VARIABLE AND METRICS FUNCTIONS ###
##########################################"""
"""############################################################
### DESIGN VARIABLE, INPUT PARAMETERS AND METRICS FUNCTIONS ###
############################################################"""

@property
def all_des_var_nodes(self) -> List[DesignVariableNode]:
Expand Down Expand Up @@ -283,21 +288,41 @@ def des_var_values(self) -> Dict[DesignVariableNode, Union[float, int]]:
def reset_des_var_values(self):
self._des_var_values = {}

@property
def inp_param_nodes(self) -> List[InputParameterNode]:
return self.get_nodes_by_type(InputParameterNode)

def set_inp_param_value(self, parameter_node: InputParameterNode, value: Union[ot.DistributionImplementation, float]):
"""
Set the value of the realization of the respective input parameter.
"""
self._inp_param_values[parameter_node] = value

def inp_param_value(self, parameter_node: InputParameterNode) -> Optional[Union[ot.DistributionImplementation, float]]:
return self._inp_param_values.get(parameter_node)

@property
def inp_param_values(self) -> Dict[InputParameterNode, Union[ot.DistributionImplementation, float]]:
return self._inp_param_values.copy()

def reset_inp_param_values(self):
self._inp_param_values = {}

@property
def metric_nodes(self) -> List[MetricNode]:
return self.get_nodes_by_type(MetricNode)

def set_metric_value(self, metric_node: MetricNode, value: float):
def set_metric_value(self, metric_node: MetricNode, value: EvaluationOutput):
"""
Set the value of a metric node.
"""
self._metric_values[metric_node] = value

def metric_value(self, metric_node) -> Optional[float]:
def metric_value(self, metric_node) -> Optional[EvaluationOutput]:
return self._metric_values.get(metric_node)

@property
def metric_values(self):
def metric_values(self) -> Dict[MetricNode, EvaluationOutput]:
return self._metric_values.copy()

def reset_metric_values(self):
Expand Down Expand Up @@ -685,7 +710,7 @@ def get_for_adjusted(self, removed_edges=None, removed_nodes=None, added_edges=N
dec_con_map_copy = self._choice_constraints.copy()
return self.__class__(_graph=graph_copy, _influence_matrix=self._influence_matrix,
_status_array=status_array if status_array is not None else self._status_array,
_choice_con_map=dec_con_map_copy, _des_var_values=self._des_var_values,
_choice_con_map=dec_con_map_copy, _des_var_values=self._des_var_values, _inp_param_values=self._inp_param_values,
_metric_values=self._metric_values, **kwargs)

def _mod_graph_adjust_kwargs(self, kwargs):
Expand All @@ -701,7 +726,7 @@ def get_for_kept_edges(self, kept_edges, **kwargs):
self._mod_graph_adjust_kwargs(kwargs)
return self.__class__(_graph=graph_copy, _influence_matrix=self._influence_matrix,
_status_array=self._status_array, _choice_con_map=self._choice_constraints,
_des_var_values=self._des_var_values, _metric_values=self._metric_values, **kwargs)
_des_var_values=self._des_var_values, _inp_param_values=self._inp_param_values, _metric_values=self._metric_values, **kwargs)

"""#########################################
### INCOMPATIBILITY CONSTRAINT FUNCTIONS ###
Expand Down
42 changes: 35 additions & 7 deletions adsg_core/graph/adsg_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
import enum
import itertools
import numpy as np
import openturns as ot
from typing import *
import networkx as nx
from collections import OrderedDict
from adsg_core.graph.graph_edges import *

__all__ = ['DSGNode', 'ChoiceNode', 'SelectionChoiceNode', 'ConnectionChoiceNode', 'ConnectorNode', 'NamedNode',
'ConnectorDegreeGroupingNode', 'DesignVariableNode', 'MetricNode', 'MetricType', 'EdgeType', 'EdgeTuple',
'ConnectorDegreeGroupingNode', 'DesignVariableNode', 'InputParameterNode', 'MetricNode', 'MetricType', 'EdgeType', 'EdgeTuple',
'NodeExportShape', 'ADSGNode', 'CollectorNode', 'NonSelectionNode']


Expand All @@ -56,7 +57,7 @@ class DSGNode:
"""

def __init__(self, obj_id=None, decision_id=None, option_id=None, src_decision_link_key=None,
tgt_decision_link_key=None, perm_decision_link_key=None, obj_ref=None):
tgt_decision_link_key=None, perm_decision_link_key=None, obj_ref=None):
self._obj_id = obj_id
self._id = None
self.update_node_id()
Expand Down Expand Up @@ -455,14 +456,44 @@ def str_context(self):
def __str__(self):
return f'DV[{self.name}]'

class InputParameterNode(DSGNode):
"""
Node representing input parameter that can be either deterministic or stochastic.
"""

def __init__(self, name, value: Union[ot.DistributionImplementation, float], idx=None, **kwargs):

self.name = name
self.idx = idx
self.value = value
self.assigned_value = None # Only for export
super(InputParameterNode, self).__init__(**kwargs)

@property
def is_stochastic(self) -> bool:
"""A parameter is stochastic if its value is a distribution; any other value is a fixed number"""
return isinstance(self.value, ot.DistributionImplementation)

def get_export_title(self) -> str:
if self.assigned_value is not None:
return f'{self.name} = {self.assigned_value}'
return f'{self.name} = {self.value}'

def get_export_color(self) -> str:
return _INP_OUT_COLOR

def str_context(self):
return 'INP[%s]' % self.name

def __str__(self):
return 'INP[%s]' % self.name

class MetricType(enum.Flag):
NONE = 0
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
Expand Down Expand Up @@ -501,10 +532,7 @@ def get_export_title(self) -> str:
role_str = ' [↑]' if self.dir > 0 else ' [↓]'

if self.assigned_value is not None:
if math.isnan(self.assigned_value):
role_str = f' = NaN'+role_str
else:
role_str = f' = {self.assigned_value:.4g}'+role_str
role_str = f' = {self.assigned_value}'+role_str

return self.name+role_str

Expand Down
2 changes: 2 additions & 0 deletions adsg_core/optimization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
from .evaluator import *
from .dv_output_defs import *
from .problem import *
from .stochastic_problem import *
from .stochastic_evaluator import *
from .graph_decoder import *
46 changes: 44 additions & 2 deletions adsg_core/optimization/dv_output_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions adsg_core/optimization/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from adsg_core.graph.adsg_nodes import MetricNode
from adsg_core.optimization.dv_output_defs import *
from adsg_core.optimization.graph_processor import *
from sb_arch_opt.uncertainty import EvaluationOutput

__all__ = ['DSGEvaluator', 'ADSGEvaluator']

Expand All @@ -49,7 +50,7 @@ def _choose_metric_type(self, objective: Objective, constraint: Constraint) -> U
raise RuntimeError(f'Metric {objective.name} can either be an objective or a constraint! '
f'Specify the metric type using node.type = MetricType.x')

def evaluate(self, dsg: DSGType) -> Tuple[List[float], List[float]]:
def evaluate(self, dsg: DSGType) -> Tuple[List[EvaluationOutput], List[EvaluationOutput]]:
"""
Evaluate a DSG instance. Returns a list of objective values and a list of constraint values.
"""
Expand All @@ -72,12 +73,12 @@ def evaluate(self, dsg: DSGType) -> Tuple[List[float], List[float]]:

return objective_values, constraint_values

def _evaluate(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, float]:
def _evaluate(self, dsg: DSGType, metric_nodes: List[MetricNode]) -> Dict[MetricNode, EvaluationOutput]:
"""
Implement this function to provide DSG evaluation.
Should return a mapping from metric node to float (NaN is allowed).
"""
raise NotImplementedError


ADSGEvaluator = DSGEvaluator # Backward compatibility
ADSGEvaluator = DSGEvaluator # Backward compatibility
Loading
Loading