コード例 #1
0
ファイル: CustomCrossover.cs プロジェクト: flipee/TestGen
        public virtual Genome Crossover(Genome first, Genome second, int generation)
        {
            CustomGenome firstR  = (CustomGenome)first;
            CustomGenome secondR = (CustomGenome)second;
            double       d       = 0;

            bool changed = false;

            for (int g = 0; g < firstR.Length; g++)
            {
                d = GeneticAlgorithmUtility.RandomProvider.NextDouble();

                if (d <= crossoverP)
                {
                    firstR[g] = secondR[g];
                    changed   = true;
                }

                d = GeneticAlgorithmUtility.RandomProvider.NextDouble();

                if (d <= mutationP)
                {
                    firstR[g] = GeneticAlgorithmUtility.RandomProvider.Next(0, firstR.MaxValue + 1);
                    changed   = true;
                }
            }

            if (changed)
            {
                firstR.Generation = generation;
            }

            return(first);
        }
コード例 #2
0
        public Genome CreateGenome(GeneticAlgorithm parent)
        {
            CustomGenome newCustomGenome = new CustomGenome(parent, numberOfValues);

            newCustomGenome.MaxValue = maxValue;
            newCustomGenome.MinValue = minValue;
            RandomizeGenome(newCustomGenome);
            return(newCustomGenome);
        }
コード例 #3
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);
        }
コード例 #4
0
        public void OnQryBestFitness(GeneticAlgorithm sender, GenomeCancelEventArgs args)
        {
            CustomGenome genomeCustom = (CustomGenome)args.Genome;

            String key = genomeCustom.ToOrderedString();

            AvaliacaoAnterior a = parameters.AvaliacoesAnteriores.Find(x => x.Key.Equals(key));

            if (genomeCustom.QtdTotal() != parameters.QtdQuestoes || (a != null && (DateTime.Today - a.DtUtilizacao).Days <= parameters.QtdNaoRepetirAvaliacao))
            {
                args.Cancel = true;
            }
        }
コード例 #5
0
        public void OnNewBestFitness(GeneticAlgorithm sender, GenomeEventArgs args)
        {
            CustomGenome genome = (CustomGenome)args.Genome;

            genome.UpdateStat(parameters);

            //int hash = genome.GetHashCode();

            if (OnGANewBestFitness != null)
            {
                CustomGenomeEventArgs newArgs = new CustomGenomeEventArgs();

                newArgs.Genome = genome;

                OnGANewBestFitness(this, newArgs);
            }
        }
コード例 #6
0
        public void RandomizeGenome(CustomGenome value)
        {
            List <int> list = new List <int>();

            for (int i = 0; i < value.Length; i++)
            {
                while (true)
                {
                    value[i] = GeneticAlgorithmUtility.RandomProvider.Next(minValue, maxValue + 1);

                    if (value[i] > maxValue)
                    {
                        value[i] = maxValue;
                    }

                    if (!list.Exists(x => x == value[i]))
                    {
                        list.Add(value[i]);
                        break;
                    }
                }
            }
        }
コード例 #7
0
        public double Eval(Genome candidate)
        {
            CustomGenome genome = (CustomGenome)candidate;

            genome.UpdateStat(parameters);

            double ret = 0;

            ret += Math.Abs(genome.QtdTotal() - genome.Length) * 200;

            if (parameters.TempoPrevistoAvaliacao > 0)
            {
                ret += Math.Abs(genome.TempoTotal() - parameters.TempoPrevistoAvaliacao);
            }

            foreach (ConfigComplexidade rule in parameters.ComplexRules)
            {
                ret += Math.Abs(genome.ComplexTotal(rule.ComplexIni, rule.ComplexFim) - rule.Quantidade) * 100;
            }

            int tipoQuestao;

            foreach (ConfigTipoQuestao rule in parameters.TipoQuestaoRules)
            {
                tipoQuestao = (int)rule.TipoQuestao;

                ret += Math.Abs(genome.TipoTotal(tipoQuestao, tipoQuestao) - rule.Quantidade) * 100;
            }

            if (ret > (double)int.MaxValue)
            {
                ret = (double)int.MaxValue;
            }

            return((((double)int.MaxValue) - ret) / ((double)int.MaxValue) * 100.0);
        }