OPTICS光学算法

  1 package com.my.optics;
  2 
  3 public class DataPoint {
  4 private String name;//样本的名字
  5 private double dimensioin[];//样本点的维度
  6 private double coreDistance;//核心距离
  7 private double reachableDistance;//可达距离
  8 public DataPoint() {
  9 }
 10 public DataPoint(DataPoint e) {
 11 this.name = e.name;
 12 this.dimensioin = e.dimensioin;
 13 this.coreDistance = e.coreDistance;
 14 this.reachableDistance = e.reachableDistance;
 15 }
 16 public DataPoint(double dimensioin[],String name) {
 17 this.name = name;
 18 this.dimensioin = dimensioin;
 19 this.coreDistance = -1;
 20 this.reachableDistance = -1;
 21 }
 22 public String getName() {
 23 return name;
 24 }
 25 public void setName(String name) {
 26 this.name = name;
 27 }
 28 public double[] getDimensioin() {
 29 return dimensioin;
 30 }
 31 public void setDimensioin(double[] dimensioin) {
 32 this.dimensioin = dimensioin;
 33 }
 34 public double getCoreDistance() {
 35 return coreDistance;
 36 }
 37 public void setCoreDistance(double coreDistance) {
 38 this.coreDistance = coreDistance;
 39 }
 40 public double getReachableDistance() {
 41 return reachableDistance;
 42 }
 43 public void setReachableDistance(double reachableDistance) {
 44 this.reachableDistance = reachableDistance;
 45 }
 46 }
 47 
 48 
 49 package com.my.optics;
 50 
 51 import java.util.ArrayList;
 52 import java.util.Collections;
 53 import java.util.Comparator;
 54 import java.util.List;
 55 
 56 public class ClusterAnalysis {
 57 class ComparatorDp implements Comparator<DataPoint> {
 58 
 59 @Override
 60 public int compare(DataPoint o1, DataPoint o2) {
 61 double temp = o1.getReachableDistance() - o2.getReachableDistance();
 62 int a = 0;
 63 if(temp<0) {
 64 a = -1;
 65 }else {
 66 a = 1;
 67 }
 68 return a;
 69 }
 70 }
 71 public List<DataPoint> startAnalysis(List<DataPoint> dataPoints,double radius,int ObjectNum) {        
 72 List<DataPoint> dpList = new ArrayList<DataPoint>(); 
 73 List<DataPoint> dpQue = new ArrayList<DataPoint>();
 74 int total = 0;        
 75 while (total < dataPoints.size()) {
 76 if (isContainedInList(dataPoints.get(total), dpList) == -1 ) {
 77 List<DataPoint> tmpDpList = isKeyAndReturnObjects(dataPoints.get(total),
 78 dataPoints, radius, ObjectNum);
 79 if(tmpDpList != null && tmpDpList.size() > 0){
 80 DataPoint newDataPoint=new DataPoint(dataPoints.get(total));
 81 dpQue.add(newDataPoint);
 82 }
 83 }
 84 while (!dpQue.isEmpty()) {
 85 DataPoint tempDpfromQ = dpQue.remove(0);
 86 DataPoint newDataPoint=new DataPoint(tempDpfromQ);
 87 dpList.add(newDataPoint);
 88 List<DataPoint> tempDpList = isKeyAndReturnObjects(tempDpfromQ,dataPoints, radius, ObjectNum);
 89 System.out.println(newDataPoint.getName()+":"+newDataPoint.getReachableDistance());
 90 if (tempDpList != null && tempDpList.size() > 0) {
 91 for (int i = 0; i < tempDpList.size(); i++) { 
 92 DataPoint tempDpfromList = tempDpList.get(i); 
 93 int indexInList = isContainedInList(tempDpfromList,dpList);
 94 int indexInQ = isContainedInList(tempDpfromList, dpQue);
 95 if (indexInList == -1) { 
 96 if (indexInQ > -1) {
 97 int index = -1;
 98 for (DataPoint dataPoint : dpQue) {
 99 index++;
100 if (index == indexInQ) {
101 if (dataPoint.getReachableDistance() > tempDpfromList.getReachableDistance()) {
102 dataPoint.setReachableDistance(tempDpfromList.getReachableDistance());
103 } 
104 } 
105 } 
106 } else {
107 dpQue.add(new DataPoint(tempDpfromList));
108 }
109 } 
110 }
111 // TODO:对Q进行重新排序
112 Collections.sort(dpQue, new ComparatorDp());
113 }
114 }
115 System.out.println("------");
116 total++;
117 }
118 return dpList;
119 }       
120 public void displayDataPoints(List<DataPoint> dps){
121 for(DataPoint dp: dps){
122 System.out.println(dp.getName()+":"+dp.getReachableDistance());
123 }
124 }
125 private int isContainedInList(DataPoint dp, List<DataPoint> dpList) {
126 int index = -1;
127 for (DataPoint dataPoint : dpList) {
128 index++;
129 if (dataPoint.getName().equals(dp.getName())) {
130 return index;
131 }
132 } 
133 return -1;
134 } 
135 private List<DataPoint> isKeyAndReturnObjects(DataPoint dataPoint,List<DataPoint> dataPoints,double radius,int ObjectNum){
136 List<DataPoint> arrivableObjects=new ArrayList<DataPoint>();
137 //用来存储所有直接密度可达对象
138 List<Double> distances=new ArrayList<Double>();
139 //欧几里得距离
140 double coreDistance;
141 //核心距离
142 for (int i = 0; i < dataPoints.size(); i++) {
143 DataPoint dp = dataPoints.get(i);
144 double distance = getDistance(dataPoint, dp);
145 if (distance <= radius) {
146 distances.add(distance);
147 arrivableObjects.add(dp); 
148 }
149 }
150 if(arrivableObjects.size()>=ObjectNum){
151 List<Double> newDistances=new ArrayList<Double>(distances);
152 Collections.sort(distances);
153 coreDistance=distances.get(ObjectNum-1);
154 for(int j=0;j<arrivableObjects.size();j++){
155 if (coreDistance > newDistances.get(j)) {
156 if(newDistances.get(j)==0){
157 dataPoint.setReachableDistance(coreDistance);
158 }
159 arrivableObjects.get(j).setReachableDistance(coreDistance);
160 }else{
161 arrivableObjects.get(j).setReachableDistance(newDistances.get(j)); 
162 }
163 }
164 return arrivableObjects;
165 }       return null;
166 }
167 private double getDistance(DataPoint dp1,DataPoint dp2){
168 double distance=0.0; 
169 double[] dim1=dp1.getDimensioin();
170 double[] dim2=dp2.getDimensioin();
171 if(dim1.length==dim2.length){
172 for(int i=0;i<dim1.length;i++){
173 double temp=Math.pow((dim1[i]-dim2[i]), 2);
174 distance=distance+temp;
175 }
176 distance=Math.pow(distance, 0.5);
177 return distance;
178 }
179 return distance;
180 } 
181 public static void main(String[] args){ 
182 List<DataPoint> dpoints = new ArrayList<DataPoint>();
183 double[] a={2,3};
184 double[] b={2,4};
185 double[] c={1,4}; 
186 double[] d={1,3}; 
187 double[] e={2,2};
188 double[] f={3,2}; 
189 double[] g={8,7};
190 double[] h={8,6};
191 double[] i={7,7};
192 double[] j={7,6};
193 double[] k={8,5}; 
194 double[] l={100,2};//孤立点 
195 double[] m={8,20};
196 double[] n={8,19}; 
197 double[] o={7,18}; 
198 double[] p={7,17};
199 double[] q={8,21};
200 dpoints.add(new DataPoint(a,"a"));
201 dpoints.add(new DataPoint(b,"b"));
202 dpoints.add(new DataPoint(c,"c")); 
203 dpoints.add(new DataPoint(d,"d")); 
204 dpoints.add(new DataPoint(e,"e")); 
205 dpoints.add(new DataPoint(f,"f"));
206 dpoints.add(new DataPoint(g,"g"));
207 dpoints.add(new DataPoint(h,"h"));
208 dpoints.add(new DataPoint(i,"i")); 
209 dpoints.add(new DataPoint(j,"j"));
210 dpoints.add(new DataPoint(k,"k"));
211 dpoints.add(new DataPoint(l,"l"));
212 dpoints.add(new DataPoint(m,"m"));
213 dpoints.add(new DataPoint(n,"n"));
214 dpoints.add(new DataPoint(o,"o"));
215 dpoints.add(new DataPoint(p,"p"));
216 dpoints.add(new DataPoint(q,"q"));
217 ClusterAnalysis ca=new ClusterAnalysis(); 
218 List<DataPoint> dps=ca.startAnalysis(dpoints, 2, 4);
219 ca.displayDataPoints(dps); 
220 }
221 }
View Code

说是这个算法我也不是太明白,因为涉及到数学上的知识我就不做太多的描述了

posted @ 2013-08-22 20:14  wq920  阅读(741)  评论(0编辑  收藏  举报