private static List<List<string>> GetGroupExportList(int groupIndex, Individual individual, TimetableData ttData)
        {
            //output[row][col]
            List<List<string>> output = InitializeExportList(individual.Groups, ttData);
            //title
            output[0][0] = ttData.Groups[groupIndex].Name;

            //Fill in the data
            for (int day = 0; day < individual.Groups.GetLength(1); day++)
            {
                for (int block = 0; block < individual.Groups.GetLength(2); block++)
                {
                    if (individual.Groups[groupIndex, day, block] != -1)
                    {
                        Course c = ttData.Courses[individual.Groups[groupIndex, day, block]];
                        if (individual.Courses[c.Index, day, block] != -1)
                        {
                            Room r = ttData.Rooms[individual.Courses[c.Index, day, block]];
                            output[block * 3 + 1][day + 1] = c.Name;
                            output[block * 3 + 2][day + 1] = r.Name;
                            for (int l = 0; l < c.Lecturers.Length; l++)
                            {
                                output[block * 3 + 3][day + 1] = output[block * 3 + 3][day + 1] + c.Lecturers[l].LastName + (l == c.Lecturers.Length - 1 ? "" : ", ");
                            }
                        }
                    }
                }
            }
            return output;
        }
 public static void ExportAllLecturers(Individual individual, TimetableData ttData, string filePath)
 {
     List<List<string>> output = new List<List<string>>();
     for (int lecuterer = 0; lecuterer < ttData.Lecturers.Length; lecuterer++)
     {
         output.AddRange(GetLecturerExportList(lecuterer, individual, ttData));
     }
     WriteCSV(filePath, output);
 }
 public static void ExportAllRooms(Individual individual, TimetableData ttData, string filePath)
 {
     List<List<string>> output = new List<List<string>>();
     for (int room = 0; room < ttData.Rooms.Length; room++)
     {
         output.AddRange(GetRoomExportList(room, individual, ttData));
     }
     WriteCSV(filePath, output);
 }
        private static void TestRequirements(TimetableData ttData, TimetableGenerator generator)
        {
            //Inspect every result population
            for (int pIndex = 0; pIndex < generator.Population.Length; pIndex++)
            {
                //Result must contain all defined courses
                Assert.AreEqual(0, GetCourseDiff(ttData, generator.Population[pIndex]),
                    "Not the expected number of courses found for individual " + pIndex + "!");

                for (int lIndex = 0; lIndex < ttData.Lecturers.Length; lIndex++)
                {
                    if (!ttData.Lecturers[lIndex].IsDummy)
                    {
                        //Every lecturer must have the defined number of courses
                        int expectedCourseCount = GetExpectedCourseCountForLecturer(ttData.Lecturers[lIndex], ttData);
                        int foundCourseCount = GetCourseCountForLecturer(generator.Population[pIndex], lIndex);
                        Assert.AreEqual(expectedCourseCount, foundCourseCount, "Not the expected number of courses found for lecturer " + lIndex + "!");

                        //Free days for research for lecturers
                        int freeDaysForLecturer = GetFreeDaysForLecturer(generator.Population[pIndex], lIndex, ttData);
                        Assert.IsTrue(ttData.Lecturers[lIndex].NeededNumberOfResearchDays <= freeDaysForLecturer,
                            "Not enough free days for research for lecturer " + lIndex);
                    }
                }

                for (int cIndex = 0; cIndex < ttData.Courses.Length; cIndex++)
                {
                    //Every course must occupy the specified room (course -> room -> course) and must be a room (room != -1)
                    Assert.IsTrue(CourseHasRoom(cIndex, generator.Population[pIndex].Courses, generator.Population[pIndex].Rooms, ttData),
                        "Course " + cIndex + " has no room!");

                    int ctr = CountRoomsForCourse(generator, pIndex, cIndex);
                    Assert.AreEqual(ttData.Courses[cIndex].NumberOfBlocks, ctr, "Not enough rooms specified for course " + cIndex);
                }

                for (int gIndex = 0; gIndex < ttData.Groups.Length; gIndex++)
                {
                    //Number of courses for each group must be as defined
                    int expectedCourseCount = GetExpectedCourseCountForGroup(ttData.Groups[gIndex], ttData);
                    int foundCourseCount = GetCourseCountForGroup(gIndex, generator.Population[pIndex]);
                    Assert.AreEqual(expectedCourseCount, foundCourseCount, "Not the expected number of courses for group " + gIndex + "!");
                }

                for (int b = 0; b < ttData.Blocks.Length; b++)
                {
                    //A defined exception block must not be occupied
                    foreach (var exceptionDay in ttData.Blocks[b].Exceptions)
                    {
                        for (int course = 0; course < generator.Population[pIndex].Courses.GetLength(0); course++)
                        {
                            Assert.AreEqual(-1, generator.Population[pIndex].Courses[course, (int)exceptionDay - 1, b],
                                "Course found for blockexception on day " + exceptionDay + ", block " + b + " !");
                        }
                    }
                }
            }
        }
 public static void ExportAllGroups(Individual individual, TimetableData ttData, string filePath)
 {
     List<List<string>> output = new List<List<string>>();
     for (int group = 0; group < ttData.Groups.Length; group++)
     {
         output.AddRange(GetGroupExportList(group, individual, ttData));
     }
     WriteCSV(filePath, output);
 }
 public static void ExportAll(Individual individual, TimetableData ttData, string filePath)
 {
     List<List<string>> output = new List<List<string>>();
     for (int group = 0; group < ttData.Groups.Length; group++)
     {
         output.AddRange(GetGroupExportList(group, individual, ttData));
     }
     for (int lecuterer = 0; lecuterer < ttData.Lecturers.Length; lecuterer++)
     {
         output.AddRange(GetLecturerExportList(lecuterer, individual, ttData));
     }
     for (int room = 0; room < ttData.Rooms.Length; room++)
     {
         output.AddRange(GetRoomExportList(room, individual, ttData));
     }
     WriteCSV(filePath, output);
 }
 private static bool CourseHasRoom(int cIndex, int[, ,] courses, int[, ,] rooms, TimetableData ttData)
 {
     for (int d = 0; d < courses.GetLength(1); d++)
     {
         for (int b = 0; b < courses.GetLength(2); b++)
         {
             if (courses[cIndex, d, b] != -1)
             {
                 if (rooms[courses[cIndex, d, b], d, b] != cIndex)
                     return false;
                 if (ttData.Rooms[courses[cIndex, d, b]].IsLab != ttData.Courses[cIndex].NeedsLab)
                     return false;
             }
         }
     }
     return true;
 }
 private static int GetExpectedCourseCountForGroup(Group group, TimetableData ttData)
 {
     int count = 0;
     for (int cIndex = 0; cIndex < ttData.Courses.Length; cIndex++)
     {
         if (ttData.Courses[cIndex].Group.Index == group.Index)
             count += ttData.Courses[cIndex].NumberOfBlocks;
     }
     return count;
 }
 private static int GetFreeDaysForLecturer(Individual individual, int lIndex, TimetableData ttData)
 {
     int freeDayCount = 0;
     for (int d = 0; d < individual.Lecturers.GetLength(1); d++)
     {
         if (ttData.Lecturers[lIndex].AvailableResearchDays.Contains(d))
         {
             bool isFree = true;
             for (int b = 0; b < individual.Lecturers.GetLength(2); b++)
             {
                 if (individual.Lecturers[lIndex, d, b] != -1)
                 {
                     isFree = false;
                 }
             }
             if (isFree)
                 freeDayCount++;
         }
     }
     return freeDayCount;
 }
 public static void ExportGroup(int groupIndex, Individual individual, TimetableData ttData, string filePath)
 {
     WriteCSV(filePath, GetGroupExportList(groupIndex, individual, ttData));
 }
        public void Load()
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "xml-Files (*.xml)|*.xml";
            fileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            fileDialog.ShowDialog();

            TimetableData t = new TimetableData();
            TimetableData ttData = TimetableConfigIO.ImportTimetableConfig(fileDialog.FileName);
            if (ttData == null)
            {
                MessageBox.Show("Data could not be loaded.", "Loading error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            BlockContext = new BlockController();
            foreach (var block in ttData.Blocks)
            {
                BlockContext.BlockList.Add(block);
            }
            if (ttData.Blocks.Length > 0)
                BlockContext.SelectedIndex = 0;

            RoomContext = new RoomController();
            foreach (var room in ttData.Rooms)
            {
                RoomContext.RoomList.Add(room);
            }
            if (ttData.Rooms.Length > 0)
                RoomContext.SelectedIndex = 0;

            GroupContext = new GroupController();
            foreach (var group in ttData.Groups)
            {
                GroupContext.GroupList.Add(group);
            }
            if (ttData.Groups.Length > 0)
                GroupContext.SelectedIndex = 0;

            LecturerContext = new LecturerController();
            foreach (var lecturer in ttData.Lecturers)
            {
                LecturerContext.LecturersList.Add(lecturer);
            }
            if (ttData.Lecturers.Length > 0)
                LecturerContext.SelectedIndex = 0;

            CourseContext = new CourseController(BlockContext, RoomContext, GroupContext, LecturerContext);
            AddCourses(CourseContext, ttData.Courses);
            if (ttData.Courses.Length > 0)
                CourseContext.SelectedIndex = 0;
        }
 private TimetableData GetTimetableData()
 {
     TimetableData ttData = new TimetableData();
     ttData.Blocks = BlockContext.BlockList.ToArray();
     ttData.Groups = GroupContext.GroupList.ToArray();
     ttData.Lecturers = LecturerContext.LecturersList.ToArray();
     ttData.Rooms = RoomContext.RoomList.ToArray();
     ttData.Courses = CourseContext.GetCourseExportArray();
     return ttData;
 }
 public static void ExportRoom(int roomIndex, Individual individual, TimetableData ttData, string filePath)
 {
     WriteCSV(filePath, GetRoomExportList(roomIndex, individual, ttData));
 }
        private static List<List<string>> InitializeExportList(int[, ,] elementData, TimetableData ttData)
        {
            List<List<string>> output = new List<List<string>>();

            //a column for each day and an additional row for the block display
            int colCount = elementData.GetLength(2) + 2;
            //a row for each block * 3 plus a row for day display and two extra rows for seperation to bottom
            int rowCount = (elementData.GetLength(1) + 1) * 3 + 3;
            for (int row = 0; row < rowCount; row++)
            {
                output.Add(new List<string>());
                for (int col = 0; col < colCount; col++)
                {
                    output[row].Add("");
                }
            }
            //blocks left
            for (int block = 0; block < ttData.Blocks.Length; block++)
            {
                output[block * 3 + 1][0] = ttData.Blocks[block].Start.ToShortTimeString() + "-" + ttData.Blocks[block].End.ToShortTimeString();
            }
            //days top
            for (int day = 0; day < elementData.GetLength(1); day++)
            {
                output[0][day + 1] = Enum.GetName(typeof(DayOfWeek), day + 1);
            }

            return output;
        }
 public static void ExportLecturer(int lecturerIndex, Individual individual, TimetableData ttData, string filePath)
 {
     WriteCSV(filePath, GetLecturerExportList(lecturerIndex, individual, ttData));
 }
 /// <summary>
 /// Create new timetable generator
 /// </summary>
 /// <param name="populationSize">size of the population to generate</param>
 /// <param name="tableData">timetable configuration</param>
 public TimetableGenerator(TimetableData tableData)
 {
     if (random == null)
         random = new Random(DateTime.Now.Millisecond);
     this.ttData = tableData;
 }
 private static int GetCourseDiff(TimetableData ttData, Individual individual)
 {
     int numberOfCoursesExpected = 0;
     for (int c = 0; c < ttData.Courses.Length; c++)
     {
         numberOfCoursesExpected += ttData.Courses[c].NumberOfBlocks;
     }
     int numberOfCoursesFound = 0;
     for (int c = 0; c < individual.Courses.GetLength(0); c++)
     {
         for (int d = 0; d < individual.Courses.GetLength(1); d++)
         {
             for (int b = 0; b < individual.Courses.GetLength(2); b++)
             {
                 if (individual.Courses[c, d, b] != -1)
                     numberOfCoursesFound++;
             }
         }
     }
     return numberOfCoursesExpected - numberOfCoursesFound;
 }
Beispiel #18
0
 private List<int> GetLecturers(int course, TimetableData ttData)
 {
     List<int> lecturers = new List<int>();
     for (int lecturer = 0; lecturer < ttData.Courses[course].Lecturers.Length; lecturer++)
     {
         lecturers.Add(ttData.Courses[course].Lecturers[lecturer].Index);
     }
     return lecturers;
 }
 private static int GetExpectedCourseCountForLecturer(Lecturer lecturer, TimetableData ttData)
 {
     int count = 0;
     foreach (Course c in ttData.Courses)
     {
         for (int lIndex = 0; lIndex < c.Lecturers.Length; lIndex++)
         {
             if (c.Lecturers[lIndex].Index == lecturer.Index)
                 count += c.NumberOfBlocks;
         }
     }
     return count;
 }
 public void New()
 {
     _currentConfigFilepath = "";
     TimetableData ttData = new TimetableData();
     BlockContext = new BlockController();
     RoomContext = new RoomController();
     LecturerContext = new LecturerController();
     GroupContext = new GroupController();
     CourseContext = new CourseController(BlockContext, RoomContext, GroupContext, LecturerContext);
 }
        public static bool ExportTimetableConfig(TimetableData ttData, string configurationFilePath)
        {
            try
            {
                StringBuilder output = new StringBuilder();
                output.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                output.AppendLine("<timetableData>");

                AppendBlocks(ttData.Blocks, output);
                AppendLecturers(ttData.Lecturers, output);
                AppendRooms(ttData.Rooms, output);
                AppendGroups(ttData.Groups, output);
                AppendCourses(ttData.Courses, output);

                output.AppendLine("</timetableData>");
                File.WriteAllText(configurationFilePath, output.ToString());
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Export failed! " + ex.Message);
                return false;
            }
        }
Beispiel #22
0
        internal void ClearChromosome(int course, TimetableData ttData)
        {
            int room = GetRoom(course);
            int group = ttData.Courses[course].Group.Index;
            List<int> lecturers = GetLecturers(course, ttData);

            for (int day = 0; day < Courses.GetLength(1); day++)
            {
                for (int block = 0; block < Courses.GetLength(2); block++)
                {
                    SetCourse(course, day, block, -1);
                    if (Rooms[room, day, block] == course)
                        SetRoom(room, day, block, -1);
                    if (Groups[group, day, block] == course)
                        SetGroup(group, day, block, -1);
                    foreach (int lecturer in lecturers)
                    {
                        if (Lecturers[lecturer, day, block] == course)
                            SetLecturer(lecturer, day, block, -1);
                    }
                }
            }
        }