| 1 |
from .pheap import pheap as _pheap |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
class heap(object): |
| 9 |
""".. note:: |
| 10 |
|
| 11 |
Using this class is not required to use |
| 12 |
:py:func:`distance`, :py:func:`travel_time` or |
| 13 |
:py:func:`extension_velocities`. It is provided for |
| 14 |
experimenting with fast marching algorithms. |
| 15 |
|
| 16 |
|
| 17 |
This class implements a binary min heap (or heap queue) to support |
| 18 |
the fast marching algorithm. A min heap is a list which has the |
| 19 |
property that the smallest element is always the first element. |
| 20 |
http://en.wikipedia.org/wiki/Binary_heap |
| 21 |
|
| 22 |
This class differs from the heap queue (:py:obj:`heapq`) in the |
| 23 |
Python standard library because it supports changing the value of |
| 24 |
elements in the heap and maintains forward and backward ids. |
| 25 |
|
| 26 |
The fast marching method uses this data structure to track |
| 27 |
elements in the solution narrow band. The fast marching method |
| 28 |
needs to know which element in the narrow band is nearest the zero |
| 29 |
level-set at each iteration. When a new point enters the solution |
| 30 |
narrow band the element is added to the heap. |
| 31 |
|
| 32 |
New elements (an address and an initial value) are added to the |
| 33 |
heap with the :py:meth:`push` method. The :py:meth:`push` method |
| 34 |
returns an integer (a :py:obj:`heap_id`) which is used to update |
| 35 |
the value of the element. |
| 36 |
|
| 37 |
As the solution evolves, the distance of points already in the heap |
| 38 |
are updated via the :py:meth:`update` method. The |
| 39 |
:py:meth:`update` method takes the :py:obj:`heap_id` returned by the |
| 40 |
:py:meth:`push` along with a new distance value. |
| 41 |
|
| 42 |
The smallest element is taken off the heap with the :py:meth:`pop` |
| 43 |
method. The address and value of the top element is returned. |
| 44 |
|
| 45 |
The constructor for heap needs to know the number of elements that |
| 46 |
will enter the heap. |
| 47 |
|
| 48 |
>>> from skfmm import heap |
| 49 |
>>> h = heap(10) |
| 50 |
>>> h.push(10,0.2) |
| 51 |
0 |
| 52 |
>>> h.push(11,0.3) |
| 53 |
1 |
| 54 |
>>> h.push(12,0.1) |
| 55 |
2 |
| 56 |
>>> h.peek() |
| 57 |
0.1 |
| 58 |
>>> h.update(1, 0.01) |
| 59 |
>>> h.peek() |
| 60 |
0.01 |
| 61 |
>>> h.pop() |
| 62 |
(11, 0.01) |
| 63 |
>>> h.pop() |
| 64 |
(12, 0.1) |
| 65 |
>>> h.pop() |
| 66 |
(10, 0.2) |
| 67 |
>>> assert h.empty() |
| 68 |
|
| 69 |
""" |
| 70 |
|
| 71 |
def __init__(self, max_size, self_test=False): |
| 72 |
"""Create a new min heap. |
| 73 |
|
| 74 |
Parameters |
| 75 |
---------- |
| 76 |
max_heap : int |
| 77 |
|
| 78 |
The maximum size of the heap. |
| 79 |
|
| 80 |
self_test : Boolean |
| 81 |
|
| 82 |
If True a consistency check is made after each |
| 83 |
heap operation. This is used in testing and |
| 84 |
results in a slower calculation. |
| 85 |
|
| 86 |
""" |
| 87 |
self._heap = _pheap(max_size, self_test) |
| 88 |
|
| 89 |
def push(self, addr, value): |
| 90 |
"""Add a value to the heap, give an address and a value. |
| 91 |
|
| 92 |
Parameters |
| 93 |
---------- |
| 94 |
addr : int |
| 95 |
|
| 96 |
An id number which is returned when the element is |
| 97 |
popped off the heap. |
| 98 |
|
| 99 |
value : float |
| 100 |
|
| 101 |
An initial numerical value for this element. |
| 102 |
|
| 103 |
Returns |
| 104 |
------- |
| 105 |
heap_id : int |
| 106 |
|
| 107 |
An id number into the heap used to update |
| 108 |
the value of this element. The value of a |
| 109 |
point in the heap can be updated by calling |
| 110 |
:py:meth:`update` with this id and a new value. |
| 111 |
|
| 112 |
""" |
| 113 |
return self._heap._push(addr, value) |
| 114 |
|
| 115 |
def pop(self): |
| 116 |
"""Remove and return the address and value of the top element on the |
| 117 |
heap. |
| 118 |
|
| 119 |
Returns |
| 120 |
------- |
| 121 |
|
| 122 |
(addr, value) : tuple |
| 123 |
|
| 124 |
A tuple of the address given when the element |
| 125 |
was pushed onto the heap and the current value |
| 126 |
of the element. |
| 127 |
|
| 128 |
""" |
| 129 |
return self._heap._pop() |
| 130 |
|
| 131 |
def update(self, heap_id, value): |
| 132 |
"""Update the value of a point already in the heap, the heap ordering |
| 133 |
is updated. |
| 134 |
|
| 135 |
Parameters |
| 136 |
---------- |
| 137 |
|
| 138 |
heap_id : int |
| 139 |
|
| 140 |
The :py:obj:`heap_id` value returned by |
| 141 |
:py:meth:`push` when this element was added to the |
| 142 |
heap. |
| 143 |
|
| 144 |
value : float |
| 145 |
|
| 146 |
The new value for this element. |
| 147 |
|
| 148 |
""" |
| 149 |
self._heap._set(heap_id, value) |
| 150 |
|
| 151 |
def empty(self): |
| 152 |
""" |
| 153 |
Returns |
| 154 |
------- |
| 155 |
|
| 156 |
empty : Boolean |
| 157 |
|
| 158 |
True if the heap is empty. |
| 159 |
|
| 160 |
""" |
| 161 |
return self._heap._empty() |
| 162 |
|
| 163 |
def peek(self): |
| 164 |
""" |
| 165 |
Returns the top (smallest) value on the heap without removing it. |
| 166 |
|
| 167 |
Returns |
| 168 |
------- |
| 169 |
value : float |
| 170 |
|
| 171 |
The top (smallest) value on the heap. |
| 172 |
""" |
| 173 |
return self._heap._peek() |