Exemplo n.º 1
0
        public Chromosome HbmoViewSelection(int generations, int spermathecaSize, float tournamentChance, float crossOverProbability, float mutationProbability, int topViewCount, int firstPoulationCount)
        {
            List <View> lattice = _view.CreateLattice();
            //Step 1:
            HbmoInitialModel hbmoInitialModel = _chromosome.InitializeHbmoModel(lattice);
            //Step 2:
            List <Chromosome> population = _chromosome.PopulateChromosome(lattice, topViewCount, firstPoulationCount);

            Random random = new Random();

            population = SetTvecForEachChromosome(lattice, population);
            //Step 3:
            Chromosome queen = population.First(p => p.Tvec == population.Min(c => c.Tvec));

            for (int i = 0; i < generations; i++)
            {
                List <Chromosome> spermatheca = new List <Chromosome>();
                //Step 4:
                while (spermatheca.Count < spermathecaSize)
                {
                    Chromosome drone = population[random.Next(population.Count)];

                    if (queen.Tvec > drone.Tvec)
                    {
                        if (spermatheca.Count >= spermathecaSize)
                        {
                            break;
                        }

                        spermatheca.Add(drone);
                        population.Remove(population.Find(p => p.Views == drone.Views));
                    }
                    else
                    {
                        double deltaTvec    = queen.Tvec - drone.Tvec;
                        double randomNumber = random.NextDouble();

                        double annealing = CalculateAnnealingFunction(deltaTvec, hbmoInitialModel.Speed);

                        if (annealing > randomNumber)
                        {
                            if (spermatheca.Count >= spermathecaSize)
                            {
                                break;
                            }

                            spermatheca.Add(drone);
                            population.Remove(population.Find(p => p.Views == drone.Views));
                        }

                        hbmoInitialModel.Speed = hbmoInitialModel.Alpha * hbmoInitialModel.Speed;

                        if (hbmoInitialModel.Speed < hbmoInitialModel.QsMin)
                        {
                            hbmoInitialModel.Speed = hbmoInitialModel.QsMax;
                        }
                    }
                }

                //step 5:
                List <Chromosome> broodViews = CrossOver(spermatheca, queen, crossOverProbability, hbmoInitialModel.BroodViews);

                //step 6:
                List <Chromosome> improvedBbroodViews = Mutation(lattice, broodViews, mutationProbability, hbmoInitialModel.BroodViews, topViewCount);

                //strp7:
                Chromosome bestBroodView = improvedBbroodViews.First(p => p.Tvec == improvedBbroodViews.Max(c => c.Tvec));

                if (bestBroodView.Tvec < queen.Tvec)
                {
                    queen = bestBroodView;
                }

                population.Clear();

                population = _chromosome.PopulateChromosome(lattice, topViewCount, firstPoulationCount);
                population = SetTvecForEachChromosome(lattice, population);
            }

            return(queen);
        }
Exemplo n.º 2
0
 public MaterializedView(View view, Chromosome chromosome)
 {
     _view       = view;
     _chromosome = chromosome;
 }