Ejemplo n.º 1
0
        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Interval = timer1.Interval;
            switch (game.State)
            {
            case GameState.Ingame:
                if (elapsed > timeForGeneration)
                {
                    game.State = GameState.Loss;
                }
                game.UpdateTime(elapsed, timeForGeneration);
                var simulationResult = game.SimulateStep(neat);
                game.MoveElements(simulationResult);
                game.CheckBallPlayerSCollision();
                Invalidate();

                elapsed += timer2.Interval;
                break;

            case GameState.Loss:
                genCount++;
                label1.Text = "Generation: " + genCount.ToString();
                neat.CreateNewGeneration();
                //game.RunChampionsAgain(neat.Champions);
                Invalidate();
                game.State = GameState.Ingame;

                game.ResetPositions();
                elapsed = 0;
                break;
            }
        }
Ejemplo n.º 2
0
        public void RunSimulation()
        {
            var results    = new List <int>(100);
            int generation = 0;

            for (int j = 0; j < 100; j++)
            {
                List <object> players = new List <object>(100);
                for (int i = 0; i < 150; i++)
                {
                    players.Add(new Player(i, this));
                }
                //List<Node> input;
                //SortedList<int, Gene> genes;
                Random rnd = new Random();
                //OptimalGenome(out input, out genes);
                //var inp = CreateInputNodes();
                var         g     = new SortedList <int, Gene>(3);
                List <Node> nodes = new List <Node>(4);
                nodes.Add(new Node(NodeType.Sensor, 0, 0));
                nodes.Add(new Node(NodeType.Sensor, 0, 1));
                nodes.Add(new Node(NodeType.Sensor, 0, 2));
                nodes.Add(new Node(NodeType.Output, 1, 3));
                for (int i = 0; i < 3; i++)
                {
                    var gene = new Gene(nodes[i], nodes[3], (float)rnd.NextDouble() * (rnd.Next(0, 10) >= 5 ? 1f : -1f), GeneType.Enabled, i);
                    g.Add(i, gene);
                    nodes[i].OutgoingConnections.Add(gene);
                }
                Neat neat = new Neat(nodes, g, FitnessFunction, players);
                neat.SetParameters(c3: 1);
                // Neat neat = new Neat(nodes, new SortedList<int, Gene>(), FitnessFunction, players);
                // Neat neat = new Neat(input, genes, FitnessFunction, players);
                generation = 0;
                bestGenome = null;
                while (bestGenome == null)
                {
                    first  = neat.SimulateStepFromNeat(GetParameters(0));
                    second = neat.SimulateStepFromNeat(GetParameters(1));
                    third  = neat.SimulateStepFromNeat(GetParameters(2));
                    fourth = neat.SimulateStepFromNeat(GetParameters(3));
                    neat.CreateNewGeneration();
                    generation++;
                }
                results.Add(generation);
            }
            StreamWriter sw = new StreamWriter("xor.txt");

            sw.WriteLine("XOR results, 100 runs");
            sw.WriteLine("how many generation it took for each run:");
            int max = 0;

            for (int i = 0; i < results.Count; i++)
            {
                max += results[i];
                sw.WriteLine(i + ". run -> " + results[i]);
            }
            sw.WriteLine("Average generation in a run: " + (double)max / results.Count);
            sw.Close();
        }