ragraph.analysis.sequence
¶
Sequencing algorithms¶
Sequencing algorithms search for sequences of nodes according to some objective or logic. Simple are sorting by name and more elaborate examples sort according to Markov transition probabilities or sequences without feedback (edges to earlier nodes).
Available algorithms¶
markov
sequencing based on Markov transition probabilities between nodes.scc_tearing
efficient algorithm to sort the "trivial" part of sequencing quickly by putting the Strongly Connected Components (largest possible cycles) in a non-feedback order and then "tears" iteratively according to a tearing indicator.name
just sorts nodes by their name.tarjans_dfs
Tarjan's Depth First Search algorithm. Efficient sorting algorithm for Directed Acyclic Graphs (DAG).axis
Typical Multi-Domain Matrix axis sorting method.
Metrics¶
There are several metrics from literature available to grade sequences as well. The
metrics are documented over at metrics
.
Utilities¶
Finally, there are some utilities like branch-sorting (recursively sorting all branches
in a hierarchical tree instead of all leaf nodes as a whole) available in
utils
:
branchsort
Recursively sort children within a branch or hierarchy using any of the above.
axis
¶
|
docstring stub
Source code in ragraph/analysis/sequence/_axis.py
branchsort
¶
|
docstring stub
Source code in ragraph/analysis/sequence/utils.py
genetic
¶
|
docstring stub
Source code in ragraph/analysis/sequence/_genetic.py
markov
¶
|
docstring stub
Source code in ragraph/analysis/sequence/_markov.py
name
¶
|
Sequence nodes by node name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph
|
Graph to sequence nodes of. |
required |
nodes
|
Optional[Union[List[str], List[Node]]]
|
Nodes to sequence. |
None
|
Returns:
Type | Description |
---|---|
Tuple[Graph, List[Node]]
|
Sequenced nodes. |
Source code in ragraph/analysis/sequence/_name.py
scc_tearing
¶
|
docstring stub
Source code in ragraph/analysis/sequence/_scc_tearing.py
tarjans_dfs
¶
|
docstring stub
Source code in ragraph/analysis/sequence/_tarjan.py
_axis
¶
Sort nodes in a typical axis-ready format.
axis
¶
|
docstring stub
Source code in ragraph/analysis/sequence/_axis.py
get_axis_sequence
¶
Get nodes in a plot-axis-ready order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes
|
Iterable[Node]
|
Axis nodes to order. |
required |
kinds
|
Optional[List[str]]
|
Optional order of node kinds. |
None
|
sort_by_bus
|
bool
|
Whether to put bus nodes first. |
True
|
sort_by_width
|
bool
|
Whether to sort nodes by width in terms of leaf nodes. |
True
|
root_cache
|
Optional[Dict[Node, Node]]
|
Node to resulting root node cache. Any supplied dictionary will be updated in-place. |
None
|
child_cache
|
Optional[Dict[Node, List[Node]]]
|
Node to child nodes that have been seen cache. |
None
|
width_cache
|
Optional[Dict[Node, int]]
|
Node to node width cache. |
None
|
Returns:
Type | Description |
---|---|
List[Node]
|
Ordered nodes. |
Note
Axis ready means ordered by kind and buses nodes first where possible. Any supplied cache dictionary will be updated in-place.
Warning
When supplying node kinds it's your own responsibility to make sure that each node's kind actually is included in that node kind order.
Source code in ragraph/analysis/sequence/_axis.py
get_kinds
¶
Get node kinds in order of occurrence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes
|
Iterable[Node]
|
Nodes to iterate over. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List of Node kinds. |
Source code in ragraph/analysis/sequence/_axis.py
get_leafs
¶
Get leaf nodes from a linked-list style child_cache (e.g. parent to children).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parent
|
Node
|
Parent node. |
required |
child_cache
|
Dict[Node, List[Node]]
|
Linked-list style parent-children dictionary. |
required |
Returns:
Type | Description |
---|---|
List[Node]
|
Displayed leaf nodes as they occur under this parent. |
Source code in ragraph/analysis/sequence/_axis.py
get_root
¶
Get root node of a node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
Node
|
Nodes to find corresponding root nodes for. |
required |
root_cache
|
Optional[Dict[Node, Node]]
|
Node to resulting root node cache. Any supplied dictionary will be updated in-place. |
None
|
child_cache
|
Optional[Dict[Node, List[Node]]]
|
Node to child nodes that have been seen cache. Any supplied dictionary will be updated in-place. |
None
|
Returns:
Type | Description |
---|---|
Node
|
Root node. |
Note
Supply cache dictionaries to store intermediate results.
Source code in ragraph/analysis/sequence/_axis.py
get_root_key
¶
Calculate a sorting score for root nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
Node
|
Root node to obtain a score for. |
required |
kind_order
|
Dict[str, int]
|
Node kind to position dictionary. |
required |
sort_by_width
|
bool
|
Whether to sort nodes by width in terms of leaf nodes. |
required |
width_cache
|
Optional[Dict[Node, int]]
|
Node to node width cache. |
None
|
Returns:
Type | Description |
---|---|
Union[int, Tuple[int, int]]
|
Root node sorting key. |
Note
Sort by kinds first, width in terms of actual (not display) leaf nodes second.
Source code in ragraph/analysis/sequence/_axis.py
get_roots
¶
Get list of root nodes corresponding to some collection of nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes
|
Iterable[Node]
|
Collection of nodes to find corresponding root nodes for. |
required |
root_cache
|
Optional[Dict[Node, Node]]
|
Node to resulting root node cache. Any supplied dictionary will be updated in-place. |
None
|
child_cache
|
Optional[Dict[Node, List[Node]]]
|
Node to child nodes that have been seen cache. Any supplied dictionary will be updated in-place. |
None
|
Returns:
Type | Description |
---|---|
List[Node]
|
Root nodes. |
Note
Supply cache dictionaries to store intermediate results.
Source code in ragraph/analysis/sequence/_axis.py
get_sibling_key
¶
Calculate a sorting score for sibling nodes. Lower scores go first.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
Node
|
Node to score. |
required |
sort_by_bus
|
bool
|
Whether to put bus nodes first. |
required |
sort_by_width
|
bool
|
Whether to sort nodes by width in terms of leaf nodes. |
required |
width_cache
|
Optional[Dict[Node, int]]
|
Dictionary of nodes to displayed children. |
None
|
Returns:
Type | Description |
---|---|
Union[int, Tuple[int, int]]
|
Sibling node sorting score. |
Note
Bus nodes first, width in terms of actual (not display) leaf nodes second.
Source code in ragraph/analysis/sequence/_axis.py
get_width
¶
Get width of a node and update width cache in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
Node
|
Node to get the width of. |
required |
width_cache
|
Optional[Dict[Node, int]]
|
Node to node width cache. |
None
|
Returns:
Type | Description |
---|---|
int
|
Node width. |
Source code in ragraph/analysis/sequence/_axis.py
_genetic
¶
Genetic algorithms for sequencing purposes.
genetic
¶
|
docstring stub
Source code in ragraph/analysis/sequence/_genetic.py
genetic_sequencing
¶
|
Genetic sequencing of nodes in a graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph
|
Graph holding data. |
required |
nodes
|
List[Node]
|
Nodes to sequence. |
required |
evaluator
|
str
|
Evaluation method to use. One of "feedback_distance", "feedback_marks", or "lower_left_distance". |
'feedback_distance'
|
n_chromosomes
|
int
|
Number of chromosomes in each generation. |
required |
n_generations
|
int
|
Number of generations to simulate. |
required |
n_hall_of_fame
|
int
|
Hall of Fame size of best performing chromosomes. |
required |
p_crossover
|
float
|
Probability for a pair to be subjected to crossover. |
required |
p_mutation
|
float
|
Probability for each chromosome to be subjected to mutation. |
required |
p_swap
|
float
|
Probability for each gene to be swapped with another during mutation. |
required |
n_records
|
Optional[int]
|
Number of generation records to keep. |
None
|
inherit
|
bool
|
Whether to inherit edges between children when getting the adjacency matrix. |
True
|
edge_weights
|
Optional[List[str]]
|
Edge weights to consider when getting the adjacency matrix. |
None
|
Returns:
Type | Description |
---|---|
Lineage
|
Lineage object containing generations of chromosomes, generation records and |
Lineage
|
a hall of fame of best performing chromosomes. |
Source code in ragraph/analysis/sequence/_genetic.py
_markov
¶
Sorting algorithm based on the node dependency/influence in a Markov chain.
markov
¶
|
docstring stub
Source code in ragraph/analysis/sequence/_markov.py
_name
¶
Sequence nodes by name.
name
¶
|
Sequence nodes by node name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph
|
Graph to sequence nodes of. |
required |
nodes
|
Optional[Union[List[str], List[Node]]]
|
Nodes to sequence. |
None
|
Returns:
Type | Description |
---|---|
Tuple[Graph, List[Node]]
|
Sequenced nodes. |
Source code in ragraph/analysis/sequence/_name.py
_scc_tearing
¶
A sequencing algorithm for cyclic graphs that tears cycles based on a metric (penalty) function. By default we use the metric that the Markov sequencing algorithm uses to sort all nodes straight away.
scc_tearing
¶
|
docstring stub
Source code in ragraph/analysis/sequence/_scc_tearing.py
_tarjan
¶
Tarjan's Depth First Search Algorithm.
tarjans_dfs
¶
|
docstring stub
Source code in ragraph/analysis/sequence/_tarjan.py
metrics
¶
Sequence metrics.
feedback_crossover
¶
Jointly measure feedback marks and crossovers in an adjacency matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mat
|
ndarray
|
Adjacency matrix to analyze. |
required |
fb
|
float
|
Weight of feedback marks. |
0.9
|
co
|
float
|
Weight of feedback crossovers. |
0.1
|
Returns:
Type | Description |
---|---|
float
|
Resulting metric. |
Tuple[ndarray, ndarray]
|
Cell contribution matrices (feedback, crossover). |
Note
Crossovers are where the "axis" from feedback marks towards the diagonal cross, except when this crossing is a feedback mark in itself. Multiple lines from "above" in the matrix are summed, those from the right are NOT (treated as binary).
References
McCulley, C., and Bloebaum, C. L., 1996, “A Genetic Tool for Optimal Design Sequencing in Complex Engineering Systems,” Struct. Optim., 12(2), pp. 186-201.
Source code in ragraph/analysis/sequence/metrics.py
feedback_distance
¶
Measure the feedback length, e.g. distance from the adjacency matrix diagonal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mat
|
ndarray
|
Adjacency matrix to analyze. |
required |
Returns:
Type | Description |
---|---|
float
|
Resulting metric. |
ndarray
|
Cell contribution matrix. |
References
Gebala, D. A., & Eppinger, S. D. (1991). Methods for analyzing design procedures. ASME Design Technical Conferences (Design Theory and Methodology), 227-233. Miami.
Source code in ragraph/analysis/sequence/metrics.py
feedback_lower_left
¶
Jointly measure lower left distance to the adjacency matrix diagonal and feedback marks. Feedback and feedforward are penalized differently, but both via a quadratic lower-left distance factor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mat
|
ndarray
|
Adjacency matrix to analyze. |
required |
fb
|
float
|
Feedback adjacency multiplier. |
100.0
|
ff
|
float
|
Feedforward adjacency multiplier. |
1.0
|
Returns:
Type | Description |
---|---|
float
|
Resulting metric. |
ndarray
|
Cell contribution matrix. |
Note
Feedback marks above the diagonal are weighted 100 times more than those below the diagonal. The multiplier is offset by (n-1)^2.
References
Scott, J. A. (1999). A strategy for modelling the design-development phase of a product. University of Newcastle. See Equation 6.1 and Figure 6.4.
Source code in ragraph/analysis/sequence/metrics.py
feedback_marks
¶
Measure the sum of feedback marks in an adjacency matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mat
|
ndarray
|
Adjacency matrix to analyze. |
required |
Returns:
Type | Description |
---|---|
float
|
Resulting metric. |
ndarray
|
Cell contribution matrix. |
References
Steward, D. V., 1981, Systems Analysis and Management: Structure, Strategy and Design, Petrocelli Books, New York. Kusiak, A., & Wang, J. (1993). Decomposition of the Design Process. Journal of Mechanical Design, 115(4), 687. https://doi.org/10.1115/1.2919255
Source code in ragraph/analysis/sequence/metrics.py
lower_left
¶
Measure the distance to the lower left of the adjacency matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mat
|
ndarray
|
Adjacency matrix to analyze. |
required |
Returns:
Type | Description |
---|---|
float
|
Resulting metric. |
ndarray
|
Cell contribution matrix. |
References
Todd, D. S. (1997). Multiple criteria genetic algorithms in engineering design and operation. University of Newcastle.
Source code in ragraph/analysis/sequence/metrics.py
net_markov_flow_adjacency
¶
Calculate a flow balance as if the matrix would constitute a (weighted) Markov chain.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mat
|
ndarray
|
Adjacency matrix to analyze. |
required |
inf
|
float
|
The weight to subtract outgoing flow by. |
1.0
|
dep
|
float
|
The weight to add incoming flow by. |
1.0
|
mu
|
float
|
Evaporation constant when calculating flow through nodes. |
2.0
|
scale
|
bool
|
Whether to scale the inflow vector according to the adjacency matrix column sums. |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
Nodal flow balance as an array. |
Source code in ragraph/analysis/sequence/metrics.py
net_markov_flow_graph
¶
Calculate a flow balance as if the graph would constitute a (weighted) Markov chain.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph
|
Graph to analyze nodes of. |
required |
nodes
|
Optional[Union[List[Node], List[str]]]
|
Set of node (names) to calculate the flow with. |
None
|
inherit
|
bool
|
Whether to take into account edges between descendants of nodes. |
True
|
loops
|
bool
|
Whether to take into account self-loop edges. |
False
|
inf
|
float
|
The weight to subtract outgoing flow by. |
1.0
|
dep
|
float
|
The weight to add incoming flow by. |
1.0
|
mu
|
float
|
Evaporation constant when calculating flow through nodes. |
2.0
|
Returns:
Type | Description |
---|---|
ndarray
|
Nodal flow balance as an array. |
Note
Uses edge inheritance to calculate an adjacency matrix.
Source code in ragraph/analysis/sequence/metrics.py
utils
¶
Sequencing utils.
branchsort
¶
|
docstring stub
Source code in ragraph/analysis/sequence/utils.py
markov_decision
¶
Make a decision based on a Markov flow analysis of the adjacency matrix. The node with the lowest net markov flow is picked.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph
|
Graph data. |
required |
options
|
List[Node]
|
Nodes to decide between. |
required |
inf
|
float
|
The weight to subtract outgoing flow by. |
1.0
|
dep
|
float
|
The weight to add incoming flow by. |
1.0
|
mu
|
float
|
Evaporation constant when calculating flow through nodes. |
1.5
|
context
|
Optional[List[Node]]
|
Optional superset of nodes with respect to the options argument that constitutes the "complete" Markov chain that should be taken into account. |
None
|
Returns:
Type | Description |
---|---|
int
|
Index of node to pick. |