/// <summary> /// Creates a genomeList from a random start, or using a list of seed genome IDs /// </summary> /// <param name="popSize"></param> /// <param name="parentGenomeIDs"></param> /// <returns></returns> public GenomeList createGenomeList(int popSize, AssessGenotypeFunction genoAssess, List <long> parentGenomeIDs = null) { //must return a genome list! GenomeList gl; //if we have parents, add their genomes to our starting genomelist if (parentGenomeIDs != null) { GenomeList seeds = new GenomeList(); seeds.AddRange(getGenomesFromIDs(parentGenomeIDs).Values); gl = new GenomeList(); gl.AddRange(GenomeFactory.CreateGenomeListPreserveIDs(seeds, popSize, neatParams, idgen, genoAssess)); } else { //we don't have any seeds, we need to form our own initial population gl = GenomeFactory.CreateGenomeListPreserveIDs(neatParams, idgen, cppnInputs, cppnOutputs, neatParams.pInitialPopulationInterconnections, popSize, genoAssess); } //add each genome to our list of generated genomes, yo. //gl.ForEach(genome => allGeneratedGenomes.Add(genome)); //now we are free to return the genomes return(gl); }
public void initializeEvolution(int populationSize) { logOutput = new StreamWriter(outputFolder + "logfile.txt"); IdGenerator idgen = new IdGenerator(); ea = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(experiment.DefaultNeatParameters, idgen, experiment.InputNeuronCount, experiment.OutputNeuronCount, experiment.OutputsPerPolicy, experiment.DefaultNeatParameters.pInitialPopulationInterconnections, populationSize)), experiment.PopulationEvaluator, experiment.DefaultNeatParameters); }
private void EvolutionaryThread() { m_exp = CreateExperiment(); var idgen = new IdGenerator(); m_evoAlg = new EvolutionAlgorithm( new Population(idgen, GenomeFactory.CreateGenomeList(m_exp.DefaultNeatParameters, idgen, m_exp.InputNeuronCount, m_exp.OutputNeuronCount, m_exp.DefaultNeatParameters.pInitialPopulationInterconnections, NeatExpParams.PopulationSize)), m_exp.PopulationEvaluator, m_exp.DefaultNeatParameters); while (!m_shouldQuit) { Console.WriteLine("::::: Performing one generation"); Console.WriteLine(); m_evoAlg.PerformOneGeneration(); if (NeatExpParams.SaveFitnessGrowth) { m_eaLogger.WriteLine(String.Format("{0,-10} {1,-20} {2,-20} {3,-20}", m_evoAlg.Generation, m_evoAlg.BestGenome.Fitness, m_evoAlg.Population.MeanFitness, m_evoAlg.Population.AvgComplexity)); } m_curBestGenome = m_evoAlg.BestGenome as NeatGenome; if (m_evoAlg.BestGenome.Fitness > m_overalBestFitness) { m_overalBestFitness = m_evoAlg.BestGenome.Fitness; m_overalBestGenome = m_curBestGenome; if (NeatExpParams.SaveEachGenerationChampionCPPN) { try { var doc = new XmlDocument(); XmlGenomeWriterStatic.Write(doc, (NeatGenome)m_evoAlg.BestGenome); var oFileInfo = new FileInfo(Path.Combine( NeatExpParams.EALogDir, String.Format("BestIndividual-{0}-{1}.xml", MyUnum, m_evoAlg.Generation.ToString()))); doc.Save(oFileInfo.FullName); } catch { } } } if (EAUpdate != null) { EAUpdate.Invoke(this, EventArgs.Empty); } } }
public void initializeEvolution(int populationSize, NeatGenome seedGenome) { if (seedGenome == null) { initializeEvolution(populationSize); return; } logOutput = new StreamWriter(outputFolder + "logfile.txt"); IdGenerator idgen = new IdGeneratorFactory().CreateIdGenerator(seedGenome); ea = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(seedGenome, populationSize, experiment.DefaultNeatParameters, idgen)), experiment.PopulationEvaluator, experiment.DefaultNeatParameters); }
public ElitistEvolution(int populationSize, int archiveSize, GenomeFactory factory, Environment environment, int numAttributes, TextWriter writer) { var random = new SystemRandom(42); var selection = new BinaryTournamentSelection(random); this.numAttributes = numAttributes; this.archiveSize = archiveSize; this.evolution = new Evolution(populationSize, 0.5, 0.4, factory, selection); this.environment = environment; this.ranking = new CrowdingDistanceRanking(numAttributes); this.writer = writer; }
public void initializeEvolution(int populationSize) { if (logOutput != null) { logOutput.Close(); } logOutput = new StreamWriter(outputFolder + "logfile.txt"); IdGenerator idgen = new IdGenerator(); ea = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(neatParams, idgen, cppnInputs, cppnOutputs, neatParams.pInitialPopulationInterconnections, populationSize)), populationEval, neatParams); }
void Awake() { if (instance == null) { instance = this; } else { Destroy(gameObject); } random = new System.Random(); }
//factory method public ICarBrainController CreateCarBrainController(List <decimal> genomeWeights) { Genome genome = null; if (genomeWeights == null) { genome = GenomeFactory.CreateRandom(); } genome = GenomeFactory.Create(genomeWeights); var neuralNetwork = new NeuralNetwork(); neuralNetwork.LoadGenome(genome); return(new CarBrainController(neuralNetwork)); }
public void CreateGenomes(int populationSize) { if (GenomeFactory == null) { throw new ApplicationException("Impossível criar genomas no algoritimo genético com o GenomeFactory nulo!"); } this.populationSize = populationSize; Genome[] genomes = new Genome[populationSize]; for (int i = 0; i < populationSize; i++) { gnomes.Add(GenomeFactory.CreateGenome(this)); } }
public void initializeEvolution(int populationSize, NeatGenome seedGenome) { if (seedGenome == null) { initializeEvolution(populationSize); return; } if (logOutput != null) { logOutput.Close(); } logOutput = new StreamWriter(outputFolder + "logfile.txt"); IdGenerator idgen = new IdGeneratorFactory().CreateIdGenerator(seedGenome); ea = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(seedGenome, populationSize, neatParams, idgen)), populationEval, neatParams); }
public IGenome fetchRandomNetwork() { //we create our objects, making sure any unique node or connection is saved in WIN //(in reality, after the first few individuals, there shouldn't be any new nodes or connections created from random networks) //if we have a seed, EVERYTHIGN MUST COME FROM TEH SEED if (officialSeedGenome != null) { return(GenomeFactory.CreateGenomePreserveID(officialSeedGenome, idgen)); } //we need to verify that we aren't duplicating nodes or connections while generating this object, and that at the end of this call //the idgenerator hasn't been destroyed! In the future, we'll phase out use of the idgenerator, in favor of WIN, but not yet. return(GenomeFactory.CreateGenomePreserveID(neatParams, idgen, cppnInputs, cppnOutputs, neatParams.pInitialPopulationInterconnections)); //big bada boom }
//private static Random random; public static void Main(string[] args) { Util.Initialize(args[0]); var idgen = new IdGenerator(); IExperiment experiment = new LimitExperiment(); XmlSerializer ser = new XmlSerializer(typeof(Settings)); //Settings settings = new Settings() //{ // SmallBlind = 1, // BigBlind = 2, // GamesPerIndividual = 100, // LogFile = "mutlithreaded_log.txt", // MaxHandsPerTourney = 200, // PlayersPerGame = 6, // StackSize = 124, // Threads = 4 //}; //ser.Serialize(new StreamWriter("settings.xml"), settings); Settings settings = (Settings)ser.Deserialize(new StreamReader("settings.xml")); var eval = new PokerPopulationEvaluator <SimpleLimitNeuralNetPlayer2, RingGamePlayerEvaluator>(settings); var ea = new EvolutionAlgorithm( new Population(idgen, GenomeFactory.CreateGenomeList(experiment.DefaultNeatParameters, idgen, experiment.InputNeuronCount, experiment.OutputNeuronCount, experiment.DefaultNeatParameters.pInitialPopulationInterconnections, experiment.DefaultNeatParameters.populationSize)), eval, experiment.DefaultNeatParameters); Console.WriteLine("Starting real evolution"); for (int i = 0; true; i++) { Console.WriteLine("Generation {0}", i + 1); ea.PerformOneGeneration(); Console.WriteLine("Champion Fitness={0}", ea.BestGenome.Fitness); var doc = new XmlDocument(); XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome); FileInfo oFileInfo = new FileInfo("genomes_simple\\" + "bestGenome" + i.ToString() + ".xml"); doc.Save(oFileInfo.FullName); } }
/// <summary> /// Initializes the EA with a random intial population. /// </summary> public void initializeEvolution(int populationSize) { LogOutput = Logging ? new StreamWriter(Path.Combine(OutputFolder, "log.txt")) : null; FinalPositionOutput = FinalPositionLogging ? new StreamWriter(Path.Combine(OutputFolder, "final-position.txt")) : null; ArchiveModificationOutput = FinalPositionLogging ? new StreamWriter(Path.Combine(OutputFolder, "archive-mods.txt")) : null; ComplexityOutput = new StreamWriter(Path.Combine(OutputFolder, "complexity.txt")); ComplexityOutput.WriteLine("avg,stdev,min,max"); if (FinalPositionLogging) { FinalPositionOutput.WriteLine("ID,x,y"); ArchiveModificationOutput.WriteLine("ID,action,time,x,y"); } IdGenerator idgen = new IdGenerator(); EA = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(experiment.DefaultNeatParameters, idgen, experiment.InputNeuronCount, experiment.OutputNeuronCount, experiment.DefaultNeatParameters.pInitialPopulationInterconnections, populationSize, SimExperiment.neatBrain)), experiment.PopulationEvaluator, experiment.DefaultNeatParameters); EA.outputFolder = OutputFolder; EA.neatBrain = NEATBrain; }
/// <summary> /// Create an initial random population. /// </summary> public void Reset() { // create the genome factory if (IsHyperNEAT) { CODEC = new HyperNEATCODEC(); GenomeFactory = new FactorHyperNEATGenome(); } else { CODEC = new NEATCODEC(); GenomeFactory = new FactorNEATGenome(); } // create the new genomes Species.Clear(); // reset counters GeneIdGenerate.CurrentID = 1; InnovationIDGenerate.CurrentID = 1; EncogRandom rnd = RandomNumberFactory.Factor(); // create one default species BasicSpecies defaultSpecies = new BasicSpecies(); defaultSpecies.Population = this; // create the initial population for (int i = 0; i < PopulationSize; i++) { NEATGenome genome = GenomeFactory.Factor(rnd, this, InputCount, OutputCount, InitialConnectionDensity); defaultSpecies.Add(genome); } defaultSpecies.Leader = defaultSpecies.Members[0]; Species.Add(defaultSpecies); // create initial innovations Innovations = new NEATInnovationList(this); }
// Adaptive GAs // In CAGA (clustering-based adaptive genetic algorithm) void Start() { settings = GetComponent <GASettings>(); populationSize = settings.PopulationSize; nrGenerations = settings.NumberOfGenerations; blocking = settings.RunAsFastAsPossible; environment.Settings = settings; selectionStrategy.Settings = settings; matingStrategy.Settings = settings; mutationStrategy.Settings = settings; population = new Population(populationSize); selectionBuffer = new SelectionBuffer(populationSize); statistics = GetComponent <Statistics>(); if (statistics == null) { statistics = gameObject.AddComponent <Statistics>(); } statistics.Population = population; BaseGenome genomeTemplate = GenomeFactory.CreateGenome(genomeType, genomeSize); for (int i = 0; i < populationSize; i++) { BaseGenome genome = genomeTemplate.CreateRandom(); // Should be possible to generate these by hand (seeded) population[i] = new PhenomeDescription(genome); } for (int i = 0; i < populationSize; i++) { BaseGenome genome = genomeTemplate.CreateRandom(); // Should be possible to generate these by hand (seeded) selectionBuffer[i] = genome; } nextStepDelegate = NextStep; currentState = State.FitnessTest; environment.Inititalize(population); }
/// <summary> /// Initializes the EA with an initial population generated from a single seed genome. /// </summary> public void initializeEvolution(int populationSize, NeatGenome seedGenome) { if (seedGenome == null) { initializeEvolution(populationSize); return; } LogOutput = Logging ? new StreamWriter(Path.Combine(OutputFolder, "log.txt")) : null; FinalPositionOutput = FinalPositionLogging ? new StreamWriter(Path.Combine(OutputFolder, "final-position.txt")) : null; ArchiveModificationOutput = FinalPositionLogging ? new StreamWriter(Path.Combine(OutputFolder, "archive-mods.txt")) : null; ComplexityOutput = new StreamWriter(Path.Combine(OutputFolder, "complexity.txt")); ComplexityOutput.WriteLine("avg,stdev,min,max"); if (FinalPositionLogging) { FinalPositionOutput.WriteLine("x,y"); ArchiveModificationOutput.WriteLine("ID,action,time,x,y"); } IdGenerator idgen = new IdGeneratorFactory().CreateIdGenerator(seedGenome); EA = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(seedGenome, populationSize, experiment.DefaultNeatParameters, idgen)), experiment.PopulationEvaluator, experiment.DefaultNeatParameters); EA.outputFolder = OutputFolder; EA.neatBrain = NEATBrain; }
public AllOnesPhenome() { Genome = GenomeFactory.CreateGenome(GenomeType.RawBinaryString64, 64); }
private static void PredatorCCEAHNMainThread(object oAgentId) { int agentId = (int)oAgentId; double maxFitness = 0; int maxGenerations = Program.MaxGenerations; int populationSize = Program.PopulationSize; IExperiment exp = new SkirmishCCEAExperiment(agentId, Program.PopulationSize); StreamWriter sw = File.CreateText(Path.Combine(Program.LogFolder, String.Format("{0}-logfile-{1}.log", Program.LogFolder, agentId))); sw.AutoFlush = true; XmlDocument doc; FileInfo oFileInfo; IdGenerator idgen = new IdGenerator(); EvolutionAlgorithm ea = new EvolutionAlgorithm( new Population(idgen, GenomeFactory.CreateGenomeList(exp.DefaultNeatParameters, idgen, exp.InputNeuronCount, exp.OutputNeuronCount, exp.DefaultNeatParameters.pInitialPopulationInterconnections, populationSize)), 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(Path.Combine(LogFolder, String.Format("BestGenome-{0}-{1}.xml", agentId, j))); 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("[{0}] {1} {2} {3}", agentId, ea.Generation, ea.BestGenome.Fitness, (DateTime.Now.Subtract(dt))); //Do any post-hoc stuff here sw.WriteLine("{0} {1} {2}", ea.Generation, maxFitness, ea.Population.MeanFitness); sw.Flush(); } sw.Close(); //doc = new XmlDocument(); //XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn")); //oFileInfo = new FileInfo(Path.Combine(LogFolder, "BestGenome.xml")); //doc.Save(oFileInfo.FullName); Environment.Exit(0); }
void buildBodyExamples() { //we need to create random genomes, then save their generated bodies! NeatParameters np = new NeatParameters(); IdGenerator idGen = new IdGenerator(); idGen.ResetNextInnovationNumber(); Random r = new Random(); JObject root = new JObject(); JObject meta = new JObject(); JArray genomeArray = new JArray(); //how many random input tests? int genomeCount = 20; meta.Add("genomeCount", genomeCount); meta.Add("weightRange", HyperNEATParameters.weightRange); NeatGenome seed = EvolutionManager.SharedEvolutionManager.getSeed(); int tEmpty = 0; int emptyCount = genomeCount / 4; for (int n = genomeArray.Count; n < genomeCount; n = genomeArray.Count) { //create our genome var inputCount = r.Next(4) + 3; var outputCount = r.Next(3) + 1; //radnom inputs 3-6, random outputs 1-3 var genome = GenomeFactory.CreateGenomePreserveID(seed, idGen);//np, idGen, inputCount, outputCount, 1); Hashtable nodeHT = new Hashtable(); Hashtable connHT = new Hashtable(); //mutate our genome for (int m = 0; m < 20; m++) { ((NeatGenome)genome).Mutate(np, idGen, nodeHT, connHT); } //now turn genome into a network var network = genome.Decode(null); //turn network into JSON, and save as the network object //genomeJSON.Add("network", JObject.FromObject(network)); //now we need a body bool isEmptyBody; //convert to body object var bodyObject = simpleCom.simpleExperiment.genomeIntoBodyObject(genome, out isEmptyBody); if ((isEmptyBody && tEmpty++ < emptyCount) || (!isEmptyBody)) { //create object and add body info to it, then save it in our array JObject genomeJSON = new JObject(); genomeJSON.Add("genome", JObject.FromObject(genome, new JsonSerializer() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })); genomeJSON.Add("network", JObject.FromObject(network, new JsonSerializer() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })); //save our body object from test genomeJSON.Add("body", JObject.FromObject(bodyObject)); genomeJSON.Add("isEmpty", isEmptyBody.ToString()); //finally, we add our network json to the body array genomeArray.Add(genomeJSON); } } //add our networks, and add our meta information root.Add("genomeCount", genomeArray.Count); root.Add("genomes", genomeArray); root.Add("meta", meta); //and away we go! Let's save to file! using (System.IO.StreamWriter file = new System.IO.StreamWriter("testgenomebodies.json")) { file.WriteLine(root.ToString()); } }
public static void ThreeDHyperNEAT() { if (!cmdParser.IsArgumentProvided("-shape", out Program.Shape)) { Program.Shape = "triangle"; } if (!cmdParser.IsArgumentProvided("-folder", out Program.LogFolder)) { Program.LogFolder = "TestLogs"; } if (!Directory.Exists(Program.LogFolder)) { Directory.CreateDirectory(Program.LogFolder); } if (!cmdParser.IsIntArgumentProvided("-gens", out Program.MaxGenerations)) { Program.MaxGenerations = 1000; } double maxFitness = 0; int maxGenerations = Program.MaxGenerations; int populationSize = Program.PopulationSize; IExperiment exp = new Skirmish3DExperiment(5, Program.Shape, Program.PopulationSize); StreamWriter sw = File.CreateText(Path.Combine(Program.LogFolder, String.Format("{0}-logfile.log", Program.LogFolder))); sw.AutoFlush = true; XmlDocument doc; FileInfo oFileInfo; IdGenerator idgen; EvolutionAlgorithm ea; 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); 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(Path.Combine(Program.LogFolder, "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("{0} {1} {2}", ea.Generation, maxFitness, ea.Population.MeanFitness); sw.Flush(); } sw.Close(); doc = new XmlDocument(); XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn")); oFileInfo = new FileInfo(Path.Combine(Program.LogFolder, "BestGenome.xml")); doc.Save(oFileInfo.FullName); }
void generateSampleCPPNs() { NeatParameters np = new NeatParameters(); IdGenerator idGen = new IdGenerator(); idGen.ResetNextInnovationNumber(); Random r = new Random(); JObject root = new JObject(); JObject meta = new JObject(); JArray networkArray = new JArray(); //how many random input tests? int testCount = 100; int networkCount = 20; meta.Add("networkCount", networkCount); meta.Add("sampleCount", testCount); Console.WriteLine("All Networks start will run:" + networkCount * testCount); for (int n = 0; n < networkCount; n++) { Console.WriteLine("Network start:" + n); JObject networkJSON = new JObject(); //create our genome var inputCount = r.Next(4) + 3; var outputCount = r.Next(3) + 1; //radnom inputs 3-6, random outputs 1-3 var genome = GenomeFactory.CreateGenome(np, idGen, inputCount, outputCount, 1); Console.WriteLine("Genoem created:" + n); Hashtable nodeHT = new Hashtable(); Hashtable connHT = new Hashtable(); //mutate our genome for (int m = 0; m < 20; m++) { ((NeatGenome)genome).Mutate(np, idGen, nodeHT, connHT); Console.WriteLine("Mutation done: " + m); } Console.WriteLine("Mutations done:" + n); //now turn genome into a network var network = genome.Decode(null); Console.WriteLine("genome decoded:" + n); //turn network into JSON, and save as the network object networkJSON.Add("network", JObject.FromObject(network)); JArray inputsAndOutputs = new JArray(); Console.WriteLine("starting tests:" + n); for (var t = 0; t < testCount; t++) { Console.WriteLine("Test " + t + " :" + "for" + n); JArray inputSamples = new JArray(); JArray outputSamples = new JArray(); network.ClearSignals(); Console.WriteLine("Testclear " + t + " :" + "for" + n); //var saveInputs = new float[inputCount]; for (int ins = 0; ins < inputCount; ins++) { //inputs from -1,1 var inF = (float)(2 * r.NextDouble() - 1); //saveInputs[ins] = inF; network.SetInputSignal(ins, inF); //add our random input inputSamples.Add(JToken.FromObject(inF)); } Console.WriteLine("Testrecursive next" + t + " :" + "for" + n); //run our network, and save the response ((ModularNetwork)network).RecursiveActivation(); //network.MultipleSteps(30); Console.WriteLine("recursive done " + t + " :" + "for" + n); //var saveOuts = new float[outputCount]; for (var outs = 0; outs < outputCount; outs++) { //saveOuts[outs] = network.GetOutputSignal(outs); //keep our outputs in an output array outputSamples.Add(JToken.FromObject(network.GetOutputSignal(outs))); } //network.ClearSignals(); //network.SetInputSignals(saveInputs); //network.MultipleSteps(30); ////((ModularNetwork)network).RecursiveActivation(); //for (var outs = 0; outs < outputCount; outs++) //{ // Console.WriteLine("Difference in activation: " + Math.Abs(network.GetOutputSignal(outs) - saveOuts[outs])); //} Console.WriteLine("test reached past outputs " + t + " :" + "for" + n); JObject test = new JObject(); test.Add("inputs", inputSamples); test.Add("outputs", outputSamples); inputsAndOutputs.Add(test); Console.WriteLine("Ins/outs done " + t + " :" + "for" + n); } Console.WriteLine("tests ended:" + n); //we add our inputs/outs for this json network networkJSON.Add("tests", inputsAndOutputs); //finally, we add our network json to the network array networkArray.Add(networkJSON); Console.WriteLine("Network finished:" + n); } Console.WriteLine("All newtorks finished, cleaning up"); //add our networks, and add our meta information root.Add("networks", networkArray); root.Add("meta", meta); //and away we go! Let's save to file! using (System.IO.StreamWriter file = new System.IO.StreamWriter("testgenomes.json")) { file.WriteLine(root.ToString()); } }
static void Main(string[] args) { NeatGenome seedGenome = null; string filename = @"seedGenome.xml"; try { XmlDocument document = new XmlDocument(); document.Load(filename); seedGenome = XmlNeatGenomeReaderStatic.Read(document); } catch (Exception e) { System.Console.WriteLine("Problem loading genome. \n" + e.Message); } string experimentName = "_2Pref"; for (int k = 3; k < 10; k++) { double maxFitness = 0; int maxGenerations = 400; //150; int populationSize = 300; //100;//150; Thread extraWindowThread; extraWindowThread = new System.Threading.Thread(delegate() { var myForm = new SharpNeatExperiments.Pacman.MyForm1(); System.Windows.Forms.Application.Run(myForm); }); extraWindowThread.Start(); IExperiment exp = null; //exp = new SUPGONLYExperiment(4,12,12,5,2); //exp = new PacmanExperimentSUPG(4, 12, 12, 5, 2); //exp = new SPMMExperiment(4, 12, 12, 5, 2); //exp = new SPCExperiment(4, 12, 12, 5, 2); exp = new SPSUPGExperiment(4, 12, 12, 5, 6); StreamWriter SW; SW = File.CreateText("logfile.txt"); //Change this line for different experiments 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, exp.DefaultNeatParameters.populationSize, exp.DefaultNeatParameters, idgen)), exp.PopulationEvaluator, exp.DefaultNeatParameters); } bool isNSGAiiEnabled = false; for (int j = 0; j < maxGenerations; j++) { DateTime dt = DateTime.Now; if (isNSGAiiEnabled) { ea.PerformOneGenerationNSGAii(); } else { ea.PerformOneGeneration(); } if (ea.BestGenome.Fitness > maxFitness) { maxFitness = ea.BestGenome.Fitness; Console.WriteLine(maxFitness + "maxFitness"); Console.WriteLine("objectiveFitness" + ea.BestGenome.MultiObjectiveFitness[0] + " " + ea.BestGenome.MultiObjectiveFitness[1]); doc = new XmlDocument(); XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome); oFileInfo = new FileInfo("bestGenome" + j.ToString() + ".xml"); doc.Save(oFileInfo.FullName); //MadsXMLWriter(j, maxFitness); } MadsXMLWriter(j, maxFitness, k, experimentName); Console.WriteLine(ea.Generation.ToString() + " " + (maxFitness).ToString() + " " + (DateTime.Now.Subtract(dt))); //Do any post-hoc stuff here SW.WriteLine(ea.Generation.ToString() + " " + (maxFitness).ToString()); } SW.Close(); //----- Write the genome to an XmlDocument. doc = new XmlDocument(); XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn")); oFileInfo = new FileInfo("bestGenome" + experimentName + "_" + k + ".xml"); doc.Save(oFileInfo.FullName); } }
public Evolution(int populationSize, double crossoverRate, double mutationRate, GenomeFactory factory, Selection selection) { this.populationSize = populationSize; this.crossoverQuota = (int)(populationSize * crossoverRate); this.mutationQuota = (int)(populationSize * mutationRate); this.factory = factory; this.selection = selection; }
public CarPhenome() { Genome = GenomeFactory.CreateGenome(GenomeType.FloatString, (5 * 8 + 8) + (8 * 3 + 3)); }
public CardPhenome() { Genome = GenomeFactory.CreateGenome(GenomeType.RawBinaryString32, 10); }
public GeneticAlgorithmSettings(GenomeFactory genomeFactory) { mGenomeFactory = genomeFactory; }
public void run(int type, int evaluationMethod, System.Threading.CancellationToken token) { double maxFitness = 0; int maxGenerations = 800; int populationSize = 300; IExperiment exp; if (evaluationMethod == 1) { novelty = true; } if (type == 0) { exp = new CTRNNExperiment(4, 12, 8, 8, 4); } else if (type == 1) { exp = new SUPGExperiment(4, 12, 12, 3, 2); } else { doClune = true; exp = new CluneExperiment(20, 20, 20, 6, 1); } XmlDocument doc; FileInfo oFileInfo; IdGenerator idgen; EvolutionAlgorithm ea; NeatGenome seedGenome = null; 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); } Directory.CreateDirectory(folder); using (var logWriter = File.CreateText(folder + "Log " + DateTime.Now.ToString("u").Replace(':', '.') + ".txt")) for (int j = 0; j < maxGenerations; j++) { if (token.IsCancellationRequested) { logWriter.WriteLine("Cancelled"); break; } DateTime dt = DateTime.Now; ea.PerformOneGeneration(); if (ea.BestGenome.ObjectiveFitness > maxFitness) { maxFitness = ea.BestGenome.ObjectiveFitness; doc = new XmlDocument(); XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome); oFileInfo = new FileInfo(folder + "bestGenome" + j + ".xml"); doc.Save(oFileInfo.FullName); /*/ This will output the substrate * doc = new XmlDocument(); * XmlGenomeWriterStatic.Write(doc, SUPGNetworkEvaluator.substrate.generateGenome(ea.BestGenome.Decode(null))); * oFileInfo = new FileInfo(folder + "bestNetwork" + j + ".xml"); * doc.Save(oFileInfo.FullName);*/ } var msg = DateTime.Now.ToLongTimeString() + "; Duration=" + DateTime.Now.Subtract(dt).ToString("mm\\:ss") + "; Gen=" + ea.Generation.ToString("000") + "; Neurons=" + (ea.Population.TotalNeuronCount / (float)ea.Population.GenomeList.Count).ToString("00.00") + "; Connections=" + (ea.Population.TotalConnectionCount / (float)ea.Population.GenomeList.Count).ToString("00.00") + "; BestFit=" + ea.BestGenome.ObjectiveFitness.ToString("0.000") + "; MaxFit=" + maxFitness.ToString("0.000"); Console.WriteLine(msg); logWriter.WriteLine(msg); logWriter.Flush(); //Do any post-hoc stuff here } doc = new XmlDocument(); XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn")); oFileInfo = new FileInfo(folder + "bestGenome.xml"); doc.Save(oFileInfo.FullName); }
static void Main(string[] args) { string folder = ""; NeatGenome seedGenome = null; string filename = null; string shape = "triangle"; bool isMulti = false; for (int j = 0; j < args.Length; j++) { if (j <= args.Length - 2) { switch (args[j]) { case "-seed": filename = args[++j]; Console.WriteLine("Attempting to use seed from file " + filename); break; case "-folder": folder = args[++j]; Console.WriteLine("Attempting to output to folder " + folder); break; case "-shape": shape = args[++j]; Console.WriteLine("Attempting to do experiment with shape " + shape); break; case "-multi": isMulti = Boolean.Parse(args[++j]); Console.WriteLine("Experiment is heterogeneous? " + isMulti); break; } } } if (filename != null) { try { XmlDocument document = new XmlDocument(); document.Load(filename); seedGenome = XmlNeatGenomeReaderStatic.Read(document); } catch (Exception e) { System.Console.WriteLine("Problem loading genome. \n" + e.Message); } } double maxFitness = 0; int maxGenerations = 1000; int populationSize = 150; int inputs = 4; IExperiment exp = new SkirmishExperiment(inputs, 1, isMulti, shape); StreamWriter SW; SW = File.CreateText(folder + "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(folder + "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(folder + "bestGenome.xml"); doc.Save(oFileInfo.FullName); }
/// <summary> /// This function contains all of the pre-run logic that doesn't involve graphics. /// </summary> protected override void Initialize() { base.Initialize(); // Create the world / region system // Note: The morphology must be generated in advance of the Load INetwork morphologyCPPN = loadCPPNFromXml(initialMorphologyFilename).Decode(ActivationFunctionFactory.GetActivationFunction("BipolarSigmoid")); morphology = generateMorphology(morphologyCPPN); redTexture = generateSolidMorphology(morphology); InitializeRegions(); // Initialize a log to track some instance-specific data using (System.IO.StreamWriter file = new System.IO.StreamWriter("RunInfo.txt", true)) { file.WriteLine("Novelty search run"); file.WriteLine("Start time: " + DateTime.Now.ToString("HH:mm:ss tt")); if (freezeAfterPlanting) { file.WriteLine("Individuals are immobilized once they attempt to plant."); } else { file.WriteLine("Individuals are allowed to keep moving even if/after they attempt to plant."); } file.WriteLine("Morphology genome XML filename: " + initialControllerFilename); file.WriteLine("Behavior update interval: " + behaviorUpdateInterval); file.WriteLine("Planting weight: " + plantingWeight); file.WriteLine("Position weight: " + positionWeight); file.WriteLine("Population size: " + populationSize); file.WriteLine("Archive threshold: " + archiveThreshold); } // Initialize some static variables for the simulation numBidirectionalPlanters = 0; numFirstTrialPlanters = 0; numSecondTrialPlanters = 0; numBidirectionalMisplanters = 0; numFirstTrialMisplanters = 0; numSecondTrialMisplanters = 0; firstTrial = true; // Set the NEAT parameters neatParams = new NeatParameters(); neatParams.archiveThreshold = archiveThreshold; neatParams.noveltyFixed = true; neatParams.noveltySearch = true; // Configure the HyperNEAT substrate controllerSubstrate = new ControllerSubstrate(308, 4, 108, new BipolarSigmoid()); controllerSubstrate.weightRange = 5.0; controllerSubstrate.threshold = 0.2; // Create a genome factory to generate a list of CPPN genomes cppnGenerator = new GenomeFactory(); idGen = new IdGenerator(); cppnGenomeList = cppnGenerator.CreateGenomeList(neatParams, idGen, 4, 8, 1.0f, populationSize); GenomeIndexOfCurrentCreature = 0; // Initialize the folders for storing the archive and planters noveltyLogsFolder = Directory.GetCurrentDirectory() + "\\archive\\" + GenomeIndexOfCurrentCreature + "\\"; if (!Directory.Exists(noveltyLogsFolder)) { Directory.CreateDirectory(noveltyLogsFolder); } plantersFolder = Directory.GetCurrentDirectory() + "\\planters\\" + GenomeIndexOfCurrentCreature + "\\"; if (!Directory.Exists(plantersFolder)) { Directory.CreateDirectory(plantersFolder); } // Create an initial population based on the genome list popn = new Population(idGen, cppnGenomeList); // Set the generation counter // Note: This must be kept seperately from the EA generation counter because novelty search here does't follow the traditional loop. generation = 1; // Create the EA // (Don't run the EA until the first generation has had a chance to go through the simulation. // The EA call happens in Simulator.NewGeneration().) ea = new EvolutionAlgorithm(popn, new ChromariaPopulationEvaluator(new ChromariaNetworkEvaluator()), neatParams); // Initialize the behavior trackers for this individual ea.Population.GenomeList[GenomeIndexOfCurrentCreature].Behavior = new BehaviorType(); ea.Population.GenomeList[GenomeIndexOfCurrentCreature].Behavior.behaviorList = new List <double>(); // Generate the initial creature int x = initialBoardWidth / 2; int y = initialBoardHeight / 2; INetwork newController = controllerSubstrate.generateGenome(ea.Population.GenomeList[GenomeIndexOfCurrentCreature].Decode(ActivationFunctionFactory.GetActivationFunction("BipolarSigmoid"))).Decode(ActivationFunctionFactory.GetActivationFunction("BipolarSigmoid")); if (bidirectionalTrials) { currentCreature = new NNControlledCreature(morphology, x, y, initialHeading + (float)(Math.PI / 2.0), newController, this, drawSensorField, trackPlanting, defaultNumSensors, freezeAfterPlanting); } else { currentCreature = new NNControlledCreature(morphology, x, y, initialHeading, newController, this, drawSensorField, trackPlanting, defaultNumSensors, freezeAfterPlanting); } currentCreature.DrawOrder = 1; indexOfCurrentCreature = Components.Count - 1; // Add the creature to the simulator's region lists int currentPointer = Components.Count - 1; regions[y / regionHeight, x / regionWidth].Add(currentPointer); if ((x % regionWidth > (x + morphology.Width) % regionWidth) && (y % regionHeight > (y + morphology.Height) % regionHeight) && !regions[(y + morphology.Height) / regionHeight, (x + morphology.Width) / regionWidth].Contains(currentPointer)) { regions[(y + morphology.Height) / regionHeight, (x + morphology.Width) / regionWidth].Add(currentPointer); } if (x % regionWidth > (x + morphology.Width) % regionWidth && !regions[(y / regionHeight), (x + morphology.Width) / regionWidth].Contains(currentPointer)) { regions[(y / regionHeight), (x + morphology.Width) / regionWidth].Add(currentPointer); } if (y % regionHeight > (y + morphology.Height) % regionHeight && !(regions[(y + morphology.Height) / regionHeight, x / regionWidth].Contains(currentPointer))) { regions[(y + morphology.Height) / regionHeight, x / regionWidth].Add(currentPointer); } // Preliminarily update the creature's sensors so its first movements are actually based on what's underneath its starting position currentCreature.InitializeSensor(); plantedInColoredSpace1 = false; plantedInColoredSpace2 = false; plantedInWhiteSpace1 = false; plantedInWhiteSpace2 = false; numUpdates = 0; }
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); }