Ejemplo n.º 1
0
            void AddLayers(List <LayerConfig> gen)
            {
                foreach (var g in gen)
                {
                    IActivationFunction act;
                    if (g.ActivationType == 0)
                    {
                        act = new ActivationBiPolar();
                    }
                    switch (g.ActivationType)
                    {
                    case 0:
                        act = new ActivationBiPolar();
                        break;

                    case 1:
                        act = new ActivationBipolarSteepenedSigmoid();
                        break;

                    case 2:
                        act = new ActivationClippedLinear();
                        break;

                    case 3:
                        act = new ActivationCompetitive();
                        break;

                    case 4:
                        act = new ActivationElliott();
                        break;

                    case 5:
                        act = new ActivationElliottSymmetric();
                        break;

                    case 6:
                        act = new ActivationGaussian();
                        break;

                    case 7:
                        act = new ActivationLinear();
                        break;

                    case 8:
                        act = new ActivationLOG();
                        break;

                    case 9:
                        act = new ActivationRamp();
                        break;

                    case 10:
                        act = new ActivationRamp();
                        break;

                    case 11:
                        act = new ActivationSigmoid();
                        break;

                    case 12:
                        act = new ActivationSIN();
                        break;

                    case 13:
                        act = new ActivationSoftMax();
                        break;

                    case 14:
                        act = new ActivationSteepenedSigmoid();
                        break;

                    case 15:
                        act = new ActivationStep();
                        break;

                    case 16:
                        act = new ActivationTANH();
                        break;

                    default:
                        act = new ActivationSoftMax();
                        break;
                    }
                    network.AddLayer(new BasicLayer(act, g.hasBias, g.neurons));
                }
            }
        /// <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);
        }