示例#1
0
        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);
        }