public override void pushAwayFrom(List <double> otherPositions, MersenneTwister rng) { // If min and max are the same, nothing to do if (this.max == this.min) { return; } // How many potential other positions to evaluate? int numPositions = otherPositions.Count * 4; if (numPositions == 0) { return; } // Assign a weight to each potential position based on how close it is // to other particles. stepSize = (double)(this.max - this.min) / numPositions; double[] positions = ArrayUtils.Arrange(this.min, this.max + stepSize.Value, stepSize.Value); // Get rid of duplicates. numPositions = positions.Length; double[] weights = new double[numPositions]; // Assign a weight to each potential position, based on a gaussian falloff // from each existing variable. The weight of a variable to each potential // position is given as: // e ^ -(dist^2/stepSize^2) double maxDistanceSq = -1 * Math.Pow(stepSize.Value, 2); foreach (var pos in otherPositions) { var distances = ArrayUtils.Sub(positions, pos);// pos - positions; var varWeights = distances.Select(d => Math.Exp(Math.Pow(d, 2) / maxDistanceSq)).ToArray(); //var varWeights = Math.Exp(Math.Pow(distances, 2) / maxDistanceSq); weights = ArrayUtils.Add(weights, varWeights); } // Put this particle at the position with smallest weight. int positionIdx = ArrayUtils.Argmin(weights); this._position = positions[positionIdx]; // Set its best position to this. this._bestPosition = this.getPosition(); // Give it a random direction. this._velocity *= rng.Choice(new[] { 1, -1 }); }
public override void pushAwayFrom(List <double> otherPositions, MersenneTwister rng) { // Get the count of how many in each position //positions = [this.choices.index(x) for x in otherPositions]; var positions = otherPositions.Select(x => this.choices.ToList().IndexOf(x)).ToList(); var positionCounts = new int[this.choices.Length]; // [0] * this.choices.Length; foreach (var pos in positions) { positionCounts[pos] += 1; } this._positionIdx = ArrayUtils.Argmin(positionCounts); this._bestPositionIdx = this._positionIdx; }