Edge

EdgeProperty Class

class constrainthg.hypergraph.EdgeProperty(*values)

Bases: Enum

Enumerated object describing various configurations of an Edge that can be passed during setup. Used as shorthand for common configurations.

Members

LEVEL1

Every source node in the edge must have the same index for the edge to be viable.

DISPOSE_ALL2

Every source node can only be used once per execution.

DISPOSE_ALL = 2
LEVEL = 1

Edge Class

class constrainthg.hypergraph.Edge(label: str, source_nodes: dict, target: Node, rel: Callable, via: Callable = None, index_via: Callable = None, weight: float = 1.0, index_offset: int = 0, disposable: list = None, edge_props: EdgeProperty = None)

Bases: object

A relationship along a set of nodes (the source) that produces a single value.

__init__(label: str, source_nodes: dict, target: Node, rel: Callable, via: Callable = None, index_via: Callable = None, weight: float = 1.0, index_offset: int = 0, disposable: list = None, edge_props: EdgeProperty = None)

Creates a new Edge object. This should generally be called from a Hypergraph object using the Hypergraph.add_edge method.

Parameters:
  • label (str) – A unique string identifier for the edge.

  • source_nodes (dict{str : Node | Tuple(str, str)} | list[Node |) – Tuple(str, str)] | Tuple(str, str) | Node A dictionary of Node objects forming the source nodes of the edge, where the key is the identifiable label for each source used in rel processing. The Node object may be a Node, or a length-2 Tuple (identifier : attribute) with the first element an identifier in the edge and the second element a string referencing an attribute of the identified Node to use as the value (a pseudo node).

  • target (Node) – Node that the edge maps to.

  • rel (Callable) – A function taking the values of the source nodes and returning a single value (the target).

  • via (Callable, optional) – A function that must be true for the edge to be traversable (viable). Defaults to unconditionally true if not set.

  • index_via (Callable, optional) – A function that takes in handles of source nodes as inputs in reference to the index of each referenced source node, returns a boolean condition relating the indices of each. Defaults to unconditionally true if not set, meaning any index of source node is valid.

  • weight (float > 0.0, default=1.0) – The quanitified cost of traversing the edge. Must be positive, akin to a distance measurement.

  • index_offset (int, default=0) – Offset to apply to the target once solved for. Akin to iterating to the next level of a cycle.

  • disposable (list, optional) – A list of source node handles that should not be evaluated for future cyclic executions of the edge. That is, each TNode that corresponds to a handle in disposable is removed from found_tnodes after a successful edge calculation.

  • edge_props (List(EdgeProperty) | EdgeProperty | str | int, optional) – A list of enumerated types that are used to configure the edge.

Properties

found_tnodesdict

A dict of lists of source_tnodes that are viable trees to a source node, with each sub_dict referenced by index. format: {node_label : list[TNode,]}

subset_alt_labelsdict

A dictionary of alternate node labels if a source node is a super set, format: {node_label : List[alt_node_label,]}

add_found_tnode(t: TNode) bool

Returns true if t successfully added as a viable path to a source node.

add_source_node(sn)

Adds a source node to an initialized edge.

Parameters:

sn (dict | Node | Tuple(str, str)) – The source node to be added to the edge.

check_tnode_already_found(t: TNode, source_node_label: str) bool

Returns True if t has already been found as a path to the source node.

create_found_tnodes_dict()

Creates the found_tnodes dictionary, accounting for super nodes.

dispose_of_tnodes_with_index(node_label: str, index: int) int

Removes each TNode from the edge property found_tnodes with a matching node_label and index. Returns the number of TNodes succesfully removed.

dispose_solved_tnodes(source_tnodes: list)

Once a TNode has been processed, it is removed from the found_tnodes list only if it has been marked for removal via inclusion in the disposable list.

This ensures that nodes from previous cycles don’t get revetted for future edges, greatly simplifying simulation.

Process

  1. Get an identifier from the disposal list

  2. Get the label for the source node corresponding to the identifier

  3. Find the tnode used in the solution (from source_tnodes) with the matching node_label

  4. Find the set of found_tnodes from the edge corresponding to the node label

  5. Remove the tnode in found_tnodes with the same index as the solved tnode

filtered_call(source_vals: dict, method: Callable)

Calls the method after filtering the source_vals to only include arguments to the method, making sure to handle issues with position.

find_mislabeled_source_node(source_nodes: dict, rel: Callable, via: Callable) str

Returns the key of the first source nodes whose label is unused for edge processing.

static get_named_arguments(methods: List[Callable]) set

Returns keywords for any keyed, required arguments (non-default).

get_psuedo_node_value(source_tnodes: list, pseudo_identifier: str, pseudo_attribute: str)

Identifies the source node and returns its attribute given by the pseudo-node notation.

get_relevant_node_label(t: TNode) str

Returns the node label of t or of the super set of t, if present.

get_source_node_identifier(offset: int = 0)

Returns a generic label for a source node.

get_source_tnode_combinations(t: TNode, DEBUG: bool = False)

Returns all viable combinations of source nodes using the TNode t.

get_source_vals_and_idxs(source_tnodes: list) tuple

Returns two dictionaries mapping a source identifier with a value (1) or its index (2).

handle_edge_property(edge_prop: EdgeProperty)

Perform macro functions defined by the EdgeProperty.

identify_labeled_source_nodes(source_nodes: dict, rel: Callable, via: Callable) dict

Makes best effort to match each relational argument in rel and via with a passed source node. Returns a {str: node} dictionary.

identify_source_nodes(source_nodes, rel: Callable = None, via: Callable = None) dict

Returns a {str: node} dictionary where each string is the keyword label used in the rel and via methods.

identify_unlabeled_source_nodes(source_nodes: list, rel: Callable, via: Callable) dict

Returns a {str: node} dictionary where each string is the keyword label used in the rel and via methods.

make_edge_level()

Adds a condition to the via function forcing all node indices to be equivalent.

process(source_tnodes: list)

Processes the tnodes to get the value of the target.

process_values(source_vals: dict, source_indices: dict = None)

Finds the target value based on the source values and indices.

setup_edge_properties(inputs: None) list

Parses the edge properties.

to_dict() dict

Returns a dictionary representation of the Edge object.

to_json() str

Returns a JSON representation of the Edge object.

static via_true(*args, **kwargs)

Returns true for all inputs (unconditional edge).

Home | Index | Search