//UPGMA (Unweighted Pair Group Method with Arithmetic Mean) static double UPGMA(cluster c1, cluster c2) { double resultsV = 0; dataRecord dr1 = null; dataRecord dr2 = null; //c1.children.Add(c1); //c2.children.Add(c2); //object monitor = new object(); /*Parallel.ForEach(c1.children.Cast<dataRecord>(), child => * { * double helpResult = 0; * for (int b = 0; b < c2.children.Count; b++) * { * dr2 = (dataRecord)c2.children[b]; * helpResult += Distance(child.data, dr2.data); * } * lock (monitor) * { * resultsV += helpResult; * } * } * );*/ for (int a = 0; a < c1.children.Count; a++) { dr1 = (dataRecord)c1.children[a]; for (int b = 0; b < c2.children.Count; b++) { dr2 = (dataRecord)c2.children[b]; //resultsV += Distance(dr1.data, dr2.data); resultsV += distances[dr1.index][dr2.index]; } } resultsV /= (c1.children.Count * c2.children.Count); //c1.children.Remove(c1); //c2.children.Remove(c2); /*double resultsV = 0; * cluster c1Help = c1; * int c1Count = 0; * int c2Count = 0; * while (c1Help != null) * { * c1Count++; * cluster c2Help = c2; * while (c2Help != null) * { * resultsV += Distance(c1Help.data, c2Help.data); * c2Help = c2Help.child; * c2Count++; * } * c1Help = c1Help.child; * } * resultsV /= (c1Count * c2Count);*/ return(resultsV); }
/// <summary> /// Incorporates all points from given cluster to this cluster /// </summary> /// <param name="p_Cluster"></param> /// <returns>true always</returns> public override bool AnnexCluster(cluster p_Cluster) { base.AnnexCluster(p_Cluster); //Unit specific UnitCluster u_Cluster = (UnitCluster)p_Cluster; ListUnits.AddRange(u_Cluster.ListUnits); if (this.NearestMonsterDistance > u_Cluster.NearestMonsterDistance) this.NearestMonsterDistance = u_Cluster.NearestMonsterDistance; Info.Merge(u_Cluster.Info); return true; }
static string ClusterToClass(cluster Cluster) { var @namespace = SyntaxFactory .NamespaceDeclaration(SyntaxFactory.ParseName(ROOT_NAMESPACE)); // Create class scaffolding var @class = SyntaxFactory .ClassDeclaration(Cluster.name.GetValidName()) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)) .AddXmlComment(Cluster.description); var attributes = Cluster.attribute; var constAttributes = attributes.Where(x => x.writable == "false"); //@class = @class // .AddMembers( // constAttributes.Select(x => // SyntaxFactory.FieldDeclaration(x.type.ParseType()))); // So funktioniert const, aber ich brauche eine methode wo ich per if mir das attribute anschauen, weil wenn string muss da was anderen in equals stehen als by byte... @class = @class .AddMembers( constAttributes.Select(x => SyntaxFactory.FieldDeclaration( SyntaxFactory.List <AttributeListSyntax>(), SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.ConstKeyword)), SyntaxFactory.VariableDeclaration( x.type.ParseType(), SyntaxFactory.SingletonSeparatedList( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier(x.name.GetValidName()), argumentList: null, initializer: SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression(x.code))))))).ToArray()); ; foreach (var attribute in Cluster.attribute) { switch (attribute.type) { default: break; } } return(@namespace .AddMembers(@class) .Format() .ToFullString()); }
private static void GenerateClusters(cluster c, int[] clustering, double distance) { if (c.distance > distance) { if (c.c1 != null) { GenerateClusters(c.c1, clustering, distance); GenerateClusters(c.c2, clustering, distance); } } else { for (int a = 0; a < c.children.Count; a++) { dataRecord dR = (dataRecord)c.children[a]; clustering[dR.index] = c.clusterId; } } }
public CrossClusterReplicationFollowTests(XPackCluster cluster, EndpointUsage usage) : base(new CoordinatedUsage(cluster, usage, Prefix) { { CreateIndexStep, u => u.Calls <CreateIndexDescriptor, CreateIndexRequest, ICreateIndexRequest, CreateIndexResponse>( v => new CreateIndexRequest(v) { Settings = new IndexSettings() { NumberOfShards = 1, NumberOfReplicas = 0, SoftDeletes = new SoftDeleteSettings { Enabled = true, Retention = new SoftDeleteRetentionSettings { Operations = 1024 } } } }, (v, d) => d.Settings(s => s .NumberOfShards(1) .NumberOfReplicas(0) .SoftDeletes(sd => sd .Enabled() .Retention(r => r.Operations(1024)) ) ), (v, c, f) => c.Indices.Create(v, f), (v, c, f) => c.Indices.CreateAsync(v, f), (v, c, r) => c.Indices.Create(r), (v, c, r) => c.Indices.CreateAsync(r) ) },
void K_Means(int K) { #region Create Output layer for (int i = 0; i < 5; i++) // Construct the output Layer { Perceptron per = new Perceptron(K); OutPut.Add(per); } #endregion #region Get_Cntres // Get Random Centers For Clusters Random rd = new Random(); for (int i = 0; i < K; i++) { cluster temp = new cluster(); temp.center = Training_data[rd.Next(0, Training_data.Count)]; Clusters.Add((temp)); sample s = new sample(); s.copysample(temp.center); old_centers.Add(s); } #endregion old_centers[0].features[0] = (float)-0.0002; // To change it for first time while (check() ) { #region updateing_old_centers for (int i = 0; i < K; i++) { Clusters[i].cluster_samples.Clear(); old_centers[i].copysample(Clusters[i].center); } #endregion #region Clustering List<double> _out = new List<double>(); for (int sampleindx = 0; sampleindx < Training_data.Count(); sampleindx ++ ) { _out.Clear(); for (int Clusterindx = 0; Clusterindx < Clusters.Count(); Clusterindx ++ ) { _out.Add(distance(Training_data[sampleindx].features, Clusters[Clusterindx].center.features)); } Clusters[get_min(_out)].cluster_samples.Add(Training_data[sampleindx]); } #endregion #region Get New Centers for (int clusterindx = 0; clusterindx < Clusters.Count(); clusterindx ++ ) { double[] x = new double[Clusters[clusterindx].center.features.Count()]; for (int sampleindx = 0 ; sampleindx < Clusters[clusterindx].cluster_samples.Count ; sampleindx++) { for (int featureindx = 0; featureindx < Clusters[clusterindx].center.features.Count(); featureindx++ ) { x[featureindx] += Clusters[clusterindx].cluster_samples[sampleindx].features[featureindx]; } } for (int featureindx = 0; featureindx < Clusters[clusterindx].center.features.Count(); featureindx++) { x[featureindx] /= Clusters[clusterindx].cluster_samples.Count(); Clusters[clusterindx].center.features[featureindx] = (float) x[featureindx]; // update new centers } } #endregion } }
public static ClasteringResults Clasterize(double[][] rawData, int numberOfClusters, double maxDistance) { ClasteringResults cr = new ClasteringResults(); distances = new double[rawData.Length][]; for (int a = 0; a < distances.Length; a++) { distances[a] = new double[rawData.Length]; for (int b = 0; b < distances[a].Length; b++) { distances[a][b] = Distance(rawData[a], rawData[b]); } } cr.clustering = new int[rawData.Length]; ArrayList allClusters = new ArrayList(); int clusterId = 1; for (int a = 0; a < rawData.Length; a++) { cluster c = new cluster(); dataRecord dr = new dataRecord(); dr.data = new double[rawData[0].Length]; for (int b = 0; b < dr.data.Length; b++) { dr.data[b] = rawData[a][b]; } dr.index = a; c.children.Add(dr); c.clusterId = clusterId; clusterId++; allClusters.Add(c); } cluster c1, c2; cluster c1ToJoin = null, c2ToJoin = null; while (allClusters.Count > 1) { c1ToJoin = null; c2ToJoin = null; double minDistance = double.MaxValue; double valueHelp = minDistance; for (int a = 0; a < allClusters.Count; a++) { c1 = (cluster)allClusters[a]; for (int b = a + 1; b < allClusters.Count; b++) { //if (a != b) { c2 = (cluster)allClusters[b]; valueHelp = UPGMA(c1, c2); if (valueHelp < minDistance) { c1ToJoin = c1; c2ToJoin = c2; minDistance = valueHelp; } } } } cluster joinCluster = new cluster(); joinCluster.c1 = c1ToJoin; joinCluster.c2 = c2ToJoin; joinCluster.distance = minDistance; joinCluster.children.AddRange(c1ToJoin.children); joinCluster.children.AddRange(c2ToJoin.children); joinCluster.clusterId = clusterId; clusterId++; //c2ToJoin.parent = c1ToJoin; //c1ToJoin.childrenHierarchy.Add(c2ToJoin); //c1ToJoin.children.Add(c2ToJoin); //c2ToJoin.parentDistance = minDistance; //c1ToJoin.children.AddRange(c2ToJoin.children); //AddAllChilderToArrayList(c1ToJoin, c2ToJoin); allClusters.Remove(c1ToJoin); allClusters.Remove(c2ToJoin); allClusters.Add(joinCluster); } cluster resultClusteringCluster = (cluster)allClusters[0]; //resultClusteringCluster.parentDistance = double.MaxValue; GenerateClusters(resultClusteringCluster, cr.clustering, 50); return(cr); }
private void kMeans(int k) { #region Create Output layer for (int i = 0; i < 5; i++) // Construct the output Layer { Perceptron per = new Perceptron(k); OutPut.Add(per); } #endregion #region Get_Cntres // Get Random Centers For Clusters Random rd = new Random(); for (int i = 0; i < k; i++) { cluster temp = new cluster(); temp.center = Training_data[rd.Next(0, Training_data.Count)]; Cluster.Add((temp)); KeyValuePair<int, float[]> ls = new KeyValuePair<int, float[]>(); sample s = new sample(); s.copysample(temp.center); old_centers.Add(s); } #endregion // old_centers[0].Value[0] = Training_data[rd.Next(0,Training_data.Count)].Value[0]; old_centers[0].features[0] = (float)-0.0002; while (chek()) { List<double> _out = new List<double>(); #region updateing_old_centers for (int i = 0; i < k; i++) { Cluster[i].cluster_samples.Clear(); old_centers[i].copysample(Cluster[i].center); } #endregion #region Clustring //Clustering // Here's a trap for (int i = 0; i < Training_data.Count; i++) // outer one { //for (int z = 0; z < Training_data[i].Count; z++) { _out.Clear(); for (int j = 0; j < k; j++) { _out.Add(distance(Training_data[i].features, Cluster[j].center.features)); } Cluster[get_min(_out)].cluster_samples.Add(Training_data[i]); } } #endregion #region get new centers for (int c = 0; c < k; c++) { double[] x = new double[128]; for (int i = 0; i < Cluster[c].cluster_samples.Count; i++) { for (int j = 0; j < 128; j++) { x[j] += Convert.ToDouble(Cluster[c].cluster_samples[i].features[j]); } } if (Cluster[c].cluster_samples.Count != 0) { for (int sd = 0; sd < 128; sd++) { x[sd] /= Cluster[c].cluster_samples.Count; } // Cluster[c].center.; for (int sd = 0; sd < 128; sd++) { Cluster[c].center.features[sd] = (float)(x[sd]); } } } #endregion } }
//用线程计算 void work1() { tempSesult = null; StListClu.Clear(); //创建存储聚类统计的数组 for (int ig = 0; ig < grade; ig++) { for (int iw = 0; iw < SlidingWindows_All.Count; iw++) { cluster c = new cluster(); c.WindowsLong = SlidingWindows_All[iw].WindowsXlength; c.grade = ig; c.PerClusContaiinBlockCount = new List <long>(); StListClu.Add(c); } } int selectIndex; long[] clusterCount = new long[grade]; double selectMinValue; double value = 0; bool tmpbool; float tmpfloat; short tmpshort; double tmpdouble; int tmpint; bool endWindows = false; tempSesult = new List <string>();//不同等级存入不同list for (int ig = 0; ig < grade; ig++) { tempSesult.Add("移动窗体的尺寸" + "\t" + "窗体总个数" + "\t" + "聚类" + "\t" + "等级\n"); } for (int ix = 0; ix < CubeInfo.Xnum; ix++) { for (int iy = 0; iy < CubeInfo.Ynum; iy++) { for (int iz = 0; iz < CubeInfo.Znum; iz++) { BlockMark[ix, iy, iz] = new int[grade];//标记交叉数组里面,存入不同等级的标记 } } } int[, , ][] arrPerBlock = new int[CubeInfo.Xnum, CubeInfo.Ynum, CubeInfo.Znum][];//初始化为0,标记当前块是否满足条件,值为所属窗体的边长 #region 初始化交叉数字 for (int ix = 0; ix < arrPerBlock.GetLength(0); ix++) { for (int iy = 0; iy < arrPerBlock.GetLength(1); iy++) { for (int iz = 0; iz < arrPerBlock.GetLength(2); iz++) { arrPerBlock[ix, iy, iz] = new int[factorCount.Length]; } } } #endregion //先获取属性信息到交叉数组 #region 取值 CacheIO.CacheAccessReader CAR = new CacheIO.CacheAccessReader(); for (int iz = 0; iz < CubeInfo.Znum; iz++) { for (int iy = 0; iy < CubeInfo.Ynum; iy++) { for (int ix = 0; ix < CubeInfo.Xnum; ix++) { for (int ifa = 0; ifa < factorCount.Length; ifa++) { #region 取值 selectIndex = seleId[ifa]; selectMinValue = factorMinValue[ifa]; switch (Cinfo.Getcolumn(selectIndex).type) { case Difftype.布尔: CAR.GetCell(ix, iy, iz, Cinfo.GetByteStartPositionWithFlag(selectIndex), out tmpbool); value = Convert.ToDouble(tmpbool); break; case Difftype.单精度: CAR.GetCell(ix, iy, iz, Cinfo.GetByteStartPositionWithFlag(selectIndex), out tmpfloat); value = tmpfloat; break; case Difftype.短整型: CAR.GetCell(ix, iy, iz, Cinfo.GetByteStartPositionWithFlag(selectIndex), out tmpshort); value = tmpshort; break; case Difftype.双精度: CAR.GetCell(ix, iy, iz, Cinfo.GetByteStartPositionWithFlag(selectIndex), out tmpdouble); value = tmpdouble; break; case Difftype.长整型: CAR.GetCell(ix, iy, iz, Cinfo.GetByteStartPositionWithFlag(selectIndex), out tmpint); value = tmpint; break; } #endregion 取值 if (value > selectMinValue) { arrPerBlock[ix, iy, iz][ifa] = 1; } } } } } CAR.Close(); # endregion