Example #1
0
    def fill_by_graph_search(self):
        """ Interpolate along edges to fill in NaN values where possible.
        Will not extrapolate, though - to update a value it must have valid
        neighbors in more than one direction.
        """
        accum = self.F.copy()
        weights = array( isfinite(accum), float64 )
        counts =  zeros( len(self.F), int32)
        counts[ isfinite(accum) ] = 1000
        accum[ isnan(accum) ] = 0.0

        for i in nonzero( isfinite(self.F) )[0]:
            # Shortest path search starting from i
            # print i

            visited = {}
            heap = priority_queue.priorityDictionary()

            heap[i] = 0.0

            while len(heap)>0:
                j = heap.smallest()
                j_dist = heap[j]
                del heap[j]
                visited[j]=1

                #print "Visiting %d - dist is %g"%(j,j_dist)

                if j_dist > 0:
                    # update costs here
                    accum[j]   += self.F[i] / j_dist
                    weights[j] += 1.0 / j_dist
                    counts[j]  += 1

                # Queue neighbors:
                for nbr in self.node_neighbors(j):
                    if (not visited.has_key(nbr)) and isnan(self.F[nbr]):
                        nbr_dist = j_dist + norm(self.X[nbr]-self.X[j])
                        if (not heap.has_key(nbr)) or (heap[nbr]>nbr_dist):
                            heap[nbr] = nbr_dist


        missing = isnan(self.F) & (counts>1)

        self.F[missing] = accum[missing] / weights[missing]
Example #2
0
    def fill_by_graph_search(self):
        """ Interpolate along edges to fill in NaN values where possible.
        Will not extrapolate, though - to update a value it must have valid
        neighbors in more than one direction.
        """
        accum = self.F.copy()
        weights = array(isfinite(accum), float64)
        counts = zeros(len(self.F), int32)
        counts[isfinite(accum)] = 1000
        accum[isnan(accum)] = 0.0

        for i in nonzero(isfinite(self.F))[0]:
            # Shortest path search starting from i
            # print i

            visited = {}
            heap = priority_queue.priorityDictionary()

            heap[i] = 0.0

            while len(heap) > 0:
                j = heap.smallest()
                j_dist = heap[j]
                del heap[j]
                visited[j] = 1

                #print "Visiting %d - dist is %g"%(j,j_dist)

                if j_dist > 0:
                    # update costs here
                    accum[j] += self.F[i] / j_dist
                    weights[j] += 1.0 / j_dist
                    counts[j] += 1

                # Queue neighbors:
                for nbr in self.node_neighbors(j):
                    if (not visited.has_key(nbr)) and isnan(self.F[nbr]):
                        nbr_dist = j_dist + norm(self.X[nbr] - self.X[j])
                        if (not heap.has_key(nbr)) or (heap[nbr] > nbr_dist):
                            heap[nbr] = nbr_dist

        missing = isnan(self.F) & (counts > 1)

        self.F[missing] = accum[missing] / weights[missing]
Example #3
0
def Dijkstra(adjacency_list, n1, n2):
  is_visited = [False] * N
  parents = [-1] * N
  # float('inf') - infinite value, initial value of marks
  marks = [float('inf')] * N
  queue = priorityDictionary()

  current = n1
  marks[current] = 0
  # add start node into the queue
  queue[current] = marks[current]
  
  # notes isn't deleted from the queue
  # (stupid implementation of the priority queue)
  # infinite value means that a node "isn't in the queue"
  while(queue[queue.smallest()] != float('inf')):
    current = queue.smallest()
    current_mark = queue[current]

    for adjacent_node in adjacency_list[current]:
      node = adjacent_node[0]
      edge_weight = adjacent_node[1]
      if not is_visited[node]:
        new_mark = current_mark + edge_weight
        if marks[node] > new_mark:
          marks[node] = new_mark
          parents[node] = current
          queue[node] = new_mark

    is_visited[current] = True;
    queue[current] = float('inf')

  # print optimal path (n1, n2)
  print "Path lenght is " + str(marks[n2])
  path = []
  current = n2
  path.append(current)
  while (current != n1):
    current = parents[current]
    path.append(current)
  path.reverse()
  print path
Example #4
0
    def shortest_path(self, n1, n2, boundary_only=False):
        """
        Dijkstra on the edge graph from node n1 to n2.
        copied and modified form Rusty's code.
        Keep in mind that the solution can be non-unique
        because there are multiple paths that have the same distances.

        Parameters
        ----------
        n1: int
            node index for one end of the path
        n2: int
            node_i for the other end
        boundary_only: boolean
            limit search to edges on the boundary

        Returns
        -------
        numpy.ndarray
            shortest node indexes or None if it cannot be found
        """
        queue = pq.priorityDictionary()
        queue[n1] = 0

        done = {}

        while True:
            # find the queue-member with the lowest cost:
            best = queue.smallest()
            best_cost = queue[best]
            del queue[best]
            done[best] = best_cost
            if best == n2:
                # print "Found the ending point"
                break
            # figure out its neighbors
            all_nodes_i = self.get_neighbor_nodes(best)
            for node_i in all_nodes_i:
                if node_i in done:
                    # both for p and for points that we've already done
                    continue
                if boundary_only:
                    e = self.find_edge((best, node_i))
                    if self.edges[e, 2] < EdgeType.BOUNDARY:
                        continue
                dist = np.sqrt(
                    ((self._nodes[node_i] - self._nodes[best])**2).sum())
                new_cost = best_cost + dist
                if node_i not in queue:
                    queue[node_i] = np.inf
                if queue[node_i] > new_cost:
                    queue[node_i] = new_cost
        # reconstruct the path:
        path = [n2]
        while True:
            node_i = path[-1]
            if node_i == n1:
                break
            # figure out its neighbors
            all_nodes_i = self.get_neighbor_nodes(node_i)
            found_prev = False
            for nbr in all_nodes_i:
                if nbr == node_i or nbr not in done:
                    continue
                dist = np.sqrt(
                    ((self._nodes[node_i] - self._nodes[nbr])**2).sum())
                if done[node_i] == done[nbr] + dist:
                    path.append(nbr)
                    found_prev = True
                    break
            if found_prev is False:
                return None
        return np.array(path[::-1])
Example #5
0
    def shortest_path(self, n1, n2, boundary_only=False):
        """ dijkstra on the edge graph from n1 to n2.
            copied and modified form Rusty's code.
            n1 = node_i for one end of the path
            n2 = node_i for the other end
            boundary_only = limit search to edges on the boundary (have
            a -1 for element2)
        """
        queue = pq.priorityDictionary()
        queue[n1] = 0

        done = {}

        while True:
            # find the queue-member with the lowest cost:
            best = queue.smallest()
            best_cost = queue[best]
            del queue[best]

            done[best] = best_cost

            if best == n2:
                # print "Found the ending point"
                break

            # figure out its neighbors
            elems_i = list(self.get_elems_i_from_node(best))
            all_nodes_i = np.unique(self._elems[elems_i])

            for node_i in all_nodes_i:
                if done.has_key(node_i):
                    # both for p and for points that we've already done
                    continue

                if boundary_only:
                    e = self._find_edge((best, node_i))
                    if self._edges[e, 4] != BOUNDARY:
                        continue


                dist = np.sqrt( ((self._nodes[node_i] \
                                  - self._nodes[best])**2).sum() )
                new_cost = best_cost + dist

                if not queue.has_key(node_i):
                    queue[node_i] = np.inf

                if queue[node_i] > new_cost:
                    queue[node_i] = new_cost

        # reconstruct the path:
        path = [n2]

        while 1:
            node_i = path[-1]
            if node_i == n1:
                break

            # figure out its neighbors
            elems = list(self.get_elems_i_from_node(node_i))
            all_nodes_i = np.unique(self._elems[elems])

            found_prev = 0
            for nbr in all_nodes_i:
                if nbr == node_i or not done.has_key(nbr):
                    continue

                dist = np.sqrt( ((self._nodes[node_i] \
                                  - self._nodes[nbr])**2).sum() )

                if done[node_i] == done[nbr] + dist:
                    path.append(nbr)
                    found_prev = 1
                    break
            if not found_prev:
                return None

        return np.array(path[::-1])