sadraddini / pypolycontain

A Toolbox For Polytopic Objects, Operations, and Containment Problems
20 stars 5 forks source link

zonotope_distance_point returns values larger than distance to centroid #1

Closed wualbert closed 5 years ago

wualbert commented 5 years ago

When given a query point q and zonotope z=zonotope(x,G), zonotope_distance_point(z,x) may return something larger than distance(x,q). Example code:

import numpy as np
from pypolycontain.lib.zonotope import zonotope,zonotope_distance_point

iterations = 100
for i in range(iterations):
    centroid_range = iterations
    generator_range = 3
    dimension = 2
    m = np.random.random_integers(dimension, 10)
    G = (np.random.rand(dimension, m) - 0.5) * generator_range
    x = (np.random.rand(dimension, 1) - 0.5) * centroid_range
    z1=zonotope(x,G,color="red")
    query_point = (np.random.rand(1, dimension) - 0.5) * centroid_range
    query_point = query_point.reshape(-1, 1)
    d_centroid_query_point = np.subtract(np.ndarray.flatten(x), \
                              np.ndarray.flatten(query_point))
    d_zonotope_query_point = zonotope_distance_point(z1,query_point)
    print('x: ',x, 'query_point: ', query_point)
    print('dist(x,q): %d, dist(zonotope, q): %d' %(np.linalg.norm(d_centroid_query_point),d_zonotope_query_point))
    assert(d_zonotope_query_point <= np.linalg.norm(d_centroid_query_point))

Example output:

...
('x: ', array([[-11.8958544 ],
       [-15.76381685]]), 'query_point: ', array([[-27.91779239],
       [ 37.10888876]]))
dist(x,q): 55, dist(zonotope, q): 72
Traceback (most recent call last):
  File ".../bounding_box_closest_polytope/pypolycontain/tests/test_zonotope_distance_2.py", line 27, in <module>
    assert(d_zonotope_query_point <= np.linalg.norm(d_centroid_query_point))
AssertionError
sadraddini commented 5 years ago

Thanks for pointing this out. It's not really a bug. The underlying metric is not L2 or L_infinity. It is based on G and it is explained in Section 4.2 in this paper. Nevertheless, it is a good idea to have L1,L2 and L_Infinity norms as well. Will work on it and push it.

sadraddini commented 5 years ago

Added a distance (P,x) function to pypolycontain.lib.AH_polytope It works for H-polytopes, zonotopes, and AH-polytopes. The norms can be L1, L2 or L infinity.