Beispiel #1
0
        /// <summary>
        /// 遗传变异
        /// </summary>
        /// <param name="mate"></param>
        /// <returns></returns>
        public override IPrey Mate(IPrey mate)
        {
            string childChromosome = string.Empty;

            EvoString evoStringMate = mate as EvoString;

            if (evoStringMate == null || (this.chromosome.Length != evoStringMate.chromosome.Length))
            {
                throw new Exception("Cross-species mating is not allowed ... yet!");
            }

            // 1. Do chromosome cross-over.
            // 1. 交叉染色体

            childChromosome = GenerateChromosome(this.chromosome, evoStringMate.chromosome);

            // 2. Spawn child.
            // 2. 生成后代
            EvoString child = new EvoString(childChromosome);

            // 3. Roll dice to mutate child.
            // 3. 后代随机突变
            if (Dice.Roll(0, (int)MAX_RAND) > MAX_RAND * MUTATION_RATE)
            {
                child.Mutate();
            }

            return(child);
        }
Beispiel #2
0
        private int EvaluateFitness(EvoString preyEvoString)
        {
            if (preyEvoString == null)
            {
                throw new Exception("Predator doesn't like this prey!");
            }

            return(this.target.Compare(preyEvoString));
        }
Beispiel #3
0
 public EvoStringPredator(EvoString target)
 {
     this.target = target;
 }
Beispiel #4
0
        /// <summary>
        /// Compares EvoString genetic heritage.
        /// </summary>
        /// <param name="lookAlike"></param>
        /// <returns>Diff between chromosomes, if chromosomes are different in lenght the delta adds up.</returns>
        public int Compare(EvoString lookAlike)
        {
            //return StringDistanceCalculator.Levenshtein(this.chromosome, lookAlike.chromosome);

            return(StringDistanceCalculator.iLevenshtein(this.chromosome, lookAlike.chromosome));
        }