Advertisement
Guest User

ffa_fnc_kmean_clustering

a guest
Mar 17th, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. /*
  2. File: ffa_fnc_kmean_clustering.sqf
  3. Author: ArseniyK
  4.  
  5. Description:
  6. Feature clustering array by the method of k-means
  7. Функция кластеризации массива методом k-means
  8.  
  9. Parameter(s):
  10. _this select 0: array (Array) [[x,y,z],[x1,y1,z1],[x2,y2,z2],[x3,y3,z3],[x4,y4,z4],[x5,y5,z5],[x6,y6,z6],[x7,y7,z7],[x8,y8,z8]...]
  11. _this select 1: the number of clusters (Number)
  12.  
  13. Returns:
  14. Array - format [[[x,y,z],[x1,y1,z1],[x2,y2,z2]],[[x3,y3,z3],[x4,y4,z4],[x5,y5,z5]],[[x6,y6,z6],[x7,y7,z7],[x8,y8,z8]]...]
  15. */
  16. private ["_means","_xx","_y","_k","_points","_centers","_new_centers","_clusters","_point","_dist","_cluster_index","_dist1"];
  17. KK_fnc_isEqual = {
  18. switch (_this select 0) do
  19. {
  20. case (_this select 1) : {true};
  21. default {false};
  22. };
  23. };
  24. _means = {
  25. _xx = 0;
  26. _y = 0;
  27. {
  28. _xx = _xx + (_x select 0);
  29. _y = _y + (_x select 1);
  30. }foreach _this;
  31. _xx = _xx / (count _this);
  32. _y = _y / (count _this);
  33. [floor _xx, floor _y];
  34. };
  35.  
  36.  
  37. _k = _this select 0;
  38. _points = _this select 1;
  39. _centers = [];
  40. _new_centers = [];
  41. _clusters = [];
  42. _clusters_2 = [];
  43. for "_i" from 0 to _k - 1 do{
  44. _centers set [_i, _points select (floor (random (count _points)))];
  45. _clusters set [_i, []];
  46. };
  47. for "_i" from 0 to _k - 1 do{
  48. _new_centers set [_i, _points select (floor (random (count _points)))];
  49. };
  50. //player sidechat str(_centers);
  51. //while {([_new_centers, _centers] call BIS_fnc_areEqual)} do //убрал
  52. for "_i" from 0 to 1 step 0 do //добавил
  53. {
  54. _centers = []+_new_centers;
  55. {
  56. _point = _x;
  57. _dist = _point distance (_centers select 0);
  58. _cluster_index = 0;
  59. {
  60. _dist1 = _x distance _point;
  61. if (_dist1 <= _dist) then {_dist = _dist1; _cluster_index = _foreachindex;}
  62. } foreach _centers;
  63. _cluster = _clusters select _cluster_index;
  64. _cluster set [count _cluster, _x];
  65. _clusters set [_cluster_index, _cluster];
  66. } foreach _points;
  67. _new_centers = [];
  68. _clusters_2 = []+_clusters;
  69. //player sideChat str(_centers);
  70. {
  71. if (count _x > 0) then {
  72. _new_centers set [count _new_centers, _x call _means];
  73. _clusters set [count _clusters, []];
  74. }
  75. }foreach _clusters_2;
  76. if !([_new_centers, _centers] call KK_fnc_isEqual ) exitwith {}; //добавил
  77. };
  78. _clusters_2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement