public Population(int count, IPopulationGenerator <T> generator, IFitnessEvaluator <T> evaluator) { if (count <= 0) { throw new ArgumentException($"{nameof(count)} must be greater than 0. Actual: {count}."); } if (generator == null) { throw new System.ArgumentNullException(nameof(generator)); } if (evaluator == null) { throw new System.ArgumentNullException(nameof(evaluator)); } TargetSize = count; // TODO: This really shouldn't be here. We should instead pass the // population for (var i = 0; i < count; i++) { var value = generator.Generate(); var fitness = evaluator.ComputeFitness(value); var chromosome = new Chromosome <T>(value, fitness); Chromosomes.Add(chromosome); } }
public void Add(I interval, string chr, char strand) { if (!Chromosomes.ContainsKey(chr)) { Chromosomes.Add(chr, new Chromosome <I, S>()); } Chromosomes[chr].Add(interval, strand); Statistics.Update(interval); }
/// <summary> /// Create a NEAT gnome. /// </summary> /// /// <param name="genomeID">The genome id.</param> /// <param name="neurons">The neurons.</param> /// <param name="links">The links.</param> /// <param name="inputCount_0">The input count.</param> /// <param name="outputCount_1">The output count.</param> public NEATGenome(long genomeID, Chromosome neurons, Chromosome links, int inputCount_0, int outputCount_1) { GenomeID = genomeID; linksChromosome = links; neuronsChromosome = neurons; AmountToSpawn = 0; AdjustedScore = 0; inputCount = inputCount_0; outputCount = outputCount_1; Chromosomes.Add(neuronsChromosome); Chromosomes.Add(linksChromosome); }
/// <summary> /// Construct a genome, do not provide links and neurons. /// </summary> /// /// <param name="id">The genome id.</param> /// <param name="inputCount_0">The input count.</param> /// <param name="outputCount_1">The output count.</param> public NEATGenome(long id, int inputCount_0, int outputCount_1) { GenomeID = id; AdjustedScore = 0; inputCount = inputCount_0; outputCount = outputCount_1; AmountToSpawn = 0; speciesID = 0; double inputRowSlice = 0.8d / (inputCount_0); neuronsChromosome = new Chromosome(); linksChromosome = new Chromosome(); Chromosomes.Add(neuronsChromosome); Chromosomes.Add(linksChromosome); for (int i = 0; i < inputCount_0; i++) { neuronsChromosome.Add(new NEATNeuronGene(NEATNeuronType.Input, i, 0, 0.1d + i * inputRowSlice)); } neuronsChromosome.Add(new NEATNeuronGene(NEATNeuronType.Bias, inputCount_0, 0, 0.9d)); double outputRowSlice = 1 / (double)(outputCount_1 + 1); for (int i_2 = 0; i_2 < outputCount_1; i_2++) { neuronsChromosome.Add(new NEATNeuronGene( NEATNeuronType.Output, i_2 + inputCount_0 + 1, 1, (i_2 + 1) * outputRowSlice)); } for (int i_3 = 0; i_3 < inputCount_0 + 1; i_3++) { for (int j = 0; j < outputCount_1; j++) { linksChromosome.Add(new NEATLinkGene( ((NEATNeuronGene)neuronsChromosome.Get(i_3)).Id, ((NEATNeuronGene)Neurons.Get( inputCount_0 + j + 1)).Id, true, inputCount_0 + outputCount_1 + 1 + NumGenes, RangeRandomizer.Randomize(-1, 1), false)); } } }
/// <summary> /// 初始化,设置染色体 /// </summary> private static void Init() { Chromosomes.Clear(); for (int i = 0; i < ChromosomeLength; i++) { int[,] valueLimit = new int[GenesLength, 2]; for (int ii = 0; ii < GenesLength; ii++) { valueLimit[ii, 0] = variateInfo[ii].MinValue; valueLimit[ii, 1] = variateInfo[ii].MaxValue; } Chromosome chromosome = new Chromosome { ValueLimit = valueLimit, }; chromosome.Genes = IniVariate(chromosome); Chromosomes.Add(chromosome); } }
/// <summary> /// Construct a genome by copying another. /// </summary> /// /// <param name="other">The other genome.</param> public NEATGenome(NEATGenome other) { neuronsChromosome = new Chromosome(); linksChromosome = new Chromosome(); GA = other.GA; Chromosomes.Add(neuronsChromosome); Chromosomes.Add(linksChromosome); GenomeID = other.GenomeID; networkDepth = other.networkDepth; Population = other.Population; Score = other.Score; AdjustedScore = other.AdjustedScore; AmountToSpawn = other.AmountToSpawn; inputCount = other.inputCount; outputCount = other.outputCount; speciesID = other.speciesID; // copy neurons foreach (IGene gene in other.Neurons.Genes) { var oldGene = (NEATNeuronGene)gene; var newGene = new NEATNeuronGene( oldGene.NeuronType, oldGene.Id, oldGene.SplitY, oldGene.SplitX, oldGene.Recurrent, oldGene.ActivationResponse); Neurons.Add(newGene); } // copy links foreach (IGene gene_0 in other.Links.Genes) { var oldGene_1 = (NEATLinkGene)gene_0; var newGene_2 = new NEATLinkGene( oldGene_1.FromNeuronID, oldGene_1.ToNeuronID, oldGene_1.Enabled, oldGene_1.InnovationId, oldGene_1.Weight, oldGene_1.Recurrent); Links.Add(newGene_2); } }
/// <summary> /// Construct a neural genome. /// </summary> /// /// <param name="network">The network to use.</param> public NeuralGenome(BasicNetwork network) { Organism = network; _networkChromosome = new Chromosome(); // create an array of "double genes" int size = network.Structure.CalculateSize(); for (int i = 0; i < size; i++) { IGene gene = new DoubleGene(); _networkChromosome.Genes.Add(gene); } Chromosomes.Add(_networkChromosome); Encode(); }
public TSPGenome(GeneticAlgorithm owner, City[] cities) { var organism = new int[cities.Length]; var taken = new bool[cities.Length]; for (int i = 0; i < organism.Length; i++) { taken[i] = false; } for (int i = 0; i < organism.Length - 1; i++) { int icandidate; do { icandidate = (int)(ThreadSafeRandom.NextDouble() * organism.Length); } while (taken[icandidate]); organism[i] = icandidate; taken[icandidate] = true; if (i == organism.Length - 2) { icandidate = 0; while (taken[icandidate]) { icandidate++; } organism[i + 1] = icandidate; } } pathChromosome = new Chromosome(); Chromosomes.Add(pathChromosome); for (int i = 0; i < organism.Length; i++) { var gene = new IntegerGene(); gene.Value = organism[i]; pathChromosome.Genes.Add(gene); } Organism = organism; Encode(); }
//随机生成若干染色体 public void RandomGenerateChromosome() { var rnd = new Random(); for (var i = 0; i < ChromosomeQuantity; i++) { var chromosome = new Chromosome { Population = this }; var valueString = ""; for (var k = 0; k < ChromosomeLength; k++) { valueString += Convert.ToInt32(rnd.Next(0, 2)).ToString(); } chromosome.Value = Convert.ToInt64(valueString, 2); Chromosomes.Add(chromosome); } }
public Population(int count, IPopulationGenerator <T> generator, IFitnessEvaluator <T> evaluator) { Contract.Requires <ArgumentOutOfRangeException>(count > 0); Contract.Requires <ArgumentNullException>(generator != null); Contract.Requires <ArgumentNullException>(evaluator != null); TargetSize = count; Evaluator = evaluator; // TODO: This really shouldn't be here. We should instead pass the // population for (var i = 0; i < count; i++) { var value = generator.Generate(); var chromosome = new Chromosome <T>(value); chromosome.Fitness = evaluator.ComputeFitness(chromosome.Genes); Chromosomes.Add(chromosome); } }
public void NextGeneration(ISelection selectionScheme) { var selected = selectionScheme.Select(Chromosomes, 5).OrderByDescending(s => s.Cost).ToList(); Chromosomes.Remove(selected[0]); Chromosomes.Remove(selected[1]); // fill the remaining empty space with children generated from selected chromosomes Chromosome parent1 = selected[3]; Chromosome parent2 = selected[4]; Chromosome child1 = OrderedCrossover.MakeChild(parent1, parent2); Chromosome child2 = OrderedCrossover.MakeChild(parent2, parent1); if (ThreadSafeRandom.CurrentThreadRandom.Next(100) < 3) { child1.Mutate(); } if (ThreadSafeRandom.CurrentThreadRandom.Next(100) < 3) { child2.Mutate(); } Chromosomes.Add(child1); Chromosomes.Add(child2); double currentTour = Chromosomes.Where(c => c.Cost > 0).Min(c => c.Cost); if (currentTour < BestTour) { BestTour = currentTour; Console.WriteLine("Generation " + currentGeneration + " Best: " + BestTour); } currentGeneration++; }
public void AddChromosome(T chromosome) { Chromosomes.Add(chromosome); }
public State <T> AddChromosome(Chromosome <T> chromosome) { var newChromosomes = Chromosomes.Add(chromosome).OrderByDescending(c => c.Fitness); return(new State <T>(newChromosomes)); }
public void AddChromosome(Chromosome <T> chromosome) { Contract.Requires <ArgumentNullException>(chromosome != null, "Chromosome cannot be null."); Chromosomes.Add(chromosome); }