示例#1
0
		/// <summary>
		/// Construct a GenomeList. This can be used to construct a new Population object.
		/// </summary>
		/// <param name="evolutionAlgorithm"></param>
		/// <param name="inputNeuronCount"></param>
		/// <param name="outputNeuronCount"></param>
		/// <param name="length"></param>
		/// <returns></returns>
        // Schrum: Added outputsPerPolicy
        public static GenomeList CreateGenomeList(NeatParameters neatParameters, IdGenerator idGenerator, int inputNeuronCount, int outputNeuronCount, int outputsPerPolicy, float connectionProportion, int length)
		{
			GenomeList genomeList = new GenomeList();
			
			for(int i=0; i<length; i++)
			{
				idGenerator.ResetNextInnovationNumber();
                // Schrum: Added outputsPerPolicy
				genomeList.Add(CreateGenome(neatParameters, idGenerator, inputNeuronCount, outputNeuronCount, outputsPerPolicy, connectionProportion));
			}

			return genomeList;
		}
示例#2
0
        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());
            }
        }
示例#3
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());
            }
        }
		/// <summary>
		/// Construct a GenomeList. This can be used to construct a new Population object.
		/// </summary>
		/// <param name="evolutionAlgorithm"></param>
		/// <param name="inputNeuronCount"></param>
		/// <param name="outputNeuronCount"></param>
		/// <param name="length"></param>
		/// <returns></returns>
		public static GenomeList CreateGenomeList(NeatParameters neatParameters, IdGenerator idGenerator, int inputNeuronCount, int outputNeuronCount, float connectionProportion, int length, bool neatBrain=false)
		{
			GenomeList genomeList = new GenomeList();
			
			for(int i=0; i<length; i++)
			{
				idGenerator.ResetNextInnovationNumber();
				genomeList.Add(CreateGenome(neatParameters, idGenerator, inputNeuronCount, outputNeuronCount, connectionProportion, neatBrain));
			}

			return genomeList;
		}