// ENHANCEMENT: Optimization candidate.
        /// <summary>
        /// Gets an array of all genomes ordered by their distance from their current specie.
        /// </summary>
        private TGenome[] GetGenomesByDistanceFromSpecie(IList <TGenome> genomeList, IList <Specie <TGenome> > specieList)
        {
            // Build an array of all genomes paired with their distance from their centroid.
            int genomeCount = genomeList.Count;

            GenomeDistancePair <TGenome>[] genomeDistanceArr = new GenomeDistancePair <TGenome> [genomeCount];

            Parallel.For(0, genomeCount, _parallelOptions, delegate(int i)
            {
                TGenome genome       = genomeList[i];
                double distance      = _distanceMetric.MeasureDistance(genome.Position, specieList[genome.SpecieIdx].Centroid);
                genomeDistanceArr[i] = new GenomeDistancePair <TGenome>(distance, genome);
            });

            // Sort list. Longest distance first.
            // TODO: Pass in parallel options.
            ParallelSort.QuicksortParallel(genomeDistanceArr);

            // Put the sorted genomes in an array and return it.
            TGenome[] genomeArr = new TGenome[genomeCount];
            for (int i = 0; i < genomeCount; i++)
            {
                genomeArr[i] = genomeDistanceArr[i]._genome;
            }

            return(genomeArr);
        }
Example #2
0
        // ENHANCEMENT: Optimization candidate.
        /// <summary>
        /// Gets an array of all genomes ordered by their distance from their current specie.
        /// </summary>
        private TGenome[] GetGenomesByDistanceFromSpecie(IList <TGenome> genomeList, IList <Specie <TGenome> > specieList)
        {
            // Build a list of all genomes paired with their distance from their centriod.
            int genomeCount = genomeList.Count;

            GenomeDistancePair <TGenome>[] genomeDistanceArr = new GenomeDistancePair <TGenome> [genomeCount];
            for (int i = 0; i < genomeCount; i++)
            {
                TGenome genome   = genomeList[i];
                double  distance = _distanceMetric.MeasureDistance(genome.Position, specieList[genome.SpecieIdx].Centroid);
                genomeDistanceArr[i] = new GenomeDistancePair <TGenome>(distance, genome);
            }

            // Sort list. Longest distance first.
            Array.Sort(genomeDistanceArr);

            // Put the sorted genomes in an array and return it.
            TGenome[] genomeArr = new TGenome[genomeCount];
            for (int i = 0; i < genomeCount; i++)
            {
                genomeArr[i] = genomeDistanceArr[i]._genome;
            }

            return(genomeArr);
        }
        /// <summary>
        /// Calculate the mean distance of the specified coord from all of the other coords using
        /// the provided distance metric.
        /// </summary>
        /// <param name="distanceMetric">The distance metric.</param>
        /// <param name="coordList">The list of coordinatres.</param>
        /// <param name="idx">The index of the coordinate to measure distance to.</param>
        private static double CalculateMeanDistanceFromCoords(IDistanceMetric distanceMetric, IList<CoordinateVector> coordList, int idx)
        {
            double totalDistance = 0.0;
            int count = coordList.Count;
            CoordinateVector targetCoord = coordList[idx];

            // Measure distance to all coords before the target one.
            for(int i=0; i<idx; i++) {
                totalDistance += distanceMetric.MeasureDistance(targetCoord, coordList[i]);
            }

            // Measure distance to all coords after the target one.
            for(int i=idx+1; i<count; i++) {
                totalDistance += distanceMetric.MeasureDistance(targetCoord, coordList[i]);
            }

            return totalDistance / (count-1);
        }
Example #4
0
        /// <summary>
        /// Calculate the mean distance of the specified coord from all of the
        /// other coords using the provided distance metric.
        /// </summary>
        /// <param name="distanceMetric">The distance metric.</param>
        /// <param name="coordList">The list of coordinatres.</param>
        /// <param name="idx">The index of the coordinate to measure distance to.</param>
        private static double CalculateMeanDistanceFromCoords(IDistanceMetric distanceMetric,
                                                              IList <CoordinateVector> coordList, int idx)
        {
            double           totalDistance = 0.0;
            int              count         = coordList.Count;
            CoordinateVector targetCoord   = coordList[idx];

            // Measure distance to all coords before the target one.
            for (int i = 0; i < idx; i++)
            {
                totalDistance += distanceMetric.MeasureDistance(targetCoord, coordList[i]);
            }

            // Measure distance to all coords after the target one.
            for (int i = idx + 1; i < count; i++)
            {
                totalDistance += distanceMetric.MeasureDistance(targetCoord, coordList[i]);
            }
            return(totalDistance / (count - 1));
        }
Example #5
0
 public override double CalculateDistance(Individual individual)
 {
     return(_distanceMetric.MeasureDistance(Genome.Position, ((RobotIndividual)individual).Genome.Position));
 }