Advertisement
cyberjab

Untitled

Apr 9th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. #pragma once
  2. #pragma GCC optimize("03")
  3. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <cstdlib>
  7. #include <cstdio>
  8. #include <string>
  9. #include <vector>
  10. #include <map>
  11. #include <set>
  12. #include <unordered_map>
  13. #include <unordered_set>
  14. #include <queue>
  15. #include <deque>
  16. #include <cmath>
  17. #include <numeric>
  18. #include <algorithm>
  19. #include <ctime>
  20. #include <chrono>
  21. #include <random>
  22. #include <functional>
  23. #include <math.h>
  24. #include <time.h>
  25. #include <ctime>
  26.  
  27. using namespace std;
  28. using ll = long long;
  29. using db = double;
  30. using ldb = long double;
  31. using pii = pair<int, int>;
  32. using pll = pair<ll, ll>;
  33. using pdd = pair<ldb, ldb>;
  34. using vint = vector<int>;
  35. using vll = vector<ll>;
  36. #define all(x) x.begin(), x.end()
  37. #define size(x) int(x.size())
  38. #define rep(x) for(int rep_i = 0; rep_i < x; ++rep_i)
  39. #define forp(i, s, f) for(int i = s; i < f; ++i)
  40. #define form(i, s, f) for(int i = s; i > f; --i)
  41. #define PB push_back
  42. #define MP make_pair
  43. #define F first
  44. #define S second
  45. #define elif else if
  46. #define dprint(x) for (auto now: x) cout << now << " "
  47.  
  48. const int MOD = 1e9 + 7;
  49. const int MOD2 = 998244353;
  50. const int INF = 2e9 + 7;
  51. const ll LINF = 1e18 + 7;
  52. const double EPS = 1e-9;
  53. const long double PI = acosl(-1.0);
  54.  
  55. struct cluster_center {
  56. pdd position = { 0, 0 };
  57. int obj_count = 0;
  58. ldb x_sum = 0;
  59. ldb y_sum = 0;
  60.  
  61. friend bool operator == (const cluster_center& a, const cluster_center& b);
  62. };
  63.  
  64. class k_meansRBT {
  65. private:
  66. vector<cluster_center> centers;
  67. vector<pdd> data;
  68. multimap <pdd, int> objects;
  69. //----------------------------------------------------------------------------
  70. //объекты в кчд - ключи, значения - куча (расстояние до кластера, кластер)
  71. //----------------------------------------------------------------------------
  72. int K = 0;
  73. int iterations = 100;
  74.  
  75. int RandNumberInt(int min, int max)
  76. {
  77. static std::mt19937 gen(time(NULL));
  78. std::uniform_int_distribution<> uid(min, max);
  79. return uid(gen);
  80. }
  81.  
  82. ldb RandNumberReal(ldb min, ldb max)
  83. {
  84. static std::mt19937 gen(time(NULL));
  85. std::uniform_real_distribution<> uid(min, max);
  86. return uid(gen);
  87. }
  88.  
  89. double mysqrt(double x) {
  90. if (x <= 0)
  91. return 0;
  92. int exp = 0;
  93. x = frexp(x, &exp);
  94. if (exp & 1) {
  95. exp--;
  96. x *= 2;
  97. }
  98. double y = (1 + x) / 2;
  99. double z = 0;
  100. while (y != z) {
  101. z = y;
  102. y = (y + x / y) / 2;
  103. }
  104. return ldexp(y, exp / 2);
  105. }
  106.  
  107. ldb pow_2(ldb x) {
  108. return x * x;
  109. }
  110.  
  111. void set_iterations(int x) {
  112. iterations = x;
  113. }
  114.  
  115. void compute_new_center(int i, pdd point, int c) {
  116. if (i < 0) return;
  117. ldb x = centers[i].x_sum + point.first;
  118. ldb y = centers[i].y_sum + point.second;
  119. int cnt = centers[i].obj_count + c;
  120. centers[i].position = MP(x / cnt, y / cnt);
  121. centers[i].x_sum = x;
  122. centers[i].y_sum = y;
  123. centers[i].obj_count = cnt;
  124. }
  125.  
  126. ldb compute_dist(pdd p1, pdd p2) {
  127. ldb x1 = p1.first;
  128. ldb y1 = p1.second;
  129. ldb x2 = p2.first;
  130. ldb y2 = p2.second;
  131. return mysqrt(pow_2(x1 - x2) + pow_2(y1 - y2));
  132. }
  133.  
  134. void make_random_centers1(int k) {
  135. map <pdd, bool> used;
  136. ldb r = RandNumberInt(0, size(data) - 1);
  137. pdd center = data[r];
  138. centers.resize(1);
  139. compute_new_center(0, center, 1);
  140. objects.find(data[r])->second = 0;
  141. used[centers[0].position] = true;
  142. int cluster = 1;
  143. int ds = size(data);
  144. while (cluster < k) {
  145. vector<ldb> sp;
  146. ldb sm = 0;
  147. forp(i, 0, ds) {
  148. priority_queue<pair<ldb, int>, vector<pair<ldb, int>>, greater<pair<ldb, int>>> pq;
  149. forp(j, 0, size(centers)) {
  150. ldb dist = pow_2(compute_dist(data[i], centers[j].position));
  151. pq.push(MP(dist, j));
  152. }
  153. pair <ldb, int> point = pq.top();
  154. sm += point.first;
  155. sp.PB(point.first);
  156. }
  157. ldb rnd = RandNumberReal(0, 1 - EPS) * sm;
  158. ldb sm2 = 0;
  159. int i = 0;
  160. while (i < ds && sm2 < rnd) {
  161. sm2 += sp[i];
  162. if (!used[data[i]] && rnd <= sm2) {
  163. centers.resize(cluster + 1);
  164. compute_new_center(cluster, data[i], 1);
  165. objects.find(data[i])->second = cluster;
  166. used[data[i]] = true;
  167. cluster++;
  168. break;
  169. }
  170. i++;
  171. }
  172. }
  173. }
  174.  
  175. void make_random_centers2(int k) {
  176. map <pdd, bool> used;
  177. sort(data.begin(), data.end());
  178. ldb r = 0;
  179. pdd center = data[r];
  180. centers.resize(1);
  181. compute_new_center(0, center, 1);
  182. objects.find(data[r])->second = 0;
  183. used[centers[0].position] = true;
  184. int cluster = 1;
  185. int ds = size(data);
  186. while (cluster < k) {
  187. priority_queue<pair<ldb, int>> sp;
  188. forp(i, 0, ds) {
  189. priority_queue<pair<ldb, int>, vector<pair<ldb, int>>, greater<pair<ldb, int>>> pq;
  190. forp(j, 0, size(centers)) {
  191. ldb dist = compute_dist(data[i], centers[j].position);
  192. pq.push(MP(dist, j));
  193. }
  194. pair <ldb, int> point = pq.top();
  195. sp.push(MP(point.first, i));
  196. }
  197. pair<ldb, int> el = sp.top();
  198. int i = el.second;
  199. if (!used[data[i]]) {
  200. centers.resize(cluster + 1);
  201. compute_new_center(cluster, data[i], 1);
  202. objects.find(data[i])->second = cluster;
  203. used[data[i]] = true;
  204. cluster++;
  205. }
  206.  
  207. }
  208. }
  209.  
  210. void load_data(vector<pdd> x) {
  211. data = x;
  212. for (auto now : x) {
  213. objects.insert(MP(now, -1));
  214. }
  215. }
  216.  
  217. void k_means_mf(int k) {
  218. K = k;
  219. forp(cnt, 0, iterations) {
  220. bool flag = true;
  221. for (auto& now : objects) {
  222. int prev_cluster = now.second;
  223. priority_queue<pair<ldb, int>, vector<pair<ldb, int>>, greater<pair<ldb, int>>> pq;
  224. forp(j, 0, K) {
  225. ldb dist = compute_dist(centers[j].position, now.first);
  226. pq.push(MP(dist, j));
  227. }
  228. pair <ldb, int> best_center = pq.top();
  229. now.second = best_center.second;
  230. if (prev_cluster != now.second || cnt == 0) {
  231. flag = false;
  232. compute_new_center(now.second, now.first, 1);
  233. compute_new_center(prev_cluster, MP(-now.first.first, -now.first.second), -1);
  234. }
  235. }
  236. if (flag) {
  237. break;
  238. }
  239. }
  240. }
  241.  
  242. void print_data() {
  243. vector<vector<pdd>> cluss(K);
  244. for (auto it = objects.begin(); it != objects.end(); it++) {
  245. cluss[it->second].push_back(it->first);
  246. }
  247. int c = 0;
  248. forp(i, 0, K) {
  249. cout << "Cluster " << i + 1 << ": ";
  250. for (auto now : cluss[i]) {
  251. cout << "( " << now.first << ", " << now.second << " ); ";
  252. c++;
  253. }
  254. cout << '\n';
  255. }
  256. //cout << c << '\n';
  257. }
  258.  
  259. public:
  260. k_meansRBT() {
  261. }
  262.  
  263. vector<vector<pdd>> data_to_draw() {
  264. vector<vector<pdd>> cluss(K);
  265. for (auto it = objects.begin(); it != objects.end(); it++) {
  266. cluss[it->second].push_back(it->first);
  267. }
  268. return cluss;
  269. }
  270.  
  271. bool clusterisation(vector<pdd> data, int k) {
  272. if (k < 1 || k > size(data)) {
  273. cout << "Invalid number of clusters!\n";
  274. return false;
  275. }
  276. else {
  277. this->load_data(data);
  278. this->make_random_centers1(k);
  279. this->k_means_mf(k);
  280. //this->print_data();
  281. return true;
  282. }
  283. }
  284.  
  285. vector<pdd> get_centers() {
  286. vector<pdd> x;
  287. for (auto now: centers) {
  288. x.push_back(now.position);
  289. }
  290. return x;
  291. }
  292.  
  293.  
  294. };
  295.  
  296.  
  297.  
  298. int main() {
  299. ios_base::sync_with_stdio(0);
  300. cin.tie(0);
  301. cout << setprecision(6);
  302. cout << fixed;
  303. return 0;
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement