Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- File: ffa_fnc_kmean_clustering2.sqf
- Author: ArseniyK
- Description:
- Feature clustering array by the method of k-means
- Функция кластеризации массива методом k-means
- Parameter(s):
- _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]...]
- _this select 1: the number of clusters (Number)
- Returns:
- 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]]...]
- */
- private ["_means","_xx","_y","_k","_points","_centers","_new_centers","_clusters","_point","_dist","_cluster_index","_dist1", "_kmeanspp"];
- KK_fnc_isEqual = {
- switch (_this select 0) do
- {
- case (_this select 1) : {true};
- default {false};
- };
- };
- _means = {
- _xx = 0;
- _y = 0;
- {
- _xx = _xx + (_x select 0);
- _y = _y + (_x select 1);
- }foreach _this;
- _xx = _xx / (count _this);
- _y = _y / (count _this);
- [floor _xx, floor _y];
- };
- _k = _this select 0;
- _points = _this select 1;
- _centers = [];
- _new_centers = [];
- _clusters = [];
- _clusters_2 = [];
- for "_i" from 0 to _k - 1 do{
- _clusters set [_i, []];
- };
- _kmeanspp = {
- private ["_sum", "_k", "_points", "_centers", "_rnd", "_dist_s"];
- _k = _this select 0;
- _points = _this select 1;
- _centers = [];
- _centers set [0, _points select (floor (random (count _points)))];
- _sum = 0;
- for "_i" from 0 to _k-2 do {
- _center = _centers select (_i);
- _sum = 0;
- {
- _dist_s = (_x distance _center)^2;
- _sum = _sum + _dist_s;
- } foreach _points;
- _rnd = (random 1)*_sum;
- _sum = 0;
- {
- _dist_s = (_x distance _center)^2;
- _sum = _sum + _dist_s;
- if (_sum > _rnd) exitWith {_centers set [_i+1, _x];};
- } foreach _points;
- };
- _centers
- };
- private ["_sum", "_k", "_points", "_centers", "_rnd", "_dist_s"];
- _k = _this select 0;
- _points = _this select 1;
- _new_centers = [_k, _points] call _kmeanspp;
- //while {([_new_centers, _centers] call BIS_fnc_areEqual)} do //убрал
- for "_i" from 0 to 1 step 0 do //добавил
- {
- _centers = []+_new_centers;
- {
- _point = _x;
- _dist = _point distance (_centers select 0);
- _cluster_index = 0;
- {
- _dist1 = _x distance _point;
- if (_dist1 <= _dist) then {_dist = _dist1; _cluster_index = _foreachindex;}
- } foreach _centers;
- _cluster = _clusters select _cluster_index;
- _cluster set [count _cluster, _x];
- _clusters set [_cluster_index, _cluster];
- } foreach _points;
- _new_centers = [];
- _clusters_2 = []+_clusters;
- // player sideChat str(_centers);
- {
- if (count _x > 0) then {
- _new_centers set [count _new_centers, _x call _means];
- _clusters set [count _clusters, []];
- }
- }foreach _clusters_2;
- if !([_new_centers, _centers] call KK_fnc_isEqual) exitwith {};//добавил
- };
- _clusters_2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement