private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 'm') { drawPie = !drawPie; } else if (e.KeyChar == '/') { if (!isMulti) { w.go(100); } else { w.goMulti(100); } timer += 100; } else if (e.KeyChar == '1') { w = SkirmishNetworkEvaluator.world1(network); timer = 0; } else if (e.KeyChar == '2') { w = SkirmishNetworkEvaluator.world2(network); timer = 0; } else if (e.KeyChar == '3') { w = SkirmishNetworkEvaluator.world3(network); timer = 0; } else if (e.KeyChar == '4') { w = SkirmishNetworkEvaluator.world4(network); timer = 0; } else if (e.KeyChar == '5') { w = SkirmishNetworkEvaluator.world5(network); timer = 0; } else if (e.KeyChar == 'q') { w = SkirmishNetworkEvaluator.pointWorldVar(network, 3 * (float)Math.PI / 8.0f); timer = 0; } else if (e.KeyChar == 'w') { w = SkirmishNetworkEvaluator.pointWorldVar(network, (float)Math.PI / 4.0f); timer = 0; } else if (e.KeyChar == 'e') { w = SkirmishNetworkEvaluator.pointWorldVar(network, (float)Math.PI / 8.0f); timer = 0; } else if (e.KeyChar == 'a') { w = SkirmishNetworkEvaluator.diamondWorldVar(network, 75); timer = 0; } else if (e.KeyChar == 's') { w = SkirmishNetworkEvaluator.diamondWorldVar(network, 100); timer = 0; } else if (e.KeyChar == 'd') { w = SkirmishNetworkEvaluator.diamondWorldVar(network, 125); timer = 0; } else if (e.KeyChar == 'z') { w = SkirmishNetworkEvaluator.squareWorldVar(network, 75); timer = 0; } else if (e.KeyChar == 'x') { w = SkirmishNetworkEvaluator.squareWorldVar(network, 100); timer = 0; } else if (e.KeyChar == 'c') { w = SkirmishNetworkEvaluator.squareWorldVar(network, 125); timer = 0; } else if (e.KeyChar == 'r') { w = SkirmishNetworkEvaluator.lWorldVar(network, 75); timer = 0; } else if (e.KeyChar == 't') { w = SkirmishNetworkEvaluator.lWorldVar(network, 100); timer = 0; } else if (e.KeyChar == 'y') { w = SkirmishNetworkEvaluator.lWorldVar(network, 125); timer = 0; } else { if (!isMulti) { w.timeStep(); } else { w.timeStepMulti(); } timer++; } Invalidate(); }
private void startEvolutionToolStripMenuItem_Click(object sender, EventArgs e) { NeatGenome seedGenome = null; if (EvoSeedFileName != null) { try { XmlDocument document = new XmlDocument(); document.Load(EvoSeedFileName); seedGenome = XmlNeatGenomeReaderStatic.Read(document); } catch (Exception ex) { System.Console.WriteLine("Problem loading genome. \n" + ex.Message); } } double maxFitness = 0; int maxGenerations = 1000; int populationSize = 150; int inputs = 4; IExperiment exp = new SkirmishExperiment(inputs, 1, EvoIsMulti, EvoShape); w = SkirmishNetworkEvaluator.lWorldVar(network, 125); timer = 0; StreamWriter SW; SW = File.CreateText(EvoOutputLogFolder + "logfile.txt"); XmlDocument doc; FileInfo oFileInfo; IdGenerator idgen; EvolutionAlgorithm ea; if (seedGenome == null) { idgen = new IdGenerator(); ea = new EvolutionAlgorithm( new Population(idgen, GenomeFactory.CreateGenomeList( exp.DefaultNeatParameters, idgen, exp.InputNeuronCount, exp.OutputNeuronCount, exp.DefaultNeatParameters.pInitialPopulationInterconnections, populationSize)), exp.PopulationEvaluator, exp.DefaultNeatParameters); } else { idgen = new IdGeneratorFactory().CreateIdGenerator(seedGenome); ea = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(seedGenome, populationSize, exp.DefaultNeatParameters, idgen)), exp.PopulationEvaluator, exp.DefaultNeatParameters); } for (int j = 0; j < maxGenerations; j++) { DateTime dt = DateTime.Now; ea.PerformOneGeneration(); if (ea.BestGenome.Fitness > maxFitness) { maxFitness = ea.BestGenome.Fitness; doc = new XmlDocument(); XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome); oFileInfo = new FileInfo(EvoOutputLogFolder + "bestGenome" + j.ToString() + ".xml"); doc.Save(oFileInfo.FullName); // This will output the substrate, uncomment if you want that /* doc = new XmlDocument(); * XmlGenomeWriterStatic.Write(doc, (NeatGenome) SkirmishNetworkEvaluator.substrate.generateMultiGenomeModulus(ea.BestGenome.Decode(null),5)); * oFileInfo = new FileInfo(folder + "bestNetwork" + j.ToString() + ".xml"); * doc.Save(oFileInfo.FullName); */ } Console.WriteLine(ea.Generation.ToString() + " " + ea.BestGenome.Fitness + " " + (DateTime.Now.Subtract(dt))); //Do any post-hoc stuff here SW.WriteLine(ea.Generation.ToString() + " " + (maxFitness).ToString()); } SW.Close(); doc = new XmlDocument(); XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn")); oFileInfo = new FileInfo(EvoOutputLogFolder + "bestGenome.xml"); doc.Save(oFileInfo.FullName); }