public void SetGens(IntChromosome unit, int unitGensCount) { unit.Gens = new List <int>(unitGensCount); for (int ind = 0; ind < unitGensCount; ind++) { unit.Gens.Add(GetIntInRange()); } }
private IntChromosome FindBest(List <IntChromosome> chromosomes, FitnessFuncGoal goal) { IntChromosome bestChromosome = null; foreach (int index in chromosomeIndexes) { if (bestChromosome == null || comparator.Compare(bestChromosome, chromosomes.ElementAt(index)) > 0) { bestChromosome = chromosomes.ElementAt(index); } } return(bestChromosome); }
protected override GeneticAlgorithm GetGA(SolverParameters parameters) { var adamChromosome = new IntChromosome(0.To(NumQueens - 1).ToArray()); var population = new Population(parameters.Population, parameters.Population, adamChromosome); return(new GeneticAlgorithm( population, FitnessProvider, new EliteSelection(), new OrderedCrossover(), new ReverseSequenceMutation())); }
protected override GeneticAlgorithm GetGA(SolverParameters parameters) { // First node is fixed var adamChromosome = new IntChromosome(1.To(NumPoints - 1).ToArray()); var population = new Population(parameters.Population, parameters.Population, adamChromosome); return(new GeneticAlgorithm( population, FitnessProvider, new TournamentSelection(), new OrderedCrossover(), new ReverseSequenceMutation())); }
private void crossbreeding(IntChromosome ch1, IntChromosome ch2) { int sharedDischargeBit = getSharedDischarge(); IntChromosome ch1Child = (IntChromosome)ch1.Clone(); IntChromosome ch2Child = (IntChromosome)ch2.Clone(); List <int> ch1ChildGens = ch1Child.Gens; List <int> ch2ChildGens = ch2Child.Gens; for (int index = 0; index < ch1ChildGens.Count; index++) { ch1ChildGens[index] = CrossbreedingGen(ch1ChildGens.ElementAt(index), ch2ChildGens.ElementAt(index), sharedDischargeBit); ch2ChildGens[index] = CrossbreedingGen(ch2ChildGens.ElementAt(index), ch1ChildGens.ElementAt(index), sharedDischargeBit); } ch1 = ch1Child; ch2 = ch2Child; }
private void Mutate(IntChromosome chromosome) { int genInd = random.Next(0, chromosome.Gens.Count); int bitInd = random.Next(0, mutateMask); if ((chromosome.Gens[genInd] >> (bitInd)) % 2 == 0) { //bit equal zero chromosome.Gens[genInd] += 1 << bitInd; } else { //bit equal one chromosome.Gens[genInd] -= 1 << bitInd; } }
public List <IntChromosome> Generate(IntRange range, int populationSize, int unitGensCount) { if (populationSize < 2 || unitGensCount < 1) { throw new ArgumentException("Population must be 2 or more, gens 1 or more"); } Range = range; List <IntChromosome> population = new List <IntChromosome>(populationSize); IntChromosome unit; for (int ind = 0; ind < populationSize; ind++) { unit = new IntChromosome(); SetGens(unit, unitGensCount); population.Add(unit); } return(population); }
public double CalcFitnessFunc(IntChromosome chromosome) { result = 0; chromosome.Gens.AsParallel().ForAll((gen) => result += Math.Pow(gen, 2)); return(result); }