Exemple #1
0
        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 });
        }
Exemple #2
0
        /**
         * Return the internal topDownMapping matrix used for handling the
         * bucketInfo() and topDownCompute() methods. This is a matrix, one row per
         * category (bucket) where each row contains the encoded output for that
         * category.
         *
         * @param c		the connections memory
         * @return		the internal topDownMapping
         */
        public SparseObjectMatrix <int[]> GetTopDownMapping()
        {
            if (base.topDownMapping == null)
            {
                //The input scalar value corresponding to each possible output encoding
                if (IsPeriodic())
                {
                    SetTopDownValues(
                        ArrayUtils.Arrange(GetMinVal() + GetResolution() / 2.0,
                                           GetMaxVal(), GetResolution()));
                }
                else
                {
                    //Number of values is (max-min)/resolutions
                    SetTopDownValues(
                        ArrayUtils.Arrange(GetMinVal(), GetMaxVal() + GetResolution() / 2.0,
                                           GetResolution()));
                }
            }

            //Each row represents an encoded output pattern
            int numCategories = GetTopDownValues().Length;
            SparseObjectMatrix <int[]> topDownMapping;

            SetTopDownMapping(
                topDownMapping = new SparseObjectMatrix <int[]>(
                    new int[] { numCategories }));

            double[] topDownValues = GetTopDownValues();
            int[]    outputSpace   = new int[GetN()];
            double   minVal        = GetMinVal();
            double   maxVal        = GetMaxVal();

            for (int i = 0; i < numCategories; i++)
            {
                double value = topDownValues[i];
                value = Math.Max(value, minVal);
                value = Math.Min(value, maxVal);
                EncodeIntoArray(value, outputSpace);
                topDownMapping.Set(i, Arrays.CopyOf(outputSpace, outputSpace.Length));
            }

            return(topDownMapping);
        }