def _find_wiring_per_pair(loc_pairs, start_proto_board, order, best_first,
    filter_wire_lengths, max_states_to_expand, verbose):
  """
  Wiring each pair separately.
  """
  proto_board = start_proto_board
  if verbose:
    print 'connecting %d pairs ...' % len(loc_pairs)
  all_num_expanded = []
  sign = 1 if order is ORDER_INCREASING else -1
  for i, loc_pair in enumerate(sorted(loc_pairs,
      key=lambda (loc_1, loc_2, resistor, node): sign * dist(loc_1, loc_2))):
    loc_1, loc_2, resistor, node = loc_pair
    if verbose:
      print '\t%d/%d connecting: %s -- %s' % (i + 1, len(loc_pairs), loc_1,
          loc_2)
    search_result, num_expanded = a_star(Proto_Board_Search_Node(proto_board,
        frozenset([loc_pair]), filter_wire_lengths=filter_wire_lengths),
        goal_test, heuristic, best_first=best_first,
        max_states_to_expand=max_states_to_expand)
    all_num_expanded.append(num_expanded)
    if search_result is not None:
      proto_board = search_result.state[0]
      if verbose:
        print proto_board
    else:
      if verbose:
        print '\tCouldn\'t do it :('
      return None, all_num_expanded
def _find_wiring_per_pair(loc_pairs, start_proto_board, order, best_first,
                          filter_wire_lengths, max_states_to_expand, verbose):
    """
  Wiring each pair separately.
  """
    proto_board = start_proto_board
    if verbose:
        print 'connecting %d pairs ...' % len(loc_pairs)
    all_num_expanded = []
    sign = 1 if order is ORDER_INCREASING else -1
    for i, loc_pair in enumerate(
            sorted(loc_pairs,
                   key=lambda
                   (loc_1, loc_2, resistor, node): sign * dist(loc_1, loc_2))):
        loc_1, loc_2, resistor, node = loc_pair
        if verbose:
            print '\t%d/%d connecting: %s -- %s' % (i + 1, len(loc_pairs),
                                                    loc_1, loc_2)
        search_result, num_expanded = a_star(
            Proto_Board_Search_Node(proto_board,
                                    frozenset([loc_pair]),
                                    filter_wire_lengths=filter_wire_lengths),
            goal_test,
            heuristic,
            best_first=best_first,
            max_states_to_expand=max_states_to_expand)
        all_num_expanded.append(num_expanded)
        if search_result is not None:
            proto_board = search_result.state[0]
            if verbose:
                print proto_board
        else:
            if verbose:
                print '\tCouldn\'t do it :('
            return None, all_num_expanded
def _find_wiring_per_node(loc_pairs, start_proto_board, order, best_first,
    filter_wire_lengths, max_states_to_expand, verbose):
  """
  Wiring the pairs of locations for each node separately.
  """
  loc_pairs_by_node = defaultdict(list)
  for loc_pair in loc_pairs:
    loc_pairs_by_node[loc_pair[3]].append(loc_pair)
  proto_board = start_proto_board
  if verbose:
    print 'interconnecting %d nodes ...' % len(loc_pairs_by_node)
  all_num_expanded = []
  f = len if order is ORDER_INCREASING else lambda l: -len(l)
  for node, loc_pair_collection in sorted(loc_pairs_by_node.items(),
      key=lambda (k, v): f(v)):
    if verbose:
      print '\tinterconnecting node \'%s\', %d pairs' % (node,
          len(loc_pair_collection))
    search_result, num_expanded = a_star(Proto_Board_Search_Node(proto_board,
        frozenset(loc_pair_collection),
        filter_wire_lengths=filter_wire_lengths), goal_test, heuristic,
        best_first=best_first, max_states_to_expand=max_states_to_expand)
    all_num_expanded.append(num_expanded)
    if search_result is not None:
      proto_board = search_result.state[0]
      if verbose:
        print proto_board
    else:
      if verbose:
        print '\tCouldn\'t do it :('
      return None, all_num_expanded
  if verbose:
    print '\tdone.'
  return proto_board, all_num_expanded
def find_wire_path(board_coverage, start_point, end_point):
  """
  Returns a list of tuples indicating a path from |start_point| to |end_point|
      on a board, doing an overall search. If the overall search takes too
      long, uses find_wire_path_simple. Tries to avoid points in
      |board_coverage|.
  """
  board_coverage = frozenset(board_coverage)
  search_result, num_expanded = a_star(Wire_Path_Search_Node(board_coverage,
      start_point), goal_test_for_end_point(end_point),
      heuristic_for_end_point(end_point), max_states_to_expand=1000,
      verbose=False)
  if search_result:
    return condensed_points([state[1] for state in search_result.get_path()])
  else:
    return find_wire_path_simple(board_coverage, start_point, end_point)
def _find_wiring_all(loc_pairs, start_proto_board, best_first,
    filter_wire_lengths, max_states_to_expand, verbose):
  """
  Wiring all pairs of locations in one search.
  """
  if verbose:
    print 'connecting %d pairs ...' % len(loc_pairs)
  search_result, num_expanded = a_star(Proto_Board_Search_Node(
      start_proto_board, frozenset(loc_pairs),
      filter_wire_lengths=filter_wire_lengths), goal_test, heuristic,
      best_first=best_first, max_states_to_expand=max_states_to_expand)
  if search_result is not None:
    if verbose:
      print '\tdone.'
    return search_result.state[0], [num_expanded]
  else:
    if verbose:
      print '\tCouldn\'t do it :('
    return None, [num_expanded]
def find_wire_path(board_coverage, start_point, end_point):
    """
  Returns a list of tuples indicating a path from |start_point| to |end_point|
      on a board, doing an overall search. If the overall search takes too
      long, uses find_wire_path_simple. Tries to avoid points in
      |board_coverage|.
  """
    board_coverage = frozenset(board_coverage)
    search_result, num_expanded = a_star(Wire_Path_Search_Node(
        board_coverage, start_point),
                                         goal_test_for_end_point(end_point),
                                         heuristic_for_end_point(end_point),
                                         max_states_to_expand=1000,
                                         verbose=False)
    if search_result:
        return condensed_points(
            [state[1] for state in search_result.get_path()])
    else:
        return find_wire_path_simple(board_coverage, start_point, end_point)
def _find_wiring_per_node(loc_pairs, start_proto_board, order, best_first,
                          filter_wire_lengths, max_states_to_expand, verbose):
    """
  Wiring the pairs of locations for each node separately.
  """
    loc_pairs_by_node = defaultdict(list)
    for loc_pair in loc_pairs:
        loc_pairs_by_node[loc_pair[3]].append(loc_pair)
    proto_board = start_proto_board
    if verbose:
        print 'interconnecting %d nodes ...' % len(loc_pairs_by_node)
    all_num_expanded = []
    f = len if order is ORDER_INCREASING else lambda l: -len(l)
    for node, loc_pair_collection in sorted(loc_pairs_by_node.items(),
                                            key=lambda (k, v): f(v)):
        if verbose:
            print '\tinterconnecting node \'%s\', %d pairs' % (
                node, len(loc_pair_collection))
        search_result, num_expanded = a_star(
            Proto_Board_Search_Node(proto_board,
                                    frozenset(loc_pair_collection),
                                    filter_wire_lengths=filter_wire_lengths),
            goal_test,
            heuristic,
            best_first=best_first,
            max_states_to_expand=max_states_to_expand)
        all_num_expanded.append(num_expanded)
        if search_result is not None:
            proto_board = search_result.state[0]
            if verbose:
                print proto_board
        else:
            if verbose:
                print '\tCouldn\'t do it :('
            return None, all_num_expanded
    if verbose:
        print '\tdone.'
    return proto_board, all_num_expanded
def _find_wiring_all(loc_pairs, start_proto_board, best_first,
                     filter_wire_lengths, max_states_to_expand, verbose):
    """
  Wiring all pairs of locations in one search.
  """
    if verbose:
        print 'connecting %d pairs ...' % len(loc_pairs)
    search_result, num_expanded = a_star(
        Proto_Board_Search_Node(start_proto_board,
                                frozenset(loc_pairs),
                                filter_wire_lengths=filter_wire_lengths),
        goal_test,
        heuristic,
        best_first=best_first,
        max_states_to_expand=max_states_to_expand)
    if search_result is not None:
        if verbose:
            print '\tdone.'
        return search_result.state[0], [num_expanded]
    else:
        if verbose:
            print '\tCouldn\'t do it :('
        return None, [num_expanded]