コード例 #1
0
 /// <summary>
 /// Create a genome factory for the experiment.
 /// Create a genome factory with our neat genome parameters object and the appropriate number of input and output neuron genes.
 /// </summary>
 public IGenomeFactory <NeatGenome> CreateGenomeFactory()
 {
     if (Genotype == Genotype.Neat)
     {
         return(new CppnGenomeFactory(InputCount, OutputCount, GetCppnActivationFunctionLibrary(), _neatGenomeParams));
     }
     else
     {
         IActivationFunctionLibrary activationFnLibrary = DefaultActivationFunctionLibrary.CreateLibraryRbf(_neatGenomeParams.ActivationFn, RbfMutationSigmaCenter, RbfMutationSigmaRadius);
         return(new RbfGenomeFactory(InputCount, OutputCount, activationFnLibrary));
     }
 }
コード例 #2
0
    public IGenomeDecoder <NeatGenome, IBlackBox> CreateGenomeDecoder()
    {
        // Create HyperNEAT network substrate.

        //-- Create input layer nodes.
        SubstrateNodeSet inputLayer = new SubstrateNodeSet(5);

        AddNode(inputLayer, 1, -1.0, 0.0, -1.0); // Left
        AddNode(inputLayer, 2, +1.0, 0.0, -1.0); // right
        AddNode(inputLayer, 3, 0.0, +1.0, -1.0); // up
        AddNode(inputLayer, 4, 0.0, -1.0, -1.0); // down
        AddNode(inputLayer, 5, 0.0, +0.0, -1.0); // Distance

        SubstrateNodeSet outputLayer = new SubstrateNodeSet(2);

        AddNode(outputLayer, 6, +1.0, 0.0, +1.0); // right
        AddNode(outputLayer, 7, 0.0, +1.0, +1.0); // up
        // AddNode(outputLayer, 8, 0.0, +1.0, +1.0);// up
        // AddNode(outputLayer, 9, 0.0, -1.0, +1.0);// down

        SubstrateNodeSet h1Layer = new SubstrateNodeSet(4);

        AddNode(h1Layer, 10, -1.0, +1.0, 0.0); // Left
        AddNode(h1Layer, 11, +1.0, +1.0, 0.0); // right
        AddNode(h1Layer, 12, -1.0, -1.0, 0.0); // up
        AddNode(h1Layer, 13, +1.0, -1.0, 0.0); // down

        // Connect up layers.
        List <SubstrateNodeSet> nodeSetList = new List <SubstrateNodeSet>(2);

        nodeSetList.Add(inputLayer);
        nodeSetList.Add(outputLayer);
        // nodeSetList.Add(h1Layer);

        List <NodeSetMapping> nodeSetMappingList = new List <NodeSetMapping>(1);

        nodeSetMappingList.Add(NodeSetMapping.Create(0, 1, (double?)null));  // Input -> Output.
        // nodeSetMappingList.Add(NodeSetMapping.Create(0, 2, (double?)null));  // Input -> Hidden.
        //nodeSetMappingList.Add(NodeSetMapping.Create(2, 1, (double?)null));  // Hidden -> Output.
        //  nodeSetMappingList.Add(NodeSetMapping.Create(1, 2, (double?)null));  // Output -> Hidden

        // Construct substrate.
        Substrate substrate = new Substrate(nodeSetList, DefaultActivationFunctionLibrary.CreateLibraryNeat(SteepenedSigmoid.__DefaultInstance), 0, 0.01, 5, nodeSetMappingList);//++


        // Create genome decoder. Decodes to a neural network packaged with an activation scheme that defines a fixed number of activations per evaluation.
        IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new HyperNeatDecoder(substrate, _activationSchemeCppn, _activationScheme, false);//++

        //return new NeatGenomeDecoder(_activationScheme);
        return(genomeDecoder);
    }
コード例 #3
0
        /// <summary>
        /// Constructs a NeatGenomeFactory with the provided NeatGenomeParameters and ID generators initialized to zero.
        /// </summary>
        public NeatGenomeFactory(int inputNeuronCount, int outputNeuronCount,
                                 NeatGenomeParameters neatGenomeParams)
        {
            _inputNeuronCount    = inputNeuronCount;
            _outputNeuronCount   = outputNeuronCount;
            _activationFnLibrary = DefaultActivationFunctionLibrary.CreateLibraryNeat(neatGenomeParams.ActivationFn);

            _neatGenomeParamsCurrent       = neatGenomeParams;
            _neatGenomeParamsComplexifying = _neatGenomeParamsCurrent;
            _neatGenomeParamsSimplifying   = NeatGenomeParameters.CreateSimplifyingParameters(_neatGenomeParamsComplexifying);

            _genomeIdGenerator     = new UInt32IdGenerator();
            _innovationIdGenerator = new UInt32IdGenerator();
        }
コード例 #4
0
        public static IGenomeFactory <NeatGenome> CreateGenomeFactory(HyperNEAT_Args args, ExperimentInitArgs_Activation activation)
        {
            // The cppn gets handed pairs of points, and there is an extra neuron for something.  See Substrate.CreateNetworkDefinition()
            // Adding 1 to the dimension because a pair of points needs an extra dimension to separate them (2 2D squares would have Z=-1 and 1)
            // Multiplying by 2 because a pair of points is loaded into the cppn at a time
            // Adding the final 1 to store the pair's connection length
            int inputCount = ((args.InputShapeDimensions + 1) * 2) + 1;

            int outputCount = args.OutputShapeDimensions;

            //NOTE: Can't use args.NeuronCount_Input and args.NeuronCount_Output.  This genome factory is for the CPPN which
            //uses the dimensions of the positions as input/output size
            return(new NeatGenomeFactory(inputCount, outputCount, DefaultActivationFunctionLibrary.CreateLibraryCppn(), GetNewNeatGenomeParameters(activation)));
        }
コード例 #5
0
        /// <summary>
        /// Creates a genome decoder. We split this code into a separate  method so that it can be re-used by the problem domain visualization code
        /// (it needs to decode genomes to phenomes in order to create a visualization).
        /// </summary>
        /// <param name="visualFieldResolution">The visual field's pixel resolution, e.g. 11 means 11x11 pixels.</param>
        /// <param name="lengthCppnInput">Indicates if the CPPNs being decoded have an extra input for specifying connection length.</param>
        public IGenomeDecoder <NeatGenome, IBlackBox> CreateGenomeDecoder(int visualFieldResolution, bool lengthCppnInput)
        {
            // Create two layer 'sandwich' substrate.
            int    pixelCount    = visualFieldResolution * visualFieldResolution;
            double pixelSize     = BoxesVisualDiscriminationEvaluator.VisualFieldEdgeLength / visualFieldResolution;
            double originPixelXY = -1 + (pixelSize / 2.0);

            SubstrateNodeSet inputLayer  = new SubstrateNodeSet(pixelCount);
            SubstrateNodeSet outputLayer = new SubstrateNodeSet(pixelCount);

            // Node IDs start at 1. (bias node is always zero).
            uint   inputId  = 1;
            uint   outputId = (uint)(pixelCount + 1);
            double yReal    = originPixelXY;

            for (int y = 0; y < visualFieldResolution; y++, yReal += pixelSize)
            {
                double xReal = originPixelXY;
                for (int x = 0; x < visualFieldResolution; x++, xReal += pixelSize, inputId++, outputId++)
                {
                    inputLayer.NodeList.Add(new SubstrateNode(inputId, new double[] { xReal, yReal, -1.0 }));
                    outputLayer.NodeList.Add(new SubstrateNode(outputId, new double[] { xReal, yReal, 1.0 }));
                }
            }

            List <SubstrateNodeSet> nodeSetList = new List <SubstrateNodeSet>(2);

            nodeSetList.Add(inputLayer);
            nodeSetList.Add(outputLayer);

            // Define connection mappings between layers/sets.
            List <NodeSetMapping> nodeSetMappingList = new List <NodeSetMapping>(1);

            nodeSetMappingList.Add(NodeSetMapping.Create(0, 1, (double?)null));

            // Construct substrate.
            Substrate substrate = new Substrate(nodeSetList, DefaultActivationFunctionLibrary.CreateLibraryNeat(LogisticFunctionSteep.__DefaultInstance), 0, 0.2, 5, nodeSetMappingList);

            // Create genome decoder. Decodes to a neural network packaged with an activation scheme that defines a fixed number of activations per evaluation.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new HyperNeatDecoder(substrate, _activationSchemeCppn, _activationScheme, lengthCppnInput);

            return(genomeDecoder);
        }
コード例 #6
0
        private static IActivationFunctionLibrary CreateActivationFunctionFromString(string name)
        {
            name = name.ToLower(CultureInfo.InvariantCulture);
            switch (name)
            {
            case "absolute":
                return(DefaultActivationFunctionLibrary.CreateLibraryNeat(Absolute.__DefaultInstance));

            case "absoluteroot":
                return(DefaultActivationFunctionLibrary.CreateLibraryNeat(AbsoluteRoot.__DefaultInstance));

            case "gaussian":
                return(DefaultActivationFunctionLibrary.CreateLibraryNeat(Gaussian.__DefaultInstance));

            case "inverseabsolutesigmoid":
                return(DefaultActivationFunctionLibrary.CreateLibraryNeat(InverseAbsoluteSigmoid.__DefaultInstance));

            case "plainsigmoid":
                return(DefaultActivationFunctionLibrary.CreateLibraryNeat(PlainSigmoid.__DefaultInstance));

            case "steepenedsigmoid":
                return(DefaultActivationFunctionLibrary.CreateLibraryNeat(SteepenedSigmoid.__DefaultInstance));

            case "steepenedsigmoidapproximation":
                return
                    (DefaultActivationFunctionLibrary.CreateLibraryNeat(
                         SteepenedSigmoidApproximation.__DefaultInstance));

            case "stepfunction":
                return(DefaultActivationFunctionLibrary.CreateLibraryNeat(StepFunction.__DefaultInstance));

            case "linear":
                return(DefaultActivationFunctionLibrary.CreateLibraryNeat(Linear.__DefaultInstance));

            default:
                throw new XmlException("Invalid substrate configuration: 'Function'");
            }
        }
コード例 #7
0
        /// <summary>
        /// Create a genome decoder for the experiment.
        /// </summary>
        protected override IGenomeDecoder <NeatGenome, IBlackBox> CreateHyperNeatGenomeDecoder()
        {
            // Create HyperNEAT network substrate.

            uint nodeId = 1;

            //-- Create input layer nodes.
            SubstrateNodeSet inputLayer = new SubstrateNodeSet(_dataset.InputCount);

            for (var i = 0; i < _dataset.InputCount; i++)
            {
                inputLayer.NodeList.Add(new SubstrateNode(nodeId++, SetInputNodePosition(i).Extend(-1)));
            }

            //-- Create output layer nodes.
            var outputLayers = new SubstrateNodeSet[nbClusters];

            for (var i = 0; i < nbClusters; i++)
            {
                outputLayers[i] = new SubstrateNodeSet(1);
                outputLayers[i].NodeList.Add(new SubstrateNode(nodeId++, SetOutputNodePosition(i).Extend(+1 + i)));
            }

            //-- Create hidden layer nodes.
            SubstrateNodeSet hiddenLayer = null;

            if (HiddenNodesCount > 0)
            {
                hiddenLayer = new SubstrateNodeSet(HiddenNodesCount);
                for (var i = 0; i < HiddenNodesCount; i++)
                {
                    hiddenLayer.NodeList.Add(new SubstrateNode(nodeId++, SetHiddenNodePosition(i).Extend(0)));
                }
            }

            // Connect up layers.
            List <SubstrateNodeSet> nodeSetList = new List <SubstrateNodeSet>(2 + nbClusters + (HiddenNodesCount > 0 ? 1 : 0));

            nodeSetList.Add(inputLayer);
            for (var i = 0; i < nbClusters; i++)
            {
                nodeSetList.Add(outputLayers[i]);
            }
            if (HiddenNodesCount > 0)
            {
                nodeSetList.Add(hiddenLayer);
            }

            List <NodeSetMapping> nodeSetMappingList = new List <NodeSetMapping>(nbClusters * (1 + (HiddenNodesCount > 0 ? 2 : 0)));
            var inputIdx  = 0;
            var hiddenIdx = nbClusters + 1;

            for (var i = 0; i < nbClusters; i++)
            {
                var outputIdx = i + 1;
                if (HiddenNodesCount > 0)
                {
                    nodeSetMappingList.Add(NodeSetMapping.Create(inputIdx, hiddenIdx, (double?)null));  // Input -> Hidden.
                    nodeSetMappingList.Add(NodeSetMapping.Create(hiddenIdx, outputIdx, (double?)null)); // Hidden-> Output.
                    nodeSetMappingList.Add(NodeSetMapping.Create(inputIdx, outputIdx, (double?)null));  // Input -> Output.
                }
                else
                {
                    nodeSetMappingList.Add(NodeSetMapping.Create(inputIdx, outputIdx, (double?)null));  // Input -> Output.
                }
            }

            // Construct substrate.
            Substrate substrate = new Substrate(nodeSetList,
                                                DefaultActivationFunctionLibrary.CreateLibraryNeat(SteepenedSigmoid.__DefaultInstance),
                                                0, 0.2, 5,
                                                nodeSetMappingList);

            // Create genome decoder. Decodes to a neural network packaged with an activation scheme that defines a fixed number of activations per evaluation.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new HyperNeatDecoder(substrate, _activationSchemeCppn, _activationScheme, _lengthCppnInput);

            return(genomeDecoder);
        }
コード例 #8
0
    public IGenomeDecoder <NeatGenome, IBlackBox> CreateGenomeDecoder()
    {
        // Create HyperNEAT network substrate.

        //-- Create input layer nodes.
        uint i = 1;

        SubstrateNodeSet inputLayer = new SubstrateNodeSet(1);

        AddNode(inputLayer, 1, 0.0, 0.0, -1.0, 0.0); //x velocity object
        AddNode(inputLayer, 2, 0.0, 0.0, -1.0, 0.0); //x velocity object

        //for (int x = 0; x < _inputCount / 3; x++)
        //{
        //    AddNode(inputLayer, i, x/ (InputCount/3), 0.0, -1.0, -1.0);//x velocity object
        //    i++;
        //    AddNode(inputLayer, i, x / (InputCount / 3), 0.0, -1.0, 0.0);// y velocity object
        //    i++;
        //    AddNode(inputLayer, i, x / (InputCount / 3), 0.0, -1.0, +1.0);// z velocity object
        //    i++;
        //}



        SubstrateNodeSet outputLayer = _optimizer.CreateSubstrate();



        //SubstrateNodeSet h1Layer = new SubstrateNodeSet(_inputCount);

        //for (int x = 0; x < _inputCount; x++)
        //{
        //    AddNode(h1Layer, i, 0.0, 0.0, -0.25, -1.0);//x velocity object
        //    i++;
        //    AddNode(h1Layer, i, 0.0, 0.0, -0.25, 0.0);// y velocity object
        //    i++;
        //    AddNode(h1Layer, i, 0.0, 0.0, -0.25, +1.0);// z velocity object
        //    i++;
        //}



        SubstrateNodeSet h2Layer = _optimizer.CreateSubstrateHidden();

        // Connect up layers.
        List <SubstrateNodeSet> nodeSetList = new List <SubstrateNodeSet>(3);

        nodeSetList.Add(inputLayer);
        nodeSetList.Add(outputLayer);
        // nodeSetList.Add(h1Layer);
        nodeSetList.Add(h2Layer);

        List <NodeSetMapping> nodeSetMappingList = new List <NodeSetMapping>(4);

        nodeSetMappingList.Add(NodeSetMapping.Create(0, 1, (double?)null));  // Input -> Output.
        nodeSetMappingList.Add(NodeSetMapping.Create(0, 2, (double?)null));  // Input -> Hidden.
        nodeSetMappingList.Add(NodeSetMapping.Create(2, 1, (double?)null));  // Hidden -> Output.
        nodeSetMappingList.Add(NodeSetMapping.Create(1, 2, (double?)null));  // Output -> Hidden
        // nodeSetMappingList.Add(NodeSetMapping.Create(2, 3, (double?)null));  // Hidden -> Hidden2
        // nodeSetMappingList.Add(NodeSetMapping.Create(0, 3, (double?)null));  // input -> Hidden2
        // nodeSetMappingList.Add(NodeSetMapping.Create(3, 1, (double?)null));  // Hidden2 -> Output

        // Construct substrate.
        Substrate substrate = new Substrate(nodeSetList, DefaultActivationFunctionLibrary.CreateLibraryNeat(SteepenedSigmoid.__DefaultInstance), 0, 0.2, 5, nodeSetMappingList);//++


        // Create genome decoder. Decodes to a neural network packaged with an activation scheme that defines a fixed number of activations per evaluation.
        IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new HyperNeatDecoder(substrate, _activationSchemeCppn, _activationScheme, false);//++

        //return new NeatGenomeDecoder(_activationScheme);
        return(genomeDecoder);
    }
コード例 #9
0
 /// <summary>
 /// Constructs with default NeatGenomeParameters, ID generators initialized to zero and a
 /// default IActivationFunctionLibrary.
 /// </summary>
 public CppnGenomeFactory(int inputNeuronCount, int outputNeuronCount)
     : base(inputNeuronCount, outputNeuronCount, DefaultActivationFunctionLibrary.CreateLibraryCppn())
 {
 }
コード例 #10
0
        /// <summary>
        /// Create a genome factory for the experiment.
        /// Create a genome factory with our neat genome parameters object and the appropriate number of input and output neuron genes.
        /// </summary>
        public IGenomeFactory <NeatGenome> CreateGenomeFactory()
        {
            IActivationFunctionLibrary activationFnLibrary = DefaultActivationFunctionLibrary.CreateLibraryRbf(_neatGenomeParams.ActivationFn, _rbfMutationSigmaCenter, _rbfMutationSigmaRadius);

            return(new RbfGenomeFactory(InputCount, OutputCount, activationFnLibrary, _neatGenomeParams));
        }
コード例 #11
0
ファイル: TestExperiment.cs プロジェクト: kalanzai/GAER
        public IGenomeFactory <NeatGenome> CreateGenomeFactory()
        {
            //return new CppnGenomeFactory(InputCount, OutputCount, DefaultActivationFunctionLibrary.CreateLibraryCppn(), _neatGenomeParams);

            return(new NeatGenomeFactory(InputCount, OutputCount, DefaultActivationFunctionLibrary.CreateLibraryCppn(), _neatGenomeParams));
        }
コード例 #12
0
        /// <summary>
        /// Create a genome decoder for the experiment.
        /// </summary>
        public IGenomeDecoder <NeatGenome, IBlackBox> CreateGenomeDecoder()
        {
            // Create HyperNEAT network substrate.

            //-- Create input layer nodes.
            SubstrateNodeSet inputLayer = new SubstrateNodeSet(13);

            //-- Hip joint inputs.
            // Left hip joint.
            AddNode(inputLayer, 1, -1.0, +1.0, -1.0);  // Angular velocity.
            AddNode(inputLayer, 2, -0.5, +1.0, -1.0);  // Angle.

            // Right hip joint.
            AddNode(inputLayer, 3, +0.5, +1.0, -1.0);  // Angle.
            AddNode(inputLayer, 4, +1.0, +1.0, -1.0);  // Angular velocity.

            //-- Knee joint inputs.
            // Left knee joint.
            AddNode(inputLayer, 5, -1.0, -1.0, -1.0);  // Angular velocity.
            AddNode(inputLayer, 6, -0.5, -1.0, -1.0);  // Angle.

            // Right knee joint.
            AddNode(inputLayer, 7, +0.5, -1.0, -1.0);  // Angular velocity.
            AddNode(inputLayer, 8, +1.0, -1.0, -1.0);  // Angle.

            //-- Torso inputs.
            AddNode(inputLayer, 9, 0.0, +1.0, -1.0);    // Torso elevation.
            AddNode(inputLayer, 10, 0.0, +0.75, -1.0);  // Torso angle.
            AddNode(inputLayer, 11, 0.0, +0.5, -1.0);   // Torso angular velocity.
            AddNode(inputLayer, 12, 0.0, +0.25, -1.0);  // Velocity X.
            AddNode(inputLayer, 13, 0.0, 0.0, -1.0);    // Velocity Y.

            //-- Output layer nodes.
            SubstrateNodeSet outputLayer = new SubstrateNodeSet(4);

            AddNode(outputLayer, 14, -1.0, +1.0, +1.0); // Left hip torque.
            AddNode(outputLayer, 15, +1.0, +1.0, +1.0); // Right hip torque.
            AddNode(outputLayer, 16, -1.0, -1.0, +1.0); // Left knee torque.
            AddNode(outputLayer, 17, +1.0, -1.0, +1.0); // Right knee torque.

            //-- Hidden layer nodes.
            SubstrateNodeSet h1Layer = new SubstrateNodeSet(4);

            AddNode(h1Layer, 18, -1.0, +1.0, 0.0);
            AddNode(h1Layer, 19, +1.0, +1.0, 0.0);
            AddNode(h1Layer, 20, -1.0, -1.0, 0.0);
            AddNode(h1Layer, 21, +1.0, -1.0, 0.0);

            // Connect up layers.
            List <SubstrateNodeSet> nodeSetList = new List <SubstrateNodeSet>(2);

            nodeSetList.Add(inputLayer);
            nodeSetList.Add(outputLayer);
            nodeSetList.Add(h1Layer);

            List <NodeSetMapping> nodeSetMappingList = new List <NodeSetMapping>(1);

            nodeSetMappingList.Add(NodeSetMapping.Create(0, 1, (double?)null));  // Input -> Output.
            nodeSetMappingList.Add(NodeSetMapping.Create(0, 2, (double?)null));  // Input -> Hidden.
            nodeSetMappingList.Add(NodeSetMapping.Create(2, 1, (double?)null));  // Hidden -> Output.
            nodeSetMappingList.Add(NodeSetMapping.Create(1, 2, (double?)null));  // Output -> Hidden

            // Construct substrate.
            Substrate substrate = new Substrate(nodeSetList, DefaultActivationFunctionLibrary.CreateLibraryNeat(SteepenedSigmoid.__DefaultInstance), 0, 0.2, 5, nodeSetMappingList);

            // Create genome decoder. Decodes to a neural network packaged with an activation scheme that defines a fixed number of activations per evaluation.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new HyperNeatDecoder(substrate, _activationSchemeCppn, _activationScheme, _lengthCppnInput);

            return(genomeDecoder);
        }
 IActivationFunctionLibrary GetCppnActivationFunctionLibrary()
 {
     return(DefaultActivationFunctionLibrary.CreateLibraryNeat(SteepenedSigmoid.__DefaultInstance));
     //return DefaultActivationFunctionLibrary.CreateLibraryCppn();
 }
コード例 #14
0
        // This is built from these two sources:
        //http://www.nashcoding.com/2010/10/29/tutorial-%E2%80%93-evolving-neural-networks-with-sharpneat-2-part-3/
        //SharpNeat.Domains.BoxesVisualDiscrimination.BoxesVisualDiscriminationExperiment.CreateGenomeDecoder()

        //TODO: Instead of hardcoding input and output as 2D squares, the constructor should take enums for common shapes { Line, Square, Cube, CirclePerimiter, CircleArea, SphereShell, SphereFilled }
        public IGenomeDecoder <NeatGenome, IBlackBox> CreateGenomeDecoder(int inputSizeXY, int outputSizeXY)
        {
            int numInputs  = inputSizeXY * inputSizeXY;
            int numOutputs = outputSizeXY * outputSizeXY;

            SubstrateNodeSet inputLayer  = new SubstrateNodeSet(numInputs);
            SubstrateNodeSet outputLayer = new SubstrateNodeSet(numOutputs);


            // Each node in each layer needs a unique ID
            // Node IDs start at 1. (bias node is always zero)
            // The input nodes use ID range { 1, inputSize^2 } and
            // the output nodes are the next outputSize^2.
            uint inputId  = 1;
            uint outputId = Convert.ToUInt32(numInputs + 1);

            var inputCells  = Math2D.GetCells_WithinSquare(_inputSizeWorld, inputSizeXY);
            var outputCells = Math2D.GetCells_WithinSquare(_outputSizeWorld, outputSizeXY);


            for (int y = 0; y < inputSizeXY; y++)
            {
                for (int x = 0; x < inputSizeXY; x++)
                {
                    inputId++;
                    outputId++;

                    Point inputPoint  = inputCells[(y * inputSizeXY) + x].center;
                    Point outputPoint = outputCells[(y * outputSizeXY) + x].center;

                    inputLayer.NodeList.Add(new SubstrateNode(inputId, new double[] { inputPoint.X, inputPoint.Y, -1d }));
                    outputLayer.NodeList.Add(new SubstrateNode(outputId, new double[] { outputPoint.X, outputPoint.Y, 1d }));
                }
            }


            List <SubstrateNodeSet> nodeSetList = new List <SubstrateNodeSet>();

            nodeSetList.Add(inputLayer);
            nodeSetList.Add(outputLayer);


            //TODO: The two samples that I copied from have the same number of inputs and outputs.  This mapping may be too simplistic when the counts are different
            // Define a connection mapping from the input layer to the output layer.
            List <NodeSetMapping> nodeSetMappingList = new List <NodeSetMapping>();

            nodeSetMappingList.Add(NodeSetMapping.Create(0, 1, (double?)null));



            // Construct the substrate using a steepened sigmoid as the phenome's
            // activation function. All weights under 0.2 will not generate
            // connections in the final phenome.
            Substrate substrate = new Substrate(nodeSetList, DefaultActivationFunctionLibrary.CreateLibraryCppn(), 0, 0.2, 5, nodeSetMappingList);



            // Create genome decoder. Decodes to a neural network packaged with
            // an activation scheme that defines a fixed number of activations per evaluation.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new HyperNeatDecoder(substrate, _activationSchemeCppn, _activationScheme, false);



            return(genomeDecoder);
        }
コード例 #15
0
 IActivationFunctionLibrary GetCppnActivationFunctionLibrary()
 {
     return(DefaultActivationFunctionLibrary.CreateLibraryCppn());
 }
コード例 #16
0
        /// <summary>
        /// Creates a new HyperNEAT genome decoder that can be used to convert a genome into a phenome.
        /// </summary>
        public IGenomeDecoder <NeatGenome, IBlackBox> CreateGenomeDecoder()
        {
            // Create an input and an output layer for the HyperNEAT
            // substrate. Each layer corresponds to the game board
            // with 9 squares.
            SubstrateNodeSet inputLayer  = new SubstrateNodeSet(9);
            SubstrateNodeSet outputLayer = new SubstrateNodeSet(9);

            // Each node in each layer needs a unique ID.
            // The input nodes use ID range [1,9] and
            // the output nodes use [10,18].
            uint inputId = 1, outputId = 10;

            // The game board is represented as a 2-dimensional plane.
            // Each square is at -1, 0, or 1 in the x and y axis.
            // Thus, the game board looks like this:
            //
            // (-1,1)  | (0,1)   | (1,1)
            // (-1,0)  | (0,0)   | (1,0)
            // (-1,-1) | (0,-1)  | (1,-1)
            //
            // Note that the NeatPlayer class orders the board from
            // left to right, then top to bottom. So we need to create
            // nodes with the following IDs:
            //
            //    1    |    2    |   3
            //    4    |    5    |   6
            //    7    |    8    |   9
            //
            for (int x = -1; x <= 1; x++)
            {
                for (int y = 1; y >= -1; y--, inputId++, outputId++)
                {
                    inputLayer.NodeList.Add(new SubstrateNode(inputId, new double[] { x, y }));
                    outputLayer.NodeList.Add(new SubstrateNode(outputId, new double[] { x, y }));
                }
            }

            List <SubstrateNodeSet> nodeSetList      = new List <SubstrateNodeSet>(2);

            nodeSetList.Add(inputLayer);
            nodeSetList.Add(outputLayer);

            // Define a connection mapping from the input layer to the output layer.
            List <NodeSetMapping> nodeSetMappingList = new List <NodeSetMapping>(1);

            nodeSetMappingList.Add(NodeSetMapping.Create(0, 1, (double?)null));

            // Construct the substrate using a steepened sigmoid as the phenome's
            // activation function. All weights under 0.2 will not generate
            // connections in the final phenome.
            Substrate substrate = new Substrate(nodeSetList,
                                                DefaultActivationFunctionLibrary.CreateLibraryCppn(),
                                                0, 0.2, 5, nodeSetMappingList);

            // Create genome decoder. Decodes to a neural network packaged with
            // an activation scheme that defines a fixed number of activations per evaluation.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder =
                new HyperNeatDecoder(substrate, _activationSchemeCppn, _activationScheme, false);

            return(genomeDecoder);
        }
コード例 #17
0
        public static void Main(string[] args)
        {
            var random   = new Random();
            var circuits = circuitsFilePaths().ToArray();

            var perceptionStep        = TimeSpan.FromSeconds(0.1);
            var simulationStep        = TimeSpan.FromSeconds(0.05); // 20Hz
            var maximumSimulationTime = TimeSpan.FromSeconds(60);

            var tracks = circuits.Select(circuitPath => Track.Load($"{circuitPath}/circuit_definition.json"));
            var worlds = tracks.Select(track => new StandardWorld(track, simulationStep)).ToArray();

            var inputSamplesCount       = 3;
            var maximumScanningDistance = 200;

            ILidar createLidarFor(ITrack track)
            => new Lidar(track, inputSamplesCount, Angle.FromDegrees(135), maximumScanningDistance);

            var settings = new EvolutionSettings
            {
                PopulationSize      = 1000,
                SpeciesCount        = 30,
                ElitismProportion   = 0,
                ComplexityThreshold = 50
            };

            // prepare simulation
            var parameters = new NeatEvolutionAlgorithmParameters
            {
                ElitismProportion = settings.ElitismProportion,
                SpecieCount       = settings.SpeciesCount
            };

            var distanceMetric  = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            var parallelOptions = new ParallelOptions {
                MaxDegreeOfParallelism = 4
            };
            var speciationStrategy           = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, parallelOptions);
            var complexityRegulationStrategy = new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Absolute, settings.ComplexityThreshold);

            var evolutionaryAlgorithm = new NeatEvolutionAlgorithm <NeatGenome>(
                parameters,
                speciationStrategy,
                complexityRegulationStrategy);

            var phenomeEvaluator = new RaceSimulationEvaluator(
                random,
                simulationStep,
                perceptionStep,
                maximumSimulationTime,
                worlds,
                createLidarFor);

            var genomeDecoder       = new NeatGenomeDecoder(NetworkActivationScheme.CreateAcyclicScheme());
            var genomeListEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(
                genomeDecoder,
                phenomeEvaluator);

            evolutionaryAlgorithm.Initialize(
                genomeListEvaluator,
                genomeFactory: new NeatGenomeFactory(
                    inputNeuronCount: inputSamplesCount,
                    outputNeuronCount: 2,
                    DefaultActivationFunctionLibrary.CreateLibraryNeat(new BipolarSigmoid()),
                    new NeatGenomeParameters
            {
                FeedforwardOnly                     = true,
                AddNodeMutationProbability          = 0.03,
                DeleteConnectionMutationProbability = 0.05,
                ConnectionWeightMutationProbability = 0.08,
                FitnessHistoryLength                = 10,
            }),
                settings.PopulationSize);

            var lastVisualization = DateTimeOffset.Now;

            evolutionaryAlgorithm.UpdateEvent += onUpdate;
            evolutionaryAlgorithm.StartContinue();

            Console.WriteLine("Press enter to stop the evolution.");
            Console.ReadLine();
            Console.WriteLine("Finishing the evolution.");

            evolutionaryAlgorithm.Stop();
            Console.WriteLine("Evolution is stopped.");

            // simulate best individual
            Console.WriteLine("Simulating best individual...");
            evaluate(evolutionaryAlgorithm.CurrentChampGenome);
            Console.WriteLine("Done.");

            void onUpdate(object sender, EventArgs e)
            {
                Console.WriteLine($"Generation #{evolutionaryAlgorithm.CurrentGeneration}");
                Console.WriteLine($"- max fitness:  {evolutionaryAlgorithm.Statistics._maxFitness}");
                Console.WriteLine($"- mean fitness: {evolutionaryAlgorithm.Statistics._meanFitness}");
                Console.WriteLine();

                if (DateTimeOffset.Now - lastVisualization > TimeSpan.FromSeconds(35))
                {
                    lastVisualization = DateTimeOffset.Now;
                    Console.WriteLine("Simulating currently best individual...");
                    evaluate(evolutionaryAlgorithm.CurrentChampGenome);
                }
            }

            void evaluate(NeatGenome genome)
            {
                var worldId = random.Next(0, worlds.Length - 1);
                var world   = worlds[worldId];

                var bestIndividual = genomeDecoder.Decode(genome);
                var agent          = new NeuralNetworkAgent(createLidarFor(world.Track), bestIndividual);
                var simulation     = new Simulation.Simulation(agent, world);
                var summary        = simulation.Simulate(simulationStep, perceptionStep, maximumSimulationTime);

                File.Copy($"{circuits[worldId]}/visualization.svg", "C:/Users/simon/Projects/racer-experiment/simulator/src/visualization.svg", overwrite: true);
                IO.Simulation.StoreResult(world.Track, world.VehicleModel, summary, "", "C:/Users/simon/Projects/racer-experiment/simulator/src/report.json");
            }
        }
コード例 #18
0
        private static IGenomeDecoder <NeatGenome, IBlackBox> CreateGenomeDecoder_Finish(HyperNEAT_Args args, NetworkActivationScheme activationSchemeCppn, NetworkActivationScheme activationSchemeSubstrate)
        {
            int numInputs  = args.NeuronCount_Input;
            int numOutputs = args.NeuronCount_Output;

            SubstrateNodeSet inputLayer  = new SubstrateNodeSet(numInputs);
            SubstrateNodeSet outputLayer = new SubstrateNodeSet(numOutputs);

            //NOTE: Can't put a neuron at the origin, because the bias node is there - see Substrate.CreateNetworkDefinition()

            // Each node in each layer needs a unique ID
            // Node IDs start at 1. (bias node is always zero)
            // The input nodes use ID range { 1, inputSize^2 } and
            // the output nodes are the next outputSize^2.
            uint inputId  = 1;
            uint outputId = Convert.ToUInt32(numInputs + 1);

            for (int cntr = 0; cntr < args.InputPositions.Length; cntr++)
            {
                inputLayer.NodeList.Add(new SubstrateNode(inputId, args.InputPositions[cntr].ToArray()));
                inputId++;
            }

            for (int cntr = 0; cntr < args.OutputPositions.Length; cntr++)
            {
                outputLayer.NodeList.Add(new SubstrateNode(outputId, args.OutputPositions[cntr].ToArray()));
                outputId++;
            }

            List <SubstrateNodeSet> nodeSetList = new List <SubstrateNodeSet>
            {
                inputLayer,
                outputLayer
            };

            //TODO: The two samples that I copied from have the same number of inputs and outputs.  This mapping may be too simplistic when the counts are different
            //TODO: Notes on using hidden layers:
            //      The above example is a simple 2-layer CPPN with no hidden layers. If you want to add hidden layers, you simply need to create an additional SubstrateNodeSet
            //      and add it to the nodeSetList. Then you will need two NodeSetMappings, one from the input (0) to the hidden (1) layer, and one from the hidden (1) to the
            //      output (2) layer.3

            // Define a connection mapping from the input layer to the output layer.
            List <NodeSetMapping> nodeSetMappingList = new List <NodeSetMapping>();

            nodeSetMappingList.Add(NodeSetMapping.Create
                                   (
                                       //NOTE: Substrate is hardcoded to 0 and 1 for input and output.  2 and up is hidden nodes.  So passing these as params unnecessary
                                       0, // this is the index into nodeSetList (the item in nodeSetList that is the input layer)
                                       1, // index of nodeSetList that is the output layer

                                       (double?)null
                                   ));

            // Construct the substrate using a steepened sigmoid as the phenome's activation function. All weights
            // under .2 will not generate connections in the final phenome
            //Substrate substrate = new Substrate(nodeSetList, DefaultActivationFunctionLibrary.CreateLibraryCppn(), 0, .2, 5, nodeSetMappingList);
            Substrate substrate = new Substrate(nodeSetList, DefaultActivationFunctionLibrary.CreateLibraryNeat(SteepenedSigmoid.__DefaultInstance), 0, .2, 5, nodeSetMappingList);

            // Create genome decoder. Decodes to a neural network packaged with
            // an activation scheme that defines a fixed number of activations per evaluation.
            //IGenomeDecoder<NeatGenome, IBlackBox> genomeDecoder = new HyperNeatDecoder(substrate, _activationSchemeCppn, _activationScheme, false);
            return(new HyperNeatDecoder(substrate, activationSchemeCppn, activationSchemeSubstrate, true));
        }
コード例 #19
0
        /// <summary>
        /// Create a genome decoder for the experiment.
        /// </summary>
        protected override IGenomeDecoder <NeatGenome, IBlackBox> CreateHyperNeatGenomeDecoder()
        {
            // Create HyperNEAT network substrate.

            uint nodeId = 1;

            //-- Create input layer nodes.
            var inputLayer = new SubstrateNodeSet(samples.Length);

            for (int v = 0; v < samples.GetLength(0); v++) // variables
            {
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        var x = 2 * (double)i / n - 1;
                        var y = 2 * (double)j / m - 1;
                        var z = -(double)v / samples.GetLength(0);
                        inputLayer.NodeList.Add(new SubstrateNode(nodeId++, new double[] { x, y, z }));
                    }
                }
            }

            //-- Create output layer nodes.
            var outputLayers = new SubstrateNodeSet[nbClusters];

            for (var c = 0; c < nbClusters; c++) // clusters
            {
                outputLayers[c] = new SubstrateNodeSet(n * m);

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        var x = 2 * (double)i / n - 1;
                        var y = 2 * (double)j / m - 1;
                        var z = c / nbClusters;
                        outputLayers[c].NodeList.Add(new SubstrateNode(nodeId++, new double[] { x, y, z }));
                    }
                }
            }

            // Connect up layers.
            List <SubstrateNodeSet> nodeSetList = new List <SubstrateNodeSet>(1 + nbClusters);

            nodeSetList.Add(inputLayer);
            foreach (var layer in outputLayers)
            {
                nodeSetList.Add(layer);
            }


            List <NodeSetMapping> nodeSetMappingList = new List <NodeSetMapping>(nbClusters);

            for (int i = 0; i < nbClusters; i++)
            {
                var inputLayerIdx  = 0;
                var outputLayerIdx = 1 + i;
                nodeSetMappingList.Add(NodeSetMapping.Create(inputLayerIdx, outputLayerIdx, (double?)null));
            }

            // Construct substrate.
            Substrate substrate = new Substrate(nodeSetList,
                                                DefaultActivationFunctionLibrary.CreateLibraryNeat(SteepenedSigmoid.__DefaultInstance),
                                                0, 0.2, 5,
                                                nodeSetMappingList);

            // Create genome decoder. Decodes to a neural network packaged with an activation scheme that defines a fixed number of activations per evaluation.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new HyperNeatDecoder(substrate, _activationSchemeCppn, _activationScheme, _lengthCppnInput);

            return(genomeDecoder);
        }
        /// <summary>
        /// Creates a genome decoder. We split this code into a separate  method so that it can be re-used by the problem domain visualization code
        /// (it needs to decode genomes to phenomes in order to create a visualization).
        /// </summary>
        /// <param name="visualFieldResolution">The visual field's pixel resolution, e.g. 11 means 11x11 pixels.</param>
        /// <param name="lengthCppnInput">Indicates if the CPPNs being decoded have an extra input for specifying connection length.</param>
        public IGenomeDecoder <NeatGenome, IBlackBox> CreateGenomeDecoder(int visualFieldResolution, bool lengthCppnInput)
        {
            // Create two layer 'sandwich' substrate.
            int    pixelCount    = visualFieldResolution * visualFieldResolution;
            double pixelSize     = BoxesVisualDiscriminationEvaluator.VisualFieldEdgeLength / visualFieldResolution;
            double originPixelXY = -1 + (pixelSize / 2.0);

            SubstrateNodeSet inputLayer  = new SubstrateNodeSet(pixelCount);
            SubstrateNodeSet outputLayer = new SubstrateNodeSet(pixelCount);

            // Node IDs start at 1. (bias node is always zero).
            uint   inputId  = 1;
            uint   outputId = (uint)(pixelCount + 1);
            double yReal    = originPixelXY;

            for (int y = 0; y < visualFieldResolution; y++, yReal += pixelSize)
            {
                double xReal = originPixelXY;
                for (int x = 0; x < visualFieldResolution; x++, xReal += pixelSize, inputId++, outputId++)
                {
                    inputLayer.NodeList.Add(new SubstrateNode(inputId, new double[] { xReal, yReal, -1.0 }));
                    outputLayer.NodeList.Add(new SubstrateNode(outputId, new double[] { xReal, yReal, 1.0 }));
                }
            }

            /*
             * //////////////////////////////////////////////////////////////////////////////
             * // Create two layer substrate.
             * SubstrateNodeSet inputLayer = new SubstrateNodeSet(Constants.INPUT_SIZE);
             * SubstrateNodeSet outputLayer = new SubstrateNodeSet(Constants.FULLY_CONNECTED_SIZE);
             *
             * // Inputs to Outputs Mapping
             * //              1 1 2 2 3 3
             * // 1 2 3        1 1 2 2 3 3
             * // 4 5 6  --->  4 4 5 5 6 6
             * // 7 8 9        4 4 5 5 6 6
             * //              7 7 8 8 9 9
             * //              7 7 8 8 9 9
             *
             * for (uint width = 0; width < Constants.INPUT_WIDTH; width++)
             * {
             *  for (uint height = 0; height < Constants.INPUT_HEIGHT; height++)
             *  {
             *      // start + 1 because of bias node
             *      uint inputID = (width * Constants.INPUT_HEIGHT) + height + 1;
             *      inputLayer.NodeList.Add(new SubstrateNode(inputID, new double[] { inputID }));
             *
             *  }
             * }
             *
             * // we're fully connected so we will have NEXT_LAYER_SIZE * IMAGE_SIZE weights. This is a lot. yes.
             * for (uint height = 0; height < Constants.OUTPUT_SIZE; height++)
             * {
             *  for (uint width = 0; width < Constants.INPUT_SIZE; width++)
             *  {
             *      uint outputID = Constants.INPUT_SIZE + (height * Constants.INPUT_SIZE) + width + 1;
             *
             *      outputLayer.NodeList.Add(new SubstrateNode(outputID, new double[] { outputID }));
             *  }
             * }
             * ///////////////////////////////////////////////////////////////////////////
             */
            List <SubstrateNodeSet> nodeSetList = new List <SubstrateNodeSet>(2);

            nodeSetList.Add(inputLayer);
            nodeSetList.Add(outputLayer);

            // Define connection mappings between layers/sets.
            List <NodeSetMapping> nodeSetMappingList = new List <NodeSetMapping>(1);

            nodeSetMappingList.Add(NodeSetMapping.Create(0, 1, (double?)null));

            // Construct substrate.
            //Substrate substrate = new Substrate(nodeSetList, DefaultActivationFunctionLibrary.CreateLibraryNeat(SteepenedSigmoid.__DefaultInstance), 0, 0.2, 5, nodeSetMappingList);
            Substrate substrate = new Substrate(nodeSetList, DefaultActivationFunctionLibrary.CreateLibraryCppn(), 0, 0.2, 5, nodeSetMappingList);

            // Create genome decoder. Decodes to a neural network packaged with an activation scheme that defines a fixed number of activations per evaluation.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new HyperNeatDecoder(substrate, _activationSchemeCppn, _activationScheme, lengthCppnInput);

            return(genomeDecoder);
        }
        /// <summary>
        /// Create a genome decoder for the experiment.
        /// </summary>
        protected override IGenomeDecoder <NeatGenome, IBlackBox> CreateHyperNeatGenomeDecoder()
        {
            // Create HyperNEAT network substrate.

            uint nodeId = 1;

            //-- Create input layer nodes.
            var inputLayer = new SubstrateNodeSet(nbInputs * f2);

            for (var i = 0; i < nbInputs; i++)
            {
                for (var j = 0; j < f; j++)
                {
                    for (var k = 0; k < f; k++)
                    {
                        if (filter[j, k])
                        {
                            var x = (double)j / f - 0.5;
                            var y = (double)k / f - 0.5;
                            inputLayer.NodeList.Add(new SubstrateNode(nodeId++, new double[] { x, y, -1 - i }));
                        }
                    }
                }
            }

            //-- Create hidden layer nodes.
            var hiddenLayers = new SubstrateNodeSet[nbClusters];

            for (var i = 0; i < nbClusters; i++)
            {
                hiddenLayers[i] = new SubstrateNodeSet(f2);

                for (var j = 0; j < f; j++)
                {
                    for (var k = 0; k < f; k++)
                    {
                        if (filter[j, k])
                        {
                            var x = (double)j / f - 0.5;
                            var y = (double)k / f - 0.5;
                            hiddenLayers[i].NodeList.Add(new SubstrateNode(nodeId++, new double[] { x, y, 0 }));
                        }
                    }
                }
            }

            //-- Create output layer nodes.
            var outputLayers = new SubstrateNodeSet[nbClusters];

            for (var i = 0; i < nbClusters; i++)
            {
                outputLayers[i] = new SubstrateNodeSet(f2);

                for (var j = 0; j < f; j++)
                {
                    for (var k = 0; k < f; k++)
                    {
                        if (filter[j, k])
                        {
                            var x = (double)j / f - 0.5;
                            var y = (double)j / f - 0.5;
                            outputLayers[i].NodeList.Add(new SubstrateNode(nodeId++, new double[] { x, y, +1 + i }));
                        }
                    }
                }
            }

            // Connect up layers.
            List <SubstrateNodeSet> nodeSetList = new List <SubstrateNodeSet>(1 + 2 * nbClusters);

            nodeSetList.Add(inputLayer);
            foreach (var layer in hiddenLayers)
            {
                nodeSetList.Add(layer);
            }
            foreach (var layer in outputLayers)
            {
                nodeSetList.Add(layer);
            }


            List <NodeSetMapping> nodeSetMappingList = new List <NodeSetMapping>(2 * nbClusters);

            for (int i = 0; i < nbClusters; i++)
            {
                var inputLayerIdx  = 0;
                var hiddenLayerIdx = 1 + i;
                var outputLayerIdx = 1 + nbClusters + i;
                nodeSetMappingList.Add(NodeSetMapping.Create(inputLayerIdx, hiddenLayerIdx, (double?)null));
                nodeSetMappingList.Add(NodeSetMapping.Create(hiddenLayerIdx, outputLayerIdx, (double?)null));
            }

            // Construct substrate.
            Substrate substrate = new Substrate(nodeSetList,
                                                DefaultActivationFunctionLibrary.CreateLibraryNeat(SteepenedSigmoid.__DefaultInstance),
                                                0, 0.2, 5,
                                                nodeSetMappingList);

            // Create genome decoder. Decodes to a neural network packaged with an activation scheme that defines a fixed number of activations per evaluation.
            IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = new HyperNeatDecoder(substrate, _activationSchemeCppn, _activationScheme, _lengthCppnInput);

            return(genomeDecoder);
        }