Mahalanobis fuzzy incremental algorithm

Input: data set 𝒜 ⊂ (ℝ^n), centers (Subscript[z, 1]),⃛,(Subscript[z, k]) ∈ (ℝ^n), number of iter-ations K.Each additional center is obtained solving a GOP
using Compile.In case when n>2,one has to adapt the
minimizing function;
Output: set of partitions and the values of MFXB,MFCH,MFDB,and MFHV values (if the original partition is known,one can also obtain the Rand and Jaccard indexes,confusion matrix and Hausdorff distance between the sets of original and calculated cluster centers).Graphical depictions of each
iteration are presented only for n=2;
Submodules: dM[], muGK[], MFz[], GKcmeans[], MFXB[], MFCH[], MFDB[], MFHV[], RandFrigue[], DIRECT[], Hdist[]

MFInc[A_,z_,KK_,Ind_:0]:=Module[{k,centri,m=Length[A],n=Length[A[[1]]],dom,TTdir=0,TTGK=0},
					(* Initialization *)
Scov=Table[IdentityMatrix[n],{i,Length[cen]}]; 
brk0=Length[cen];
sl2=slA;
FF={}; PART={};
centri=z;
PART={};IndFXB=IndFCH=IndFDB=IndFHV=IndRand=IndJaccard=IndH={};
				(* Iterative Procedure *)
Do[
Print["\nNa postojecih ",Length[centri]," centara dodaje se 1 novi"];
Clear[u,v,F2]; 
dom=AG;
(* Only a few iteration of DIRECT is sufficient because this is a initial approximation for WKMeans *)
					(* Compile *)
FCompile=Compile[{{u,_Real},{v,_Real}},
Evaluate[Sum[Min[Append[Table[Norm[A[[i]]-centri[[s]]]^2,{s,Length[centri]}],Norm[A[[i]]-{u,v}]^2]],{i,m}] ]];	
F2[u_,v_]:=Apply[FCompile,{u,v}];	
						(* Non Compile 
F2[u_,v_]:=Sum[Min[Append[Table[Norm[A[[i]]-centri[[s]]]^2,{s,Length[centri]}],Norm[A[[i]]-{u,v}]^2]],{i,m}];
*)
								(* DIRECT *)
minp=DIRECT[F2,dom,DMin->10.^(-3),MaxIter->10, Eps->10^(-4)]//Timing; min=minp[[2]];
cen=Append[centri,min[[1]]];
If[Ind≠0,Print[Length[cen]-1," old centers and one new optimized center: ",Table[cen[[j]],{j,Length[cen]}]]];
k=Length[cen];
			(* Call of Adaptive Mahalanobis k-means *)
t1=Timing[solGK=GKcmeans[A,cen,0]];
TTdir=TTdir+minp[[1]];TTGK=TTGK+t1[[1]];
                                            (*  *)
cenN=solGK[[1]];
UN=solGK[[2]];
ScovN=solGK[[3]];
FN=MFz[A,cenN,UN,ScovN];
If[Ind≠0,Print["New Fuzzy GKMeans refined centers: ",cenN]];
If[Ind≠0,Print["GKMeans iterations = ",solGK[[4]],"; MFz = ",FN];
Print["Time (DIRECT+GKMeans) = ",minp[[1]],"+",t1[[1]]," = ",minp[[1]]+t1[[1]]]];
					(* Indexes *)
fxb=FXB[A,solGK[[1]],solGK[[2]],solGK[[3]]];IndFXB=Append[IndFXB,fxb];
fch=FCH[A,solGK[[1]],solGK[[2]],solGK[[3]]];IndFCH=Append[IndFCH,fch];
fdb=FDB[A,solGK[[1]],solGK[[2]],solGK[[3]]];IndFDB=Append[IndFDB,fdb];
fhv=FHV[solGK[[3]]];IndFHV=Append[IndFHV,fhv];
If[Ind≠0,Print["k=",Length[cenN],"; FXB=",fxb,"; FCH=",fch,"; FDB=",fdb,"; FHV=",fhv]];
ind=RandFrigue[UN,U];
HD=Hdist[centers,cenN];
If[Ind≠0,Print["{Rand, Jaccard, Hausdorff}: ",{ind,HD}]];
IndRand=Append[IndRand,ind[[1]]];
IndJaccard=Append[IndJaccard,ind[[2]]];
IndH=Append[IndH,HD];
				  (* Figures *)  
If[n==2,k=Length[solGK[[1]]];
slcen=ListPlot[cenN,PlotMarkers->{"✶",20},PlotStyle->{Orange,PointSize[.04],Opacity[.7]}];
Clear[x,y];  (* This M-circle contains about 68% data points *)
sle=Table[ContourPlot[dM[cenN[[j]],{x,y},ScovN[[j]]]==1,
{x,AG[[1,1]],AG[[1,2]]},{y,AG[[2,1]],AG[[2,2]]},ContourStyle->{Gray}],{j,k}];
				(* Ovo je samo za crtanje sljedeceg centra *)
					(* Compile *)
Clear[u,v,F2]; 
FCompile=Compile[{{u,_Real},{v,_Real}},
Evaluate[Sum[Min[Append[Table[Norm[A[[i]]-cenN[[s]]]^2,{s,Length[cenN]}],Norm[A[[i]]-{u,v}]^2]],{i,m}] ]];	
F2[u_,v_]:=Apply[FCompile,{u,v}];
mini=DIRECT[F2,dom,DMin->10.^(-3),MaxIter->20, Eps->10^(-4)];
nextc=ListPlot[{mini[[1]]},PlotStyle->{Red,PointSize[.03],Opacity[.8]}];
								(*  *)
Print[Show[slA,sle,slcen,nextc,AspectRatio->Automatic,GridLines->Automatic,PlotRange->AG,ImageSize->Small]];
];				 
centri=solGK[[1]];     
PART=Append[PART,{cenN,UN,ScovN,FN}]; 
,{brk,KK}];
(* Graphs of Indexes *)
If[Ind≠0,Print["IndFXB: ",IndFXB,"\nIndFCH: ",IndFCH,"\nIndFDB: ",IndFDB,"\nIndFHV: ",IndFHV]];
If[Ind≠0,Print[GraphicsGrid[{{ListPlot[Table[{i+1,IndFXB[[i]]},{i,Length[IndFXB]}],Joined->True,AxesOrigin->{0,0}],ListPlot[Table[{i+1,IndFCH[[i]]},{i,Length[IndFCH]}],Joined->True,AxesOrigin->{0,0}],ListPlot[Table[{i+1,IndFDB[[i]]},{i,Length[IndFDB]}],Joined->True,AxesOrigin->{0,0}],ListPlot[Table[{i+1,IndFHV[[i]]},{i,Length[IndFHV]}],Joined->True,AxesOrigin->{0,0}]}},ImageSize->Large]]];
If[Ind≠0,Print["RandFrigue: ",IndRand,"\nJaccardFrigue: ",IndJaccard,"\nHausdorff: ",IndH]];
{PART,{IndFXB,IndFCH,IndFDB,IndFHV,IndRand,IndJaccard,IndH},{TTdir,TTGK}}
]