Exemple #1
0
        public double[] Train(double[] inputs, double trainSpeed = 0.05)
        {
            if (inputs.Length != InputNeurons.Count)
            {
                throw new ArgumentException();
            }
            for (int i = 0; i < InputNeurons.Count; i++)
            {
                InputNeurons[i].Value = inputs[i];
            }
            Parallel.ForEach(OutputNeurons, x => x.Calculate());
            var winner =
                OutputNeurons.Select(
                    x =>
                    new
            {
                Neuron = x,
                Value  =
                    x.Dendrites.Sum(item => Math.Abs(item.Neuron.Value - item.Weight)) *
                    (Wins.ContainsKey(x) ? Wins[x] : 1)
            })
                .OrderBy(x => x.Value)
                .First();

            Wins[winner.Neuron] = Wins.ContainsKey(winner.Neuron) ? Wins[winner.Neuron] + 1 : 2;

            foreach (var dendrite in winner.Neuron.Dendrites)
            {
                dendrite.Weight = dendrite.Weight + trainSpeed * (dendrite.Neuron.Value - dendrite.Weight);
            }

            return(OutputNeurons.Select(x => x.Value).ToArray());
        }
Exemple #2
0
 public void AddOutputNeuronAndMesh(WorkingNeuron neuron)
 {
     OutputNeurons.Add(neuron);
     foreach (WorkingNeuron wn in LastHiddenLayer)
     {
         neuron.AddNeuronConnection(new Connection(wn, 0));
     }
 }
Exemple #3
0
        public Genome(Genome target, List <Gene> otherGenes = null)
        {
            SpeciesId = target.SpeciesId;
            Fitness   = target.Fitness;
            CreateNetwork(target.InputCount, target.OutputCount, otherGenes ?? target.Genes);

            Assert.AreEqual(InputNeurons.Count(), target.InputCount);
            Assert.AreEqual(OutputNeurons.Count(), target.OutputCount);
        }
Exemple #4
0
        public IEnumerable <double> CalculateOutputs()
        {
            foreach (var cachedNeuron in AllNeurons.OfType <ACachedNeuron>())
            {
                cachedNeuron.ResetCache();
            }

            return(OutputNeurons.Select(o => o.Value));
        }
Exemple #5
0
        /// <summary>
        /// Saves the neural network as a .ann file.
        /// </summary>
        /// <param name="name">The .ann file name.</param>
        /// <returns>The .ann file.</returns>
        public AnnFile Save(string name)
        {
            var map = ComputeNodeIdMap();

            return(new AnnFile
            {
                Name = name,
                Activation = Activation,
                InputNeurons = InputNeurons.Select(neuron => (uint)map[neuron.ID]).ToList(),
                OutputNeurons = OutputNeurons.Select(neuron => (uint)map[neuron.ID]).ToList(),
                AdjacencyMatrix = AdjacencyMatrix
            });
        }
Exemple #6
0
 public double[] Process(double[] inputs)
 {
     if (inputs.Length != InputNeurons.Count)
     {
         throw new ArgumentException();
     }
     for (int i = 0; i < InputNeurons.Count; i++)
     {
         InputNeurons[i].Value = inputs[i];
     }
     foreach (var outputNeuron in OutputNeurons)
     {
         outputNeuron.Calculate();
     }
     return(OutputNeurons.Select(x => x.Value).ToArray());
 }
Exemple #7
0
        public Genome(int inputCount, int outputCount, float geneWeightRandomVal = 0)
        {
            if (inputCount < 1 || outputCount < 1)
            {
                throw new System.ArgumentException("Invalid initialization", "inputCount or outputCount");
            }

            SpeciesId = null;
            Fitness   = 0;

            InitStartingNeurons(inputCount, outputCount);
            InitStartingGenes(geneWeightRandomVal);

            Assert.AreEqual(InputNeurons.Count(), inputCount);
            Assert.AreEqual(OutputNeurons.Count(), outputCount);
            Assert.AreEqual(Neurons.Count, inputCount + outputCount + 1);
            Assert.AreEqual(Genes.Count, inputCount * outputCount);
        }
Exemple #8
0
        /// <summary>
        /// Create a network from the given genes without any prerequesties.
        /// </summary>
        /// <param name="targetGenes"></param>
        private void CreateNetwork(int inputCount, int outputCount, IEnumerable <Gene> targetGenes)
        {
            Neurons = new List <Neuron>();
            Genes   = new List <Gene>(targetGenes.Count());

            AddStartingNeurons(inputCount, outputCount);
            foreach (var gene in targetGenes)
            {
                Genes.Add(ForceConnection(gene));
            }

            InputNeurons  = Neurons.Where(x => x.Type == ENeuronType.Input);
            OutputNeurons = Neurons.Where(x => x.Type == ENeuronType.Output);

            Debug.Assert(Genes.Count == targetGenes.Count());
            Debug.Assert(InputNeurons.Count() == inputCount);
            Debug.Assert(OutputNeurons.Count() == outputCount);
        }
Exemple #9
0
        /// <summary>
        /// Initialize the network with the starting genes.
        /// All inputs, but the bias, will be connected to output neurons.
        /// </summary>
        private void InitStartingGenes(float geneWeightRandomVal)
        {
            Genes = new List <Gene>(InputCount * OutputCount);
            var inputNeurons  = InputNeurons.ToArray();
            var outputNeurons = OutputNeurons.ToArray();

            for (int i = 0; i < InputCount; i++)
            {
                for (int j = 0; j < OutputCount; j++)
                {
                    Gene newGene = new Gene(
                        inputNeurons[i],
                        outputNeurons[j],
                        Random.Range(-geneWeightRandomVal, geneWeightRandomVal)
                        );

                    Genes.Add(newGene);
                    inputNeurons[i].OutGenes.Add(newGene);
                    outputNeurons[j].InGenes.Add(newGene);
                }
            }
        }
Exemple #10
0
        private void Initialize(int inputs, int outputs)
        {
            var random = new Random();

            for (int i = 0; i < inputs; i++)
            {
                InputNeurons.Add(new Neuron());
            }
            for (int i = 0; i < outputs; i++)
            {
                var dentrites = new List <Synapse>();
                foreach (var input in InputNeurons)
                {
                    dentrites.Add(new Synapse()
                    {
                        Neuron = input, Weight = (random.NextDouble() - 0.5) * 0.01
                    });
                }
                OutputNeurons.Add(new Neuron()
                {
                    Dendrites = dentrites
                });
            }
        }
Exemple #11
0
 public double[] GetOutputValues(int cycle)
 {
     return(OutputNeurons.Select(x => x.Values[cycle] / Divisor).ToArray());
 }
Exemple #12
0
 public void RemoveOutputNeuron(WorkingNeuron neuron)
 {
     OutputNeurons.Remove(neuron);
 }
Exemple #13
0
 public void AddOutputNeuron(Neuron neuron)
 {
     OutputNeurons.Add(neuron);
 }