public INeuralNetwork Create(NeuralNetworkGene genes)
        {
            var network = _decoratedFactory.Create(genes);

            SetUpSynapseMappingForPropogation(network);
            return(network);
        }
Exemplo n.º 2
0
        internal INeuralNetwork mate(INeuralNetwork mother, INeuralNetwork father)
        {
            NeuralNetworkGene motherGenes      = mother.GetGenes();
            NeuralNetworkGene childFatherGenes = father.GetGenes();
            Random            random           = new Random();

            for (int n = 0; n < childFatherGenes.InputGene.Neurons.Count; n++)
            {
                var neuron       = childFatherGenes.InputGene.Neurons[n];
                var motherNeuron = motherGenes.InputGene.Neurons[n];
                childFatherGenes.InputGene.Neurons[n] = BreedNeuron(neuron, motherNeuron, random);
            }

            if (childFatherGenes.HiddenGenes.Count >= motherGenes.HiddenGenes.Count)
            {
                childFatherGenes.HiddenGenes = MateHiddenLayers(childFatherGenes.HiddenGenes, motherGenes.HiddenGenes, random);
            }
            else
            {
                childFatherGenes.HiddenGenes = MateHiddenLayers(motherGenes.HiddenGenes, childFatherGenes.HiddenGenes, random);
            }

            for (int n = 0; n < childFatherGenes.OutputGene.Neurons.Count; n++)
            {
                var neuron       = childFatherGenes.OutputGene.Neurons[n];
                var motherNeuron = motherGenes.OutputGene.Neurons[n];
                childFatherGenes.OutputGene.Neurons[n] = BreedNeuron(neuron, motherNeuron, random);
            }

            INeuralNetwork child = _networkFactory.Create(childFatherGenes);

            return(child);
        }
Exemplo n.º 3
0
        public INeuralNetwork Mutate(INeuralNetwork network, double mutateChance, out bool didMutate)
        {
            NeuralNetworkGene childGenes = network.GetGenes();
            bool mutated;

            didMutate = false;
            if (_config.MutateNumberOfHiddenLayers)
            {
                childGenes = TryAddLayerToNetwork(childGenes, mutateChance, out mutated);
                didMutate  = mutated;
            }
            for (int n = 0; n < childGenes.InputGene.Neurons.Count; n++)
            {
                var neuron = childGenes.InputGene.Neurons[n];
                childGenes.InputGene.Neurons[n] = TryMutateNeuron(neuron, mutateChance, out mutated);;
                didMutate = didMutate || mutated;
            }

            for (int h = 0; h < childGenes.HiddenGenes.Count; h++)
            {
                if (_config.MutateNumberOfHiddenNeuronsInLayer)
                {
                    childGenes.HiddenGenes[h] = TryAddNeuronsToLayer(childGenes, h, mutateChance, out mutated);
                    didMutate = didMutate || mutated;
                }

                for (int j = 0; j < childGenes.HiddenGenes[h].Neurons.Count; j++)
                {
                    var neuron = childGenes.HiddenGenes[h].Neurons[j];
                    childGenes.HiddenGenes[h].Neurons[j] = TryMutateNeuron(neuron, mutateChance, out mutated);
                    didMutate = didMutate || mutated;
                }
            }
            return(_networkFactory.Create(childGenes));
        }
Exemplo n.º 4
0
 internal LayerGene GetNextLayerGene(NeuralNetworkGene genes, int hiddenLayerIndex)
 {
     if (hiddenLayerIndex == genes.HiddenGenes.Count - 1)
     {
         return(genes.OutputGene);
     }
     else
     {
         return(genes.HiddenGenes[hiddenLayerIndex + 1]);
     }
 }
Exemplo n.º 5
0
 internal LayerGene GetPreviousLayerGene(NeuralNetworkGene genes, int hiddenLayerIndex)
 {
     if (hiddenLayerIndex == 0)
     {
         return(genes.InputGene);
     }
     else
     {
         return(genes.HiddenGenes[hiddenLayerIndex - 1]);
     }
 }
Exemplo n.º 6
0
        internal LayerGene TryAddNeuronsToLayer(NeuralNetworkGene networkGenes, int hiddenLayerIndex, double mutateChance, out bool didMutate)
        {
            LayerGene hiddenLayer = networkGenes.HiddenGenes[hiddenLayerIndex];

            didMutate = false;
            while (_random.NextDouble() <= mutateChance)
            {
                didMutate = true;
                //update layer-1 axon terminals
                LayerGene previousLayer = GetPreviousLayerGene(networkGenes, hiddenLayerIndex);
                foreach (NeuronGene neuron in previousLayer.Neurons)
                {
                    neuron.Axon.Weights.Add(_weightInitializer.InitializeWeight());
                }

                hiddenLayer.Neurons.Add(GetRandomHiddenNeuronGene(networkGenes, hiddenLayerIndex));
            }
            return(hiddenLayer);
        }
Exemplo n.º 7
0
        public INeuralNetwork Create(NeuralNetworkGene genes)
        {
            var mapping = CreateSynapsesFromGenes(genes);

            var inputs  = GetAllSynapsesFromLayerMapping(mapping[0]);
            var outputs = GetAllSynapsesFromLayerMapping(mapping[mapping.Keys.Count - 1]);

            ILayer inputLayer = CreateLayerFromGene(genes.InputGene, mapping, 0);

            //Hidden layers
            IList <ILayer> hiddenLayers = new List <ILayer>();

            for (int h = 0; h < genes.HiddenGenes.Count; h++)
            {
                hiddenLayers.Add(CreateLayerFromGene(genes.HiddenGenes[h], mapping, h + 1));
            }

            ILayer outputLayer = CreateLayerFromGene(genes.OutputGene, mapping, genes.HiddenGenes.Count + 1);

            return(NeuralNetwork.GetInstance(inputs, inputLayer, hiddenLayers, outputLayer, outputs));
        }
Exemplo n.º 8
0
        internal int DetermineNumberOfHiddenNeuronsInLayer(NeuralNetworkGene networkGenes, double mutateChance)
        {
            int  hiddenLayerSize = networkGenes.InputGene.Neurons.Count;
            bool increase        = !(_random.NextDouble() <= 0.5);

            while (_random.NextDouble() <= mutateChance)
            {
                if (!increase && hiddenLayerSize == 1)
                {
                    break;
                }
                else if (!increase)
                {
                    hiddenLayerSize--;
                }
                else
                {
                    hiddenLayerSize++;
                }
            }
            return(hiddenLayerSize);
        }
Exemplo n.º 9
0
        internal NeuralNetworkGene TryAddLayerToNetwork(NeuralNetworkGene genes, double mutateChance, out bool didMutate)
        {
            NeuralNetworkGene newGenes = genes;

            didMutate = false;
            while (_random.NextDouble() <= mutateChance)
            {
                didMutate = true;
                int layerToReplace  = _random.Next(newGenes.HiddenGenes.Count);
                int hiddenLayerSize = DetermineNumberOfHiddenNeuronsInLayer(genes, mutateChance);

                //update layer-1 axon terminals
                LayerGene previousLayer = GetPreviousLayerGene(newGenes, layerToReplace);
                foreach (NeuronGene neuron in previousLayer.Neurons)
                {
                    neuron.Axon.Weights.Clear();
                    for (int i = 0; i < hiddenLayerSize; i++)
                    {
                        neuron.Axon.Weights.Add(_weightInitializer.InitializeWeight());
                    }
                }

                LayerGene newLayer = new LayerGene
                {
                    Neurons = new List <NeuronGene>()
                };
                newGenes.HiddenGenes.Insert(layerToReplace, newLayer);

                for (int i = 0; i < hiddenLayerSize; i++)
                {
                    var newNeuron = GetRandomHiddenNeuronGene(newGenes, layerToReplace);
                    newGenes.HiddenGenes[layerToReplace].Neurons.Add(newNeuron);
                }
            }
            return(newGenes);
        }
Exemplo n.º 10
0
        internal NeuronGene GetRandomHiddenNeuronGene(NeuralNetworkGene networkGenes, int hiddenLayerIndex)
        {
            var neuronGene = new NeuronGene
            {
                Axon = new AxonGene
                {
                    Weights            = new List <double>(),
                    ActivationFunction = GetRandomActivationFunction().GetType()
                },
                Soma = new SomaGene
                {
                    Bias = _weightInitializer.InitializeWeight(),
                    SummationFunction = GetRandomSummationFunction().GetType()
                }
            };
            //update terminals for current neuron
            LayerGene nextlayer = GetNextLayerGene(networkGenes, hiddenLayerIndex);

            for (int i = 0; i < nextlayer.Neurons.Count; i++)
            {
                neuronGene.Axon.Weights.Add(_weightInitializer.InitializeWeight());
            }
            return(neuronGene);
        }
Exemplo n.º 11
0
 internal LayerGene GetPreviousLayerGene(NeuralNetworkGene genes, int hiddenLayerIndex)
 {
     if (hiddenLayerIndex == 0)
     {
         return genes.InputGene;
     }
     else
     {
         return genes.HiddenGenes[hiddenLayerIndex - 1];
     }
 }
Exemplo n.º 12
0
        internal Dictionary <int, Dictionary <int, IList <Synapse> > > CreateSynapsesFromGenes(NeuralNetworkGene genes)
        {
            //layer number + position in layer --> list of terminals
            var mapping = new Dictionary <int, Dictionary <int, IList <Synapse> > >();

            //Input synapses
            mapping[0] = CreateSynapseMapLayer(_inputOutputSynapseFactory, genes.InputGene.Neurons.Count, 1);

            //Input neuron terminals
            mapping[1] = CreateSynapseMapLayerFromLayerGene(_inputOutputSynapseFactory, genes.InputGene);

            //Hidden layers
            for (int h = 0; h < genes.HiddenGenes.Count; h++)
            {
                mapping[h + 2] = CreateSynapseMapLayerFromLayerGene(_hiddenSynapseFactory, genes.HiddenGenes[h]);
            }

            //Output layer
            mapping[genes.HiddenGenes.Count + 2] = CreateSynapseMapLayer(_hiddenSynapseFactory, genes.OutputGene.Neurons.Count, 1);

            return(mapping);
        }
Exemplo n.º 13
0
        public INeuralNetwork Create(NeuralNetworkGene genes)
        {
            var mapping = CreateSynapsesFromGenes(genes);

            var inputs = GetAllSynapsesFromLayerMapping(mapping[0]);
            var outputs = GetAllSynapsesFromLayerMapping(mapping[mapping.Keys.Count - 1]);

            ILayer inputLayer = CreateLayerFromGene(genes.InputGene, mapping, 0);

            //Hidden layers
            IList<ILayer> hiddenLayers = new List<ILayer>();
            for (int h = 0; h < genes.HiddenGenes.Count; h++)
            {
                hiddenLayers.Add(CreateLayerFromGene(genes.HiddenGenes[h], mapping, h + 1));
            }

            ILayer outputLayer = CreateLayerFromGene(genes.OutputGene, mapping, genes.HiddenGenes.Count + 1);

            return NeuralNetwork.GetInstance(inputs, inputLayer, hiddenLayers, outputLayer, outputs);
        }
Exemplo n.º 14
0
        internal Dictionary<int, Dictionary<int, IList<Synapse>>> CreateSynapsesFromGenes(NeuralNetworkGene genes)
        {
            //layer number + position in layer --> list of terminals
            var mapping = new Dictionary<int, Dictionary<int, IList<Synapse>>>();

            //Input synapses
            mapping[0] = CreateSynapseMapLayer(_inputOutputSynapseFactory, genes.InputGene.Neurons.Count, 1);

            //Input neuron terminals
            mapping[1] = CreateSynapseMapLayerFromLayerGene(_inputOutputSynapseFactory, genes.InputGene);

            //Hidden layers
            for (int h = 0; h < genes.HiddenGenes.Count; h++)
            {
                mapping[h + 2] = CreateSynapseMapLayerFromLayerGene(_hiddenSynapseFactory, genes.HiddenGenes[h]);
            }

            //Output layer
            mapping[genes.HiddenGenes.Count + 2] = CreateSynapseMapLayer(_hiddenSynapseFactory, genes.OutputGene.Neurons.Count, 1);

            return mapping;
        }
Exemplo n.º 15
0
        internal NeuralNetworkGene TryAddLayerToNetwork(NeuralNetworkGene genes, double mutateChance, out bool didMutate)
        {
            NeuralNetworkGene newGenes = genes;
            didMutate = false;
            while (_random.NextDouble() <= mutateChance)
            {
                didMutate = true;
                int layerToReplace = _random.Next(newGenes.HiddenGenes.Count);
                int hiddenLayerSize = DetermineNumberOfHiddenNeuronsInLayer(genes, mutateChance);

                //update layer-1 axon terminals
                LayerGene previousLayer = GetPreviousLayerGene(newGenes, layerToReplace);
                foreach (NeuronGene neuron in previousLayer.Neurons)
                {
                    neuron.Axon.Weights.Clear();
                    for (int i = 0; i < hiddenLayerSize; i++)
                    {
                        neuron.Axon.Weights.Add(_weightInitializer.InitializeWeight());
                    }
                }

                LayerGene newLayer = new LayerGene
                {
                    Neurons = new List<NeuronGene>()
                };
                newGenes.HiddenGenes.Insert(layerToReplace, newLayer);

                for (int i = 0; i < hiddenLayerSize; i++)
                {
                    var newNeuron = GetRandomHiddenNeuronGene(newGenes, layerToReplace);
                    newGenes.HiddenGenes[layerToReplace].Neurons.Add(newNeuron);
                }
            }
            return newGenes;
        }
Exemplo n.º 16
0
 internal NeuronGene GetRandomHiddenNeuronGene(NeuralNetworkGene networkGenes, int hiddenLayerIndex)
 {
     var neuronGene = new NeuronGene
     {
         Axon = new AxonGene
         {
             Weights = new List<double>(),
             ActivationFunction = GetRandomActivationFunction().GetType()
         },
         Soma = new SomaGene
         {
             Bias = _weightInitializer.InitializeWeight(),
             SummationFunction = GetRandomSummationFunction().GetType()
         }
     };
     //update terminals for current neuron
     LayerGene nextlayer = GetNextLayerGene(networkGenes, hiddenLayerIndex);
     for (int i = 0; i < nextlayer.Neurons.Count; i++)
     {
         neuronGene.Axon.Weights.Add(_weightInitializer.InitializeWeight());
     }
     return neuronGene;
 }
Exemplo n.º 17
0
 internal LayerGene GetNextLayerGene(NeuralNetworkGene genes, int hiddenLayerIndex)
 {
     if (hiddenLayerIndex == genes.HiddenGenes.Count - 1)
     {
         return genes.OutputGene;
     }
     else
     {
         return genes.HiddenGenes[hiddenLayerIndex + 1];
     }
 }
Exemplo n.º 18
0
 internal int DetermineNumberOfHiddenNeuronsInLayer(NeuralNetworkGene networkGenes, double mutateChance)
 {
     int hiddenLayerSize = networkGenes.InputGene.Neurons.Count;
     bool increase = !(_random.NextDouble() <= 0.5);
     while (_random.NextDouble() <= mutateChance)
     {
         if (!increase && hiddenLayerSize == 1)
         {
             break;
         }
         else if (!increase)
         {
             hiddenLayerSize--;
         }
         else
         {
             hiddenLayerSize++;
         }
     }
     return hiddenLayerSize;
 }
Exemplo n.º 19
0
        internal LayerGene TryAddNeuronsToLayer(NeuralNetworkGene networkGenes, int hiddenLayerIndex, double mutateChance, out bool didMutate)
        {
            LayerGene hiddenLayer = networkGenes.HiddenGenes[hiddenLayerIndex];
            didMutate = false;
            while (_random.NextDouble() <= mutateChance)
            {
                didMutate = true;
                //update layer-1 axon terminals
                LayerGene previousLayer = GetPreviousLayerGene(networkGenes, hiddenLayerIndex);
                foreach (NeuronGene neuron in previousLayer.Neurons)
                {
                    neuron.Axon.Weights.Add(_weightInitializer.InitializeWeight());
                }

                hiddenLayer.Neurons.Add(GetRandomHiddenNeuronGene(networkGenes, hiddenLayerIndex));
            }
            return hiddenLayer;
        }