Exemplo n.º 1
0
        private void AddNeuronGene(CPPNNEATNeuronGene neuron)
        {
            if (!neuronGeneSet.Contains(neuron))
            {
                neuronGeneSet.Add(neuron);

                foreach (var toNeuron in neuronGeneSet.Where(gene => gene.Type != CPPNNeuronType.Input &&
                                                             gene.Type != CPPNNeuronType.Bias && (
                                                                 !ParentGA.FeedForwardOnly ||
                                                                 gene.Level > neuron.Level
                                                                 )))
                {
                    possibleConnections.Add(Tuple.Create(neuron, toNeuron));
                }

                if (neuron.Type != CPPNNeuronType.Input && neuron.Type != CPPNNeuronType.Bias)
                {
                    foreach (var fromNeuron in neuronGeneSet.Where(gene => !ParentGA.FeedForwardOnly ||
                                                                   gene.Level < neuron.Level
                                                                   ))
                    {
                        possibleConnections.Add(Tuple.Create(fromNeuron, neuron));
                    }
                }

                Parent.Update();
            }
        }
        public CPPNNEATLinkGene(int innovationNumber, CPPNNEATNeuronGene from,
                                CPPNNEATNeuronGene to, Complex weight)
        {
            this.InnovationNumber = innovationNumber;
            this.Enabled          = true;

            this.From = from;
            this.To   = to;

            this.Weight = weight;
        }
        public CPPNNEATLinkGene(int innovationNumber, CPPNNEATNeuronGene from,
CPPNNEATNeuronGene to, Complex weight)
        {
            this.InnovationNumber = innovationNumber;
            this.Enabled = true;

            this.From = from;
            this.To = to;

            this.Weight = weight;
        }
        public CPPNNEATNeuronGene GetHiddenNeuron(int innovationNumber)
        {
            if (!hiddenNeuronMap.ContainsKey(innovationNumber))
            {
                var edge  = edgeMap[innovationNumber];
                var level = (edge.Item1.Level + edge.Item2.Level) / 2;

                hiddenNeuronMap[innovationNumber] = new CPPNNEATNeuronGene(neuronInnovationNumber++, level, CPPNNeuronType.Hidden,
                                                                           CanonicalFunctionList.RandomSingle()());
            }

            return(hiddenNeuronMap[innovationNumber]);
        }
        public int GetEdgeInnovationNumber(CPPNNEATNeuronGene from, CPPNNEATNeuronGene to)
        {
            var key = new Tuple <CPPNNEATNeuronGene, CPPNNEATNeuronGene>(from, to);

            if (!edgeInnovationNumberMap.ContainsKey(key))
            {
                edgeInnovationNumberMap[key]  = edgeInnovationNumber;
                edgeMap[edgeInnovationNumber] = key;

                edgeInnovationNumber++;
            }

            return(edgeInnovationNumberMap[key]);
        }
Exemplo n.º 6
0
        private bool TryCreateLinkGene(CPPNNEATNeuronGene from, CPPNNEATNeuronGene to)
        {
            if (to.Level == 0)
            {
                throw new ApplicationException("Links should not go into input or bias neurons");
            }
            else if (ParentGA.FeedForwardOnly && from.Level >= to.Level)
            {
                throw new ApplicationException("Cannot create recursive connections in feed forward only networks");
            }

            return(TryAddLinkGene(new CPPNNEATLinkGene(ParentGA.GetEdgeInnovationNumber(from, to), from, to,
                                                       ParentGA.GetRandomWeight())));
        }
        public BaseCPPNNEATGA(int numberOfInputs, int populationSize, Func <GenomeType, double> scoreFunction,
                              List <Func <CPPNNEATActivationFunction> > canonicalFunctionList,
                              Func <CPPNNEATActivationFunction> outputActivationFunction,
                              bool feedForwardOnly)
            : base(populationSize, scoreFunction)
        {
            if (numberOfInputs < 1)
            {
                throw new ApplicationException("Numbers of inputs cannot be less than 1.");
            }

            this.NumberOfInputs        = numberOfInputs;
            this.CanonicalFunctionList = canonicalFunctionList;

            this.edgeInnovationNumberMap = new Dictionary <Tuple <CPPNNEATNeuronGene, CPPNNEATNeuronGene>, int>();
            this.hiddenNeuronMap         = new Dictionary <int, CPPNNEATNeuronGene>();
            this.edgeMap = new Dictionary <int, Tuple <CPPNNEATNeuronGene, CPPNNEATNeuronGene> >();

            this.defaultNeuronGenes = new List <CPPNNEATNeuronGene>();
            this.defaultLinkGenes   = new List <CPPNNEATLinkGene>();

            this.FeedForwardOnly = feedForwardOnly;

            this.IterationComplete += new EventHandler <IterationEventArgs>(CPPNNEATGA_IterationComplete);

            var outputGene = new CPPNNEATNeuronGene(neuronInnovationNumber++, 1, CPPNNeuronType.Output,
                                                    (outputActivationFunction == null) ?
                                                    canonicalFunctionList.RandomSingle()() : outputActivationFunction());

            var currentGene = new CPPNNEATNeuronGene(neuronInnovationNumber++, 0, CPPNNeuronType.Bias, null);

            defaultNeuronGenes.Add(currentGene);
            defaultLinkGenes.Add(new CPPNNEATLinkGene(GetEdgeInnovationNumber(currentGene, outputGene), currentGene,
                                                      outputGene, 0));

            foreach (var i in Enumerable.Range(0, numberOfInputs))
            {
                currentGene = new CPPNNEATNeuronGene(neuronInnovationNumber++, 0, CPPNNeuronType.Input, null);

                defaultNeuronGenes.Add(currentGene);
                defaultLinkGenes.Add(new CPPNNEATLinkGene(GetEdgeInnovationNumber(currentGene, outputGene), currentGene,
                                                          outputGene, 0));
            }

            defaultNeuronGenes.Add(outputGene);
        }
Exemplo n.º 8
0
        private void HasBeenUnOrphaned(CPPNNEATNeuronGene neuron, CPPNNEATLinkGene propogatingLink,
                                       Func <CPPNNEATLinkGene, CPPNNEATNeuronGene> parentFunction,
                                       Func <CPPNNEATLinkGene, CPPNNEATNeuronGene> childFunction)
        {
            if (neuron.Type != CPPNNeuronType.Hidden || !orphanedNeurons.Contains(neuron) ||
                orphanedNeurons.Contains(parentFunction(propogatingLink)))
            {
                return;
            }

            orphanedNeurons.Remove(neuron);

            var enabledLinks = LinkGenes.Where(link => link.Enabled);

            foreach (var enabledLink in enabledLinks.Where(link => parentFunction(link) == neuron).ToList())
            {
                HasBeenUnOrphaned(childFunction(enabledLink), enabledLink, parentFunction, childFunction);
            }
        }
Exemplo n.º 9
0
        private void HasBeenOrphaned(CPPNNEATNeuronGene neuron, CPPNNEATLinkGene propogatingLink,
                                     Func <CPPNNEATLinkGene, CPPNNEATNeuronGene> parentFunction,
                                     Func <CPPNNEATLinkGene, CPPNNEATNeuronGene> childFunction)
        {
            if (neuron.Type != CPPNNeuronType.Hidden || orphanedNeurons.Contains(neuron))
            {
                return;
            }

            var validLinks = ValidLinks;

            if (validLinks.Where(link => link != propogatingLink && childFunction(link) == neuron).Count() == 0)
            {
                orphanedNeurons.Add(neuron);

                foreach (var validLink in validLinks.Where(link => parentFunction(link) == neuron).ToList())
                {
                    HasBeenOrphaned(childFunction(validLink), validLink, parentFunction, childFunction);
                }
            }
        }
        private bool TryCreateLinkGene(CPPNNEATNeuronGene from, CPPNNEATNeuronGene to)
        {
            if (to.Level == 0)
            {
                throw new ApplicationException("Links should not go into input or bias neurons");
            }
            else if (ParentGA.FeedForwardOnly && from.Level >= to.Level)
            {
                throw new ApplicationException("Cannot create recursive connections in feed forward only networks");
            }

            return TryAddLinkGene(new CPPNNEATLinkGene(ParentGA.GetEdgeInnovationNumber(from, to), from, to,
                    ParentGA.GetRandomWeight()));
        }
        private void HasBeenUnOrphaned(CPPNNEATNeuronGene neuron, CPPNNEATLinkGene propogatingLink,
Func<CPPNNEATLinkGene, CPPNNEATNeuronGene> parentFunction,
Func<CPPNNEATLinkGene, CPPNNEATNeuronGene> childFunction)
        {
            if (neuron.Type != CPPNNeuronType.Hidden || !orphanedNeurons.Contains(neuron) ||
                    orphanedNeurons.Contains(parentFunction(propogatingLink)))
            {
                return;
            }

            orphanedNeurons.Remove(neuron);

            var enabledLinks = LinkGenes.Where(link => link.Enabled);

            foreach (var enabledLink in enabledLinks.Where(link => parentFunction(link) == neuron).ToList())
            {
                HasBeenUnOrphaned(childFunction(enabledLink), enabledLink, parentFunction, childFunction);
            }
        }
        private void HasBeenOrphaned(CPPNNEATNeuronGene neuron, CPPNNEATLinkGene propogatingLink,
Func<CPPNNEATLinkGene, CPPNNEATNeuronGene> parentFunction,
Func<CPPNNEATLinkGene, CPPNNEATNeuronGene> childFunction)
        {
            if (neuron.Type != CPPNNeuronType.Hidden || orphanedNeurons.Contains(neuron))
            {
                return;
            }

            var validLinks = ValidLinks;

            if (validLinks.Where(link => link != propogatingLink && childFunction(link) == neuron).Count() == 0)
            {
                orphanedNeurons.Add(neuron);

                foreach (var validLink in validLinks.Where(link => parentFunction(link) == neuron).ToList())
                {
                    HasBeenOrphaned(childFunction(validLink), validLink, parentFunction, childFunction);
                }
            }
        }
        private void AddNeuronGene(CPPNNEATNeuronGene neuron)
        {
            if (!neuronGeneSet.Contains(neuron))
            {
                neuronGeneSet.Add(neuron);

                foreach (var toNeuron in neuronGeneSet.Where(gene => gene.Type != CPPNNeuronType.Input &&
                                                gene.Type != CPPNNeuronType.Bias && (
                                                    !ParentGA.FeedForwardOnly ||
                                                    gene.Level > neuron.Level
                                                )))
                {
                    possibleConnections.Add(Tuple.Create(neuron, toNeuron));
                }

                if (neuron.Type != CPPNNeuronType.Input && neuron.Type != CPPNNeuronType.Bias)
                {
                    foreach (var fromNeuron in neuronGeneSet.Where(gene => !ParentGA.FeedForwardOnly ||
                                            gene.Level < neuron.Level
                                        ))
                    {
                        possibleConnections.Add(Tuple.Create(fromNeuron, neuron));
                    }
                }

                Parent.Update();
            }
        }