// TODO: Testing public override void ValidateIndividual(IndividualBase individual) { TaskSchedulingSolution schedulingSolution = (individual as TaskSchedulingSolution); List <List <TaskIndexingNode> > TasksGroupedByProcessor = GroupTasksByProcessor(schedulingSolution); for (int processorIdx = 0; processorIdx < ProcessorCount; ++processorIdx) { List <TaskIndexingNode> TasksInProcessor = TasksGroupedByProcessor[processorIdx]; for (int probableDependantIdx = 0; probableDependantIdx < TasksInProcessor.Count; ++probableDependantIdx) { int probableDependantTask = TasksInProcessor[probableDependantIdx].Task; for (int probableDependencyIdx = probableDependantIdx + 1; probableDependencyIdx < TasksInProcessor.Count; ++probableDependencyIdx) { int probableDependencyTask = TasksInProcessor[probableDependencyIdx].Task; if (CommGraph.DependsOn(probableDependantTask, probableDependencyTask)) { schedulingSolution.SwapGenesAt(TasksInProcessor[probableDependantIdx].Index, TasksInProcessor[probableDependencyIdx].Index); TasksInProcessor.Swap(probableDependantIdx, probableDependencyIdx); int Temp = TasksInProcessor[probableDependantIdx].Index; TasksInProcessor[probableDependantIdx].Index = TasksInProcessor[probableDependencyIdx].Index; TasksInProcessor[probableDependencyIdx].Index = Temp; probableDependantIdx = -1; break; } } } } }
public override void EvaluateIndividual(IndividualBase individual) { REDO: TaskSchedulingSolution schedulingSolution = individual as TaskSchedulingSolution; schedulingSolution.SpentPower = 0; double pCommunication = 0; int pTask = 0; bool[] scheduledTasks = new bool[TaskCount]; int remainingTasks = TaskCount; int[] processorsSchedullingTimeForTask = new int[TaskCount]; schedulingSolution.ProcessorStatus = new int[ProcessorCount]; int currentAllocationCost; List <List <TaskIndexingNode> > tasksGroupedByProcessor = GroupTasksByProcessor(schedulingSolution); bool deadLockOcurred; do { for (int currentProcessor = 0; currentProcessor < ProcessorCount; ++currentProcessor) { if (tasksGroupedByProcessor[currentProcessor].Count != 0) { int nextTaskForProcessor = tasksGroupedByProcessor[currentProcessor][0].Task; IEnumerable <int> notScheduledTaskDependencies = CommGraph.GetDirectDependencies(nextTaskForProcessor).Where(Dep => scheduledTasks[Dep] == false); bool readyForScheduling = (notScheduledTaskDependencies.Count() == 0); if (readyForScheduling) { int currentTask = nextTaskForProcessor; currentAllocationCost = schedulingSolution.ProcessorStatus[currentProcessor]; foreach (int dependencyTask in CommGraph.GetDirectDependencies(currentTask)) { int idx = 0; while (schedulingSolution.GeneticMaterial[0, idx] != dependencyTask) { ++idx; } int dependencyProcessor = schedulingSolution.GeneticMaterial[1, idx]; if (dependencyProcessor != currentProcessor) { pCommunication += Math.Pow(CommGraph.GetCommunicationCost(dependencyTask, currentTask), 2); int dependencyCost = processorsSchedullingTimeForTask[dependencyTask] + CommGraph.GetCommunicationCost(dependencyTask, currentTask); if (dependencyCost > currentAllocationCost) { currentAllocationCost = dependencyCost; } } } currentAllocationCost += CommGraph.GetEdgeCost(currentTask); schedulingSolution.ProcessorStatus[currentProcessor] = currentAllocationCost; processorsSchedullingTimeForTask[currentTask] = currentAllocationCost; pTask += CommGraph.GetEdgeCost(currentTask); scheduledTasks[currentTask] = true; tasksGroupedByProcessor[currentProcessor].RemoveAt(0); remainingTasks--; currentProcessor = -1; } } } if (remainingTasks != 0) { deadLockOcurred = true; // Tratamento de DeadLock int chosenSourceProcessor; do { chosenSourceProcessor = Aleatoriety.GetRandomInt(ProcessorCount); } while (tasksGroupedByProcessor[chosenSourceProcessor].Count == 0); int chosenDestProcessor; do { chosenDestProcessor = Aleatoriety.GetRandomInt(ProcessorCount); } while (chosenSourceProcessor == chosenDestProcessor); int taskToBeTransfered = tasksGroupedByProcessor[chosenSourceProcessor].First().Task; int idx = 0; while (schedulingSolution.GeneticMaterial[0, idx] != taskToBeTransfered) { ++idx; } schedulingSolution.GeneticMaterial[1, idx] = chosenDestProcessor; ValidateIndividual(schedulingSolution); goto REDO; } else { deadLockOcurred = false; } } while (deadLockOcurred); // Houve DeadLock schedulingSolution.MakeSpan = schedulingSolution.ProcessorStatus.Max(); schedulingSolution.SpentPower = (pTask * k1) + (pCommunication * k2); }