예제 #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);
        }