예제 #1
0
        protected override void PerformMutate(IChromosome chromosome, float probability)
        {
            Random r = new Random();

            if (r.NextDouble() <= probability)
            {
                var test = chromosome as Test;

                var    possibleQuesitons = dataAccess.GetQuestionsWhichContainDomains(oblasti);
                var    fFunc             = new TestFitness(oblasti, zastupljenost, 0);
                double fitness           = fFunc.Evaluate(test);


                int bestIndex = -1;
                int qIndex;
                do
                {
                    qIndex = r.Next(possibleQuesitons.Count);
                    for (int index = 0; index < test.questions.Count; index++)
                    {
                        var q = test.CreateNew() as Test;

                        q.questions[index] = possibleQuesitons[qIndex];
                        if (!q.HasDuplicate() && fFunc.Evaluate(q) >= fitness)
                        {
                            bestIndex = index;
                        }
                    }
                } while (bestIndex == -1);

                test.questions[bestIndex] = possibleQuesitons[qIndex];
            }
        }
예제 #2
0
        protected override IList <IChromosome> PerformCross(IList <IChromosome> parents)
        {
            List <IChromosome> tests = new List <IChromosome>();
            Random             r     = new Random();

            for (int i = 0; i < ParentsNumber - 1; i += 2)
            {
                Test test1 = parents[i] as Test;
                Test test2 = parents[i + 1] as Test;

                List <Question> allQuestions = new List <Question>();
                allQuestions.AddRange(test1.questions);
                allQuestions.AddRange(test2.questions);
                allQuestions = allQuestions.Distinct().ToList();

                Test        bestChild       = null;
                TestFitness fFunc           = new TestFitness(oblasti, zastupljenost, 0);
                int         BrojPermutacija = test1.questions.Count * 50;
                for (int j = 0; j < BrojPermutacija; j++)
                {
                    MiSe.Shuffle.ShuffleOps.ShuffleInPlace(allQuestions, r);
                    Test temp = new Test(allQuestions.Take(test1.questions.Count).ToList(), test1.questions.Count);
                    if (bestChild == null)
                    {
                        bestChild = temp;
                    }
                    else
                    {
                        if (fFunc.Evaluate(temp) > fFunc.Evaluate(bestChild))
                        {
                            bestChild = temp;
                        }
                    }
                }

                tests.Add(bestChild);
            }

            return(tests);
        }
예제 #3
0
        public Test UseAlgorithm(List <int> oblasti, List <double> zastupljenost, int Test_Length)
        {
            Random      r          = new Random(10);
            IDataAccess dataAccess = DataAccess.DataAccess.GetInstance();

            List <Question> qs = new List <Question>();

            for (int i = 0; i < Test_Length; i++)
            {
                qs.Add(dataAccess.GetQuestionById(1 + r.Next(1000)));
            }

            Test adamTest = new Test(qs, Test_Length);

            List <double> zastupljenost_kao_br_pitanja = new List <double>();

            foreach (var vrednost in zastupljenost)
            {
                zastupljenost_kao_br_pitanja.Add(Math.Round(vrednost * Test_Length + 0.01));
            }

            var fitness = new TestFitness(oblasti, zastupljenost_kao_br_pitanja, 0);

            IGenerationStrategy generationStrategy = new PerformanceGenerationStrategy(3);
            var population = new TestPopulation(20, 50, adamTest, oblasti)
            {
                GenerationStrategy = generationStrategy
            };

            ISelection selection = new EliteSelection();

            ICrossover crossover = new TestCrossover(oblasti, zastupljenost_kao_br_pitanja, 8, 4);

            IMutation mutation = new TestMutation(oblasti, zastupljenost_kao_br_pitanja);

            ITermination termination = new OrTermination(new ITermination[] { new TestTermination(100, oblasti),
                                                                              new FitnessThresholdTermination(oblasti.Count),
                                                                              new GenerationNumberTermination(3000) });

            GeneticAlgorithm ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
            {
                Termination          = termination,
                CrossoverProbability = 1f,
                MutationProbability  = 0.7f
            };


            double      oldFitness = 0;
            List <Test> tests      = new List <Test>();

            //Callback
            ga.GenerationRan += (sender, arg) =>
            {
                if (ga.BestChromosome.Fitness >= oldFitness)
                {
                    oldFitness = ga.BestChromosome.Fitness.Value;
                    tests.Add(ga.BestChromosome as Test);
                    //Console.WriteLine($"Current fitness: {ga.BestChromosome.Fitness} for generation : {ga.GenerationsNumber}");
                }
            };

            ga.Start();

            if (oldFitness == 0)
            {
                return(null);
            }
            var  lista          = tests.Where(x => !x.HasDuplicate()).OrderByDescending(x => x.Fitness.Value).ToList();
            Test bestChromosome = lista.FirstOrDefault();

            foreach (var q in bestChromosome.questions)
            {
                q.Izabrano = true;
            }

            return(bestChromosome);
        }