Exemplo n.º 1
0
        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));
            }
        }
Exemplo n.º 2
0
    // 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);
    }
 public AllOnesPhenome()
 {
     Genome = GenomeFactory.CreateGenome(GenomeType.RawBinaryString64, 64);
 }
 public CarPhenome()
 {
     Genome = GenomeFactory.CreateGenome(GenomeType.FloatString, (5 * 8 + 8) + (8 * 3 + 3));
 }
 public CardPhenome()
 {
     Genome = GenomeFactory.CreateGenome(GenomeType.RawBinaryString32, 10);
 }
Exemplo n.º 6
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());
            }
        }