private IndividualBase BestByCrowdedComparison(IndividualBase individual1, IndividualBase individual2)
 {
     if (individual1.NonDominationRank < individual2.NonDominationRank)
     {
         return(individual1);
     }
     if (individual2.NonDominationRank < individual1.NonDominationRank)
     {
         return(individual2);
     }
     // Se Estão no Mesmo Ranking de Não-Dominância
     if (individual1.CrowdingDistance > individual2.CrowdingDistance)
     {
         return(individual1);
     }
     if (individual2.CrowdingDistance > individual1.CrowdingDistance)
     {
         return(individual2);
     }
     // Se têm a Mesma CrowdingDistance
     if (Aleatoriety.GetRandomInt(2) == 0)
     {
         return(individual1);
     }
     else
     {
         return(individual2);
     }
 }
        public override void CiclicCrossover(IndividualBase parent1, IndividualBase parent2, out IndividualBase child1, out IndividualBase child2)
        {
            int[] L1 = (int[])(parent1 as CryptArithmeticSolution).LettersValues.Clone();
            int[] L2 = (int[])(parent2 as CryptArithmeticSolution).LettersValues.Clone();

            List <int> positionsToShare = new List <int>();

            int amountOfChars = charArray.Count();
            int itemIdx       = Aleatoriety.GetRandomInt(amountOfChars);
            int tempValue;

            while (!positionsToShare.Contains(itemIdx))
            {
                positionsToShare.Add(itemIdx);
                tempValue = L1[itemIdx];
                itemIdx   = Array.IndexOf(L2, tempValue);
            }

            int tempVal;

            foreach (int position in positionsToShare)
            {
                tempVal      = L2[position];
                L2[position] = L1[position];
                L1[position] = tempVal;
            }
            child1 = new CryptArithmeticSolution(this, L1);
            child2 = new CryptArithmeticSolution(this, L2);
        }
        //Ha alguma garantia de que essas particulas aleatorias sejam circuitos hamiltonianos?

        public static TSPParticle RandomGenerate(TSPParticleSwarm containingSwarm, TravellingSalesmanMap Map)
        {
            TSPParticle newParticle = new TSPParticle(containingSwarm, Map);

            newParticle.Velocity = TSPVelocity.RandomGenerate(Map);
            newParticle.Position = new TSPPosition(Aleatoriety.GetRandomIntegerSequencePermutation(1, Map.CityCount).ToList());
            newParticle.EvaluateSelf();
            return(newParticle);
        }
        public void SwapProcessorAt(int index)
        {
            int mutationNodeProcessor = GeneticMaterial[1, index];
            int differentProcessor;

            do
            {
                differentProcessor = Aleatoriety.GetRandomInt(0, (Problem as TaskSchedulingProblem).ProcessorCount);
            } while (differentProcessor == mutationNodeProcessor);
            GeneticMaterial[1, index] = differentProcessor;
        }
        public override void MutateIndividual(IndividualBase individual)
        {
            TaskSchedulingSolution schedulingSolution = (individual as TaskSchedulingSolution);
            int indexA = Aleatoriety.GetRandomInt(TaskCount);
            int indexB;

            do
            {
                indexB = Aleatoriety.GetRandomInt(TaskCount);
            } while (indexB == indexA);
            schedulingSolution.SwapGenesAt(indexA, indexB);
            schedulingSolution.SwapProcessorAt(indexA);
        }
        public static TSPVelocity RandomGenerate(TravellingSalesmanMap Map)
        {
            // 0.0 <= Valor < 1.0
            double speed             = Aleatoriety.GetRandomDouble();
            int    permutationsCount = (int)(speed * (Map.CityCount / 2)) + 1;
            List <Tuple <int, int> > permutations = new List <Tuple <int, int> >();

            for (int i = 0; i < permutationsCount; ++i)
            {
                Tuple <int, int> permutation = Aleatoriety.GetTwoDiferentValuesInRange(0, Map.CityCount);
                permutations.Add(permutation);
            }
            return(new TSPVelocity(permutations));
        }
        public override void PMXCrossover(IndividualBase parent1, IndividualBase parent2, out IndividualBase child1, out IndividualBase child2)
        {
            int[,] parent1Scheduling = (parent1 as TaskSchedulingSolution).GeneticMaterial;
            int[,] parent2Scheduling = (parent2 as TaskSchedulingSolution).GeneticMaterial;

            int startIdxInclusive = Aleatoriety.GetRandomInt(TaskCount - 1);
            int endIdxExclusive   = Aleatoriety.GetRandomInt(startIdxInclusive + 1, TaskCount + 1);

            int[,] L1 = new int[2, TaskCount];
            int[,] L2 = new int[2, TaskCount];

            int L1idx;
            int L2idx;

            for (int idx = 0; idx < TaskCount; ++idx)
            {
                if ((idx < startIdxInclusive) || (idx >= endIdxExclusive))
                {
                    int L1CopyFromTask      = parent1Scheduling[0, idx];
                    int L1CopyFromProcessor = parent1Scheduling[1, idx];
                    while ((L1idx = IndexOfTaskInInterval(L1CopyFromTask, startIdxInclusive, endIdxExclusive, parent2Scheduling)) != -1)
                    {
                        L1CopyFromTask      = parent1Scheduling[0, L1idx];
                        L1CopyFromProcessor = parent1Scheduling[1, L1idx];
                    }
                    L1[0, idx] = L1CopyFromTask;
                    L1[1, idx] = L1CopyFromProcessor;

                    int L2CopyFromTask      = parent2Scheduling[0, idx];
                    int L2CopyFromProcessor = parent2Scheduling[1, idx];
                    while ((L2idx = IndexOfTaskInInterval(L2CopyFromTask, startIdxInclusive, endIdxExclusive, parent1Scheduling)) != -1)
                    {
                        L2CopyFromTask      = parent2Scheduling[0, L2idx];
                        L2CopyFromProcessor = parent2Scheduling[1, L2idx];
                    }
                    L2[0, idx] = L2CopyFromTask;
                    L2[1, idx] = L2CopyFromProcessor;
                }
                else
                {
                    L1[0, idx] = parent2Scheduling[0, idx];
                    L1[1, idx] = parent2Scheduling[1, idx];
                    L2[0, idx] = parent1Scheduling[0, idx];
                    L2[1, idx] = parent1Scheduling[1, idx];
                }
            }

            child1 = new TaskSchedulingSolution(this, L1);
            child2 = new TaskSchedulingSolution(this, L2);
        }
        public TaskSchedulingSolution(ProblemBase problem) : base(problem)
        {
            TaskSchedulingProblem schedulingProblem = (Problem as TaskSchedulingProblem);

            this.GeneticMaterial = new int[2, schedulingProblem.TaskCount];

            IEnumerable <int> randomTaskPermutation = Aleatoriety.GetRandomIntegerSequencePermutation(0, schedulingProblem.TaskCount);

            int idx = 0;

            foreach (int task in randomTaskPermutation)
            {
                this.GeneticMaterial[0, idx] = task;
                this.GeneticMaterial[1, idx] = Aleatoriety.GetRandomInt(schedulingProblem.ProcessorCount);
                ++idx;
            }
        }
Exemple #9
0
        private Population_MultiObjective_AG Procreate(Population_MultiObjective_AG mating)
        {
            int expectedChildCount = InitialPopulationSize;

            Population_MultiObjective_AG newGeneration = new Population_MultiObjective_AG(Problem, expectedChildCount);

            while (newGeneration.IndividualCount < expectedChildCount)
            {
                // Selection Method
                IndividualBase parent1 = BinaryTournment(mating);
                IndividualBase parent2 = BinaryTournment(mating);
                IndividualBase child1 = null, child2 = null;

                // Crossover Method
                Problem.PMXCrossover(parent1, parent2, out child1, out child2);
                for (int i = 0; i < 10; ++i)
                {
                    if (!newGeneration.Content.Contains(child1) || i == 9)
                    {
                        newGeneration.AddIndividual(child1);
                        break;
                    }
                }
                for (int i = 0; i < 10; ++i)
                {
                    if (!newGeneration.Content.Contains(child2) || i == 9)
                    {
                        newGeneration.AddIndividual(child2);
                        break;
                    }
                }
            }

            foreach (IndividualBase individual in newGeneration.Content)
            {
                int aleatoryPercentage = Aleatoriety.GetRandomInt(100);
                if (aleatoryPercentage < mutationPct)
                {
                    Problem.MutateIndividual(individual);
                }
                Problem.ValidateIndividual(individual);
                IndividualEvaluator.Execute(individual, Problem);
            }
            return(newGeneration);
        }
        public override void MutateIndividual(IndividualBase individual)
        {
            CryptArithmeticSolution cryptoSolution = (individual as CryptArithmeticSolution);
            int amountOfChars    = charArray.Count();
            int firstPositionIdx = Aleatoriety.GetRandomInt(amountOfChars);
            int secondPositionIdx;

            do
            {
                secondPositionIdx = Aleatoriety.GetRandomInt(amountOfChars);
            } while (secondPositionIdx == firstPositionIdx);

            int tempValue = cryptoSolution.LettersValues[secondPositionIdx];

            cryptoSolution.LettersValues[secondPositionIdx] = cryptoSolution.LettersValues[firstPositionIdx];
            cryptoSolution.LettersValues[firstPositionIdx]  = tempValue;
            individual = cryptoSolution;
        }
Exemple #11
0
 public IndividualBase GetRandomIndividual()
 {
     return(Content[Aleatoriety.GetRandomInt(Content.Count)]);
 }
        public CryptArithmeticSolution(ProblemBase problem) : base(problem)
        {
            int amountOfChars = Enumerable.Range(0, 10).Count();

            LettersValues = Aleatoriety.GetRandomIntegerSequencePermutation(0, amountOfChars).ToArray();
        }
        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);
        }