public void Mutate() { for (int i = 0; i < _jobsLength; i++) { if (SimpleRNG.GetUniform() < _mutationRate) { int r = SimpleRNG.Next(0, _jobsLength); int temp = JobGenes[i]; JobGenes[i] = JobGenes[r]; JobGenes[r] = temp; // Mutate the delay time r = SimpleRNG.Next(0, _modesLength); double mutatedDelay = 0; //mutatedDelay = SimpleRNG.GetExponential(_delayMean); mutatedDelay = SimpleRNG.GetNormal(TimeGenes[r], 1.0); //mutatedDelay = _rand.NextDouble() * _delayMean; if (mutatedDelay < 0.0) { mutatedDelay = 0.0; } TimeGenes[r] = mutatedDelay; } } //Mutate the Mode vector: for (int i = 0; i < _modesLength; i++) { if (SimpleRNG.GetUniform() < _mutationRate) { ModeGenes[i] = SimpleRNG.Next(0, _numberOfModes); } } }
public void Crossover(ref ScheduleGenome p2, ref ScheduleGenome o1) { int cutpoint = SimpleRNG.Next(0, _jobsLength); //Debug.Write(Environment.NewLine + "Distance between " + p1 + " - " + p2 + ": " + population[p1].Distance(population[p2])); for (int i = 0; i < _jobsLength; i++) { o1.JobGenes[i] = JobGenes[i]; } Debug.Assert(o1.IsValid(), "Invalid creature before crossover"); List <int> remainder = new List <int>(p2.JobGenes); for (int i = 0; i < cutpoint; i++) { remainder.Remove(JobGenes[i]); } for (int i = cutpoint; i < _jobsLength; i++) { o1.JobGenes[i] = remainder[i - cutpoint]; } Debug.Assert(o1.IsValid(), "Invalid creature after crossover"); RealCrossover(ref p2, ref o1); CombinationCrossover(ref p2, ref o1); }
public void RealCrossover(ref ScheduleGenome p2, ref ScheduleGenome o1) { switch (realCrossover) { case RealCrossoverOp.MeanWithNoise: // Mean-with-noise Crossover: for (int i = 0; i < _modesLength; i++) { double mean = TimeGenes[i] + p2.TimeGenes[i]; mean = mean / 2.0; o1.TimeGenes[i] = SimpleRNG.GetNormal(mean, 0.5); if (o1.TimeGenes[i] < 0.0) { o1.TimeGenes[i] = 0.0; } } break; case RealCrossoverOp.Uniform: // Uniform Crossover: int cutpoint = SimpleRNG.Next(0, _modesLength + 1); for (int i = 0; i < cutpoint; i++) { o1.TimeGenes[i] = TimeGenes[i]; } for (int i = cutpoint; i < _modesLength; i++) { o1.TimeGenes[i] = p2.TimeGenes[i]; } break; } }
public void RandomInit() { List <int> randarray = new List <int>(); for (int i = 0; i < _jobsLength; i++) { randarray.Add(i); } for (int i = 0; i < _jobsLength; i++) { int r = SimpleRNG.Next(0, _jobsLength - i); JobGenes[i] = randarray[r]; randarray.RemoveAt(r); } for (int i = 0; i < _timesLength; i++) { if (SimpleRNG.GetUniform() < _delayRate) { TimeGenes[i] = SimpleRNG.GetExponential(_delayMean); } else { TimeGenes[i] = 0.0; } ModeGenes[i] = SimpleRNG.Next(0, _numberOfModes); } fitness = -1; }
public void CombinationCrossover(ref ScheduleGenome p2, ref ScheduleGenome o1) { int cutpoint = SimpleRNG.Next(0, _modesLength); for (int i = 0; i < cutpoint; i++) { o1.ModeGenes[i] = p2.ModeGenes[i]; } for (int i = cutpoint; i < _modesLength; i++) { o1.ModeGenes[i] = ModeGenes[i]; } }
private int SelectParent() { int p = 0; switch (parentSelection) { case ParentSelectionOp.FitnessProportional: double totalFitness = 0; for (int i = 0; i < _popsize; i++) { totalFitness += population[i].fitness; } //double r = _rand.NextDouble() * totalFitness; double r = SimpleRNG.GetUniform() * totalFitness; double runningTotal = population[p].fitness; while (runningTotal > r) { p++; runningTotal += population[p].fitness; } break; case ParentSelectionOp.Tournament: int k = _popsize / 10; //p = _rand.Next(_popsize); p = (int)(SimpleRNG.GetUniform() * _popsize); double bestfitness = population[p].fitness; for (int i = 0; i < k; i++) { int px = SimpleRNG.Next(0, _popsize); if (population[px].fitness > bestfitness) { bestfitness = population[px].fitness; p = px; } } break; } return(p); }