My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Copyright 2007 Tom Gibara
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0

* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.tomgibara.cluster;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

import com.tomgibara.cluster.gvm.dbl.DblClusters;
import com.tomgibara.cluster.gvm.dbl.DblListKeyer;
import com.tomgibara.cluster.gvm.dbl.DblResult;

public class ClusterPoints {

public static void main(String[] args) throws IOException {
cluster("cross", 2);
cluster("gmouse", 3);
cluster("umouse", 3);
cluster("faithful", 2);
}

private static void cluster(String name, int capacity) throws IOException {
Scanner scanner = new Scanner(new File("../cluster-common/R/" + name + ".txt"));
ArrayList<double[]> pts = new ArrayList<double[]>();
while (scanner.hasNext()) {
double[] pt = new double[2];
pt[0] = scanner.nextDouble();
pt[1] = scanner.nextDouble();
pts.add(pt);
}
scanner.close();

Collections.shuffle(pts, new Random(0L));

DblClusters<List<double[]>> clusters = new DblClusters<List<double[]>>(2, capacity);
clusters.setKeyer(new DblListKeyer<double[]>());
for (double[] pt : pts) {
ArrayList<double[]> key = new ArrayList<double[]>();
key.add(pt);
clusters.add(1.0, pt, key);
}

FileWriter writer = new FileWriter("../cluster-common/R/" + name + "-clustered.txt");
final List<DblResult<List<double[]>>> results = clusters.results();
for (int i = 0; i < results.size(); i++) {
for (double[] pt : results.get(i).getKey()) {
writer.write(String.format("%3.3f %3.3f %d%n", pt[0], pt[1], i+1));
}
}
writer.close();
}

}

Change log

r176 by tomgibara on Apr 14, 2011   Diff
[maven-release-plugin]  copy for tag
cluster-all-1.0
Go to: 
Project members, sign in to write a code review

Older revisions

r167 by tomgibara on Apr 14, 2011   Diff
Added licence notices to source files.
r162 by tomgibara on Apr 12, 2011   Diff
Initial code commit for GVM.
All revisions of this file

File info

Size: 2245 bytes, 72 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting