Ejemplo n.º 1
0
        public bool Execute()
        {
            ga.Crossover     = new CustomCrossover();
            ga.GenomeFactory = new CustomFactory(0, parameters.Questoes.Count - 1, parameters.QtdQuestoes);

            switch (parameters.Seletor)
            {
            case SeletorGA.Roleta:
                ga.Selector = new WeightedSelector(ga.Genomes);
                break;

            case SeletorGA.Sequencial:
                ga.Selector = new SequentialSelector(ga.Genomes);
                break;

            case SeletorGA.Randomico:
                ga.Selector = new RandomSelector(ga.Genomes);
                break;
            }

            ga.Crossover.CrossoverProbability = parameters.ProbabilidadeReproducao / 100.0; //0.3;0.2;
            ga.Crossover.MutationProbability  = parameters.ProbabilidadeMutacao / 100.0;    //0.015;
            ga.Elitism = parameters.HabilitaElitismo;

            ga.CustomObj = parameters.Questoes;

            if (OnGANewGeneration != null)
            {
                ga.NewGeneration += new GeneticAlgorithmEventHandler(this.OnNewGeneration);
            }

            if (OnGANewBestFitness != null)
            {
                ga.NewBestFitness += new GeneticAlgorithmGenomeEventHandler(this.OnNewBestFitness);
            }

            ga.QryBestFitness += new GeneticAlgorithmCancelEventHandler(this.OnQryBestFitness);

            ga.Evaluator = new CustomEvaluator(parameters);

            ga.CreateGenomes(9);

            ga.ExitConditions.Duration    = TimeSpan.FromSeconds(parameters.MaxDuration);
            ga.ExitConditions.Generations = parameters.MaxGenerations;
            ga.ExitConditions.FitnessGoal = parameters.MinFitnessGoal;

            bestGenome = (CustomGenome)ga.Execute();

            stopTime = DateTime.Now;

            exitCondiction = ga.ExitConditions.ExitCondiction;

            bestGenome.UpdateStat(parameters);

            return(true);
        }
Ejemplo n.º 2
0
        public virtual bool DoesContinue(GeneticAlgorithm gaToEvaluate)
        {
            DateTime now = DateTime.Now;

            bool ret = true;

            lock (this)
            {
                ret = (!stopProcess) &&
                      (now - gaToEvaluate.StartTime) < Duration &&
                      gaToEvaluate.GenerationCount < Generations &&
                      gaToEvaluate.Genomes[gaToEvaluate.Genomes.Count - 1].Fitness < FitnessGoal;
            }

            if (!ret)
            {
                if (stopProcess)
                {
                    exitCondiction = ExitCondictionType.Stopped;
                }
                else if (gaToEvaluate.Genomes[gaToEvaluate.Genomes.Count - 1].Fitness >= FitnessGoal)
                {
                    exitCondiction = ExitCondictionType.FitnessGoal;
                }
                else if ((now - gaToEvaluate.StartTime) >= Duration)
                {
                    exitCondiction = ExitCondictionType.Overtime;
                }
                else if (gaToEvaluate.GenerationCount >= Generations)
                {
                    exitCondiction = ExitCondictionType.OverGenerationCount;
                }
            }

            return(ret);
        }