social_learning.SocialExperiment.CreateNetwork C# (CSharp) Метод

CreateNetwork() публичный статический Метод

public static CreateNetwork ( string filename ) : void
filename string
Результат void
        public static void CreateNetwork(string filename, params int[] layers)
        {
            Debug.Assert(layers.Length >= 2);
            Debug.Assert(layers.Min() > 0);

            const string inputNodeFormat = @"        <Node type=""in"" id=""{0}"" />";
            const string outputNodeFormat = @"        <Node type=""out"" id=""{0}"" />";
            const string hiddenNodeFormat = @"        <Node type=""hid"" id=""{0}"" />";
            int nextId = 1;

            StringBuilder nodes = new StringBuilder();

            int[][] layerIds = new int[layers.Length][];
            for (int i = 0; i < layers.Length; i++)
                layerIds[i] = new int[layers[i]];

            // Add the input neurons
            for (int i = 0; i < layers[0]; i++)
            {
                int id = nextId++;
                nodes.AppendLine(string.Format(inputNodeFormat, id));
                layerIds[0][i] = id;
            }

            // Add the output neurons (have to do this now because of conventions in SharpNEAT)
            for (int i = 0; i < layers.Last(); i++)
            {
                int id = nextId++;
                nodes.AppendLine(string.Format(outputNodeFormat, id));
                layerIds[layers.Length - 1][i] = id;
            }

            // Add the hidden neurons
            for (int i = 1; i < layers.Length - 1; i++)
                for (int j = 0; j < layers[i]; j++)
                {
                    int id = nextId++;
                    nodes.AppendLine(string.Format(hiddenNodeFormat, id));
                    layerIds[i][j] = id;
                }

            const string connectionFormat = @"        <Con id=""{0}"" src=""{1}"" tgt=""{2}"" wght=""{3}"" />";
            StringBuilder connections = new StringBuilder();

            // Add the bias connections to all non-input nodes
            int lastInput = nextId;
            for (int i = layers[0] + 1; i < lastInput; i++)
                connections.AppendLine(string.Format(connectionFormat, nextId++, 0, i, _random.NextDouble() * 10.0 - 5.0));

            // Add the connections
            // Note that you need to randomize these connections externally
            for (int i = 1; i < layers.Length; i++)
                for (int j = 0; j < layers[i]; j++)
                    for (int k = 0; k < layers[i - 1]; k++)
                        connections.AppendLine(string.Format(connectionFormat, nextId++, layerIds[i - 1][k], layerIds[i][j], _random.NextDouble() * 10.0 - 5.0));

            using (TextWriter writer = new StreamWriter(filename))
                writer.WriteLine(string.Format(NETWORK_FORMAT, nodes.ToString(), connections.ToString(), nextId++));

        }

Usage Example

Пример #1
0
        private void GenomesToAcceptability(IList <TGenome> genomeList)
        {
            string TEMP_NETWORK_FILE = string.Format("____temp{0}____network.xml", TrialId);

            var neatGenomeParams = new NeatGenomeParameters()
            {
                ActivationFn = PlainSigmoid.__DefaultInstance,
                InitialInterconnectionsProportion = 1
            };

            int inputs = _world.PlantTypes.Count() * World.SENSORS_PER_OBJECT_TYPE
                         + _world.Predators.Count() * World.SENSORS_PER_OBJECT_TYPE
                         + 1;
            int outputs = 2;

            var factory = new NeatGenomeFactory(inputs, outputs, neatGenomeParams);

            for (int i = 0; i < _agents.Length; i++)
            {
                // Decode the genome.
                IBlackBox phenome             = _genomeDecoder.Decode(genomeList[i]);
                IAcceptabilityFunction accept = new RecurrentNeuralAcceptability(phenome);

                // Check that the genome is valid.
                if (phenome == null)
                {
                    Console.WriteLine("Couldn't decode genome {0}!", i);
                    _agents[i] = new SpinningAgent(i);
                    continue;
                }

                // Create a feed forward network with 10 hidden nodes and random weights
                SocialExperiment.CreateNetwork(TEMP_NETWORK_FILE, inputs, outputs);
                using (var xr = XmlReader.Create(TEMP_NETWORK_FILE))
                {
                    var controllerGenome  = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, factory)[0];
                    var controllerPhenome = _genomeDecoder.Decode((TGenome)controllerGenome);
                    _agents[i] = new SocialAgent(i, _genomeList[i].SpecieIdx, controllerPhenome, _agentsNavigate, _agentsHide, accept)
                    {
                        MemorySize = CurrentMemorySize
                    };
                    var network = (FastCyclicNetwork)controllerPhenome;
                    network.Momentum             = ((SocialAgent)_agents[i]).Momentum;
                    network.BackpropLearningRate = ((SocialAgent)_agents[i]).LearningRate;
                }
            }

            File.Delete(TEMP_NETWORK_FILE);
        }