Esempio n. 1
0
            public void Mutate(int index, bool all = false)
            {
                int chance = FastRandom.Rnd(1);

                if (all || chance == 0)
                {
                    Genes[index].Column = FastRandom.Rnd(Width - 1);
                }
                if (all || chance == 1)
                {
                    Genes[index].Rotation = FastRandom.Rnd(3);
                }

                Fitness = int.MinValue;
            }
Esempio n. 2
0
            internal DNA Crossover(DNA partner)
            {
                var genes = new Gene[Lifespan];
                var mid   = (int)FastRandom.Rnd(Lifespan);

                for (int i = 0; i < Lifespan; i++)
                {
                    if (i > mid)
                    {
                        genes[i] = Genes[i];
                    }
                    else
                    {
                        genes[i] = partner.Genes[i];
                    }
                }

                return(new DNA(Grid, false)
                {
                    Genes = genes
                });
            }
Esempio n. 3
0
 public void Mutate()
 {
     Mutate(FastRandom.Rnd(Lifespan));
 }
Esempio n. 4
0
            internal DNA GetBest(int timelimit, int np)
            {
                for (int i = 0; i < PopulationSize; i++)
                {
                    Pop[i].Grid.ApplyEnemyNuisance(np);
                    if (round > 0)
                    {
                        Pop[i].Shift();
                    }
                }

                //Console.Error.WriteLine("Applied: {0}", np);
                CalcFitness();

                DNA best = Pop.Max().Clone();

                //for (int i = 1; i < PopulationSize; i++)
                //    if (Pop[i].Fitness > best.Fitness) best = Pop[i];

                //Console.Error.WriteLine("Best Init: {0}", best.Fitness);
                while (_stopwatch.ElapsedMilliseconds < timelimit)
                {
                    for (int i = 0; i < PopulationSize; i++)
                    {
                        var mom   = Pop[FastRandom.Rnd(PopulationSize)];
                        var dad   = Pop[FastRandom.Rnd(PopulationSize)];
                        var child = mom.Crossover(dad);
                        child.Mutate();
                        child.CalcFitness();
                        Pop[i] = child;
                        //var sb = new StringBuilder();
                        //for (int k = 0; k < Height; k++)
                        //{
                        //    for (int j = 0; j < Width; j++)
                        //    {
                        //        sb.Append(Pop[i].Grid.Field[j, k] + " ");
                        //    }

                        //    sb.AppendLine();
                        //}
                        //Console.Error.WriteLine("{0}\nCloned Score: {1}", sb.ToString(), Pop[i].Fitness);
                    }

                    //var temp = Pop.Max();
                    //if (temp.Fitness > best.Fitness)
                    //{
                    //    Console.Error.WriteLine("B: {0} C: {1}", best.Fitness, temp.Fitness);
                    //    best = temp.Clone();
                    //}
                    int bestIndex  = -1;
                    int maxFitness = best.Fitness;
                    for (int i = 0; i < PopulationSize; i++)
                    {
                        if (Pop[i].Fitness > best.Fitness)
                        {
                            //Console.Error.WriteLine("B: {0} C: {1} M:{2}", best.Fitness, Pop[i].Fitness, Pop[i].Genes[0]);
                            bestIndex  = i;
                            maxFitness = Pop[i].Fitness;
                        }
                    }
                    if (bestIndex > -1)
                    {
                        best = Pop[bestIndex].Clone();
                    }
                }

                return(best);
            }