|
| 1 | +package org.mwg.experiments.kdtree; |
| 2 | + |
| 3 | + |
| 4 | +import org.mwg.ml.common.distance.Distance; |
| 5 | + |
| 6 | +/** |
| 7 | + * Created by assaad on 29/06/16. |
| 8 | + */ |
| 9 | +public class KDNodeSync { |
| 10 | + |
| 11 | + |
| 12 | + KDNodeSync right; |
| 13 | + KDNodeSync left; |
| 14 | + double[] key; |
| 15 | + Object value; |
| 16 | + |
| 17 | + public int getNum() { |
| 18 | + return num; |
| 19 | + } |
| 20 | + |
| 21 | + private int num; |
| 22 | + |
| 23 | + |
| 24 | + public void setThreshold(double threshold) { |
| 25 | + this.threshold = threshold; |
| 26 | + } |
| 27 | + |
| 28 | + double threshold; |
| 29 | + Distance distance; |
| 30 | + |
| 31 | + |
| 32 | + public void setDistance(Distance d) { |
| 33 | + this.distance = d; |
| 34 | + } |
| 35 | + |
| 36 | + private Distance getDistance() { |
| 37 | + return distance; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + public void insert(final double[] key, final Object value) { |
| 42 | + final int dim = key.length; |
| 43 | + |
| 44 | + if (key.length != dim) { |
| 45 | + throw new RuntimeException("Key size should always be the same"); |
| 46 | + } |
| 47 | + |
| 48 | + Distance distance = getDistance(); |
| 49 | + internalInsert(this, this, distance, key, 0, dim, threshold, value); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public Object nearest(final double[] key) { |
| 54 | + |
| 55 | + // initial call is with infinite hyper-rectangle and max distance |
| 56 | + HRect hr = HRect.infiniteHRect(key.length); |
| 57 | + double max_dist_sqd = Double.MAX_VALUE; |
| 58 | + |
| 59 | + NearestNeighborList nnl = new NearestNeighborList(1); |
| 60 | + Distance distance = getDistance(); |
| 61 | + internalNearest(this, distance, key, hr, max_dist_sqd, 0, key.length, threshold, nnl); |
| 62 | + |
| 63 | + Object res = nnl.getHighest(); |
| 64 | + return res; |
| 65 | + } |
| 66 | + |
| 67 | + public Object nearestWithinDistance(final double[] key) { |
| 68 | + |
| 69 | + |
| 70 | + // initial call is with infinite hyper-rectangle and max distance |
| 71 | + HRect hr = HRect.infiniteHRect(key.length); |
| 72 | + double max_dist_sqd = Double.MAX_VALUE; |
| 73 | + |
| 74 | + NearestNeighborList nnl = new NearestNeighborList(1); |
| 75 | + Distance distance = getDistance(); |
| 76 | + internalNearest(this, distance, key, hr, max_dist_sqd, 0, key.length, threshold, nnl); |
| 77 | + |
| 78 | + if (nnl.getBestDistance() <= threshold) { |
| 79 | + Object res = nnl.getHighest(); |
| 80 | + return res; |
| 81 | + } else { |
| 82 | + return null; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public Object[] nearestN(final double[] key, final int n) { |
| 88 | + |
| 89 | + HRect hr = HRect.infiniteHRect(key.length); |
| 90 | + double max_dist_sqd = Double.MAX_VALUE; |
| 91 | + |
| 92 | + NearestNeighborList nnl = new NearestNeighborList(n); |
| 93 | + Distance distance = getDistance(); |
| 94 | + internalNearest(this, distance, key, hr, max_dist_sqd, 0, key.length, threshold, nnl); |
| 95 | + |
| 96 | + Object[] res = nnl.getAllNodes(); |
| 97 | + return res; |
| 98 | + |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + private static void internalNearest(KDNodeSync node, final Distance distance, double[] target, HRect hr, double max_dist_sqd, int lev, int dim, double err, NearestNeighborList nnl) { |
| 104 | + // 1. if kd is empty exit. |
| 105 | + if (node == null) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + double[] pivot = node.key; |
| 110 | + if (pivot == null) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + // 2. s := split field of kd |
| 116 | + int s = lev % dim; |
| 117 | + |
| 118 | + // 3. pivot := dom-elt field of kd |
| 119 | + |
| 120 | + double pivot_to_target = distance.measure(pivot, target); |
| 121 | + |
| 122 | + // 4. Cut hr into to sub-hyperrectangles left-hr and right-hr. |
| 123 | + // The cut plane is through pivot and perpendicular to the s |
| 124 | + // dimension. |
| 125 | + HRect left_hr = hr; // optimize by not cloning |
| 126 | + HRect right_hr = (HRect) hr.clone(); |
| 127 | + left_hr.max[s] = pivot[s]; |
| 128 | + right_hr.min[s] = pivot[s]; |
| 129 | + |
| 130 | + // 5. target-in-left := target_s <= pivot_s |
| 131 | + boolean target_in_left = target[s] < pivot[s]; |
| 132 | + |
| 133 | + KDNodeSync nearer_kd; |
| 134 | + HRect nearer_hr; |
| 135 | + KDNodeSync further_kd; |
| 136 | + HRect further_hr; |
| 137 | + |
| 138 | + // 6. if target-in-left then |
| 139 | + // 6.1. nearer-kd := left field of kd and nearer-hr := left-hr |
| 140 | + // 6.2. further-kd := right field of kd and further-hr := right-hr |
| 141 | + if (target_in_left) { |
| 142 | + nearer_kd = node.left; |
| 143 | + nearer_hr = left_hr; |
| 144 | + further_kd = node.right; |
| 145 | + further_hr = right_hr; |
| 146 | + } |
| 147 | + // |
| 148 | + // 7. if not target-in-left then |
| 149 | + // 7.1. nearer-kd := right field of kd and nearer-hr := right-hr |
| 150 | + // 7.2. further-kd := left field of kd and further-hr := left-hr |
| 151 | + else { |
| 152 | + nearer_kd = node.right; |
| 153 | + nearer_hr = right_hr; |
| 154 | + further_kd = node.left; |
| 155 | + further_hr = left_hr; |
| 156 | + } |
| 157 | + |
| 158 | + // 8. Recursively call Nearest Neighbor with paramters |
| 159 | + // (nearer-kd, target, nearer-hr, max-dist-sqd), storing the |
| 160 | + // results in nearest and dist-sqd |
| 161 | + //nnbr(nearer_kd, target, nearer_hr, max_dist_sqd, lev + 1, K, nnl); |
| 162 | + internalNearest(nearer_kd, distance, target, nearer_hr, max_dist_sqd, lev + 1, dim, err, nnl); |
| 163 | + |
| 164 | + |
| 165 | + double dist_sqd; |
| 166 | + |
| 167 | + if (!nnl.isCapacityReached()) { |
| 168 | + dist_sqd = Double.MAX_VALUE; |
| 169 | + } else { |
| 170 | + dist_sqd = nnl.getMaxPriority(); |
| 171 | + } |
| 172 | + |
| 173 | + // 9. max-dist-sqd := minimum of max-dist-sqd and dist-sqd |
| 174 | + max_dist_sqd = Math.min(max_dist_sqd, dist_sqd); |
| 175 | + |
| 176 | + // 10. A nearer point could only lie in further-kd if there were some |
| 177 | + // part of further-hr within distance sqrt(max-dist-sqd) of |
| 178 | + // target. If this is the case then |
| 179 | + double[] closest = further_hr.closest(target); |
| 180 | + if (distance.measure(closest, target) < max_dist_sqd) { |
| 181 | + |
| 182 | + // 10.1 if (pivot-target)^2 < dist-sqd then |
| 183 | + if (pivot_to_target < dist_sqd) { |
| 184 | + |
| 185 | + // 10.1.2 dist-sqd = (pivot-target)^2 |
| 186 | + dist_sqd = pivot_to_target; |
| 187 | + nnl.insert(node.value, dist_sqd); |
| 188 | + |
| 189 | + // 10.1.3 max-dist-sqd = dist-sqd |
| 190 | + // max_dist_sqd = dist_sqd; |
| 191 | + if (nnl.isCapacityReached()) { |
| 192 | + max_dist_sqd = nnl.getMaxPriority(); |
| 193 | + } else { |
| 194 | + max_dist_sqd = Double.MAX_VALUE; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + // 10.2 Recursively call Nearest Neighbor with parameters |
| 199 | + // (further-kd, target, further-hr, max-dist_sqd), |
| 200 | + // storing results in temp-nearest and temp-dist-sqd |
| 201 | + //nnbr(further_kd, target, further_hr, max_dist_sqd, lev + 1, K, nnl); |
| 202 | + internalNearest(further_kd, distance, target, further_hr, max_dist_sqd, lev + 1, dim, err, nnl); |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | + |
| 209 | + private static boolean internalInsert(final KDNodeSync node, final KDNodeSync root, final Distance distance, final double[] key, final int lev, final int dim, final double err, final Object value) { |
| 210 | + |
| 211 | + double[] tk = node.key; |
| 212 | + if (tk == null) { |
| 213 | + node.key = key.clone(); |
| 214 | + node.value = value; |
| 215 | + |
| 216 | + if (node == root) { |
| 217 | + node.num = 1; |
| 218 | + } |
| 219 | + return true; |
| 220 | + |
| 221 | + } else if (distance.measure(key, tk) < err) { |
| 222 | + node.value = value; |
| 223 | + return true; |
| 224 | + } else if (key[lev] > tk[lev]) { |
| 225 | + //check right |
| 226 | + if (node.right == null) { |
| 227 | + KDNodeSync rightNode = new KDNodeSync(); |
| 228 | + rightNode.key = key.clone(); |
| 229 | + rightNode.value = value; |
| 230 | + node.right = rightNode; |
| 231 | + root.num = root.num + 1; |
| 232 | + return true; |
| 233 | + } else { |
| 234 | + internalInsert(node.right, root, distance, key, (lev + 1) % dim, dim, err, value); |
| 235 | + return true; |
| 236 | + } |
| 237 | + |
| 238 | + } else { |
| 239 | + if (node.left == null) { |
| 240 | + KDNodeSync leftNode = new KDNodeSync(); |
| 241 | + leftNode.key = key.clone(); |
| 242 | + leftNode.value = value; |
| 243 | + node.left = leftNode; |
| 244 | + root.num = root.num + 1; |
| 245 | + return true; |
| 246 | + } else { |
| 247 | + internalInsert(node.left, root, distance, key, (lev + 1) % dim, dim, err, value); |
| 248 | + return true; |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + |
| 254 | +} |
0 commit comments