Skip to content

Commit

Permalink
Experiment 2, 3 from gdymind
Browse files Browse the repository at this point in the history
  • Loading branch information
gdymind authored and Shimingyi committed Feb 13, 2018
1 parent 9fd3368 commit 5cf6786
Show file tree
Hide file tree
Showing 36 changed files with 3,607 additions and 0 deletions.
126 changes: 126 additions & 0 deletions students/DongHuibing/Experiment1/DBSCAN/src/DBSCAN.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
struct Node
{
double x, y;
int truth;
int cluster;//-1:undefined;0:noise;

bool operator< (const Node &rhs) const
{
if (x != rhs.x) return x < rhs.x;
return y < rhs.y;
}
};
vector<Node> nodes;
const int MAXN = 1e4 + 5;
double dis[MAXN][MAXN];
double eps;
int minPts;

void readData()
{
string s = "";
cout << "Please enter the file name , eps and minPts:" << endl;
cin >> s >> eps >> minPts;
freopen(("../../data/synthetic_data/" + s).c_str(), "r", stdin);
double x, y;
int t;
while(~scanf("%lf,%lf,%d", &x, &y, &t))
{
nodes.push_back((Node){x, y, t, -1});
}
}

double squDistance(Node &u, Node &v)
{
double dx = u.x - v.x;
double dy = u.y - v.y;
return dx * dx + dy * dy;
}

double mydistance(Node &u, Node &v)
{
return sqrt(squDistance(u, v));
}

void calcDis()
{
for(int i = 0; i < nodes.size(); ++i)
{
for(int j = i + 1; j < nodes.size(); ++j)
{
dis[i][j] = dis[j][i] = squDistance(nodes[i], nodes[j]);
}
}
}

void dbscan()
{
int clusterId = 0;
for(int i = 0; i < nodes.size(); ++i)
{
Node &cur = nodes[i];
if(cur.cluster >= 0) continue;
queue<int> neighbors;
set<int> se;
se.insert(i);
for(int j = 0; j < nodes.size(); ++j)
{
if(i == j || nodes[j].cluster > 0) continue;
if(mydistance(cur, nodes[j]) <= eps)
{
neighbors.push(j);
se.insert(j);
}
}
if(neighbors.size() < minPts)
{
nodes[i].cluster = 0;
continue;
}
nodes[i].cluster = ++clusterId;
while(!neighbors.empty())
{
int tp = neighbors.front();
neighbors.pop();
nodes[tp].cluster = clusterId;
for(int j = 0; j < nodes.size(); ++j)
{
if(j == i || j == tp || nodes[j].cluster > 0 || se.find(j) != se.end()) continue;
if(mydistance(nodes[tp], nodes[j]) <= eps)
{
se.insert(j);
neighbors.push(j);
}
}
}
}
}

void out(vector<Node> &nodes)
{
for(int i = 0; i < nodes.size(); ++i)
cout << nodes[i].x << ' ' << nodes[i].y << ' ' << nodes[i].truth << ' ' << nodes[i].cluster << endl;
}

void outputResult()
{
freopen("../data/data.tsv", "w", stdout);
puts("x\ty\tclusters");
for(int i = 0; i < nodes.size(); ++i)
{
printf("%.2f\t%.2f\tCluster%d\n", nodes[i].x, nodes[i].y, nodes[i].cluster);
}
}


int main()
{
readData();
calcDis();
dbscan();
outputResult();
return 0;
}
114 changes: 114 additions & 0 deletions students/DongHuibing/Experiment1/DBSCAN/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
font: 10px sans-serif;
}

.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

.dot {
stroke: #eee;
}

</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.7/d3.min.js"></script>
<!–– do not use version 4 of d3, some functions API are different ––>
<script>

var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var x = d3.scale.linear()
.range([0, width]);

var y = d3.scale.linear()
.range([height, 0]);

var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(y)
.orient("left");

var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.tsv("../data/data.tsv", function(error, data) {
if (error) throw error;

data.forEach(function(d) {
d.y = +d.y;
d.x = +d.x;
});

x.domain(d3.extent(data, function(d) { return d.x; })).nice();
y.domain(d3.extent(data, function(d) { return d.y; })).nice();

svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("x");

svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("y")

svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.x); })
.attr("cy", function(d) { return y(d.y); })
.style("fill", function(d) { return color(d.clusters); });

var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);

legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });

});

</script>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
struct Node
{
double x, y;
int truth;
int cluster;//-1:undefined;0:noise;

bool operator< (const Node &rhs) const
{
if (x != rhs.x) return x < rhs.x;
return y < rhs.y;
}
};
vector<Node> nodes;
const int MAXN = 1e4 + 5;
double sim[MAXN][MAXN], deg[MAXN][MAXN], L[MAXN][MAXN];
double sigma;
int k;

void readData()
{
string s = "";
cout << "Please enter the file name , sigma and k:" << endl;
cin >> s >> sigma >> k;
freopen(("../data/synthetic_data/" + s).c_str(), "r", stdin);
double x, y;
int t;
while(~scanf("%lf,%lf,%d", &x, &y, &t))
nodes.push_back((Node){x, y, t, -1});
}

double squDistance(Node &u, Node &v)
{
double dx = u.x - v.x;
double dy = u.y - v.y;
return dx * dx + dy * dy;
}


void calcSim()
{
for(int i = 0; i < nodes.size(); ++i)
for(int j = i; j < nodes.size(); ++j)
sim[i][j] = sim[j][i] = exp(-squDistance(nodes[i], nodes[j])*0.5/sigma/sigma);
}

void calcDeg()
{
for(int i = 0; i < nodes.size(); ++i)
for(int j = i; j < nodes.size(); ++j) deg[i][j] = deg[j][i] = 0;
for(int i = 0; i < nodes.size(); ++i)
for(int j = i + 1; j < nodes.size(); ++j)
{
deg[i][i] += sim[i][j];
deg[j][j] += sim[i][j];
}
}

void calcLap()
{
for(int i = 0; i < nodes.size(); ++i)
for(int j = 0; j < nodes.size(); ++j) L[i][j] = sim[i][j] - deg[i][j];
}

void outputResult()
{
freopen("../data/data.tsv", "w", stdout);
puts("x\ty\tclusters");
for(int i = 0; i < nodes.size(); ++i)
{
printf("%.2f\t%.2f\tCluster%d\n", nodes[i].x, nodes[i].y, nodes[i].cluster);
}
}


int main()
{
readData();
calcSim();
calcDeg();
calcLap();
outputResult();
return 0;
}
Loading

0 comments on commit 5cf6786

Please sign in to comment.