// ルーレット選択 public void Reproduce_Roulette(IntArrayChromosomes _intarraychromosomes) { _intarraychromosomes.SortChromosomes(); var random = new System.Random(); double scoreSum = 0; for (int i = 0; i < _intarraychromosomes.GetPopulation(); i++) { scoreSum += _intarraychromosomes.ReadScore(i); } for (int i = ELITE_POPULATION; i < _intarraychromosomes.GetPopulation() && _intarraychromosomes.GetPopulation() > POPULATION; i++) { if (random.NextDouble() <= 1 - _intarraychromosomes.ReadScore(i) / scoreSum) { _intarraychromosomes.RemoveChromosome(i); } } int population = _intarraychromosomes.GetPopulation(); for (int i = 0; i < population - POPULATION; i++) { _intarraychromosomes.RemoveChromosome(random.Next(ELITE_POPULATION, _intarraychromosomes.GetPopulation())); } }
public CellAutomataGA() { chromosomeMaxNumber = FastPower(CELLSTATESIZE, MOORENEIGHBORHOOD + 1) - 1; intArrayChromosomes = new IntArrayChromosomes(POPULATION, CHROMOSOMESIZE, chromosomeMaxNumber); scores = new double[POPULATION]; elitelist = new List <int>(); rulesForEvalate = new int[2] { ConvertToConditionNo(new int[] { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 }) , ConvertToConditionNo(new int[] { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }) }; }
public void Mutation(IntArrayChromosomes intChromosomes, double mutationrate, List <int> _elitelist, int chromosomeMaxNumber) { Random random = new Random(); for (int i = 0; i < POPULATION; i++) { double rndnum = random.NextDouble(); if (rndnum <= mutationrate && !_elitelist.Contains <int>(i)) { intChromosomes.SetChromosome(i, random.Next(0, CHROMOSOMESIZE), random.Next(0, chromosomeMaxNumber)); } } }
// 交叉 public void Crossover(IntArrayChromosomes intChromosomes, double crossoverrate) { System.Random random = new System.Random(); for (int i = 0; i < POPULATION - 1; i++) { for (int j = i + 1; j < POPULATION; j++) { if (random.NextDouble() <= crossoverrate) { intChromosomes.CrossOverChromosomes( i, j, random.Next(0, CHROMOSOME_SIZE), random.Next(0, CHROMOSOME_SIZE)); } } } }
public CellAutomataGA(int initBoardSize) { boardSize = initBoardSize; chromosomeMaxNumber = FastPower(CELL_STATE_SIZE, MOORE_NEIGHBORHOOD + 1) - 1; intArrayChromosomes = new IntArrayChromosomes(POPULATION, CHROMOSOME_SIZE, chromosomeMaxNumber); SetUpChromosomes(); rulesForEvalate = new int[5] { ConvertToConditionNo(new int[] { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 }), ConvertToConditionNo(new int[] { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }), ConvertToConditionNo(new int[] { 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 }), ConvertToConditionNo(new int[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0 }), ConvertToConditionNo(new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 }), }; }
// 選択・淘汰 //ランキング選択 public void Reproduce_Ranking(IntArrayChromosomes _intArrayChromosomes) { _intArrayChromosomes.SortChromosomes(); var random = new System.Random(); int population = _intArrayChromosomes.GetPopulation(); for (int i = 0; i < population - POPULATION; i++) { int individual = (int)(DensityFunction(random) * _intArrayChromosomes.GetPopulation()) + ELITE_POPULATION; if (individual >= POPULATION) { individual = POPULATION - 1; } _intArrayChromosomes.RemoveChromosome(individual); } }
// 突然変異 public void Mutation(IntArrayChromosomes intChromosomes, double mutationrate, int chromosomeMaxNumber) { System.Random random = new System.Random(); for (int i = 0; i < POPULATION; i++) // Change { double rndnum = random.NextDouble(); if (rndnum <= mutationrate) { int startIndex = random.Next(0, (int)(CHROMOSOME_SIZE * (1 - MUTATION_RANGE))); for (int j = startIndex; j < startIndex + CHROMOSOME_SIZE * MUTATION_RANGE; j++) { intChromosomes.SetChromosome( i, random.Next(0, CHROMOSOME_SIZE), random.Next(0, chromosomeMaxNumber)); } } } }
public void Crossover(IntArrayChromosomes intChromosomes, double crossoverrate, List <int> _elitelist) { Random random = new Random(); for (int i = 0; i < POPULATION - 1; i++) { double rnddbl = random.NextDouble(); int rndindex = random.Next(i, POPULATION - 1); if (crossoverrate <= rnddbl && !_elitelist.Contains <int>(i) && !_elitelist.Contains <int>(rndindex)) { int point1 = random.Next(0, CHROMOSOMESIZE); int point2 = random.Next(0, CHROMOSOMESIZE); if (point1 <= point2) { intArrayChromosomes.TransChromosome(i, rndindex, point1, point2); } else { intArrayChromosomes.TransChromosome(i, rndindex, point2, point1); } } } }