Example #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);
        }
Example #2
0
 public void Attack(IPrey prey)
 {
     if (_attackSpeed > prey.FleeSpeed)
     {
         Console.WriteLine("Caught prey");
     }
     else
     {
         Console.WriteLine("Prey escaped");
     }
 }
Example #3
0
        private void GuideEvolution()
        {
            // Instatiate Evolution Manager - setup Evolution.
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            string sourceStr = InputBox.Text;

            sourceStr = StringHelper.ClearRedundantChar(sourceStr);

            TypingCharacters.GetCharacterSet(sourceStr);

            this.evolutionManager = new EvolutionManager((int)this.PopSizeNumUpDown.Value, sourceStr);

            stopwatch.Stop();
            Debug.WriteLine($"Init time is : {stopwatch.Elapsed}");

            IPrey mostFit = this.evolutionManager.GetMostFit();


            while ((mostFit != null) && (mostFit.ToString() != sourceStr))
            {
                stopwatch.Reset();
                stopwatch.Start();

                int generationNo = this.evolutionManager.IterationCount;

                this.PrintToOutputConsole($"Best: {mostFit.ToString()} ({generationNo})");

                // Evolve next generation.
                mostFit = this.evolutionManager.EvolveGeneration();

                this.ScrollOutputConsoleToEnd();

                // Necessary Evil!
                Application.DoEvents();

                stopwatch.Stop();
                Debug.WriteLine($"OneTime : {stopwatch.Elapsed}");
            }

            if (mostFit != null)
            {
                // Final result.
                this.PrintToOutputConsole($"Best: {mostFit.ToString()} ({this.evolutionManager.IterationCount})");
            }
            else
            {
                // Generation Limit Reached.
                this.PrintToOutputConsole($"Generation Limit Reached: {this.evolutionManager.IterationCount}");
            }
        }
        private void _tracker_OnTracking(IPrey prey, ITracker tracker, EventArgs e)
        {
            ProcessPrey processPrey = prey as ProcessPrey;

            if (processPrey != null)
            {
                Console.WriteLine(string.Format("process name: {0}", processPrey.GetProcessName()));
                Console.WriteLine(string.Format("machine name: {0}", processPrey.GetMachineName()));
                Console.WriteLine(string.Format("total start time(min): {0}", processPrey.GetTotalStartTime().TotalMinutes));
                Console.WriteLine(string.Format("total cpu usage: {0}%", processPrey.GetUseCpu()));
                Console.WriteLine(string.Format("total physical memory usage: {0}", processPrey.GetUsePhysicalMemory()));
                Console.WriteLine("cikis icin bir tusa basiniz.");
            }
            Thread.Sleep(800);
            Console.Clear();
        }
Example #5
0
        private void SetupEvolution(int populationSize, string target)
        {
            target = StringHelper.ClearRedundantChar(target);

            this.target   = PreyFactory.CreateEvoStringPrey(target);
            this.predator = PredatorFactory.CreateEvoStringPredator(this.target as EvoString);

            // 判重字典
            TypingCharacters.DicCharacters.Clear();
            TypingCharacters.DicCharacters.Add(target, 0);

            this.population = new List <IPrey>(populationSize);

            // Let the Typing Monkey in the factory type away.
            for (int i = 0; i < populationSize; i++)
            {
                this.population.Add(PreyFactory.CreateRandomEvoStringPrey(target.Length));
            }
        }
Example #6
0
        /// <summary>
        /// Evolve Generation and return most Fit.
        /// </summary>
        /// <returns>Returns mostFit - null if MAXIMUM number of Iterations has been reached.</returns>
        public IPrey EvolveGeneration()
        {
            // 按照字符串距离进行排序
            this.population = this.population.AsParallel().OrderBy(each => this.predator.EvaluateFitness(each)).ToList();


            IPrey mostFit = null;

            if (this.generationsCount < MAX_ITER)
            {
                // 1. Retain Most Fitbased on Elitism rate - the king of the jungle will survive more generations!
                // 1. 维持最符合的种群后代
                int          eliteSize = Convert.ToInt32(this.population.Count * ELITISM_RATE);
                List <IPrey> buffer    = this.CopyEliteMembers(this.population.GetRange(0, eliteSize));

                // 2. Mate The rest.
                // 2. 对剩余的进行遗传变异
                for (int i = eliteSize; i < this.population.Count; i++)
                {
                    int activeMateIndex  = Dice.Roll(0, this.population.Count - 1);
                    int passiveMateIndex = Dice.Roll(0, this.population.Count - 1);

                    // 进行遗传变异
                    IPrey child = this.population[activeMateIndex].Mate(this.population[passiveMateIndex]);

                    buffer.Add(child);
                }

                this.population = buffer;

                this.population = this.population.AsParallel().OrderBy(each => this.predator.EvaluateFitness(each)).ToList();

                // Get Most Fit
                mostFit = this.population[0];

                //Increase Iteration Count
                this.generationsCount++;
            }

            return(mostFit);
        }
Example #7
0
 public int EvaluateFitness(IPrey prey)
 {
     return(EvaluateFitness(prey as EvoString));
 }
Example #8
0
        private void SetupEvolution(int populationSize, string target)
        {
            this.target = PreyFactory.CreateEvoStringPrey(target);
            this.predator = PredatorFactory.CreateEvoStringPredator(this.target as EvoString);

            this.population = new List<IPrey>(populationSize);

            // Let the Typing Monkey in the factory type away.
            for (int i = 0; i < populationSize; i++)
            {
                this.population.Add(PreyFactory.CreateRandomEvoStringPrey(target.Length));
            }
        }
Example #9
0
 public int EvaluateFitness(IPrey prey)
 {
     return EvaluateFitness(prey as EvoString);
 }
Example #10
0
 abstract public IPrey Mate(IPrey mate);
Example #11
0
 public double Distance(IPrey iPrey)
 {
     return(Math.Sqrt(Math.Pow(XCoord - ((Louse)iPrey).XCoord, 2) + Math.Sqrt(Math.Pow(YCoord - ((Louse)iPrey).YCoord, 2))));
 }