示例#1
0
        public void Epoch()
        {
            double totalFitness = 0;
            BoxChomosome populationBest = null;
            foreach (var chromosome in this.population)
            {
                this.CalculateFitness(chromosome);
                totalFitness += chromosome.Fitness;
                if (populationBest == null || populationBest.Fitness < chromosome.Fitness)
                {
                    populationBest = chromosome;
                }
            }

            var avarageFitness = totalFitness / this.population.Count;

            Console.WriteLine("Avarage fitness {0}", avarageFitness);
            Console.WriteLine("Population best fitness {0}", populationBest.Fitness);

            if (this.best == null || this.best.Fitness < populationBest.Fitness)
            {
                this.best = populationBest;
            }
            if (this.best != null)
            {
                Console.WriteLine("Best fitness {0}", this.best.Fitness);
                Console.WriteLine("------");
                var decode = this.Best.Decode();
                foreach (var point in decode)
                {
                    Console.WriteLine("X{0} Y{1}", point.X, point.Y);
                }
            }

            var oldPopulation = this.population;

            oldPopulation.Sort(this.comparison);
            this.population = new List<BoxChomosome>();
            while (this.population.Count < Population)
            {
                BoxChomosome mum = this.GetChromosome(oldPopulation, totalFitness);
                BoxChomosome dad = this.GetChromosome(oldPopulation, totalFitness);
                BoxChomosome baby1;
                BoxChomosome baby2;
                mum.Crossover(dad, out baby1, out baby2);
                this.population.Add(baby1);
                this.population.Add(baby2);
            }

            Debug.Assert(this.population.Count == Population, "Invalid population length");
        }
示例#2
0
 private int Compare(BoxChomosome x, BoxChomosome y)
 {
     return (int)(x.Fitness - y.Fitness);
 }
示例#3
0
        private void CalculateFitness(BoxChomosome chromosome)
        {
            var decode = chromosome.Decode();
            double fitness = 0;
            for (int i = 0; i < this.cargo.Count; i++)
            {
                var box = this.cargo[i];
                box.X = decode[i].X;
                box.Y = decode[i].Y;
            }

            List<Box> nonOverlaping = new List<Box>();
            foreach (var box in cargo)
            {
                if ( box.Width + box.X > this.width ||
                    box.Height + box.Y > this.length)
                {
                        fitness = 0;
                }
                else
                {
                    fitness += 10;
                }

                if (!this.Overlaps(box))
                {
                    nonOverlaping.Add(box);
                    fitness += 10;
                }
                else
                {
                    fitness = 0;
                }

            }

            if (nonOverlaping.Count > 1)
            {
                var reference = nonOverlaping[0];
                double distance = 0;
                double coordinatesSum = 0;
                for (int i = 1; i < nonOverlaping.Count; i++)
                {
                    distance = reference.Center.Distance(nonOverlaping[i].Center);
                    coordinatesSum += nonOverlaping[i].X + nonOverlaping[i].Y;
                }
                if (distance == 0)
                {
                    distance = 100;
                }
                if (coordinatesSum == 0)
                {
                    coordinatesSum = 100;
                }
                fitness += 100/distance;
                fitness += 100 / coordinatesSum;
            }
            if (fitness < 0)
            {
                fitness = 0;
            }
            chromosome.Fitness = fitness;
        }