Skip to main content

compute_layers

Function compute_layers 

Source
fn compute_layers(
    graph: &Graph<MarketSet, GraphEdge, Directed>,
    order: &[NodeIndex],
) -> Vec<MarketSet>
Expand description

Compute layers of market sets from the topological order

This function works by computing the rank of each node in the graph based on the longest path from any root node to that node. Any nodes with the same rank are independent and can be solved in parallel. Nodes with different rank must be solved in order from highest rank (leaf nodes) to lowest rank (root nodes).

This function computes the ranks of each node, groups nodes by rank, and then produces a final ordered Vec of MarketSets which gives the order in which to solve the investment decisions.

Market sets with the same rank (i.e., can be solved in parallel) are grouped into MarketSet::Layer. Market sets that are alone in their rank remain as-is (i.e. either Single or Cycle). Layers can contain a mix of Single and Cycle market sets.

For example, given the following graph:

    A
   / \
  B   C
 / \   \
D   E   F

Rank 0: A -> MarketSet::Single Rank 1: B, C -> MarketSet::Layer Rank 2: D, E, F -> MarketSet::Layer

These are returned as a Vec<MarketSet> from highest rank to lowest (i.e. the D, E, F layer first, then the B, C layer, then the singleton A).

Arguments:

  • graph - The investment graph. Any cycles in the graph MUST have already been compressed. This will be necessary anyway as computing a topological sort to obtain the order requires an acyclic graph.
  • order - The topological order of the graph nodes. Computed using petgraph::algo::toposort.

Returns: A Vec of MarketSets in the order they should be solved, with independent sets grouped into MarketSet::Layers.