// Finds the fittest individual from a subset of population private Allocation tournamentSelect(Population population) { Population tournament = new Population(TOURNAMENT_SIZE, false, this.coefficients, this.refFreq, this.tasks, this.processors); for (int allocationIndex = 0; allocationIndex < TOURNAMENT_SIZE; allocationIndex++) { int randomAllocationIndex = Rand.Next(0, population.Size); tournament.SaveAlloc(population.GetAllocation(randomAllocationIndex)); } Allocation fittest = tournament.GetFittest(); return(fittest); }
public Allocation Clone() { var clonedAlloc = new Allocation(this.coefficients, this.refFreq, this.tasks, this.processorFreqs, true) { energyConsumed = this.energyConsumed }; var clonedProcessors = new Dictionary <string, Processor>(); foreach (var proc in this.processors) { clonedProcessors.Add(proc.Key, proc.Value.Clone()); } clonedAlloc.processors = clonedProcessors; return(clonedAlloc); }
// Creates a child from 2 provided parents private Allocation crossover(Allocation parent1, Allocation parent2) { Allocation child = new Allocation(this.coefficients, this.refFreq, this.tasks, this.processors, true); List <string> processorIds = parent1.ProcessorIds; int startPos = Rand.Next(0, processorIds.Count); int endPos = Rand.Next(startPos + 1, processorIds.Count); while (startPos >= endPos) { startPos = Rand.Next(0, processorIds.Count); endPos = Rand.Next(startPos + 1, processorIds.Count); } // Select processors from parent 1 for (int processorIndex = startPos; processorIndex < endPos; processorIndex++) { string processorId = processorIds[processorIndex]; child.SetProcessor(processorId, parent1.GetProcessor(processorId)); } // Select processors from parent 2 for (int processorIndex = 0; processorIndex < processorIds.Count; processorIndex++) { string processorId = processorIds[processorIndex]; if (!child.ContainsProcessor(processorId)) { child.SetProcessor(processorId, parent2.GetProcessor(processorId)); } } // Assign tasks that don't belong to any processor yet var unassignedTasks = child.GetUnassignedTasks(); foreach (var taskId in unassignedTasks) { string randomProcessorId = processorIds[Rand.Next(0, processorIds.Count)]; child.AssignTaskToProcessor(randomProcessorId, taskId); } return(child); }
// Evolves a population over 1 generation private Population evolvePopulation(Population population) { Population newPopulation = new Population(population.Size, false, this.coefficients, this.refFreq, this.tasks, this.processors); Allocation fittest = population.GetFittest(); newPopulation.SaveAlloc(fittest); if (fittest.ProgramRuntime < this.maxDuration) { correctAllocs.Add(fittest); } for (int allocIndex = 1; allocIndex < population.Size; allocIndex++) { if (Rand.NextDouble() < CROSSOVER_RATE) { Allocation parent1 = this.tournamentSelect(population); Allocation parent2 = this.tournamentSelect(population); Allocation child = this.crossover(parent1, parent2); newPopulation.SaveAlloc(child); } else { newPopulation.SaveAlloc(population.GetAllocation(allocIndex)); } } for (int allocationIndex = 0; allocationIndex < newPopulation.Size; allocationIndex++) { Allocation mutatedAlloc = this.mutate(newPopulation.GetAllocation(allocationIndex)); newPopulation.ReplaceAlloc(allocationIndex, mutatedAlloc); } return(newPopulation); }