/// <summary> /// Draws a BSpline curve from parent branch to new branch /// </summary> /// <param name="canvas"></param> /// <param name="allBranches"></param> public void DrawOriginCurve(Canvas canvas, List <BioBranch> allBranches) { OriginCurve = new Path(); System.Windows.Point outNode = allBranches[ParentBranchIndex].Twigs[ParentTwigIndex].HistoryNodeOUT; System.Windows.Point inNode = this.Twigs[0].HistoryNodeIN; int offset = (int)((inNode.X - outNode.X) / 2); // Add a random factor to separate lines. int randomY = Friends.GetRandomInt(0, 32); System.Windows.Point P1 = new System.Windows.Point(outNode.X, outNode.Y + randomY); System.Windows.Point P2 = new System.Windows.Point(outNode.X + offset, outNode.Y + randomY); System.Windows.Point P3 = new System.Windows.Point(inNode.X - offset, inNode.Y + randomY); System.Windows.Point P4 = new System.Windows.Point(inNode.X, inNode.Y + randomY); OriginCurve.Data = Friends.MakeBezierGeometry(P1, P2, P3, P4); OriginCurve.Stroke = Brushes.LightGray; OriginCurve.StrokeThickness = 0.6; //Canvas.SetLeft(OriginCurve, 0); //Canvas.SetTop(OriginCurve, 0); Canvas.SetZIndex(OriginCurve, -1); canvas.Children.Add(OriginCurve); }
/// <summary> /// Single point crossover /// </summary> /// <param name="probability"></param> public void CrossoverPop(double probability) { if (probability > 0) { int size = chromosomes[0].GetGenes().Length; for (int i = 0; i < chromosomes.Length; i += 2) { if (Friends.GetRandomDouble() < probability) { int splice = Friends.GetRandomInt(0, size); double[] limbo1 = new double[size]; double[] limbo2 = new double[size]; chromosomes[i].GetGenes().CopyTo(limbo1, 0); chromosomes[i + 1].GetGenes().CopyTo(limbo2, 0); for (int s = splice; s < size; s++) { limbo1[s] = chromosomes[i + 1].GetGenes()[s]; limbo2[s] = chromosomes[i].GetGenes()[s]; } chromosomes[i].SetGenes(limbo1); chromosomes[i + 1].SetGenes(limbo2); } } } }
/// <summary> /// Calculate cluster centroids using k-means++ /// </summary> /// <param name="numClusters"></param> /// <returns></returns> public double[][] calcClusterCentroidsInit(int numClusters) { int numGenes = chromosomes[0].GetGenes().Length; //Initialise array double[][] centroidVectorsInit = new double[numClusters][]; List <int> centroidChromoIndexes = new List <int>(); // 1. Choose random initial centroid int rndCentroidChromo = Friends.GetRandomInt(0, chromosomes.Length); centroidChromoIndexes.Add(rndCentroidChromo); while (centroidChromoIndexes.Count < numClusters) { // 2. Calculate distance from each chromo to the nearest centroid that has already been chosen //the distance to the nearest centroid for each chromosome List <double> chromoDistances = new List <double>(); for (int i = 0; i < chromosomes.Length; i++) { //distances from one chromo to all of the already chosen centroids List <double> distances = new List <double>(); for (int j = 0; j < centroidChromoIndexes.Count; j++) { int centroidIndex = centroidChromoIndexes[j]; distances.Add(Friends.calcDistance(chromosomes[i].GetGenes(), chromosomes[centroidIndex].GetGenes())); } chromoDistances.Add(distances.Min()); //if the chromosome compares to itself and is chosen as a centroid, the distance will be zero (fine as we choose the largest distance for all chromosomes afterwards) } //3. Choose next centroid furthest away from the already selected ones int indexOfMaxDist = chromoDistances.IndexOf(chromoDistances.Max()); centroidChromoIndexes.Add(indexOfMaxDist); } //Update array for (int i = 0; i < numClusters; i++) { centroidVectorsInit[i] = new double[numGenes]; for (int j = 0; j < numGenes; j++) { centroidVectorsInit[i][j] = chromosomes[centroidChromoIndexes[i]].GetGenes()[j]; } } return(centroidVectorsInit); }
/// <summary> /// Draws a BSpline curve from parent branch to new branch /// </summary> /// <param name="canvas"></param> /// <param name="allBranches"></param> public void DrawOriginCurve(Canvas canvas, List <BioBranch> allBranches) { OriginCurve = new Path(); System.Windows.Point outNode = allBranches[ParentBranchIndex].Twigs[ParentTwigIndex].HistoryNodeOUT; System.Windows.Point inNode = this.Twigs[0].HistoryNodeIN; int offset = (int)((inNode.X - outNode.X) / 2); // Add a random factor to separate lines. int randomY = Friends.GetRandomInt(0, 32); System.Windows.Point P1 = new System.Windows.Point(outNode.X, outNode.Y + randomY); System.Windows.Point P2 = new System.Windows.Point(outNode.X + offset, outNode.Y + randomY); System.Windows.Point P3 = new System.Windows.Point(inNode.X - offset, inNode.Y + randomY); System.Windows.Point P4 = new System.Windows.Point(inNode.X, inNode.Y + randomY); OriginCurve.Data = Friends.MakeBezierGeometry(P1, P2, P3, P4); OriginCurve.Stroke = Brushes.White; OriginCurve.StrokeThickness = 0.6; //Canvas.SetLeft(OriginCurve, 0); //Canvas.SetTop(OriginCurve, 0); Canvas.SetZIndex(OriginCurve, -1); canvas.Children.Add(OriginCurve); //Add outline circle double s = 6; System.Windows.Shapes.Ellipse nodule = new System.Windows.Shapes.Ellipse(); nodule.Height = s; nodule.Width = s; nodule.Fill = Brushes.White; System.Windows.Shapes.Ellipse nodule2 = new System.Windows.Shapes.Ellipse(); nodule2.Height = s; nodule2.Width = s; nodule2.Fill = Brushes.White; Canvas.SetLeft(nodule, P1.X - s / 2); Canvas.SetTop(nodule, P1.Y - s / 2); canvas.Children.Add(nodule); Canvas.SetLeft(nodule2, P4.X - s / 2); Canvas.SetTop(nodule2, P4.Y - s / 2); canvas.Children.Add(nodule2); }