Esempio n. 1
0
def test_coordinates_from_distance(p, n):
    # Build vector of possible distances
    distances = np.arange(2**(n * p), dtype=np.int64)

    # Compute coordinates as vector result
    vector_result = coordinates_from_distances(p, n, distances)

    # Compare with reference
    reference_hc = HilbertCurve(p, n)
    for i, distance in enumerate(distances):
        # Reference
        expected = tuple(reference_hc.point_from_distance(distance))

        # Scalar result
        scalar_result = tuple(coordinate_from_distance(p, n, distance))
        assert scalar_result == expected

        # Vector result
        assert tuple(vector_result[i, :]) == expected
Esempio n. 2
0
def makeHilbertLocationHeuristic(
    preferences: NDArray[np.floating[Any]]
) -> Callable[[NDArray[np.bool_]], Tuple[int, int]]:
    curve_size = math.ceil(
        math.sqrt(max(preferences.shape[0], preferences.shape[1])))
    logger.debug(curve_size)
    curve_size = 4
    h_curve = HilbertCurve(curve_size, 2)
    h_coords = (h_curve.point_from_distance(i) for i in itertools.count())
    cell_order = fill_with_curve(preferences, h_coords)

    # logger.debug(cell_order)

    def hilbertLocationHeuristic(wave: NDArray[np.bool_]) -> Tuple[int, int]:
        unresolved_cell_mask = numpy.count_nonzero(wave, axis=0) > 1
        cell_weights = numpy.where(unresolved_cell_mask, cell_order, numpy.inf)
        row, col = numpy.unravel_index(numpy.argmin(cell_weights),
                                       cell_weights.shape)
        return row.item(), col.item()

    return hilbertLocationHeuristic