Changeset 14 for trunk/CrossPare/src/de/ugoe/cs/cpdp
- Timestamp:
- 08/29/14 19:33:14 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CrossPare/src/de/ugoe/cs/cpdp/training/WekaLocalTraining2.java
r13 r14 50 50 public static int SIZE = 0; 51 51 52 private static QuadTree TREE; 53 private static Fastmap FMAP; 54 private static EuclideanDistance DIST; 55 52 56 // cluster 53 57 private static ArrayList<ArrayList<QuadTreePayload<Instance>>> cluster = new ArrayList<ArrayList<QuadTreePayload<Instance>>>(); … … 130 134 Instance clusterInstance = createInstance(traindata, instance); 131 135 136 // build temp dist matrix 137 double[][] distmat = new double[3][3]; 138 139 140 132 141 // get distance of this instance to every other instance 133 142 // if the distance is minimal apply the classifier of the current cluster 143 144 /* 134 145 int cnumber; 135 146 int min_cluster = -1; … … 155 166 throw new RuntimeException("min_cluster not found"); 156 167 } 168 */ 157 169 158 170 // classify the passed instance with the cluster we found … … 180 192 181 193 // 3. calculate distance matrix (needed for Fastmap because it starts at dimension 1) 182 EuclideanDistance d= new EuclideanDistance(train);194 DIST = new EuclideanDistance(train); 183 195 double[][] dist = new double[train.size()][train.size()]; 184 196 for(int i=0; i < train.size(); i++) { 185 197 for(int j=0; j < train.size(); j++) { 186 dist[i][j] = d.distance(train.get(i), train.get(j));198 dist[i][j] = DIST.distance(train.get(i), train.get(j)); 187 199 } 188 200 } 189 201 190 202 // 4. run fastmap for 2 dimensions on the distance matrix 191 F astmap f= new Fastmap(2, dist);192 f.calculate();193 double[][] X = f.getX();203 FMAP = new Fastmap(2, dist); 204 FMAP.calculate(); 205 double[][] X = FMAP.getX(); 194 206 195 207 // quadtree payload generation … … 219 231 220 232 // 5. generate quadtree 221 QuadTree q= new QuadTree(null, qtp);233 TREE = new QuadTree(null, qtp); 222 234 ALPHA = Math.sqrt(train.size()); 223 235 SIZE = train.size(); … … 226 238 227 239 // set the size and then split the tree recursively at the median value for x, y 228 q.setSize(new double[] {small[0], big[0]}, new double[] {small[1], big[1]});229 q.recursiveSplit(q);240 TREE.setSize(new double[] {small[0], big[0]}, new double[] {small[1], big[1]}); 241 TREE.recursiveSplit(TREE); 230 242 231 243 // generate list of nodes sorted by density (childs only) 232 ArrayList<QuadTree> l = new ArrayList<QuadTree>( q.getList(q));244 ArrayList<QuadTree> l = new ArrayList<QuadTree>(TREE.getList(TREE)); 233 245 234 246 // recursive grid clustering (tree pruning), the values are stored in cluster 235 q.gridClustering(l);247 TREE.gridClustering(l); 236 248 237 249 // wir iterieren durch die cluster und sammeln uns die instanzen daraus … … 301 313 302 314 /*2 x k pivot Array one pair per recursive call*/ 303 private double[][] PA;315 private int[][] PA; 304 316 305 317 /*Objects we got (distance matrix)*/ … … 312 324 private int target_dims = 0; 313 325 326 /*3 x k tmp projections array, we need this for later projections*/ 327 double[][] tmpX; 328 329 /**/ 314 330 public Fastmap(int k, double[][] O) { 331 this.tmpX = new double[2*k+1][k]; 315 332 this.O = O; 316 333 int N = O.length; … … 319 336 320 337 this.X = new double[N][k]; 321 this.PA = new double[2][k];338 this.PA = new int[2][k]; 322 339 } 323 340 … … 349 366 350 367 /** 368 * Distance calculation used for adding an Instance after initialization is complete 369 * 370 * @param x x index of x image (if k==0 x object) 371 * @param y y index of y image (if k==0 y object) 372 * @param kdimensionality 373 * @param distmat temp distmatrix for the instance to be added 374 * @return distance between x, y 375 */ 376 public double tmpDist(int x, int y, int k, double[][] distmat) { 377 double tmp = distmat[x][y] * distmat[x][y]; 378 379 // decrease by projections 380 for(int i=0; i < k; i++) { 381 double tmp2 = (this.tmpX[x][i] - this.tmpX[y][i]); 382 tmp -= tmp2 * tmp2; 383 } 384 385 //return Math.abs(tmp); 386 return tmp; 387 } 388 389 /** 390 * Projects an instance after initialization is complete 391 * 392 * This uses the previously saved pivot elements. 393 * 394 * @param distmat distance matrix of the instance and pivot elements (3x3 matrix) 395 * @return vector of the projection values (k-vector) 396 */ 397 public double[] addInstance(double[][] distmat) { 398 399 for(int k=0; k < this.target_dims; k++) { 400 401 double dxy = this.dist(this.PA[0][k], this.PA[1][k], k); 402 403 for(int i=0; i < distmat.length; i++) { 404 405 double dix = this.tmpDist(i, 2*k+1, k, distmat); 406 double diy = this.tmpDist(i, 2*k+2, k, distmat); 407 408 // projektion speichern 409 this.tmpX[i][k] = (dix + dxy - diy) / (2 * Math.sqrt(dxy)); 410 } 411 } 412 413 double[] ret = new double[this.target_dims]; 414 for(int k=0; k < this.target_dims; k++) { 415 ret[k] = this.tmpX[0][k]; 416 } 417 return ret; 418 } 419 420 /** 351 421 * Find the object farthest from the given index 352 422 * This method is a helper Method for findDistandObjects … … 418 488 double diy = this.dist(i, pivots[1], this.col); 419 489 420 double tmp = (dix + dxy - diy) / 2 * Math.sqrt(dxy);490 double tmp = (dix + dxy - diy) / (2 * Math.sqrt(dxy)); 421 491 422 492 this.X[i][this.col] = tmp;
Note: See TracChangeset
for help on using the changeset viewer.