Weighted incremental algorithm

Input: data set 𝒜 ⊂ (ℝ^n), data weights w, initial center(s) cen, number of iterations K;Each additional center is obtained by solving a GOP using Compile. In case when n>2, one has to adapt the minimizing function. Test-examples 9.2–9.4 can be used;
Output: set of partitions, CH, DB, SSWC,and Dunn indexes. Each partition consists of clusters with corresponding weights associated to their elements. Graphical depictions of each iteration are presented only for n=2;
Submodules: CH[], DB[], SSWC[], Dunn[], DIRECT[], WKMeans[];

Inc[w_,A_,cen_,KK_,Ind_:0]:=Module[{k,centri,centriN,m=Length[A],n=Length[A[[1]]],dom},
AG=Table[{Min[A[[All,i]]]-.5,Max[A[[All,i]]]+.5},{i,n}];
					(* Initialization *)
centri=cen;
PART={};IndCH=IndDB=IndSSWC=IndDunn=IndH={};
				(* Iterative procedure *)
Do[
Print["\nTo existing ",Length[centri]," centers one new is added"];
	(* Compile in the case n=2 - can be extended to an arbitrary n *)
Clear[u,v];
FCompile=Compile[{{u,_Real},{v,_Real}},
Evaluate[Sum[w[[i]]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[w[[i]]Min[Append[Table[Norm[A[[i]]-centri[[s]]]^2,{s,Length[centri]}],Norm[A[[i]]-{u,v}]^2]],{i,m}];
*)
					(* Applying of the DIRECT-algorithm *)
dom=AG;
minp=DIRECT[F2,dom,DMin->10.^(-3),MaxIter->50, Eps->10^(-4)]//Timing; min=minp[[2]];(* Print["DIRECT: ",min]; *)
centriN=Append[centri,min[[1]]];
If[Ind≠0,Print["Novi optimirani centar: ",centriN]];
If[Ind≠0,slcen=ListPlot[centriN,PlotMarkers->{"✶",20},PlotStyle->{Gray,PointSize[.025],Opacity[.7]}]];
					(* Call WKMeans *)
t1=Timing[sol=WKMeans[w,A,centriN]];
k=Length[sol[[1]]]; 
If[Ind≠0,Print["Novi WKMeans profinjeni centar: ",sol[[2]]]];
If[Ind≠0,Print["WKMeans: {WF,WG}= ",{WF[sol[[1]]],WG[sol[[1]]]},"; mj: ",Table[Length[sol[[1,j]]],{j,Length[PI]}]]];
If[Ind≠0,Print["Time (DIRECT+WKM) = ",minp[[1]],"+",t1[[1]]," = ",minp[[1]]+t1[[1]]]];
vch=VCH[sol[[1]]];IndCH=Append[IndCH,vch];
vdb=VDB[sol[[1]]];IndDB=Append[IndDB,vdb];
svc=SSWC[sol[[1]]];IndSSWC=Append[IndSSWC,svc];
vd=VD[sol[[1]]];IndDunn=Append[IndDunn,vd];
If[Ind≠0,Print["k=",Length[centriN],"; CH=",vch,"; DB=",vdb,"; SSWC=",svc,"; Dunn=",vd]];
                                                                     (* FIGURES *)
If[n==2,
k=Length[sol[[1]]];
slc=ListPlot[sol[[2]],PlotMarkers->{"✶",20},PlotStyle->{Gray,PointSize[.025],Opacity[.7]}];
slPI=ListPlot[Table[sol[[1,j,All,2]],{j,k}],PlotStyle->Table[{PointSize[.01],Hue[j/k]},{j,k}]]; 
	(* This is just for drawing the next center in the case n=2
 - can be extended to an arbitrary n *)
						(* Compile *)
Clear[u,v,F2]; 
FCompile=Compile[{{u,_Real},{v,_Real}},
Evaluate[Sum[Min[Append[Table[Norm[A[[i]]-centriN[[s]]]^2,{s,Length[centriN]}],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[.04],Opacity[.8]}];
								(*  *)
sl2=Print[Show[slPI,slc,nextc,AspectRatio->Automatic,ImageSize->Small]]
];			      
centri=sol[[2]];     
PART=Append[PART,sol[[1]]]; 
,{brk,KK}];
If[Ind≠0,Print["IndCH: ",IndCH,"\nIndDB: ",IndDB,"\nIndSSWC: ",IndSSWC,"\nIndDunn: ",IndDunn]];
If[Ind≠0,Print[GraphicsGrid[{{ListPlot[Table[{i+1,IndCH[[i]]},{i,Length[IndCH]}],Joined->True,AxesOrigin->{0,1000}],ListPlot[Table[{i+1,IndDB[[i]]},{i,Length[IndDB]}],Joined->True,AxesOrigin->{0,.5}],ListPlot[Table[{i+1,IndSSWC[[i]]},{i,Length[IndSSWC]}],Joined->True,AxesOrigin->{0,.75}],ListPlot[Table[{i+1,IndDunn[[i]]},{i,Length[IndDunn]}],Joined->True,AxesOrigin->{0,0}]}},ImageSize->Large]]];
{PART,IndCH,IndDB,IndSSWC,IndDunn}
]