package com.my.optics;

 public class DataPoint {
private String name;//样本的名字
private double dimensioin[];//样本点的维度
private double coreDistance;//核心距离
private double reachableDistance;//可达距离
public DataPoint() {
}
public DataPoint(DataPoint e) {
this.name = e.name;
this.dimensioin = e.dimensioin;
this.coreDistance = e.coreDistance;
this.reachableDistance = e.reachableDistance;
}
public DataPoint(double dimensioin[],String name) {
this.name = name;
this.dimensioin = dimensioin;
this.coreDistance = -1;
this.reachableDistance = -1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double[] getDimensioin() {
return dimensioin;
}
public void setDimensioin(double[] dimensioin) {
this.dimensioin = dimensioin;
}
public double getCoreDistance() {
return coreDistance;
}
public void setCoreDistance(double coreDistance) {
this.coreDistance = coreDistance;
}
public double getReachableDistance() {
return reachableDistance;
}
public void setReachableDistance(double reachableDistance) {
this.reachableDistance = reachableDistance;
}
} package com.my.optics; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class ClusterAnalysis {
class ComparatorDp implements Comparator<DataPoint> { @Override
public int compare(DataPoint o1, DataPoint o2) {
double temp = o1.getReachableDistance() - o2.getReachableDistance();
int a = 0;
if(temp<0) {
a = -1;
}else {
a = 1;
}
return a;
}
}
public List<DataPoint> startAnalysis(List<DataPoint> dataPoints,double radius,int ObjectNum) {
List<DataPoint> dpList = new ArrayList<DataPoint>();
List<DataPoint> dpQue = new ArrayList<DataPoint>();
int total = 0;
while (total < dataPoints.size()) {
if (isContainedInList(dataPoints.get(total), dpList) == -1 ) {
List<DataPoint> tmpDpList = isKeyAndReturnObjects(dataPoints.get(total),
dataPoints, radius, ObjectNum);
if(tmpDpList != null && tmpDpList.size() > 0){
DataPoint newDataPoint=new DataPoint(dataPoints.get(total));
dpQue.add(newDataPoint);
}
}
while (!dpQue.isEmpty()) {
DataPoint tempDpfromQ = dpQue.remove(0);
DataPoint newDataPoint=new DataPoint(tempDpfromQ);
dpList.add(newDataPoint);
List<DataPoint> tempDpList = isKeyAndReturnObjects(tempDpfromQ,dataPoints, radius, ObjectNum);
System.out.println(newDataPoint.getName()+":"+newDataPoint.getReachableDistance());
if (tempDpList != null && tempDpList.size() > 0) {
for (int i = 0; i < tempDpList.size(); i++) {
DataPoint tempDpfromList = tempDpList.get(i);
int indexInList = isContainedInList(tempDpfromList,dpList);
int indexInQ = isContainedInList(tempDpfromList, dpQue);
if (indexInList == -1) {
if (indexInQ > -1) {
int index = -1;
for (DataPoint dataPoint : dpQue) {
index++;
if (index == indexInQ) {
if (dataPoint.getReachableDistance() > tempDpfromList.getReachableDistance()) {
dataPoint.setReachableDistance(tempDpfromList.getReachableDistance());
}
}
}
} else {
dpQue.add(new DataPoint(tempDpfromList));
}
}
}
// TODO:对Q进行重新排序
Collections.sort(dpQue, new ComparatorDp());
}
}
System.out.println("------");
total++;
}
return dpList;
}
public void displayDataPoints(List<DataPoint> dps){
for(DataPoint dp: dps){
System.out.println(dp.getName()+":"+dp.getReachableDistance());
}
}
private int isContainedInList(DataPoint dp, List<DataPoint> dpList) {
int index = -1;
for (DataPoint dataPoint : dpList) {
index++;
if (dataPoint.getName().equals(dp.getName())) {
return index;
}
}
return -1;
}
private List<DataPoint> isKeyAndReturnObjects(DataPoint dataPoint,List<DataPoint> dataPoints,double radius,int ObjectNum){
List<DataPoint> arrivableObjects=new ArrayList<DataPoint>();
//用来存储所有直接密度可达对象
List<Double> distances=new ArrayList<Double>();
//欧几里得距离
double coreDistance;
//核心距离
for (int i = 0; i < dataPoints.size(); i++) {
DataPoint dp = dataPoints.get(i);
double distance = getDistance(dataPoint, dp);
if (distance <= radius) {
distances.add(distance);
arrivableObjects.add(dp);
}
}
if(arrivableObjects.size()>=ObjectNum){
List<Double> newDistances=new ArrayList<Double>(distances);
Collections.sort(distances);
coreDistance=distances.get(ObjectNum-1);
for(int j=0;j<arrivableObjects.size();j++){
if (coreDistance > newDistances.get(j)) {
if(newDistances.get(j)==0){
dataPoint.setReachableDistance(coreDistance);
}
arrivableObjects.get(j).setReachableDistance(coreDistance);
}else{
arrivableObjects.get(j).setReachableDistance(newDistances.get(j));
}
}
return arrivableObjects;
} return null;
}
private double getDistance(DataPoint dp1,DataPoint dp2){
double distance=0.0;
double[] dim1=dp1.getDimensioin();
double[] dim2=dp2.getDimensioin();
if(dim1.length==dim2.length){
for(int i=0;i<dim1.length;i++){
double temp=Math.pow((dim1[i]-dim2[i]), 2);
distance=distance+temp;
}
distance=Math.pow(distance, 0.5);
return distance;
}
return distance;
}
public static void main(String[] args){
List<DataPoint> dpoints = new ArrayList<DataPoint>();
double[] a={2,3};
double[] b={2,4};
double[] c={1,4};
double[] d={1,3};
double[] e={2,2};
double[] f={3,2};
double[] g={8,7};
double[] h={8,6};
double[] i={7,7};
double[] j={7,6};
double[] k={8,5};
double[] l={100,2};//孤立点
double[] m={8,20};
double[] n={8,19};
double[] o={7,18};
double[] p={7,17};
double[] q={8,21};
dpoints.add(new DataPoint(a,"a"));
dpoints.add(new DataPoint(b,"b"));
dpoints.add(new DataPoint(c,"c"));
dpoints.add(new DataPoint(d,"d"));
dpoints.add(new DataPoint(e,"e"));
dpoints.add(new DataPoint(f,"f"));
dpoints.add(new DataPoint(g,"g"));
dpoints.add(new DataPoint(h,"h"));
dpoints.add(new DataPoint(i,"i"));
dpoints.add(new DataPoint(j,"j"));
dpoints.add(new DataPoint(k,"k"));
dpoints.add(new DataPoint(l,"l"));
dpoints.add(new DataPoint(m,"m"));
dpoints.add(new DataPoint(n,"n"));
dpoints.add(new DataPoint(o,"o"));
dpoints.add(new DataPoint(p,"p"));
dpoints.add(new DataPoint(q,"q"));
ClusterAnalysis ca=new ClusterAnalysis();
List<DataPoint> dps=ca.startAnalysis(dpoints, 2, 4);
ca.displayDataPoints(dps);
}
}

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

OPTICS光学算法的更多相关文章

  1. OPTICS聚类算法原理

    OPTICS聚类算法原理 基础 OPTICS聚类算法是基于密度的聚类算法,全称是Ordering points to identify the clustering structure,目标是将空间中 ...

  2. 【Python机器学习实战】聚类算法(2)——层次聚类(HAC)和DBSCAN

    层次聚类和DBSCAN 前面说到K-means聚类算法,K-Means聚类是一种分散性聚类算法,本节主要是基于数据结构的聚类算法--层次聚类和基于密度的聚类算法--DBSCAN两种算法. 1.层次聚类 ...

  3. 应用.Net+Consul维护RabbitMq的高可用性

    懒人学习的过程就是工作中老大让干啥让做啥就研究研究啥,国庆放假回来的周末老大通过钉钉给我布置了个任务, RabbitMQ高可用解决方案,我想说钉钉太坑了: 这是国庆过后9号周日晚上下班给的任务,我周一 ...

  4. tmp

    Hello 大家好,这次给大家带来的是Gear VR4代,首先我得感谢下我们的虎友Hide兄弟友情提供Gear给我们测评,感谢 感谢.之前我录的前哨战也说过,这次Gear VR 4代较3代变化并不是很 ...

  5. English_word_learning

    这次报名参加了学院的21天打卡活动,说实话,也是想给自己一个积累的平台. 毕竟,真的有时候感觉挺弱的 有的人用了一年考完了四六级,而有人却用四年还未考完. 听到有一位学长因为自己的四级成绩没有达到48 ...

  6. 收藏单词TOEFL备份托福英语

    TOEFL托福词汇串讲(文本) alchemy(chem-化学)n. 炼金术 chemistry 化学 alder 赤杨树 联想:older 老人坐在赤杨树下 sloth 树懒 algae n.海藻 ...

  7. Electromagnetic

    1. 电磁辐射 2. 电磁频谱 3. 可见光 4. 微波 5. 更多相关链接 1. 电磁辐射 https://en.wikipedia.org/wiki/Electromagnetic_radiati ...

  8. LeetCode 56,57,60,连刷三题不费劲

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第34篇文章,刚好接下来的题目比较简单,很多和之前的做法类似.所以我们今天出一个合集,一口气做完接下来的57.5 ...

  9. Streamlit:快速数据可视化界面工具

    目录 Streamlit简介 Streamlit使用指南 常用命令 显示文本 显示数据 显示图表 显示媒体 交互组件 侧边栏 缓存机制 Streamlit使用Hack Streamlit的替代品 相关 ...

随机推荐

  1. 执行asp.net上传下载Excel时出现“未在本地计算机上注册“Microsoft.ACE.Oledb.12.0”提供程序。(System.Data)”错误

    服务器没有安装Office导致的错误,如何不想安装庞大的Office,可以下载安装: Microsoft Office Access Database Engine 2007 http://downl ...

  2. JavaScript “\”替换成 “\\”

    str=str.replace(/\\/g,'\\\\');

  3. ActiveMQ(5.10.0) - 使用 JDBC 持久化消息

    1. 编辑 ACTIVEMQ_HOME/conf/activemq.xml. <beans> <broker brokerName="localhost" per ...

  4. Ehcache(2.9.x) - API Developer Guide, Write-Through and Write-Behind Caches

    About Write-Through and Write-Behind Caches Write-through caching is a caching pattern where writes ...

  5. HTTP - 摘要认证

    基本认证便捷灵活,但极不安全.用户名和密码都是以明文形式传送的,也没有采取任何措施防止对报文的篡改.安全使用基本认证的唯一方式就是将其与 SSL 配合使用. 摘要认证是另一种 HTTP 认证协议,它与 ...

  6. Cocos2d-x中SQLite数据库管理工具

    数据库创建完成后,我们可能需要看看数据库中数据是否成功插入,很多人喜欢使用图形界面工具来管理SQLite数据库.SQLite图形界面管理工具有很多,我推荐使用SQLiteStudio工具,下载地址ht ...

  7. 升级NppAstyle中的AstyleLib为最高版本

    注:本文撰写时,NppAstyle的最高版本为0.10.02.14(更新于2013-04-08),Astyle的最高版本为2.05.1(更新于2014-12-11). Astyle是一个很好的代码格式 ...

  8. Windows下Android模拟环境的搭建

  9. LibCurl笔记四

    1,curl设置fiddler代理curl_easy_setopt(m_easyHandle, CURLOPT_PROXY, "127.0.0.1:8888"); 2,

  10. c++ algorithm 的用法

    1 , accumulate()template<class _II, class _Ty> inline_Ty accumulate(_II _F, _II _L, _Ty _V){fo ...