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(); }
public List <List <float> > SimulateStep(Neat neat) { var parameters = CountParameters(); var floatResults = neat.SimulateStepFromNeat(parameters); return(floatResults); }
private void timer4_Tick(object sender, EventArgs e) { switch (dinoGame.State) { case Dino.GameState.Ingame: dinoGame.ManageObstacles(); var results = neat.SimulateStepFromNeat(dinoGame.CountParameters()); if (dinoGame.MoveElements(results, ref elapsed, ref timeRun, ref score)) { dinoGame.State = Dino.GameState.Loss; } dinoGame.CheckCollision(true); label2.Text = "Score: " + score; elapsed += timer4.Interval; timeRun += timer4.Interval; Invalidate(); break; case Dino.GameState.Loss: score = int.MinValue; genCount++; label1.Text = "Generation: " + genCount.ToString(); neat.CreateNewGeneration(); elapsed = 0; timeRun = 0; dinoGame.ResetPositions(); dinoGame.State = Dino.GameState.Ingame; Invalidate(); break; } }