public void DifferentDimensionException()
        {
            EuclideanDistance distance = new EuclideanDistance();
            Vector            v1       = new Vector(new double[] { 1, 2, 3 });
            Vector            v2       = new Vector(new double[] { 1, 2, 3, 4 });

            Assert.Throws <ArgumentException>(() => distance.Measure(v1, v2));
            Assert.Throws <ArgumentException>(() => distance.Measure(v2, v1));
        }
        public void Distance()
        {
            EuclideanDistance distance = new EuclideanDistance();

            Vector v1 = new Vector(new double[] { 4, 3, 2 });
            Vector v2 = new Vector(new double[] { 2, 3, 4 });

            Assert.AreEqual(Math.Sqrt(8), distance.Measure(v1, v2));
            Assert.AreEqual(distance.Measure(v2, v1), distance.Measure(v1, v2));

            Assert.AreEqual(0, distance.Measure(v1, v1));
            Assert.AreEqual(0, distance.Measure(v2, v2));
        }
        public double Calculate(IEnumerable <OptimizationSolution> solutions, Bounds[] bounds, double sampleSize = 0.1)
        {
            OptimizationSolution[] sorted = solutions.OrderByDescending(x => x.Fitness).Select(x => Helpers.Normalize(x, bounds)).ToArray();

            int numSamples = sorted.Length;
            int subsetSize = (int)(sampleSize * numSamples);

            //take subset of best entities
            OptimizationSolution[] subset = sorted.Take(subsetSize).ToArray();

            double totalDispersion  = 0;
            double subsetDispersion = 0;

            double distance = 0;

            //calculate dispersion of all points (use size - 1 to account for comparison with self)
            //must use sorted as it has been normalized!
            foreach (var sample in sorted)
            {
                distance += sorted.Sum(x => distanceMeasure.Measure(sample.Position, x.Position)) / (numSamples - 1);
            }

            totalDispersion = distance / numSamples;

            distance = 0; //reset total distance

            //calculate dispersion of best points (use size - 1 to account for comparison with self)
            foreach (var sample in subset)
            {
                distance += subset.Sum(x => distanceMeasure.Measure(sample.Position, x.Position)) / (subsetSize - 1);
            }

            subsetDispersion = distance / subsetSize;

            return(subsetDispersion - totalDispersion);//(Math.Sqrt(3.0 * 30) / 4.0 - 0.1);//
        }
        public double Calculate(IEnumerable <OptimizationSolution> solutions)
        {
            OptimizationSolution[] samples = solutions.ToArray();
            int numSamples = samples.Length;

            OptimizationSolution best = Helpers.GetBest(samples);

            List <double> fitnesses = new List <double>(numSamples);
            List <double> distances = new List <double>(numSamples);

            double fitnessSum  = 0;
            double distanceSum = 0;

            foreach (var sample in samples)
            {
                fitnessSum += sample.Fitness.Value;
                fitnesses.Add(sample.Fitness.Value);

                double distance = distanceMeasure.Measure(sample.Position, best.Position);
                distanceSum += distance;
                distances.Add(distance);
            }

            double avgFitness  = fitnessSum / numSamples;
            double avgDistance = distanceSum / numSamples;

            double numerator    = 0;
            double lDenominator = 0;
            double rDenominator = 0;

            //calculate the sums used in the final calculation
            for (int i = 0; i < numSamples; i++)
            {
                double fitnessDiff  = fitnesses[i] - avgFitness;
                double distanceDiff = distances[i] - avgDistance;
                numerator += fitnessDiff * distanceDiff;

                lDenominator += fitnessDiff * fitnessDiff;
                rDenominator += distanceDiff * distanceDiff;
            }

            return(numerator / (Math.Sqrt(lDenominator) * Math.Sqrt(rDenominator)));
        }