private Allocation mutate(Allocation alloc) { Allocation mutatedAlloc = alloc.Clone(); if (Rand.NextDouble() < MUTATION_RATE) { for (int _ = 0; _ < MUTATE_TASKS_COUNT; _++) { string taskId = ""; string originalProcId = ""; do { // Get a random task id int randOriginalProcIndex = Rand.Next(0, mutatedAlloc.ProcessorIds.Count); originalProcId = mutatedAlloc.ProcessorIds[randOriginalProcIndex]; Processor proc = mutatedAlloc.GetProcessor(originalProcId); taskId = proc.GetRandomTask(); } while (taskId == ""); // Assign the task id to another processor string newProcId; do { int randNewProcIndex = Rand.Next(0, mutatedAlloc.ProcessorIds.Count); newProcId = mutatedAlloc.ProcessorIds[randNewProcIndex]; } while (newProcId == originalProcId); mutatedAlloc.AssignTaskToProcessor(newProcId, taskId); mutatedAlloc.RemoveTask(originalProcId, taskId); } } return(mutatedAlloc); }
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)); } // Remove duplicated tasks //parent2.RemoveDuplicatedTasks(child.GetAssignedTasks()); // 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)); } } var unassignedTasks = child.GetUnassignedTasks(); foreach (var taskId in unassignedTasks) { string randomProcessorId = processorIds[Rand.Next(0, processorIds.Count)]; child.AssignTaskToProcessor(randomProcessorId, taskId); } return(child); }