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 }
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()); } }