public void EndInit() { if (this.mIsInitBegan == false) { throw new GeneticAlgorithmInitializationNotStartedException("EndInit() invoked before BeginInit()"); } try { this.CheckInitFinished(); } catch (GeneticAlgorithmNotInitializedException e) { throw new GeneticAlgorithmNotInitializedException("Initialization not completed", e); } this.mCurrentGeneration = new List<Schedule> { }; var data = from p in this.mData group p by p.Class into gr select new { Class = gr.Key, Subjects = from t in gr group t by t.Subject into c select new { Subject = c.Key, Groups = c } }; List<Day> timetable = new List<Day>{ }; for (int i = 0; i < mDaysCount; ++i) { Day d = new Day(new List<ClassSchedule>{}); for (int j = 0;j < data.Count(); ++j) { List<Lesson> lessons = new List<Lesson>{}; for (int k = 0; k < PeriodsCount; ++k) { lessons.Add(null); } ClassSchedule cs = new ClassSchedule(null, lessons); d.Classes.Add(cs); } timetable.Add(d); } int day = 0; int period = 0; int classN = 0; int hoursWeekly; foreach (var item in data) { day = 0; period = 0; for (int i = 0; i < Days; ++i) { timetable[i].Classes[classN].Class = item.Class; } foreach (var current in item.Subjects) { hoursWeekly = 0; List<Group> groups = new List<Group>(); foreach (Curriculum curric in current.Groups) { hoursWeekly = curric.HoursPerWeek; Group g = new Group(curric.Teacher, curric.Group, curric.DesiredClassroom, curric.AlternativeClassrooms); groups.Add(g); } for (int i = 0; i < hoursWeekly; i++) { Lesson l = new Lesson(item.Class, current.Subject, groups); timetable[day].Classes[classN].Lessons[period] = l; if (period == PeriodsCount - 1) { day++; period = 0; } else { period++; } } } classN++; } Random rnd = new Random(); Schedule s = new Schedule(timetable, mSubjectsDifficulty); SaveCopy = new Schedule(s.Timetable, mSubjectsDifficulty); s.Shuffle(rnd); this.mLastSuiting = s; this.mSuitingConstraints = s; this.mCurrentGeneration.Add(s); for (int i = 0; i < GenerationSize * 10; ++i) { s = new Schedule(timetable, mSubjectsDifficulty); s.Shuffle(rnd); this.mCurrentGeneration.Add(s); } EqualityComparer<Schedule> comp = new ScheduleComparator(); this.mCurrentGeneration = mCurrentGeneration.OrderByDescending(x => x.Rating.Errors). Distinct(comp).ToList(); this.mCurrentGeneration = mCurrentGeneration.GetRange(0, GenerationSize); this.mIsInitialized = true; }