コード例 #1
0
    private void CreateDendrites(NeuralChromosome chromosome)
    {
        foreach (DendriteGene dendrite in chromosome.dendriteGenes)
        {
            List <Neuron> startingNeurons = new List <Neuron>(); //neurons that will be added as input
            List <Neuron> endNeurons      = new List <Neuron>(); //neurons that require input

            if (inputNeurons.ContainsKey(dendrite.startNeuron))
            {
                startingNeurons.AddRange(inputNeurons[dendrite.startNeuron]);
            }
            if (hiddenNeurons.ContainsKey(dendrite.startNeuron))
            {
                startingNeurons.AddRange(hiddenNeurons[dendrite.startNeuron]);
            }

            if (outputNeurons.ContainsKey(dendrite.endNeuron))
            {
                endNeurons.AddRange(outputNeurons[dendrite.endNeuron]);
            }
            if (hiddenNeurons.ContainsKey(dendrite.endNeuron))
            {
                endNeurons.AddRange(hiddenNeurons[dendrite.endNeuron]); //hidden neurons can be both start and end
            }


            foreach (Neuron neuron in endNeurons)
            {
                neuron.AddInput(startingNeurons, dendrite.weight);//add all starting neurons as input
            }
        }
    }
コード例 #2
0
    private static NeuralChromosome Elimination(NeuralChromosome chromosome)
    {
        if (chromosome.dendriteGenes.Length == 0)
        {
            return(chromosome);
        }


        int pos = r.Next(chromosome.dendriteGenes.Length);

        DendriteGene[] dGenes = new DendriteGene[chromosome.dendriteGenes.Length - 1];

        for (int i = 0; i < pos; i++)
        {
            dGenes[i] = chromosome.dendriteGenes[i];
        }

        for (int i = pos; i < dGenes.Length; i++)
        {
            dGenes[i] = chromosome.dendriteGenes[i + 1];
        }


        return(new NeuralChromosome(dGenes));
    }
コード例 #3
0
 public Chromosome(Gene[] genes, NeuralChromosome neuralChromosome, string mutationTracker, ChromosomeParameters parameters)
 {
     this.Genes            = new List <Gene>(genes).ToArray();
     this.MutationTracker  = mutationTracker;
     this.neuralChromosome = neuralChromosome;
     this.Parameters       = parameters;
 }
コード例 #4
0
    private static NeuralChromosome InsertNeuron(NeuralChromosome chromosome)
    {
        List <int> numbers = chromosome.GetNeuronsNumbers();

        int newNumber;

        if (numbers.Any())
        {
            newNumber = numbers.Max() + 1;
        }
        else
        {
            newNumber = 1;
        }

        //substitute a existing dendrite
        int          pos      = r.Next(chromosome.dendriteGenes.Length);
        DendriteGene dendrite = chromosome.dendriteGenes[pos];

        DendriteGene[] dendriteGenes = new DendriteGene[chromosome.dendriteGenes.Length + 1];
        for (int i = 0; i < chromosome.dendriteGenes.Length; i++)
        {
            dendriteGenes[i] = chromosome.dendriteGenes[i];
        }


        dendriteGenes[chromosome.dendriteGenes.Length] = new DendriteGene(newNumber, dendriteGenes[pos].endNeuron, dendriteGenes[pos].weight);

        dendriteGenes[pos] = new DendriteGene(dendrite.startNeuron, newNumber, 1f);



        return(new NeuralChromosome(dendriteGenes));
    }
コード例 #5
0
        /// <summary>
        /// Runs the evolution process.
        /// </summary>
        /// <param name="evolveSpecies">A delegate that evolves the species subpopulations.</param>
        private void RunEvolution(Action evolveSpecies)
        {
            NeuralChromosome.ResetAllInnovationTrackers();

            while (true)
            {
                evolveSpecies();
                _optimalSpecies = FindOptimalSpecies();
                _generation++;

                if (_loggingEnabled)
                {
                    _logger.Log(_generation, ((Creature)OptimalCreature).TrueFitness, OptimalCreature.Fitness, _optimalSpecies.Niches);
                }

                if (_populationMetadata.TerminationConditions.Any(condition => condition.Check(this)))
                {
                    foreach (var species in _species)
                    {
                        foreach (var creature in species)
                        {
                            ((Creature)creature).UnregisterNiche();
                        }
                    }
                    break;
                }
            }
        }
コード例 #6
0
    private static NeuralChromosome WeightModification(NeuralChromosome chromosome)
    {
        if (chromosome.dendriteGenes.Length == 0)
        {
            return(chromosome);
        }

        return(ModifyWeights(chromosome));
    }
コード例 #7
0
    /// <summary>
    /// Creates the neural network by linking the hidden neurons (as described in the chromosome )to the input and output layer
    /// </summary>
    /// <param name="chromosome">chromosome from which to create the NN</param>
    /// <param name="inputLayer">Neurons that give input to the neural network</param>
    /// <param name="outputLayer">neurons that are in the output layer of the neural network</param>
    public void BuildFromChromosome(NeuralChromosome chromosome, Neuron[] inputLayer, Neuron[] outputLayer)
    {
        //Create all the neurons
        CreateHiddenNeurons(chromosome, inputLayer, outputLayer);


        //Add links between neurons
        CreateDendrites(chromosome);

        output = ConvertToArray(outputNeurons);
        hidden = ConvertToArray(hiddenNeurons);
    }
コード例 #8
0
 private static NeuralChromosome StructureModification(NeuralChromosome chromosome, HashSet <int> inputNeuronsNumbers, HashSet <int> outputNeuronsNumbers)
 {
     if (chromosome.dendriteGenes.Length > 0 && r.NextDouble() < 0.35)
     {
         if (r.NextDouble() < 0.1)
         {
             return(Elimination(chromosome));
         }
         return(InsertNeuron(chromosome));
     }
     return(InsertDendrite(chromosome, inputNeuronsNumbers, outputNeuronsNumbers));
 }
コード例 #9
0
    private static NeuralChromosome InsertDendrite(NeuralChromosome chromosome, HashSet <int> inputNeuronsNumbers, HashSet <int> outputNeuronsNumbers)
    {
        HashSet <int> startingNeurons     = new HashSet <int>(); //neurons that will be added as input
        HashSet <int> endNeurons          = new HashSet <int>(); //neurons that require input
        List <int>    neuronsInChromosome = chromosome.GetNeuronsNumbers();

        startingNeurons.UnionWith(inputNeuronsNumbers);
        startingNeurons.UnionWith(neuronsInChromosome);
        foreach (int n in outputNeuronsNumbers)
        {
            startingNeurons.Remove(n);
        }

        endNeurons.UnionWith(outputNeuronsNumbers);
        endNeurons.UnionWith(neuronsInChromosome);
        foreach (int n in inputNeuronsNumbers)
        {
            endNeurons.Remove(n);
        }

        if (!startingNeurons.Any() || !endNeurons.Any())
        {
            return(chromosome);
        }

        int startingNeuron = startingNeurons.ToArray()[r.Next(startingNeurons.Count)];
        int endingNeuron   = endNeurons.ToArray()[r.Next(endNeurons.Count)];

        HashSet <DendriteGene.ComparableGene> existingGenes = new HashSet <DendriteGene.ComparableGene>();

        foreach (DendriteGene dendrite in chromosome.dendriteGenes)
        {
            existingGenes.Add(dendrite.GetComparableGene());
        }
        if (existingGenes.Contains(new DendriteGene.ComparableGene(new DendriteGene(startingNeuron, endingNeuron, 0))))
        {
            return(chromosome);
        }

        DendriteGene[] dendriteGenes = new DendriteGene[chromosome.dendriteGenes.Length + 1];
        for (int i = 0; i < chromosome.dendriteGenes.Length; i++)
        {
            dendriteGenes[i] = chromosome.dendriteGenes[i];
        }

        dendriteGenes[chromosome.dendriteGenes.Length] = new DendriteGene(startingNeuron, endingNeuron, (float)r.NextDouble() * 2 - 1);

        return(new NeuralChromosome(dendriteGenes));
    }
コード例 #10
0
    private static NeuralChromosome ModifyWeights(NeuralChromosome chromosome)
    {
        DendriteGene[] dendriteGenes = new DendriteGene[chromosome.dendriteGenes.Length];

        for (int i = 0; i < dendriteGenes.Length; i++)
        {
            dendriteGenes[i] = new DendriteGene(chromosome.dendriteGenes[i].startNeuron, chromosome.dendriteGenes[i].endNeuron, ModifyWeight(chromosome.dendriteGenes[i].weight));
        }

        /*
         * int pos = r.Next(dendriteGenes.Length);
         * dendriteGenes[pos] = new DendriteGene(dendriteGenes[pos].startNeuron, dendriteGenes[pos].endNeuron, ModifyWeight(dendriteGenes[pos].weight));
         */
        chromosome = new NeuralChromosome(dendriteGenes);

        return(chromosome);
    }
コード例 #11
0
    /// <summary>
    /// Creates all hidden neurons and links them to the input and output layer. Fills
    /// the dictionaries variables
    /// </summary>
    /// <param name="chromosome"></param>
    /// <param name="inputLayer">Neurons that comes from the organism and can be used as input in the neural network</param>
    /// <param name="outputLayer">Neurons that will be used by the organism to make decisions, are the output of the neural network</param>
    private void CreateHiddenNeurons(NeuralChromosome chromosome, Neuron[] inputLayer, Neuron[] outputLayer)
    {
        List <int> hiddenNeuronsNumbers = chromosome.GetNeuronsNumbers();

        foreach (Neuron neuron in inputLayer)
        {
            AddNeuron(inputNeurons, neuron);
            hiddenNeuronsNumbers.Remove(neuron.number);
        }
        foreach (Neuron neuron in outputLayer)
        {
            AddNeuron(outputNeurons, neuron);
            hiddenNeuronsNumbers.Remove(neuron.number);
        }

        foreach (int hn in hiddenNeuronsNumbers)
        {
            Neuron neuron = new Neuron(hn);

            AddNeuron(hiddenNeurons, neuron);
        }
    }
コード例 #12
0
    public static Chromosome CreateCrossover(Chromosome ch1, Chromosome ch2)
    {
        Gene[] genes = CrossoverManager.Crossover(ch1.Genes, ch2.Genes);


        var neuralComparableGenes = CrossoverManager.Crossover(ch1.neuralChromosome.GetComparableGenes(), ch2.neuralChromosome.GetComparableGenes());

        DendriteGene[] dendriteGenes = new DendriteGene[neuralComparableGenes.Length];
        for (int i = 0; i < dendriteGenes.Length; i++)
        {
            dendriteGenes[i] = neuralComparableGenes[i].GetGene();
        }


        NeuralChromosome neuralChromosome = new NeuralChromosome(dendriteGenes);

        ChromosomeParameters parameters = CrossoverManager.CrossParameters(ch1.Parameters, ch2.Parameters);

        Chromosome chromosome = new Chromosome(genes, neuralChromosome, ch1.MutationTracker + "CR", parameters);

        return(chromosome);
    }
コード例 #13
0
        public double getScore()
        {
            NeuralChromosome c = (NeuralChromosome)GetChromosome(0);

            return(c.Cost);
        }
コード例 #14
0
        private NeuralChromosome GetCrossover(NeuralChromosome x, NeuralChromosome y, NeuralCrossover crossover)
        {
            var rator = GeneticFactory.ConstructNeuralCrossoverOperators(crossover).First();

            return(rator.Invoke(x, y) as NeuralChromosome);
        }
コード例 #15
0
        private NeuralChromosome GetMutation(NeuralChromosome chromosome, NeuralMutation mutation)
        {
            var rator = GeneticFactory.ConstructNeuralMutationOperators(mutation).First();

            return(rator.Invoke(chromosome) as NeuralChromosome);
        }