//Empty public Particle() { X = 0; Y = 0; Similarity = 0; StepId = -1; StepLength = 0; CampBias = 0; Offspring = 0; Weight = 0; GpWeight = 0; IsRentry = false; Id = new[] {1, 0, 0}; CellIndex = new[] {0, 0}; Father = null; Child = null; Left = null; Right = null; HistoryGridIndex = new Dictionary<int, int[]>(); HistoryPosition = new Dictionary<int, double[]>(); }
private void PruneTree() { //Set virtual pointers. Particle workPar1 = HeadPar; //Prune the upper branch. while (workPar1 != null) { PruneBranch(workPar1); workPar1 = workPar1.Right; } //find the lower level curpar. Particle tmpPar = _iniPar; while (tmpPar.Child != null) { tmpPar = tmpPar.Child; } HeadPar = tmpPar; //The head par is set here. //Connect the lower branch. workPar1 = HeadPar; int tempNum = 0; while (tempNum < (_parNum - 1)) //To be tested. { while (workPar1.Right != null) { workPar1 = workPar1.Right; tempNum++; //Record the shift. } if (workPar1.Father.Right == null) continue; Particle workPar2 = workPar1.Father.Right.Child; workPar1.Right = workPar2; workPar2.Left = workPar1; workPar1 = workPar1.Right; tempNum++; } }
private static void PruneBranch(Particle par) { //Delete the particle. //Prune the resampled branch. if (par.Offspring == 0) { par.Father.Offspring--; if (par.Right != null && par.Left != null) { if (par.Father.Child == par) { if (par.Right.Id[0] == par.Father.Id[0]) { par.Father.Child = par.Right; par.Left.Right = par.Right; par.Right.Left = par.Left; } else { par.Left.Right = par.Right; par.Right.Left = par.Left; par.Father.Child = null; } } else { par.Left.Right = par.Right; par.Right.Left = par.Left; } } else if (par.Right != null && par.Left == null) //PROBLEM HERE { if (par.Right.Id[0] == par.Father.Id[0] || par.Id[1] == 1) { par.Father.Child = par.Right; par.Right.Left = null; } else { par.Right.Left = null; par.Father.Child = null; } } else if (par.Right == null && par.Left != null) { par.Left.Right = null; } } //Recursion if (par.Father != null) { PruneBranch(par.Father); } }
//For initial public Particle(Particle father, int id, double range, double avgSl, double avgCb, double iniX, double iniY) { X = iniX; Y = iniY; StepId = 0; Weight = 0; GpWeight = 0; Similarity = 0; IsRentry = false; CellIndex = new int[2]; HistoryGridIndex = new Dictionary<int, int[]>(); HistoryPosition = new Dictionary<int, double[]>(); //Build the tree Id = new int[3]; Id[0] = id; Id[1] = 1; if (father.Child == null) { Father = father; father.Child = this; Left = null; Right = null; Child = null; } else { Father = father; Particle tmpPar = Father.Child; while (tmpPar.Right != null) { tmpPar = tmpPar.Right; } tmpPar.Right = this; Left = tmpPar; Child = null; Right = null; Id[2] = Left.Id[2] + 1; } Father.Offspring++; Offspring = 0; //Set the initial position var ran = new Random(Guid.NewGuid().GetHashCode()); double theta = ran.NextDouble()*2*Math.PI; double radius; do { radius = Math.Abs(BasicMath.GetGaussianRandom(0, 10)); //@CONTROL } while (radius > range); X += Math.Cos(theta)*radius; Y += Math.Sin(theta)*radius; HistoryPosition.Add(StepId, new[] {X, Y}); //Set the sl and cb do { //Here I set the initial step length as a little bigger value based on the experienment results. StepLength = BasicMath.GetGaussianRandom(avgSl*1.25, 5); //@CONTROL } while (StepLength < 1*avgSl || StepLength > 1.5*avgSl); //@CONTROL CampBias = (ran.NextDouble()*2 - 1)*avgCb; //@CONTROL }
/// <summary> /// Initial the particle cloud using the given parameters. /// </summary> /// <param name="iniRss"></param> /// <param name="iniX">Current position X</param> /// <param name="iniY">Current position Y</param> public void IniCloud(Dictionary<string, double> iniRss, double iniX, double iniY) { _iniX = iniX; _iniY = iniY; _iniPar = new Particle(); for (int i = 1; i <= _parNum; i++) { _og.RecordParticle(new Particle(_iniPar, i, _iniRange, _preStepLength, _preCampBias, _iniX, _iniY)); } HeadPar = _iniPar.Child; _historyRss.Add(HeadPar.StepId, iniRss); }
//For repopulate. public Particle(Particle father) { //Ini vars. StepId = father.StepId; IsRentry = false; Weight = 0; GpWeight = father.GpWeight; //@TEST Similarity = 0; CellIndex = new int[2]; HistoryGridIndex = new Dictionary<int, int[]>(); //Here the map is newly set,that is, there's only 1 gen history path saved in a particle. HistoryPosition = new Dictionary<int, double[]>(); //When we need the former path ,we look up the father particle. //Build the particle tree. Id = new int[3]; Id[0] = father.Id[0]; //Father ID Id[1] = father.Id[1] + 1; //Gen ID if (father.Child == null) { Father = father; father.Child = this; Id[2] = 1; Left = null; Right = null; Child = null; } else { Father = father; Particle tmpPar = father.Child; while (tmpPar.Right != null) { tmpPar = tmpPar.Right; } tmpPar.Right = this; Left = tmpPar; Child = null; Right = null; Id[2] = Left.Id[2] + 1; } father.Offspring++; Offspring = 0; //Set the current position. //Attention here: cause this cur-par is a newly created particle at the same position as its father's //so here we don't need to save it's current position,we record it's position (by method 'move') from its first step. X = father.X; Y = father.Y; //Reset the step length. Principle: Add a little pertubance to the parent's. var ran = new Random(Guid.NewGuid().GetHashCode()); double sldisturb = (ran.NextDouble()*0.1 - 0.05)*father.StepLength; //@CONTROL ran = new Random(Guid.NewGuid().GetHashCode()); double cbdisturb = (ran.NextDouble()*0.1 - 0.05)*father.CampBias; //@CONTROL StepLength = Father.StepLength + sldisturb; CampBias = Father.CampBias + cbdisturb; }
/// <summary> /// Record the given particle into the cell it in. /// </summary> /// <param name="par">The given particle.</param> public void RecordParticle(Particle par) { par.HistoryGridIndex.Add(par.StepId, GetCellIndex(par.X, par.Y)); par.CellIndex = par.HistoryGridIndex[par.StepId]; }