Example #1
0
    def compress(self, X):
        n = X.shape[0]

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)

        # Construct nearest neighbour graph
        G = np.zeros([n, n])
        for i in range(n):
            neighbours = np.argsort(D[i])[:self.nn + 1]
            for j in neighbours:
                G[i, j] = D[i, j]
                G[j, i] = D[j, i]

        # Compute ISOMAP distances
        D = utils.dijkstra(G)

        # If two points are disconnected (distance is Inf)
        # then set their distance to the maximum
        # distance in the graph, to encourage them to be far apart.
        D[np.isinf(D)] = D[~np.isinf(D)].max()

        # Initialize low-dimensional representation with PCA
        pca = PCA(self.k)
        pca.fit(X)
        Z = pca.compress(X)

        # Solve for the minimizer
        z, f = findMin(self._fun_obj_z, Z.flatten(), 500, D)
        Z = z.reshape(n, self.k)
        return Z
Example #2
0
    def compress(self, X, n_components, n_neighbours):
        n = X.shape[0]
        k = self.k
        numNeighbours = self.numNeighbours

        # find the distances to every other point
        euclD = utils.euclidean_dist_squared(X, X)
        euclD = np.sqrt(euclD)

        knnD = np.zeros((n, n))

        # get the KNN of point i
        for i in range(n):
            # finds numNeighbours smallest distances from obj_i
            # +1 because it will always select itself as (distance of 0), and distances are non-negative
            minIndexes = np.argsort(euclD[i])[:numNeighbours + 1]

            for index in minIndexes:
                # add distances of KNN_i to the distance matrix
                knnD[i, index] = euclD[i, index]

        D = np.zeros((n, n))
        # get distance of every other path using only KNN
        for i in range(n):
            for j in range(n):
                if i != j:
                    D[i, j] = utils.dijkstra(knnD, i, j)

        Z = AlternativePCA(k).fit(X).compress(X)
        z = find_min(self._fun_obj_z, Z.flatten(), 500, False, D)
        Z = z.reshape(n, k)
        return Z
Example #3
0
    def compress(self, X):
        n = X.shape[0]

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)

        # TODO: Convert these Euclidean distances into geodesic distances
        order = np.argsort(D, axis=1)[:, :self.k + 1]
        distance_mask = np.zeros(D.shape)
        for i in range(n):
            for j in order[i]:
                distance_mask[i, j] = 1
                distance_mask[j, i] = 1
        D = utils.dijkstra(D * distance_mask)

        # If two points are disconnected (distance is Inf)
        # then set their distance to the maximum
        # distance in the graph, to encourage them to be far apart.
        D[np.isinf(D)] = D[~np.isinf(D)].max()

        # Initialize low-dimensional representation with PCA
        pca = PCA(self.k)
        pca.fit(X)
        Z = pca.transform(X)

        # Solve for the minimizer
        z, f = findMin(self._fun_obj_z, Z.flatten(), 500, D)
        Z = z.reshape(n, self.k)
        return Z
Example #4
0
    def compress(self, X):
        n = X.shape[0]

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)

        # Convert these Euclidean distances into geodesic distances
        sorted_dist_indices = np.argsort(D)
        G = np.zeros((n, n))
        for i in range(n):
            for j in range(self.nn):
                G[i, sorted_dist_indices[i, j]] = D[i, sorted_dist_indices[i,
                                                                           j]]
                G[sorted_dist_indices[i, j], i] = D[sorted_dist_indices[i, j],
                                                    i]
        D = utils.dijkstra(G)

        # If two points are disconnected (distance is Inf)
        # then set their distance to the maximum
        # distance in the graph, to encourage them to be far apart.
        D[np.isinf(D)] = D[~np.isinf(D)].max()

        # Initialize low-dimensional representation with PCA
        pca = PCA(self.k)
        pca.fit(X)
        Z = pca.transform(X)

        # Solve for the minimizer
        z, f = findMin(self._fun_obj_z, Z.flatten(), 500, D)
        Z = z.reshape(n, self.k)
        return Z
Example #5
0
    def compress(self, X):
        n = X.shape[0]

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)

        sorted_indices = np.argsort(D)
        G = np.zeros((n, n))

        for i in range(D.shape[0]):
            for j in range(self.nn + 1):
                G[i, sorted_indices[i, j]] = D[i, sorted_indices[i, j]]
                G[sorted_indices[i, j], i] = D[sorted_indices[i, j], i]

        dist = utils.dijkstra(G)

        dist[np.isinf(dist)] = dist[~np.isinf(dist)].max()

        # Initialize low-dimensional representation with PCA
        pca = PCA(self.k)
        pca.fit(X)
        Z = pca.compress(X)

        # Solve for the minimizer
        z, f = findMin(self._fun_obj_z, Z.flatten(), 500, dist)
        Z = z.reshape(n, self.k)
        return Z
Example #6
0
    def compress(self, X):
        n = X.shape[0]
        k = self.k
        K = self.K

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)
        nbrs = np.argsort(D, axis=1)[:, 1:K + 1]
        G = np.zeros((n, n))

        for i in range(n):
            for j in nbrs[i]:
                G[i, j] = D[i, j]
                G[j, i] = D[j, i]

        D = utils.dijkstra(G)
        D[D == np.inf] = -np.inf
        max = np.max(D)
        D[D == -np.inf] = max

        # Initialize low-dimensional representation with PCA
        Z = PCA(k).fit(X).compress(X)

        # Solve for the minimizer
        z = find_min(self._fun_obj_z, Z.flatten(), 500, False, D)
        Z = z.reshape(n, k)
        return Z
Example #7
0
    def compress(self, X):
        n = X.shape[0]
        # nearest_neighbours = np.zeros((n, self.nn))

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)

        # If two points are disconnected (distance is Inf)
        # then set their distance to the maximum
        # distance in the graph, to encourage them to be far apart.

        adjacency_matrix = np.zeros((n, n))
        nearest_neighbours = self.knn(X)
        for i, j in enumerate(nearest_neighbours):
            for neighbour in j:
                adjacency_matrix[i, neighbour] = D[i, neighbour]
                adjacency_matrix[neighbour, i] = D[neighbour, i]

        dijkstra = utils.dijkstra(adjacency_matrix)

        dijkstra[np.isinf(dijkstra)] = dijkstra[~np.isinf(dijkstra)].max()
        # Initialize low-dimensional representation with PCA
        Z = PCA(self.k).fit(X).compress(X)

        # Solve for the minimizer
        z = find_min(self._fun_obj_z, Z.flatten(), 500, False, dijkstra)
        Z = z.reshape(n, self.k)
        return Z
Example #8
0
    def compress(self, X):
        n = X.shape[0]

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)
        # D is symmetric matrix

        geoD = np.zeros((n, n))

        # find nn-neighbours
        for i in range(n):
            sort = np.argsort(D[:, i])
            neigh = np.setdiff1d(sort[0:self.nn + 1], i)
            # find the nn+1 smallest indexes that are not i
            for j in range(len(neigh)):
                t = neigh[j]
                geoD[i, t] = D[i, t]
                geoD[t, i] = D[t, i]

        D = utils.dijkstra(geoD)
        # for disconnected vertices (distance is Inf)
        # set their dist = max_dist(graph)
        # to encourage they are far away from each other
        D[np.isinf(D)] = D[~np.isinf(D)].max()

        # Initialize low-dimensional representation with PCA
        pca = PCA(self.k)
        pca.fit(X)
        Z = pca.compress(X)

        # Solve for the minimizer
        z, f = findMin(self._fun_obj_z, Z.flatten(), 500, D)
        Z = z.reshape(n, self.k)
        return Z
Example #9
0
    def find_and_draw_path(self):
        if self.is_bi_dir:
            self.build_bi_rrt()
        else:
            self.build_rrt()

        shortest_path = utils.dijkstra(self.adj_matrix, self.start, self.goal)
        if shortest_path is None:
            print("Path not found")
            return

        self.draw_shortest_path(shortest_path)
        plt.show()
        return
Example #10
0
def shortest(source, destination, city_data, routes):
  """Given a source and destination, find the shortest path."""
  if source not in city_data.keys() or destination not in city_data.keys():
    raise KeyError('Both source and destination must be in the database.')

  path, distance = utils.dijkstra(source, destination, city_data, routes)
  cost = get_cost(path, city_data, routes)
  time = get_time(path, city_data, routes)

  codes = [ path[0][0] ]
  codes.extend([ route[1] for route in path ])
  route = ' -> '.join(codes)
  
  return route, distance, time, cost
Example #11
0
    def compress(self, X):
        n = X.shape[0]

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X,X)
        D = np.sqrt(D)

        assert np.allclose(D,D.T)

        # TODO: Convert these Euclidean distances into geodesic distances
        D_args= np.argsort(D)
        D_indx = np.argsort(D_args)

        for i in range(n):
            for j in range(n):
                if D_indx[i,j] <= self.nn:
                    D_indx[j,i] = D_indx[i,j]

        adj = np.zeros((n,n))

        for i in range(n):
            for j in range(n):
                if D_indx[i,j] <= self.nn:
                    adj[i,j] = 1
                if i == j:
                    adj[i,j] = 0

        assert np.allclose(adj,adj.T)

        for i in range(n):
            for j in range(n):
                D[i,j] = utils.dijkstra(adj,i,j)

        # If two points are disconnected (distance is Inf)
        # then set their distance to the maximum
        # distance in the graph, to encourage them to be far apart.
        D[np.isinf(D)] = D[~np.isinf(D)].max()

        # Initialize low-dimensional representation with PCA
        pca = PCA(self.k)
        pca.fit(X)
        Z = pca.transform(X)

        # Solve for the minimizer
        z,f = findMin(self._fun_obj_z, Z.flatten(), 500, D)
        Z = z.reshape(n, self.k)
        return Z
Example #12
0
    def compress(self, X):
        n = X.shape[0]

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)

        # TODO: Convert these Euclidean distances into geodesic distances
        # find the neighbours of each point (just re-order in distance)
        # initialize GD (0 means no edge)
        GD = np.zeros([n, n])
        for one_point in range(n):
            sorted_indices = np.argsort(D[one_point])
            knn_indices = sorted_indices[0:self.nn + 1]

            # fill in the geodesic distances
            for neighbour in knn_indices:
                GD[one_point, neighbour] = D[one_point, neighbour]
                GD[neighbour, one_point] = D[neighbour, one_point]

        # compute edge weights
        # edge weights are just distance and already stored in GD

        # compute weighted shortest path via Dijkstra
        D = utils.dijkstra(GD)

        # If two points are disconnected (distance is Inf)
        # then set their distance to the maximum
        # distance in the graph, to encourage them to be far apart.
        D[np.isinf(D)] = D[~np.isinf(D)].max()

        # Initialize low-dimensional representation with PCA
        pca = PCA(self.k)
        pca.fit(X)
        Z = pca.transform(X)

        # Solve for the minimizer
        z, f = findMin(self._fun_obj_z, Z.flatten(), 500, D)
        Z = z.reshape(n, self.k)
        return Z
Example #13
0
    def compress(self, X):
        n = X.shape[0]

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)

        np.fill_diagonal(D, np.inf)
        ########
        #"finding the neighbor at each point"
        G = np.matrix(np.ones((n, n)) * 0)
        for i in range(n):
            neighbours = np.argsort(D[:, i])
            #want only the k nearest
            for j in neighbours[1:self.nn + 1]:
                G[i, j] = D[i, j]
                G[j, i] = D[j, i]

        #weighted shortest path between points (dijksta's)
        D = np.zeros((n, n))
        for i in range(n):
            for j in range(i + 1, n):
                D[i, j] = utils.dijkstra(G, i, j)

        ########

        # If two points are disconnected (distance is Inf)
        # then set their distance to the maximum
        # distance in the graph, to encourage them to be far apart.
        D[np.isinf(D)] = D[~np.isinf(D)].max()

        # Initialize low-dimensional representation with PCA
        pca = PCA(self.k)
        pca.fit(X)
        Z = pca.compress(X)

        # Solve for the minimizer
        z, f = findMin(self._fun_obj_z, Z.flatten(), 500, D)
        Z = z.reshape(n, self.k)
        return Z
Example #14
0
def four_way_path_sum(matrix):
    '''
    problem 83
    '''
    dimension = max(matrix)[0]

    @memo
    def neighbor_function(node):
        neighbors = set()
        a,b = node
        if a+1 <= dimension: neighbors.add((a+1,b))
        if a-1 >= 0: neighbors.add((a-1,b))
        if b-1 >= 0: neighbors.add((a,b-1))
        if b+1 <= dimension: neighbors.add((a,b+1))
        return neighbors

    def distance_func(first,next):
        return matrix[next]

    min_path = dijkstra(matrix.keys(),(0,0),(dimension,dimension),neighbor_function,distance_func)
    value = sum(matrix[a,b] for a,b in min_path)
    return min_path,value
    def compress(self, X):
        n = X.shape[0]

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X, X)
        D = np.sqrt(D)

        ########
        # TODO #
        G = np.full((n, n), np.inf)
        for i in range(n):
            for j in range(n):
                #temp = np.list(D[i]).sort
                temp = sorted(D[i])
                #print(self.nn+1)
                if D[i][j] in temp[:(self.nn + 1)]:
                    G[i][j] = D[i][j]

        for i in range(n):
            for j in range(n):
                D[i][j] = utils.dijkstra(G, i, j)

        ########

        # If two points are disconnected (distance is Inf)
        # then set their distance to the maximum
        # distance in the graph, to encourage them to be far apart.
        D[np.isinf(D)] = D[~np.isinf(D)].max()
        #G[np.isinf(G)] = G[~np.isinf(G)].max()

        # Initialize low-dimensional representation with PCA
        pca = PCA(self.k)
        pca.fit(X)
        Z = pca.compress(X)

        # Solve for the minimizer
        z, f = findMin(self._fun_obj_z, Z.flatten(), 500, D)
        Z = z.reshape(n, self.k)
        return Z
    def compress(self, X):
        n = X.shape[0]

        # Compute Euclidean distances
        D = utils.euclidean_dist_squared(X,X)
        D = np.sqrt(D)

        # TODO: Convert these Euclidean distances into geodesic distances
        G = np.zeros((n,n))
        for j in range(n):
            # Find indices of k + 1 nearest neighbours (including itself)
            ind = np.argpartition(D[j,:], (self.nn+1))
            ind = ind[:(self.nn + 1)]
            print(print(j), print(ind))
            for l in ind:
                G[j][l] = D[j][l]
                G[l][j] = D[l][j]

        for j in range(n):
            for l in range(j, n):
                D[j][l] = utils.dijkstra(G, j, l)
                D[l][j] = D[j][l]


        # If two points are disconnected (distance is Inf)
        # then set their distance to the maximum
        # distance in the graph, to encourage them to be far apart.
        D[np.isinf(D)] = D[~np.isinf(D)].max()

        # Initialize low-dimensional representation with PCA
        pca = PCA(self.k)
        pca.fit(X)
        Z = pca.transform(X)

        # Solve for the minimizer
        z,f = findMin(self._fun_obj_z, Z.flatten(), 500, D)
        Z = z.reshape(n, self.k)
        return Z
Example #17
0
def three_way_path_sum(matrix):
    '''
    problem 82
    '''
    dimension = max(matrix)[0]

    @memo
    def neighbor_function(node):
        neighbors = set()
        a,b = node
        if a+1 <= dimension: neighbors.add((a+1,b))
        if b-1 >= 0: neighbors.add((a,b-1))
        if b+1 <= dimension: neighbors.add((a,b+1))
        return neighbors

    minimum, min_path, targets = float('inf'), None, tuple((dimension,i) for i in range(dimension))
    for j in range(dimension):
        source = (0,j)
        previous,paths = dijkstra(matrix.keys(),source,None,neighbor_function,lambda x,y:matrix[y]),set()
        for a in targets:
            if a not in previous or previous[a] in targets:
                continue
            b,s = a,[]
            while previous[b]:
                s.append(previous[b])
                if previous[b] == source:
                    break
                b = previous[b]
            else:
                continue
            s.reverse()
            paths.add(tuple(s+[a]))
        path_sums = {path:sum(matrix[p] for p in path) for path in paths}
        this_min = min(path_sums.items(),key=lambda x: x[1])
        if this_min[1] < minimum:
            min_path,minimum = this_min
    return minimum,min_path
Example #18
0
#!/usr/bin/env python
import sys
import utils


# argv[1] is topology file name
nodes=utils.top2obj(sys.argv[1])

cost = []
tmp = 0

keys = nodes.iterkeys()
for i in keys:
    print "start : ", i
    tmp = utils.dijkstra(nodes, nodes[i], None)
    cost.append(tmp)

print cost
print max(cost)
Example #19
0
for i in range(len(chord_sets)):  
    neighbor_near_list.update(neighbor_near(chord_sets[i]))
for i in range(len(chord_sets)):  
    neighbor_far_list.update(neighbor_far(chord_sets[i]))

# Create distance table and save it to file. 
# Later, we can look up the table to find distance between 2 chords
w1 = 1
w2 = 3
weight_table =[]
count = 0
from datetime import datetime
for chord in named_chords_encode:
    print str(datetime.now())
    print count
    g = Graph()
    for i in range(1,4096):
        g.add_vertex(i)
    for i in range(1,4096):
        for j in neighbor_near_list[i]:
            g.add_edge(i,j,w1)
        for k in neighbor_far_list[i]:
            g.add_edge(i,k,w2)
    dijkstra(g,g.get_vertex(chord))
    for i in range(1,4096):
        n = g.get_vertex(i).get_distance()
        weight_table.append([chord,i,n])
    count = count + 1

pickle.dump(distance_table,open(config.distance_table, "wb" ) )
Example #20
0
 def _generate_path(self):
     self.log.info("Generating path...")
     self.path = utils.dijkstra(self.graph, self.origin, self.destination)
     self.next_node_idx = 1
     self.log.info("Got path: %s", str(self.path))
Example #21
0
coodinates = it.product(range(score_kde_fg.shape[0]), range(score_kde_fg.shape[1]))

value = len(tqdm_notebook(coodinates, total=np.prod(score_kde_fg.shape)))

for x, y in tqdm_notebook(coodinates, total=np.prod(score_kde_fg.shape)):
    score_kde_fg[x, y] = np.exp(kde_fg.score(img_input[x, y, :].reshape(1, -1)))
    score_kde_bg[x, y] = np.exp(kde_bg.score(img_input[x, y, :].reshape(1, -1)))
    n = score_kde_fg[x, y] + score_kde_bg[x, y]
    if n == 0:
        n = 1
    likelihood_fg[x, y] = score_kde_fg[x, y]/n

print('Finish!')

# вызываем алгоритм для двух масок
d_fg = dijkstra(xy_fg, likelihood_fg)
d_bg = dijkstra(xy_bg, 1 - likelihood_fg)

print('Finish 2 !')

margin = 1.0
mask = (d_fg < (d_bg + margin)).astype(np.uint8)

cv2.imwrite('face-points6.jpg', mask)

img_fg = img_input.copy()
img_bg = (np.array(Image.open('max.jpg'))/255.0)[:800, :800, :]

x = int(img_bg.shape[0] - img_fg.shape[0])
y = int(img_bg.shape[1]/2 - img_fg.shape[1]/2)
Example #22
0
 def _generate_path(self):
     self.log.info('Generating path...')
     self.path = utils.dijkstra(self.graph, self.origin, self.destination)
     self.next_node_idx = 1
     self.log.info('Got path: %s', str(self.path))