Beispiel #1
0
        /// <inheritdoc/>
        public IMLMethod Decode(IGenome genome)
        {
            NEATPopulation pop = (NEATPopulation)genome.Population;

            Substrate.Substrate substrate = pop.CurrentSubstrate;
            return(Decode(pop, substrate, genome));
        }
        /// <summary>
        /// Create a sandwich substrate. A sandwich has an input layer connected
        /// directly to an output layer, both are square.
        /// </summary>
        /// <param name="inputEdgeSize">The input edge size.</param>
        /// <param name="outputEdgeSize">The output edge size.</param>
        /// <returns>The substrate.</returns>
        public static Substrate factorSandwichSubstrate(int inputEdgeSize,
                int outputEdgeSize)
        {
            Substrate result = new Substrate(3);

            double inputTick = 2.0 / inputEdgeSize;
            double outputTick = 2.0 / inputEdgeSize;
            double inputOrig = -1.0 + (inputTick / 2.0);
            double outputOrig = -1.0 + (inputTick / 2.0);

            // create the input layer

            for (int row = 0; row < inputEdgeSize; row++)
            {
                for (int col = 0; col < inputEdgeSize; col++)
                {
                    SubstrateNode inputNode = result.CreateInputNode();
                    inputNode.Location[0] = -1;
                    inputNode.Location[1] = inputOrig + (row * inputTick);
                    inputNode.Location[2] = inputOrig + (col * inputTick);
                }
            }

            // create the output layer (and connect to input layer)

            for (int orow = 0; orow < outputEdgeSize; orow++)
            {
                for (int ocol = 0; ocol < outputEdgeSize; ocol++)
                {
                    SubstrateNode outputNode = result.CreateOutputNode();
                    outputNode.Location[0] = 1;
                    outputNode.Location[1] = outputOrig + (orow * outputTick);
                    outputNode.Location[2] = outputOrig + (ocol * outputTick);

                    // link this output node to every input node
                    foreach (SubstrateNode inputNode in result.InputNodes)
                    {
                        result.CreateLink(inputNode, outputNode);
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// Construct a starting HyperNEAT population.  does not generate the
        /// initial random population of genomes.
        /// </summary>
        /// <param name="theSubstrate">The substrate ID.</param>
        /// <param name="populationSize">The population size.</param>
        public NEATPopulation(Substrate theSubstrate, int populationSize)
            : base(populationSize, new FactorHyperNEATGenome())
        {
            SurvivalRate = DefaultSurvivalRate;
            WeightRange = 5;
            InitialConnectionDensity = 0.1;
            RandomNumberFactory = EncogFramework.Instance
                .RandomFactory.FactorFactory();

            CurrentSubstrate = theSubstrate;
            InputCount = 6;
            OutputCount = 2;
            HyperNEATGenome.BuildCPPNActivationFunctions(_activationFunctions);
        }
Beispiel #4
0
        /// <inheritdoc/>
        public IMLMethod Decode(NEATPopulation pop, Substrate.Substrate substrate,
                                IGenome genome)
        {
            // obtain the CPPN
            NEATCODEC   neatCodec = new NEATCODEC();
            NEATNetwork cppn      = (NEATNetwork)neatCodec.Decode(genome);

            List <NEATLink> linkList = new List <NEATLink>();

            IActivationFunction[] afs = new IActivationFunction[substrate.NodeCount];

            IActivationFunction af = new ActivationSteepenedSigmoid();

            // all activation functions are the same
            for (int i = 0; i < afs.Length; i++)
            {
                afs[i] = af;
            }

            double      c     = this.MaxWeight / (1.0 - this.MinWeight);
            BasicMLData input = new BasicMLData(cppn.InputCount);

            // First create all of the non-bias links.
            foreach (SubstrateLink link in substrate.Links)
            {
                SubstrateNode source = link.Source;
                SubstrateNode target = link.Target;

                int index = 0;
                foreach (double d in source.Location)
                {
                    input.Data[index++] = d;
                }
                foreach (double d in target.Location)
                {
                    input.Data[index++] = d;
                }
                IMLData output = cppn.Compute(input);

                double weight = output[0];
                if (Math.Abs(weight) > this.MinWeight)
                {
                    weight = (Math.Abs(weight) - this.MinWeight) * c
                             * Math.Sign(weight);
                    linkList.Add(new NEATLink(source.ID, target.ID,
                                              weight));
                }
            }

            // now create biased links
            input.Clear();
            int d2 = substrate.Dimensions;
            IList <SubstrateNode> biasedNodes = substrate.GetBiasedNodes();

            foreach (SubstrateNode target in biasedNodes)
            {
                for (int i = 0; i < d2; i++)
                {
                    input.Data[d2 + i] = target.Location[i];
                }

                IMLData output = cppn.Compute(input);

                double biasWeight = output[1];
                if (Math.Abs(biasWeight) > this.MinWeight)
                {
                    biasWeight = (Math.Abs(biasWeight) - this.MinWeight) * c
                                 * Math.Sign(biasWeight);
                    linkList.Add(new NEATLink(0, target.ID, biasWeight));
                }
            }

            // check for invalid neural network
            if (linkList.Count == 0)
            {
                return(null);
            }

            linkList.Sort();

            NEATNetwork network = new NEATNetwork(substrate.InputCount,
                                                  substrate.OutputCount, linkList, afs);

            network.ActivationCycles = substrate.ActivationCycles;
            return(network);
        }
        /// <inheritdoc/>
        public IMLMethod Decode(NEATPopulation pop, Substrate.Substrate substrate,
                IGenome genome)
        {
            // obtain the CPPN
            NEATCODEC neatCodec = new NEATCODEC();
            NEATNetwork cppn = (NEATNetwork)neatCodec.Decode(genome);

            List<NEATLink> linkList = new List<NEATLink>();

            IActivationFunction[] afs = new IActivationFunction[substrate.NodeCount];

            IActivationFunction af = new ActivationSteepenedSigmoid();
            // all activation functions are the same
            for (int i = 0; i < afs.Length; i++)
            {
                afs[i] = af;
            }

            double c = this.MaxWeight / (1.0 - this.MinWeight);
            BasicMLData input = new BasicMLData(cppn.InputCount);

            // First create all of the non-bias links.
            foreach (SubstrateLink link in substrate.Links)
            {
                SubstrateNode source = link.Source;
                SubstrateNode target = link.Target;

                int index = 0;
                foreach (double d in source.Location)
                {
                    input.Data[index++] = d;
                }
                foreach (double d in target.Location)
                {
                    input.Data[index++] = d;
                }
                IMLData output = cppn.Compute(input);

                double weight = output[0];
                if (Math.Abs(weight) > this.MinWeight)
                {
                    weight = (Math.Abs(weight) - this.MinWeight) * c
                            * Math.Sign(weight);
                    linkList.Add(new NEATLink(source.ID, target.ID,
                            weight));
                }
            }

            // now create biased links
            input.Clear();
            int d2 = substrate.Dimensions;
            IList<SubstrateNode> biasedNodes = substrate.GetBiasedNodes();
            foreach (SubstrateNode target in biasedNodes)
            {
                for (int i = 0; i < d2; i++)
                {
                    input.Data[d2 + i] = target.Location[i];
                }

                IMLData output = cppn.Compute(input);

                double biasWeight = output[1];
                if (Math.Abs(biasWeight) > this.MinWeight)
                {
                    biasWeight = (Math.Abs(biasWeight) - this.MinWeight) * c
                            * Math.Sign(biasWeight);
                    linkList.Add(new NEATLink(0, target.ID, biasWeight));
                }
            }

            // check for invalid neural network
            if (linkList.Count == 0)
            {
                return null;
            }

            linkList.Sort();

            NEATNetwork network = new NEATNetwork(substrate.InputCount,
                    substrate.OutputCount, linkList, afs);

            network.ActivationCycles = substrate.ActivationCycles;
            return network;
        }