Ejemplo n.º 1
0
 public GameOfLife(String gameInput, GameCriteria criteria, 
                   ITranslator<GameData> translator, BoardFactory boardFactory)
 {
     this.criteria = criteria;
     var gameData = translator.Translate(gameInput);
     board = boardFactory.GetBoard(gameData);
 }
Ejemplo n.º 2
0
        private void Start(object sender, RoutedEventArgs e)
        {
            var button = (Button)sender;

            var name   = button.Content.ToString();
            var window = new BoardWindow(name);

            Game game = BoardFactory.CreateGame(window, name);

            game.Start();

            window.Show();
        }
Ejemplo n.º 3
0
        private Game CreateDescendants(int bestChromosomeNum, double crossoverProb)
        {
            while (true)
            {
                var boardA = _chromosomes[Random.Next(bestChromosomeNum)].Board;
                var boardB = _chromosomes[Random.Next(bestChromosomeNum)].Board;

                var boardChild = BoardFactory.Crossover(boardA, boardB, crossoverProb);

                var game = new Game(boardChild);
                game.Play(MaxIterationToPlay);

                if (game.Generation < MaxIterationToPlay) //the board didn't stabilize
                {
                    return(game);
                }
            }
        }
Ejemplo n.º 4
0
        public static Game Create(int rows, int columns, int minGenerationsAlive, double maxCoverage = 1)
        {
            //Console.WriteLine($"Start Create Game witch alive at minimum {minGenerationsAlive} generations");

            var random = new Random();

            while (true)
            {
                var coverage = random.NextDouble() * maxCoverage;
                var board    = BoardFactory.Create(rows, columns, coverage);
                var game     = new Game(board);
                game.Play(minGenerationsAlive);

                if (game.Generation >= minGenerationsAlive)
                {
                    return(game);
                }
            }
        }
Ejemplo n.º 5
0
        public (Game, IList <Coords>) FindNumberphile(int chromosomeNum, double mutationProb, double crossoverProb, double keepBestRation = 0.2, double newGenerationRation = 0.1, int maxIteration = 1000)
        {
            var sw = new Stopwatch();

            sw.Start();
            var coordinates = new List <Coords>();

            _chromosomes = new ConcurrentDictionary <int, Game>();
            var bestChromosomeNum = (int)(chromosomeNum * keepBestRation);
            var newChromosomeNum  = (int)(chromosomeNum * newGenerationRation);
            var round             = 0;

            //init first generation
            // var files = Directory.GetFiles(@"C:\Temp\InitData2");
            // for (var index = 0; index < files.Length && index < chromosomeNum; index++)
            // {
            //     var jsonPath = files[index];
            //     var game = Game.Load(jsonPath);
            //     _chromosomes.TryAdd(index, game);
            // }

            Parallel.For(0, chromosomeNum, index => _chromosomes.TryAdd(index, CreateGameAndRun(index)));

            foreach (var chromosome in _chromosomes)
            {
                chromosome.Value.Save();
            }

            double    previousFitness = 1;
            const int period          = 100;
            var       movingAverage   = new MovingAverage(period);

            while (round < maxIteration)
            {
                round++;

                //Rank Selection
                var games       = _chromosomes.Values.OrderBy(game => game.IsNumberphile).ThenBy(game => game.Fitness()).Reverse();
                var bestGames   = games.Take(bestChromosomeNum).ToList();
                var bestFitness = bestGames.First().Fitness();
                Console.WriteLine($"best fitness={bestFitness} for round {round}");
                coordinates.Add(new Coords(round, bestFitness));
                movingAverage.Push(bestFitness / previousFitness);

                if (round > period && bestGames.First().IsNumberphile&& movingAverage.Average < 1.01)
                {
                    break;
                }

                previousFitness = bestFitness;
                _chromosomes.Clear();
                for (var i = 0; i < bestChromosomeNum; i++)
                {
                    _chromosomes.TryAdd(i, bestGames[i]);
                }

                //create new
                Parallel.For(bestChromosomeNum, bestChromosomeNum + newChromosomeNum, index => _chromosomes.TryAdd(index, CreateGameAndRun(index)));

                //crossover
                Parallel.For(bestChromosomeNum + newChromosomeNum, chromosomeNum, index => _chromosomes.TryAdd(index, CreateDescendants(bestChromosomeNum, crossoverProb)));

                //mutation
                Parallel.For(0, chromosomeNum, index =>
                {
                    _chromosomes[index].Board = BoardFactory.Mutation(_chromosomes[index].Board, mutationProb);
                    _chromosomes[index].Play(MaxIterationToPlay);
                });
            }

            Console.WriteLine($"stopwatch={sw.Elapsed}");
            sw.Stop();
            foreach (var game in _chromosomes.Reverse().Take(10))
            {
                game.Value.Save();
            }

            SaveCoordinates(coordinates);
            //return the best game
            return(_chromosomes.Values.OrderBy(game => game.Fitness()).Last(), coordinates);
        }
Ejemplo n.º 6
0
        private static void SetUpBoard(GameOfLifeGameLogic.Game game)
        {
            List <string> boardPattern = BoardFactory.BuildBoard(GameOfLifePatterns.CellularAutomationGosperGliderGun);

            game.InitializeGame(boardPattern);
        }