AddChromosome() public method

Add chromosome to the population.

The method adds specified chromosome to the current population. Manual adding of chromosome maybe useful, when it is required to add some initialized chromosomes instead of random.

Adding chromosome manually should be done very carefully, since it may break the population. The manually added chromosome must have the same type and initialization parameters as the ancestor passed to constructor.

public AddChromosome ( IChromosome chromosome ) : void
chromosome IChromosome Chromosome to add to the population.
return void
Beispiel #1
0
        public override void takeTurn()
        {
            Board.useDictionary = true;
            bool tempRecording = GameController.recording;
            GameController.recording = false;
            fitness.cards = Board.current.viewableCards.FindAll(x => x.Deck != Card.Decks.nobles);
            var ga = new Population(popSize, new BuyOrderChromosome(2*fitness.cards.Count), fitness, new RankSelection(), random);
            lastBestChromosome = ga.population[0] as BuyOrderChromosome;
            ga.CrossoverRate = 0.5;

               //     if (!predicted.Equals(Board.current.PrevMove)) RecordHistory.current.record("!!! Prediction failed.");
               //     predicted = null;
            int i = 0;
            turnTimer.Restart();
            while (fitness.timesEvaluated < evaluations)//(turnTimer.Elapsed < Board.current.notCurrentPlayer.turnTimer.Elapsed && i < 40)
            {
                ga.RunEpoch();
                ga.AddChromosome(lastBestChromosome);
                if (ga.BestChromosome.Fitness > lastBestChromosome.Fitness) lastBestChromosome = ga.BestChromosome as BuyOrderChromosome;
                if (i % 6 == 0) Board.current.ResetDictionary();
                CONSOLE.Overwrite(6, "Generations " + i + " Evaluation " + fitness.timesEvaluated);
                RecordHistory.current.plot(i + "," + ga.FitnessMax + Environment.NewLine);
                if ((GameController.turn % 3 == 0) && (i < 4)) takeSnapshot(ga);
                //setData(i);
                i++;
            }
            fitness.timesEvaluated = 0;
            GameController.recording = tempRecording;
            if (lastBestChromosome == null) throw new Exception("Null chromosome after evaluation " + i);
            lastBestChromosome.Evaluate(fitness);
            Move m = fitness.simulateMyTurn(lastBestChromosome, Board.current).PrevMove;
            RecordHistory.current.record(this + " took move " + m);

            //foreach (BuyOrderChromosome p in chromosomes)
            //{
            //    RecordHistory.writeToFile("Chromosomes.txt", string.Format("{0:0.00}",p.Fitness) + "|" + p.depth + "|" + p.Value.String() + "|" + string.Format("{0:0.00}",p.parentFitness));
            //}
            //RecordHistory.writeToFile("Chromosomes.txt", "");
            //List<BuyOrderChromosome> chromosomes = BuyOrderChromosome.diversify(ga.population);
            //CONSOLE.Overwrite(12, "XOver Impnts over gen.: " + xoverimpnts.String());
            //CONSOLE.Overwrite(13, "Mut Impnts over gen.: " + mutimpnts.String());
            //CONSOLE.Overwrite(14, "Crossover Improvements: " + BuyOrderChromosome.crossOverImprovements + " / " + BuyOrderChromosome.totalCrossOvers);
            //CONSOLE.Overwrite(15, "Mutation Improvements: " + BuyOrderChromosome.mutationImprovements + " / " + BuyOrderChromosome.totalMutations);
            //CONSOLE.Overwrite(16, "Number of Diverse Chromosomes: " + chromosomes.Count);

            //totalChromosomes += ga.population.Count;
            //diverseChromosomes += chromosomes.Count;
            //CONSOLE.Overwrite(17, "Total diverse/chromosomes: " + ((double)diverseChromosomes / totalChromosomes));
            //for (int j = 0; j < chromosomes.Count; j++)
            //{
            //    CONSOLE.Overwrite(18 + j, chromosomes[j].Value.String() + "   depth: " +chromosomes[j].depth + "    fitness: " + chromosomes[j].Fitness);
            //}
            takeAction(m);
            Board.useDictionary = false;
        }