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]; } }
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); }