Advertisement
Guest User

ffa_fnc_kmean_clustering2

a guest
Mar 17th, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. /*
  2.     File: ffa_fnc_kmean_clustering2.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", "_kmeanspp"];
  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. _k = _this select 0;
  37. _points = _this select 1;
  38. _centers = [];
  39. _new_centers = [];
  40. _clusters = [];
  41. _clusters_2 = [];
  42.  
  43. for "_i" from 0 to _k - 1 do{  
  44.     _clusters set [_i, []];
  45. };
  46.  
  47. _kmeanspp = {
  48.     private ["_sum", "_k", "_points", "_centers", "_rnd", "_dist_s"];
  49.     _k = _this select 0;
  50.     _points = _this select 1;
  51.     _centers = []; 
  52.     _centers set [0, _points select (floor (random (count _points)))];
  53.     _sum = 0;
  54.    
  55.     for "_i" from 0 to _k-2 do {
  56.         _center = _centers select (_i);
  57.         _sum = 0;
  58.         {
  59.            
  60.             _dist_s = (_x distance _center)^2;
  61.             _sum = _sum + _dist_s;
  62.         } foreach _points;
  63.         _rnd = (random 1)*_sum;
  64.         _sum = 0;
  65.         {
  66.             _dist_s = (_x distance _center)^2;
  67.             _sum = _sum + _dist_s;         
  68.             if (_sum > _rnd) exitWith {_centers set [_i+1, _x];};
  69.         } foreach _points;
  70.        
  71.     };
  72.     _centers
  73. };
  74.  
  75. private ["_sum", "_k", "_points", "_centers", "_rnd", "_dist_s"];
  76. _k = _this select 0;
  77. _points = _this select 1;
  78. _new_centers = [_k, _points] call _kmeanspp;
  79. //while {([_new_centers, _centers] call BIS_fnc_areEqual)} do //убрал
  80. for "_i" from 0 to 1 step 0 do //добавил
  81. {
  82.     _centers = []+_new_centers;
  83.     {
  84.         _point = _x;
  85.         _dist = _point distance (_centers select 0);
  86.         _cluster_index = 0;
  87.        
  88.         {
  89.             _dist1 = _x distance _point;
  90.             if (_dist1 <= _dist) then {_dist = _dist1; _cluster_index = _foreachindex;}
  91.         } foreach _centers;
  92.        
  93.         _cluster = _clusters select _cluster_index;
  94.         _cluster set [count _cluster, _x];
  95.         _clusters set [_cluster_index, _cluster];
  96.     } foreach _points;
  97.    
  98.     _new_centers = [];
  99.     _clusters_2 = []+_clusters;
  100. //  player sideChat str(_centers);
  101.     {
  102.         if (count _x > 0) then {
  103.             _new_centers set [count _new_centers, _x call _means];
  104.             _clusters set [count _clusters, []];
  105.         }
  106.     }foreach _clusters_2;
  107.    
  108.     if !([_new_centers, _centers] call KK_fnc_isEqual) exitwith {};//добавил
  109. };
  110. _clusters_2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement