Exemple #1
0
 private void mutation()
 {
     for (int i = 0; i < population.Count; i++)
     {
         for (int r = 1; r <= MAZE_R; r++)
         {
             for (int c = 1; c <= MAZE_C; c++)
             {
                 if (random.Next(1000) < 1) // 0.1% chance to crossover
                 {
                     population[i].setBlock(r, c, !population[i].isEmptyBlock(r, c));
                 }
             }
         }
         population[i].setFitness();
         if (i == 0 || (population[i].getFitness() > best.getFitness()))
         {
             best = new BasicMaze(population[i]);
         }
         if (best.getFitness() > allBest.getFitness())
         {
             allBest = new BasicMaze(best);
         }
     }
 }
Exemple #2
0
 public BasicMaze(BasicMaze other)
 {
     R          = other.R;
     C          = other.C;
     startPoint = new Point(other.startPoint);
     endPoint   = new Point(other.endPoint);
     blocks     = new List <List <bool> >();
     for (int r = 0; r < other.blocks.Count; r++)
     {
         blocks.Add(new List <bool>());
         for (int c = 0; c < other.blocks[r].Count; c++)
         {
             blocks[r].Add(other.blocks[r][c]);
         }
     }
     availableBlocks = new List <Point>();
     for (int i = 0; i < other.availableBlocks.Count; i++)
     {
         availableBlocks.Add(other.availableBlocks[i]);
     }
     isCorner = new List <List <bool> >();
     for (int r = 0; r < other.isCorner.Count; r++)
     {
         isCorner.Add(new List <bool>());
         for (int c = 0; c < other.isCorner[r].Count; c++)
         {
             isCorner[r].Add(other.isCorner[r][c]);
         }
     }
     isDeadend = new List <List <bool> >();
     for (int r = 0; r < other.isDeadend.Count; r++)
     {
         isDeadend.Add(new List <bool>());
         for (int c = 0; c < other.isDeadend[r].Count; c++)
         {
             isDeadend[r].Add(other.isDeadend[r][c]);
         }
     }
     fitness             = other.fitness;
     cornerNumber        = other.cornerNumber;
     deadendNumber       = other.deadendNumber;
     blocksSize          = other.blocksSize;
     longestShortestPath = other.longestShortestPath;
     isAvailable         = new List <List <bool> >();
     for (int r = 0; r < other.isAvailable.Count; r++)
     {
         isAvailable.Add(new List <bool>());
         for (int c = 0; c < other.isAvailable[r].Count; c++)
         {
             isAvailable[r].Add(other.isAvailable[r][c]);
         }
     }
 }
Exemple #3
0
 private void initial()
 {
     population    = new List <BasicMaze>();
     crossoverPool = new List <BasicMaze>();
     for (int num = 0; num < NUM_OF_POPULATION; num++)
     {
         population.Add(new BasicMaze(MAZE_R, MAZE_C));
         if (num == 0 || (population[population.Count - 1].getFitness() > best.getFitness()))
         {
             best    = new BasicMaze(population[population.Count - 1]);
             allBest = new BasicMaze(best);
         }
     }
 }
Exemple #4
0
    private void crossover()
    {
        population.Clear();

        while (population.Count < NUM_OF_POPULATION)
        {
            int index1 = random.Next(crossoverPool.Count);
            int index2;
            do
            {
                index2 = random.Next(crossoverPool.Count);
            } while (index1 == index2);

            if (random.Next(100) < 80) // 80% chance to crossover
            {
                int       midR     = random.Next(MAZE_R) + 1;
                int       midC     = random.Next(MAZE_C) + 1;
                BasicMaze newMaze1 = new BasicMaze(crossoverPool[index1]);
                BasicMaze newMaze2 = new BasicMaze(crossoverPool[index2]);
                for (int r = 1; r <= MAZE_R; r++)
                {
                    for (int c = 1; c <= MAZE_C; c++)
                    {
                        if (r < midR || (r == midR && c < midC))
                        {
                            newMaze2.setBlock(r, c, crossoverPool[index1].isEmptyBlock(r, c));
                        }
                        else
                        {
                            newMaze1.setBlock(r, c, crossoverPool[index2].isEmptyBlock(r, c));
                        }
                    }
                }
                newMaze1.setFitness();
                newMaze2.setFitness();
                population.Add(new BasicMaze(newMaze1));
                population.Add(new BasicMaze(newMaze2));
            }
            else
            {
                population.Add(new BasicMaze(crossoverPool[index1]));
                population.Add(new BasicMaze(crossoverPool[index2]));
            }
        }
    }
Exemple #5
0
 private void setMazeInformation()
 {
     StartCoroutine(mazeGenerator.generateBasicMaze(value => maze = value));
 }
Exemple #6
0
 private void setMazeInformation()
 {
     GameState.difficulty = difficulty;
     maze = new BasicMaze(R, C);
     path = maze.getShortestPath(maze.getStartPoint(), maze.getEndPoint());
 }
Exemple #7
0
    public BasicMaze(BasicMaze other)
    {
        other.setFitness();
        R          = other.R;
        C          = other.C;
        startPoint = new Point(other.startPoint);
        endPoint   = new Point(other.endPoint);
        blocks     = new List <List <bool> >();
        for (int r = 0; r < other.blocks.Count; r++)
        {
            blocks.Add(new List <bool>());
            for (int c = 0; c < other.blocks[r].Count; c++)
            {
                blocks[r].Add(other.blocks[r][c]);
            }
        }
        switch (DemoState.mode)
        {
        case DemoState.SIMPLE:
            isPattern = new List <List <bool> >();
            for (int r = 0; r < other.isPattern.Count; r++)
            {
                isPattern.Add(new List <bool>());
                for (int c = 0; c < other.isPattern[r].Count; c++)
                {
                    isPattern[r].Add(other.isPattern[r][c]);
                }
            }
            fitness       = other.fitness;
            patternNumber = other.patternNumber;
            break;

        case DemoState.MAZE:
            availableBlocks = new List <Point>();
            for (int i = 0; i < other.availableBlocks.Count; i++)
            {
                availableBlocks.Add(other.availableBlocks[i]);
            }
            isCorner = new List <List <bool> >();
            for (int r = 0; r < other.isCorner.Count; r++)
            {
                isCorner.Add(new List <bool>());
                for (int c = 0; c < other.isCorner[r].Count; c++)
                {
                    isCorner[r].Add(other.isCorner[r][c]);
                }
            }
            isDeadend = new List <List <bool> >();
            for (int r = 0; r < other.isDeadend.Count; r++)
            {
                isDeadend.Add(new List <bool>());
                for (int c = 0; c < other.isDeadend[r].Count; c++)
                {
                    isDeadend[r].Add(other.isDeadend[r][c]);
                }
            }
            fitness             = other.fitness;
            cornerNumber        = other.cornerNumber;
            deadendNumber       = other.deadendNumber;
            blocksSize          = other.blocksSize;
            longestShortestPath = other.longestShortestPath;
            isAvailable         = new List <List <bool> >();
            for (int r = 0; r < other.isAvailable.Count; r++)
            {
                isAvailable.Add(new List <bool>());
                for (int c = 0; c < other.isAvailable[r].Count; c++)
                {
                    isAvailable[r].Add(other.isAvailable[r][c]);
                }
            }
            break;
        }
    }