-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
104 lines (94 loc) · 3.04 KB
/
utils.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "utils.h"
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <tuple>
#include <cmath>
#include <algorithm>
void readFile(pointsType& points,std::string filename){
std::ifstream inputFile;
inputFile.open(filename);
std::string line, token;
while(std::getline(inputFile, line)){
std::stringstream lineStream(line);
double x,y;
bool isFirst;
std::getline(lineStream, token,',');
x = std::atof(token.c_str());
std::getline(lineStream, token,',');
y = std::atof(token.c_str());
points.push_back(std::pair<double, double>(x,y));
}
inputFile.close();
}
void writeFile(std::vector<KnnType*>& knns,std::string filename){
std::ofstream outFile;
outFile.open(filename);
for (int i = 0; i < knns.size(); i++){
for (auto const& x : *knns[i]){
outFile << x.first << ": ";
auto neighbours = x.second;
for(int j = 0; j < neighbours.size(); j++){
outFile << neighbours[j].second << " ";
}
outFile << std::endl;
}
}
outFile.close();
}
void writeFile(std::vector<KnnType>& knns,std::string filename){
std::ofstream outFile;
outFile.open(filename);
for (int i = 0; i < knns.size(); i++){
for (auto const& x : knns[i]){
outFile << x.first << ": ";
auto neighbours = x.second;
for(int j = 0; j < neighbours.size(); j++){
outFile << neighbours[j].second << " ";
}
outFile << std::endl;
}
}
outFile.close();
}
KnnType* computeKNN(Task* t){
KnnType* knn = new KnnType();
for(int i = t->start; i < t->end; i++){
std::vector<pi> neighbour(t->k,std::make_pair(DBL_MAX,0));
double max = DBL_MAX;
int index_max = 0;
for(int j = 0; j < t->points->size(); j++){
if(i != j){
double distance = computeDistance(t->points->at(i),t->points->at(j));
pi newPoint = std::make_pair(distance,j);
if(neighbour.size() >= t->k){
if(distance < max){
neighbour[index_max] = newPoint;
std::pair<int,double> max_point = findMax(neighbour);
max = max_point.second;
index_max = max_point.first;
}
}
}
}
std::sort(neighbour.begin(),neighbour.end());
(*knn)[i] = neighbour;
}
return knn;
}
std::pair<int,double> findMax(std::vector< pi >& vec){
double max = vec[0].first;
int index = 0;
for(int i = 1; i < vec.size(); i++){
if (vec[i].first > max){
max = vec[i].first;
index = i;
}
}
return std::make_pair(index,max);
}
double computeDistance(std::pair<double, double> first, std::pair<double, double> second){
return std::sqrt(std::pow((first.first - second.first),2) + std::pow((first.second - second.second),2));
}