示例#1
0
        public int Fitness(Maze maze, ChromosomeEntity ch)
        {
            bool isFound = false;

            //iterate over chromosome's movement array
            foreach (int move in ch.Chromosome)
            {
                switch (move)
                {
                case 0:
                    break;

                case 1:
                    maze.CurrentPlayer.MoveUp();
                    isFound = maze.UpdateMaze();
                    break;

                case 2:
                    maze.CurrentPlayer.MoveDown();
                    isFound = maze.UpdateMaze();

                    break;

                case 3:
                    maze.CurrentPlayer.MoveLeft();
                    isFound = maze.UpdateMaze();

                    break;

                case 4:
                    maze.CurrentPlayer.MoveRight();
                    isFound = maze.UpdateMaze();
                    break;

                default:
                    break;
                }
                if (isFound)
                {
                    break;
                }
            }
            if (!isFound)
            {
                ch.Score = AppHelper.ManDistance(maze) + maze.CurrentPlayer.Penalties * 100;
            }
            else
            {
                ch.Score = -1;
            }
            return(ch.Score);
        }
示例#2
0
        public ChromosomeEntity Mutation(ChromosomeEntity ch)
        {
            Random rnd = new Random();

            if (MutationRate > rnd.NextDouble())
            {
                //ch.Chromosome[rnd.Next(MaxMove)] = rnd.Next(1, 5); //single gene mutation
                int genescount = new Random().Next(5); //to mutate genes count
                for (int i = 0; i < genescount; i++)
                {
                    ch.Chromosome[rnd.Next(MaxMove)] = rnd.Next(1, 5); //new genes value
                }
            }
            return(ch);
        }
示例#3
0
        public void CreatePopulation()
        {
            Random rnd = new Random();

            //Generation population
            for (int pop = 0; pop < PopulationSize; pop++)
            {
                ChromosomeEntity ch = new ChromosomeEntity();
                ch.Chromosome = new int[MaxMove];
                for (int i = 0; i < MaxMove; i++)
                {
                    ch.Chromosome[i] = rnd.Next(1, 5);
                }
                Population.Add(ch);
            }
        }
示例#4
0
        public List <ChromosomeEntity> Crossover(List <ChromosomeEntity> FitPopulation, Maze defaultMaze)
        {
            Random rnd = new Random();
            List <ChromosomeEntity> newPopulation = new List <ChromosomeEntity>(); //for next generation

            for (int i = 0; i < PopulationSize; i++)
            {
                ChromosomeEntity parent  = FitPopulation[rnd.Next(FitPopulation.Count)]; //find a parent from prev generation
                ChromosomeEntity parent2 = FitPopulation[rnd.Next(FitPopulation.Count)]; //find other parent
                int crossoverIndex       = rnd.Next(MaxMove);
                ChromosomeEntity child   = new ChromosomeEntity();
                var newCh = new List <int>();
                newCh.AddRange(parent.Chromosome.Take(crossoverIndex));                                 //taking genes from parent1
                newCh.AddRange(parent2.Chromosome.Skip(crossoverIndex).Take(MaxMove - crossoverIndex)); //genes from parent2
                child.Chromosome = newCh.ToArray();
                child            = Mutation(child);                                                     //Mutation func for new child

                //to make better new generation if child has worst score keep adding parents to new generation
                int p1 = Fitness(defaultMaze.DeepCopy(), parent);
                int p2 = Fitness(defaultMaze.DeepCopy(), parent2);
                int c1 = Fitness(defaultMaze.DeepCopy(), child);
                if (c1 != -1 || p1 != -1 || p2 != -1)
                {
                    if (p1 <= c1 && p1 <= p2)
                    {
                        newPopulation.Add(parent);
                    }
                    else if (c1 < p2)
                    {
                        newPopulation.Add(child);
                    }
                    else
                    {
                        newPopulation.Add(parent2);
                    }
                }
            }
            return(newPopulation);
        }