Example #1
0
def loc_pairs_for_node(node_locs, node):
    """
  Returns a list of tuples of the form  (loc_1, loc_2, node) such that the set
      of locations in |nodes_locs| is fully connected if all the output pairs of
      locations are connected. This is essentially an MST problem where the
      locations are the nodes and the weight of an edge between two locations
      is the distance (manhattan) between the two locations. We use Kruskal's
      greedy algorithm. |node| is the corresponding node in for the locations
      in the circuit.
  """
    if node == GROUND or node == POWER:
        return [(loc,
                 closest_rail_loc(
                     loc, GROUND_RAIL if node == GROUND else POWER_RAIL), node)
                for loc in node_locs]
    # find all possible pairs of locations
    all_loc_pairs = [(loc_1, loc_2) for i, loc_1 in enumerate(node_locs)
                     for loc_2 in node_locs[i + 1:]]
    # sort in increasing order of loc pair distance
    all_loc_pairs.sort(key=lambda loc_pair: dist(*loc_pair))
    disjoint_loc_pair_sets = Disjoint_Set_Forest()
    # initialize the graph as fully disconnected
    for loc in node_locs:
        disjoint_loc_pair_sets.make_set(loc)
    mst_loc_pairs = []
    # add edges to the graph until fully connected, but use the least expensive
    # edges to do so in the process
    for (loc_1, loc_2) in all_loc_pairs:
        if (disjoint_loc_pair_sets.find_set(loc_1) !=
                disjoint_loc_pair_sets.find_set(loc_2)):
            disjoint_loc_pair_sets.union(loc_1, loc_2)
            mst_loc_pairs.append((loc_1, loc_2, node))
    return mst_loc_pairs
Example #2
0
def node_disjoint_set_forest(node_locs_mapping):
  """
  Returns a Disjoint_Set_Forest representation of |node_locs_mapping|, a
      dictionary mapping node names to lists of locations on the proto board to
      be connected to the corresponding node.
  """
  forest = Disjoint_Set_Forest()
  for node, locs in node_locs_mapping.items():
    forest.make_set(node)
    for loc in locs:
      for section_loc in section_locs(loc):
        forest.make_set(section_loc)
        forest.union(section_loc, node)
  return forest
def loc_pairs_for_node(node_locs, node):
  """
  Returns a list of tuples of the form  (loc_1, loc_2, node) such that the set
      of locations in |nodes_locs| is fully connected if all the output pairs of
      locations are connected. This is essentially an MST problem where the
      locations are the nodes and the weight of an edge between two locations
      is the distance (manhattan) between the two locations. We use Kruskal's
      greedy algorithm. |node| is the corresponding node in for the locations
      in the circuit.
  """
  if node == GROUND or node == POWER:
    return [(loc, closest_rail_loc(loc, GROUND_RAIL if node == GROUND else
        POWER_RAIL), node) for loc in node_locs]
  # find all possible pairs of locations
  all_loc_pairs = [(loc_1, loc_2) for i, loc_1 in enumerate(node_locs) for
      loc_2 in node_locs[i + 1:]]
  # sort in increasing order of loc pair distance
  all_loc_pairs.sort(key=lambda loc_pair: dist(*loc_pair))
  disjoint_loc_pair_sets = Disjoint_Set_Forest()
  # initialize the graph as fully disconnected
  for loc in node_locs:
    disjoint_loc_pair_sets.make_set(loc)
  mst_loc_pairs = []
  # add edges to the graph until fully connected, but use the least expensive
  # edges to do so in the process
  for (loc_1, loc_2) in all_loc_pairs:
    if (disjoint_loc_pair_sets.find_set(loc_1) !=
        disjoint_loc_pair_sets.find_set(loc_2)):
      disjoint_loc_pair_sets.union(loc_1, loc_2)
      mst_loc_pairs.append((loc_1, loc_2, node))
  return mst_loc_pairs
 def __init__(self, wire_mappings=None, wires=None, pieces=None,
     loc_disjoint_set_forest=None):
   """
   |wire_mappings|: a dictionary mapping locations to other locations to which
       they are connected by a wire.
   |wires|: a list of the Wires on this proto board.
   |pieces|: a set of the Circuit_Pieces on this proto board.
   |loc_disjoint_set_forest|: an instance of Disjoint_Set_Forest representing
       disjoint sets of locations on the proto board that should alwas remain
       disconnected. This is used to avoid shorts.
   """
   self._wire_mappings = wire_mappings if wire_mappings is not None else {}
   self._wires = wires if wires is not None else []
   self._pieces = pieces if pieces is not None else set()
   self._loc_disjoint_set_forest = (loc_disjoint_set_forest if
       loc_disjoint_set_forest is not None else Disjoint_Set_Forest())