Ejemplo n.º 1
0
        public double fitnessFromPrior(Gene guess)
        {
            var fitness = 0.0D;

            for (var i = 0; i < nGuesses; ++i)
            {
                var result          = calcFitness(guess.value, i);
                var comparisonScore = compareToPredecessor(result, i);
                fitness += comparisonScore / 4.0D;
            }

            fitness += 0.1D;

            return(fitness);
        }
Ejemplo n.º 2
0
        public List <Gene> Crossover(Gene parent2)
        {
            var p1a    = this.value.Substring(0, value.Length - crossover);
            var p1b    = this.value.Substring(value.Length - crossover, value.Length - p1a.Length);
            var p2a    = parent2.value.Substring(0, value.Length - crossover);
            var p2b    = parent2.value.Substring(value.Length - crossover, value.Length - p2a.Length);
            var child1 = new Gene(p1a + p2b);
            var child2 = new Gene(p2a + p1b);

            child1.crossover = this.crossover;
            child2.crossover = this.crossover;

            return(new List <Gene>()
            {
                child1, child2
            });
        }
Ejemplo n.º 3
0
        private List <Gene> generateTestPopulation()
        {
            List <Gene>   pop   = new List <Gene>();
            List <string> codes = new List <string>();

            for (var i = 0; i < popSize; ++i)
            {
                Gene code = null;
                while (code == null || codes.Contains(code.value))
                {
                    code = new Gene(puzzleBoard.generateCodes());
                }

                codes.Add(code.value);
                code.crossover = 2;
                code.fitness   = puzzleBoard.fitnessFromPrior(code);
                pop.Add(code);
            }

            pop.Sort((x, y) => y.fitness.CompareTo(x.fitness));
            pop = generation(pop);

            return(pop);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Set the entity to the values of the gene.
 /// </summary>
 /// <param name="gene">The gene to get values from.</param>
 public void SetNextGene(Gene gene)
 {
     jumpPower = gene.jumpPower;
     rotation  = gene.rotation;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Set a gene at specific index.
 /// </summary>
 /// <param name="index">The index we want to insert to.</param>
 /// <param name="gene">The gene we want to insert.</param>
 public void SetGene(int index, Gene gene)
 {
     genes.Insert(index, gene);
 }