Weighted k-means algorithm

Input: data set 𝒜 ⊂ (ℝ^n), data weights w, initial circle  centers Circ;
Output: partition Π={(Subscript[π, 1]),⃛,(Subscript[π, k])} of the set 𝒜 ⊂ (ℝ^n)
 (associated to each element of the cluster (Subscript[π, j])∈Π is the corresponding weight), generalized  circle centers (Subscript[Cir, 1]),⃛,(Subscript[Cir, k]), number of iterations It;
Submodules: DIRECT[], dist[],  Dmn[],  WGLS[];

(* Circ are initial approximations for generalized circles
It is used DIRECT algorithm without Compile *)
WKGC[w_,A_,Circ_,Iter_]:=Module[{It=0,m=Length[w],k=Length[Circ],Kr,PI,tab,fc,min,imin,Pod,F0,F1,p,q,r,dom,min1,fCompile,FF,EPS=0.5},
Kr=Table[0,{j,k}];
Pod=Table[{w[[i]],A[[i]]},{i,m}];
						(* Odredjivanje particije *)
PI=Table[{},{i,k}];
Do[
tab=Table[DGcir[Pod[[i,2]],Circ[[j,1]],Circ[[j,2]]],{j,k}]//N;
imin=Ordering[tab,1][[1]];
PI[[imin]]=Append[PI[[imin]],Pod[[i]]],
{i,m}];
PI=DeleteCases[PI,{}];
k=Length[PI]; 
F1=WGLS[PI,Circ]; 
                                              (* Calculation of circle-centers *)
Do[
c0=Mean[WeightedData[PI[[j,All,2]],PI[[j,All,1]]]];
Cov=Sum[PI[[j,s,1]]KroneckerProduct[(PI[[j,s,2]]-c0),(PI[[j,s,2]]-c0)],{s,Length[PI[[j]]]}]/Length[PI[[j]]];
v=Eigensystem[Cov][[2,1]];
d0=2Sum[Abs[Dot[PI[[j,s,2]]-c0,v]],{s,Length[PI[[j]]]}]/Length[PI[[j]]];
seg0={c0-.5d0 v,c0+.5d0  v};
r0=Mean[Table[dist[PI[[j,s,2]],seg0],{s,Length[PI[[j]]]}]];
(* Print[j,". appr: ",{seg0,r0}]; *)
lam=d0/4;
dom={{seg0[[1,1]]-lam,seg0[[1,1]]+lam},{seg0[[1,2]]-lam,seg0[[1,2]]+lam},{seg0[[2,1]]-lam,seg0[[2,1]]+lam},{seg0[[2,2]]-lam,seg0[[2,2]]+lam},{r0-lam,r0+lam}};(* Print[j,". dom: ",dom]; *)
distGC[xm_,ym_,xn_,yn_,r_]:=Sum[(Dmn[PI[[j,s,2]],xm,ym,xn,yn]^2-r^2)^2,{s,Length[PI[[j]]]}];
(*FCompile=Compile[{{z1,_Real},{z2,_Real},{z3,_Real},{z4,_Real},{z5,_Real}},
Evaluate[Sum[(Dmn[PI[[j,s,2]],z1,z2,z3,z4]^2-z5^2)^2,{s,Length[PI[[j]]]}]]];	
distGC[z1_,z2_,z3_,z4_,z5_]:=Apply[FCompile,{z1,z2,z3,z4,z5}];*)
(*Print[distGC[j,seg0[[1,1]],seg0[[1,2]],seg0[[2,1]],seg0[[2,2]],r0]];*)
tDIR=Timing[min=DIRECT[distGC,dom,DMin->10.^(-4),MaxIter->30, Eps->10^(-4)]];
Kr[[j]]={{{min[[1,1]],min[[1,2]]},{min[[1,3]],min[[1,4]]}},min[[1,5]]};
,{j,k}];
F0=WGLS[PI,Kr];  
While[
(* Chop[F0-F1]≠0&&It<Iter, *)
Abs[F0-F1]/F0>EPS&&It<Iter,
F0=F1;It=It+1;
PI=Table[{},{i,k}];
Do[
tab=Table[DGcir[Pod[[i,2]],Kr[[j,1]],Kr[[j,2]]],{j,k}]//N;
imin=Ordering[tab,1][[1]];
PI[[imin]]=Append[PI[[imin]],Pod[[i]]],
{i,m}];
PI=DeleteCases[PI,{}];
k=Length[PI];
Kr=Table[0,{j,k}];
Do[
c0=Mean[WeightedData[PI[[j,All,2]],PI[[j,All,1]]]];
Cov=Sum[PI[[j,s,1]]KroneckerProduct[(PI[[j,s,2]]-c0),(PI[[j,s,2]]-c0)],{s,Length[PI[[j]]]}]/Length[PI[[j]]];
v=Eigensystem[Cov][[2,1]];
d0=2Sum[Abs[Dot[PI[[j,s,2]]-c0,v]],{s,Length[PI[[j]]]}]/Length[PI[[j]]];
seg0={c0-.5d0 v,c0+.5d0  v};
r0=Mean[Table[dist[PI[[j,s,2]],seg0],{s,Length[PI[[j]]]}]];
(* Print[j,". appr: ",{seg0,r0}]; *)
lam=d0/4;
dom={{seg0[[1,1]]-lam,seg0[[1,1]]+lam},{seg0[[1,2]]-lam,seg0[[1,2]]+lam},{seg0[[2,1]]-lam,seg0[[2,1]]+lam},{seg0[[2,2]]-lam,seg0[[2,2]]+lam},{r0-lam,r0+lam}};(* Print[j,". dom: ",dom]; *)
distGC[xm_,ym_,xn_,yn_,r_]:=Sum[(Dmn[PI[[j,s,2]],xm,ym,xn,yn]^2-r^2)^2,{s,Length[PI[[j]]]}];
(*FCompile=Compile[{{z1,_Real},{z2,_Real},{z3,_Real},{z4,_Real},{z5,_Real}},
Evaluate[Sum[(Dmn[PI[[j,s,2]],z1,z2,z3,z4]^2-z5^2)^2,{s,Length[PI[[j]]]}]]];	
distGCz1_,z2_,z3_,z4_,z5_]:=Apply[FCompile,{z1,z2,z3,z4,z5}];*)
(*Print[distGC[j,seg0[[1,1]],seg0[[1,2]],seg0[[2,1]],seg0[[2,2]],r0]];*)
tDIR=Timing[min=DIRECT[distGC,dom,DMin->10.^(-4),MaxIter->15, Eps->10^(-4)]];
Kr[[j]]={{{min[[1,1]],min[[1,2]]},{min[[1,3]],min[[1,4]]}},min[[1,5]]};
(* Print[Kr[[j]]];*)
,{j,k}];
F1=WGLS[PI,Kr];  
];
{PI,Kr,F1,It}
]