public async Task GenerateSchedule() { var periodTimeslots = (await PeriodTimeslotRepository.GetEntityListAsync()) .Select(x => PeriodTimeslotConverter.Convert(x)).ToList(); var classrooms = (await ClassroomRepository.GetEntityListAsync()) .Select(x => ClassroomConverter.Convert(x)).ToList(); var teachingUnits = (await TeachingUnitRepository.GetEntityListAsync()) .Select(x => TeachingUnitConverter.Convert(x)).ToList(); var generation = new GeneticScheduleGeneration(10, teachingUnits.Count); generation.GlobalGA.IterationCompleted += async() => { var bestChromosome = generation.GlobalGA.ChromosomePool.First(); await HubContext.Clients.All.SendAsync("SendScheduleInfo", new ScheduleInfo() { MaxValue = bestChromosome.Value, AverageValue = generation.GlobalGA.Pool.Average(x => x.Value), AverageAge = generation.GlobalGA.Pool.Average(x => x.Age), Evaluations = { Function1.Count(bestChromosome.Schedule), Function3.Count(bestChromosome.Schedule), Function4.Count(bestChromosome.Schedule), Function5.Count(bestChromosome.Schedule), Function6.Count(bestChromosome.Schedule), Function7.Count(bestChromosome.Schedule) } }); }; var schedules = generation.Run(classrooms, periodTimeslots, teachingUnits); await ScheduleCellRepository.Clear(); await ScheduleCellRepository.AddRangeAsync(schedules.First().ScheduleCells.Select(x => ScheduleCellConverter.Convert(x))); var gridResponse = await GetScheduleGridPrivate(); await HubContext.Clients.All.SendAsync("GenerateScheduleCompleted", gridResponse); }
private async Task <GridResponse> GetScheduleGridPrivate() { var periodTimeslots = await PeriodTimeslotRepository.GetEntityListAsync(); var weeks = ScheduleConverter.Convert(periodTimeslots); var classrooms = await ClassroomRepository.GetEntityListAsync(); var buildings = ScheduleConverter.Convert(classrooms); var schedule = await ScheduleRepository.GetEntityListAsync(); var cells = new List <List <ScheduleCellResponse> >(); foreach (var building in buildings) { foreach (var room in building.ClassroomsResponse) { var row = new List <ScheduleCellResponse>(); foreach (var week in weeks) { foreach (var day in week.DaysResponse) { foreach (var dayTimeslot in day.DayTimeslotsResponse) { var cell = new ScheduleCellResponse(); var isBlocked = classrooms .FirstOrDefault(c => c.Id == room.Id) ?.BanClassroomPeriodTimeslots .Any(b => b.PeriodTimeslotId == dayTimeslot.Id) ?? false; if (isBlocked) { cell.CellType = CellType.Blocked; } var teachingUnit = schedule .FirstOrDefault(s => s.ClassroomId == room.Id && s.PeriodTimeslotId == dayTimeslot.Id) ?.TeachingUnit; if (teachingUnit != null) { cell.GroupName = $"{teachingUnit.Group.Name} ({teachingUnit.Group.StudentsCount} чел.)"; cell.CourseName = teachingUnit.Course.Name; cell.LessonTypeName = $"{teachingUnit.LessonType.Name}"; cell.TeacherName = $"{teachingUnit.Teacher.SecondName} {teachingUnit.Teacher.FirstName.Substring(0, 1)}. {teachingUnit.Teacher.MiddleName?.Substring(0, 1)}."; cell.TeacherPost = $"({teachingUnit.Teacher.Post.Description})"; cell.CellType = teachingUnit.LessonTypeId == 1 ? CellType.Lecture : teachingUnit.LessonTypeId == 2 ? CellType.Practice : teachingUnit.LessonTypeId == 3 ? CellType.LaboratoryWork : CellType.Empty; } row.Add(cell); } } } cells.Add(row); } } return(new GridResponse() { Weeks = weeks, Buildings = buildings, Cells = cells }); }