Exemplo n.º 1
0
 public Gene(Gene gene)
 {
     foreach (String key in _weights.Keys)
     {
         _weights[key] = gene._weights[key];
     }
 }
Exemplo n.º 2
0
        public Gene CrossWith(Gene gene)
        {
            Dictionary<String, double> newWeights = new Dictionary<string, double>();
            foreach (String key in _weights.Keys)
            {
                newWeights[key] = _Random.Next() % 2 == 0 ? gene._weights[key] : _weights[key];
            }

            return new Gene(newWeights);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Random random = new Random();
            int populationSize = 12;
            int rounds = 3;
            Population population = (UseSeed)? new Population(populationSize, SeedGene) : new Population(populationSize);
            List<Gene> genePool;

            while (true)
            {
                genePool = new List<Gene>();

                for (int i = 0; i < rounds; i++)
                {
                    genePool = population.GetTopHalf();

                    while (genePool.Count > 0
                            && genePool.Count < populationSize)
                    {
                        Gene newGene;
                        if (random.Next() % 4 == 0)
                        {
                            newGene = new Gene();
                        }
                        else
                        {
                            Gene randomGene1 = genePool[random.Next() % genePool.Count];
                            Gene randomGene2 = genePool[random.Next() % genePool.Count];
                            newGene = randomGene1.CrossWith(randomGene2);
                        }
                        genePool.Add(newGene);
                    }

                    if (genePool.Any())
                    {
                        population = new Population(genePool);
                    }
                    else
                    {
                        population = new Population(populationSize);
                    }
                }

                Gene bestGene = population.GetBestGene();
                bestGene.Print();
                bestGene.WriteToFile(LogPath);
                population = new Population(populationSize, bestGene);
            }
        }
Exemplo n.º 4
0
        public Population( int size, Gene seedGene)
        {
            GenePool = new List<Gene>();

            for (int i = 0; i < size; i++)
            {
                if (i % 2 == 0)
                {
                    GenePool.Add(new Gene().CrossWith(seedGene));
                }
                else
                {
                    GenePool.Add(seedGene.Mutate());
                }
            }
        }
Exemplo n.º 5
0
        public Gene GetBestGene()
        {
            Gene bestGene = new Gene();
            List<Gene> VictoryGenes = new List<Gene>();

            int roundNumber = 0;
            while (GenePool.Count > 1)
            {
                Console.WriteLine("Round {0}", roundNumber++);
                Console.WriteLine("Gene Pool Size: {0}\n", GenePool.Count);

                VictoryGenes = GetTopHalf();

                if (VictoryGenes.Any())
                {
                    bestGene = VictoryGenes.First();
                }

                GenePool = VictoryGenes;
                VictoryGenes = new List<Gene>();
            }

            return bestGene;
        }
Exemplo n.º 6
0
        public bool Defeats(Gene opponent)
        {
            try
            {
                int moveCount = 0;
                CreeperColor turn = CreeperColor.Ice;
                CreeperBoard board = new CreeperBoard();
                CreeperAI.AI thisAI = new CreeperAI.AI(_weights);
                CreeperAI.AI opponentAI = new CreeperAI.AI(opponent._weights);

                while (!board.IsFinished(turn))
                {
                    moveCount++;
                    if (moveCount > 70)
                    {
                        Console.WriteLine("Move Loop");
                        return false;
                    }
                    turn = turn.Opposite();

                    if (turn == CreeperColor.Fire)
                    {
                        board.Move(thisAI.GetMove(board, turn));
                    }
                    else
                    {
                        board.Move(opponentAI.GetMove(board, turn));
                    }
                }

                return turn == CreeperColor.Fire && board.GetGameState(turn) == CreeperGameState.Complete;
            }
            catch (Exception)
            {
                Console.WriteLine("Exception");
                return false;
            }
        }