示例#1
0
        /// <summary>
        /// Runs simulation of Dino game
        /// </summary>
        private void button5_Click(object sender, EventArgs e)
        {
            this.BackColor = Color.SandyBrown;
            int width  = 1000;
            int height = 500;

            this.Size = new Size(width, height);
            dinoGame  = new Dino.Game(width, height, 500);
            var nodes = dinoGame.CreateNodes();

            neat = new Neat(nodes, new SortedList <int, Gene>(), dinoGame.FitnessFunction, dinoGame.players);
            neat.SetParameters();
            timer4.Enabled  = true;
            button4.Visible = false;
            button4.Enabled = false;
            button5.Visible = false;
            button5.Enabled = false;
            button1.Visible = false;
            button1.Enabled = false;
            button2.Visible = false;
            button2.Enabled = false;
            elapsed         = 0;
            genCount        = 0;
            button3.Visible = false;
            button3.Enabled = false;
            label1.Visible  = true;
            label2.Visible  = true;
            timer4.Interval = timer1.Interval;
        }
示例#2
0
        /// <summary>
        /// Runs World's hardest game simulation
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            g               = CreateGraphics();
            game            = new Game("levels.txt", "tilesF.png", "square.png", "ball.png", "champ.png", 300);
            this.Size       = new Size(Game.COLUMNS * game.tileSize, Game.ROWS * game.tileSize);
            button4.Visible = false;
            button4.Enabled = false;
            button5.Visible = false;
            button5.Enabled = false;
            button1.Visible = false;
            button1.Enabled = false;
            button2.Visible = false;
            button2.Enabled = false;
            elapsed         = 0;
            genCount        = 0;
            button3.Visible = false;
            button3.Enabled = false;
            var inputNodes  = game.CreateInputNodes();
            var outputNodes = game.CreateOutputNodes();
            var nodes       = game.CreateNodes();
            var genes       = game.GetStartGenes(nodes);

            neat = new Neat(nodes, genes, new Func <Genome, float>(game.FitnessFunction), game.Players);
            neat.SetParameters(c3: 0.5f);
            timer2.Enabled = true;
            label1.Text    = "Generation: " + genCount.ToString();
            label1.Visible = true;
        }
示例#3
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();
        }