CommunityMap

(class from pyomo.contrib.community_detection.detection)

class pyomo.contrib.community_detection.detection.CommunityMap(community_map, type_of_community_map, with_objective, weighted_graph, random_seed, use_only_active_components, model, graph, graph_node_mapping, constraint_variable_map, graph_partition)[source]

Bases: object

This class is used to create CommunityMap objects which are returned by the detect_communities function. Instances of this class allow dict-like usage and store relevant information about the given community map, such as the model used to create them, their networkX representation, etc.

The CommunityMap object acts as a Python dictionary, mapping integer keys to tuples containing two lists (which contain the components in the given community) - a constraint list and variable list.

Methods: generate_structured_model visualize_model_graph

__init__(community_map, type_of_community_map, with_objective, weighted_graph, random_seed, use_only_active_components, model, graph, graph_node_mapping, constraint_variable_map, graph_partition)[source]

Constructor method for the CommunityMap class

Parameters:
  • community_map (dict) – a Python dictionary that maps arbitrary keys (in this case, integers from zero to the number of communities minus one) to two-list tuples containing Pyomo components in the given community

  • type_of_community_map (str) – a string that specifies the type of community map to be returned, the default is ‘constraint’. ‘constraint’ returns a dictionary (community_map) with communities based on constraint nodes, ‘variable’ returns a dictionary (community_map) with communities based on variable nodes, ‘bipartite’ returns a dictionary (community_map) with communities based on a bipartite graph (both constraint and variable nodes)

  • with_objective (bool) – a Boolean argument that specifies whether or not the objective function is included in the model graph (and thus in ‘community_map’); the default is True

  • weighted_graph (bool) – a Boolean argument that specifies whether community_map is created based on a weighted model graph or an unweighted model graph; the default is True (type_of_community_map=’bipartite’ creates an unweighted model graph regardless of this parameter)

  • random_seed (int or None) – an integer that is used as the random seed for the (heuristic) Louvain community detection

  • use_only_active_components (bool, optional) – a Boolean argument that specifies whether inactive constraints/objectives are included in the community map

  • model (Block) – a Pyomo model or block to be used for community detection

  • graph (nx.Graph) – a NetworkX graph with nodes and edges based on the Pyomo optimization model

  • graph_node_mapping (dict) – a dictionary that maps a number (which corresponds to a node in the networkX graph representation of the model) to a component in the model

  • constraint_variable_map (dict) – a dictionary that maps a numbered constraint to a list of (numbered) variables that appear in the constraint

  • graph_partition (dict) – the partition of the networkX model graph based on the Louvain community detection

Methods

__init__(community_map, ...)

Constructor method for the CommunityMap class

generate_structured_model()

Using the community map and the original model used to create this community map, we will create structured_model, which will be based on the original model but will place variables, constraints, and objectives into or outside of various blocks (communities) based on the community map.

items()

keys()

values()

visualize_model_graph([type_of_graph, ...])

This function draws a graph of the communities for a Pyomo model.

Member Documentation

generate_structured_model()[source]

Using the community map and the original model used to create this community map, we will create structured_model, which will be based on the original model but will place variables, constraints, and objectives into or outside of various blocks (communities) based on the community map.

Returns:

structured_model – a Pyomo model that reflects the nature of the community map

Return type:

Block

visualize_model_graph(type_of_graph='constraint', filename=None, pos=None)[source]

This function draws a graph of the communities for a Pyomo model.

The type_of_graph parameter is used to create either a variable-node graph, constraint-node graph, or bipartite graph of the Pyomo model. Then, the nodes are colored based on the communities they are in - which is based on the community map (self.community_map). A filename can be provided to save the figure, otherwise the figure is illustrated with matplotlib.

Parameters:
  • type_of_graph (str, optional) – a string that specifies the types of nodes drawn on the model graph, the default is ‘constraint’. ‘constraint’ draws a graph with constraint nodes, ‘variable’ draws a graph with variable nodes, ‘bipartite’ draws a bipartite graph (with both constraint and variable nodes)

  • filename (str, optional) – a string that specifies a path for the model graph illustration to be saved

  • pos (dict, optional) – a dictionary that maps node keys to their positions on the illustration

Returns:

  • fig (matplotlib figure) – the figure for the model graph drawing

  • pos (dict) – a dictionary that maps node keys to their positions on the illustration - can be used to create consistent layouts for graphs of a given model