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); }
public static List <AllocOutput> GetAllocations(AllocInput allocInput) { List <AllocOutput> allocations = new List <AllocOutput>(); Allocation newAllocation = new Allocation(allocInput.Tasks.Count, allocInput.Processors, allocInput.Coefficients); var grid = new Grid(allocInput.Tasks, allocInput.Processors, allocInput.RefFrequency); while (!newAllocation.IsDone) { TaskAssignment newTaskAssignment = new TaskAssignment(grid); var taskRuntime = grid.Content[newTaskAssignment.TaskId][newTaskAssignment.ProcessorId]; newAllocation.AssignTaskToProcessor(newTaskAssignment.TaskId, newTaskAssignment.ProcessorId, taskRuntime); grid.UpdateProcessorRuntime(newTaskAssignment.ProcessorId, grid.GetRuntime(newTaskAssignment.TaskId, newTaskAssignment.ProcessorId)); grid.RemoveRow(newTaskAssignment.TaskId); } var allocOutput = new AllocOutput((allocations.Count + 1).ToString(), 0, newAllocation.EnergyConsumed, newAllocation.Processors); allocations.Add(allocOutput); return(allocations); }
public static List <AllocOutput> GetAllocations(AllocInput allocInput) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); List <AllocOutput> allocations = new List <AllocOutput>(); uint trialCount = 0; while (stopWatch.ElapsedMilliseconds < MAX_DURATION_MS && allocations.Count < MAX_ALLOCATIONS) { Allocation newAllocation = new Allocation(allocInput.Tasks.Count, allocInput.Processors, allocInput.Coefficients); var unassignedTasks = new Dictionary <string, float>(allocInput.Tasks); var processorRuntimes = new Dictionary <string, float>(); var shouldStopAllocating = false; while (!newAllocation.IsDone && !shouldStopAllocating) { var availableProcessors = new Dictionary <string, float>(allocInput.Processors); string longestRuntimeTaskId; string hiqhestFreqProcessorId; float taskRuntime; do { longestRuntimeTaskId = unassignedTasks.Aggregate((x, y) => x.Value > y.Value ? x : y).Key; hiqhestFreqProcessorId = availableProcessors.Aggregate((x, y) => x.Value > y.Value ? x : y).Key; taskRuntime = unassignedTasks[longestRuntimeTaskId] * (allocInput.RefFrequency / availableProcessors[hiqhestFreqProcessorId]); availableProcessors.Remove(hiqhestFreqProcessorId); if (!processorRuntimes.ContainsKey(hiqhestFreqProcessorId)) { processorRuntimes.Add(hiqhestFreqProcessorId, 0); } } while ((processorRuntimes[hiqhestFreqProcessorId] + taskRuntime) > (allocInput.MaxDuration - (trialCount * TIME_STEP)) && availableProcessors.Count != 0); if (!String.IsNullOrEmpty(hiqhestFreqProcessorId)) { newAllocation.AssignTaskToProcessor(longestRuntimeTaskId, hiqhestFreqProcessorId, taskRuntime); processorRuntimes[hiqhestFreqProcessorId] += taskRuntime; unassignedTasks.Remove(longestRuntimeTaskId); } else { shouldStopAllocating = true; } } float programRuntime = processorRuntimes.Max((x) => x.Value); if (newAllocation.IsDone && programRuntime <= allocInput.MaxDuration) { AllocOutput allocOutput = new AllocOutput((allocations.Count + 1).ToString(), programRuntime, newAllocation.EnergyConsumed, newAllocation.Processors); bool isAllocationUnique = true; // Check if the same allocation already exists foreach (var allocation in allocations) { if (allocation.GetUniqueId() == allocOutput.GetUniqueId()) { isAllocationUnique = false; } } if (isAllocationUnique) { allocations.Add(allocOutput); } } trialCount += 1; } return(allocations.ToList()); }