Exemple #1
0
        public bool AddLesson(TheClass theClass, TheAppointment teacher)
        {
            if (TeacherToClass.ContainsKey(teacher) || ClassToTeacher.ContainsKey(theClass))
            {
                return(false);//в этот час уже есть пара у препода или у группы
            }
            ClassToTeacher[theClass] = teacher;
            TeacherToClass[teacher]  = theClass;

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Добавить группу с преподом на любой час
        /// </summary>
        bool AddToAnyHour(byte day, TheClass theClass, TheAppointment appointment)
        {
            for (byte hour = 0; hour < HoursPerDay; hour++)
            {
                var les = new Lessоn(day, hour, theClass, appointment);
                if (AddLesson(les))
                {
                    return(true);
                }
            }

            return(false);//нет свободных часов в этот день
        }
        public bool CreateSchedule()
        {
            if (appointmentsNumber == 0)
            {
                return(false);
            }

            plans = new List <Plan>();

            if (studyingSystem == 6)
            {
                Plan.SetStudyingSystem(6, 6);

                // Initializing data storages
                int numberOfLessons = studyingSystem * lessonsPerDay;

                for (int planI = 0; planI < 100; planI++)
                {
                    int[,] graph         = new int[appointmentsNumber, appointmentsNumber];
                    int[,] allowedColors = new int[appointmentsNumber, numberOfLessons];
                    List <int> verticesColors = new List <int>();

                    for (int i = 0; i < appointmentsNumber; i++)
                    {
                        for (int j = 0; j < appointmentsNumber; j++)
                        {
                            graph[i, j] = 0;
                        }
                    }

                    for (int i = 0; i < appointmentsNumber; i++)
                    {
                        for (int j = 0; j < numberOfLessons; j++)
                        {
                            allowedColors[i, j] = 0;
                        }
                    }

                    for (int i = 0; i < appointmentsNumber; i++)
                    {
                        verticesColors.Add(-1);
                    }

                    for (int i = 0; i < appointmentsNumber; i++)
                    {
                        for (int j = 0; j < appointmentsNumber; j++)
                        {
                            if (j == i)
                            {
                                graph[i, j] = 0;
                            }
                            else if (appointments[j].Grade == appointments[i].Grade && appointments[j].ClassName == appointments[i].ClassName)
                            {
                                graph[i, j] = 1;
                            }
                            else if (appointments[j].Teacher == appointments[i].Teacher)
                            {
                                graph[i, j] = 1;
                            }
                            else
                            {
                                graph[i, j] = 0;
                            }
                        }
                    }

                    for (int i = 0; i < appointmentsNumber; i++)
                    {
                        for (int j = 0; j < numberOfLessons; j++)
                        {
                            if (appointments[i].Grade == 1)
                            {
                                if (j > 23 || j % 6 == 5)
                                {
                                    allowedColors[i, j] = 1;
                                }
                            }
                            else if (appointments[i].Grade == 2)
                            {
                                if (j > 30 || j % 6 == 5)
                                {
                                    allowedColors[i, j] = 1;
                                }
                            }
                        }
                    }

                    for (int i = 0; i < appointmentsNumber; i++)
                    {
                        if (verticesColors[i] == -1)
                        {
                            for (int j = 0; j < numberOfLessons; j++)
                            {
                                if (allowedColors[i, j] == 0)
                                {
                                    verticesColors[i] = j;

                                    for (int k = 0; k < appointmentsNumber; k++)
                                    {
                                        if (graph[k, i] == 1)
                                        {
                                            allowedColors[k, j] = 1;
                                        }
                                    }
                                    break;
                                }
                            }

                            if (verticesColors[i] == -1)
                            {
                                verticesColors[i] = 0;
                            }
                        }
                    }

                    int penalty = 1;

                    while (penalty >= 10)
                    {
                        penalty = 0;

                        List <int> conflictVertices = new List <int>();

                        for (int i = 0; i < appointmentsNumber; i++)
                        {
                            for (int j = 0; j < appointmentsNumber; j++)
                            {
                                if (graph[i, j] == 1 && i != j && verticesColors[i] == verticesColors[j])
                                {
                                    if (!conflictVertices.Contains(i))
                                    {
                                        conflictVertices.Add(i);
                                    }
                                    if (!conflictVertices.Contains(j))
                                    {
                                        conflictVertices.Add(j);
                                    }
                                }
                            }
                        }

                        penalty = conflictVertices.Count;

                        foreach (int vertice in conflictVertices)
                        {
                            List <int> allowedColorsForVertice = new List <int>();
                            for (int i = 0; i < numberOfLessons; i++)
                            {
                                if (allowedColors[vertice, i] == 0)
                                {
                                    allowedColorsForVertice.Add(i);
                                }
                            }

                            Random random = new Random();

                            if (allowedColorsForVertice.Count > 0)
                            {
                                verticesColors[vertice] = allowedColorsForVertice[random.Next(allowedColorsForVertice.Count)];
                            }
                            else
                            {
                                if (appointments[vertice].Grade == 1)
                                {
                                    do
                                    {
                                        verticesColors[vertice] = random.Next(numberOfLessons);
                                    } while (verticesColors[vertice] > 23 || verticesColors[vertice] % 6 == 5);
                                }
                                else if (appointments[vertice].Grade == 2)
                                {
                                    do
                                    {
                                        verticesColors[vertice] = random.Next(numberOfLessons);
                                    } while (verticesColors[vertice] > 30 || verticesColors[vertice] % 6 == 5);
                                }
                                else
                                {
                                    verticesColors[vertice] = random.Next(numberOfLessons);
                                }
                            }
                        }
                    }

                    Console.WriteLine("Plan #{0} generated", planI);

                    var plan = new Plan();
                    for (int i = 0; i < appointmentsNumber; i++)
                    {
                        var theClass = new TheClass
                        {
                            Grade     = appointments[i].Grade,
                            ClassName = appointments[i].ClassName
                        };
                        var theAppointment = new TheAppointment
                        {
                            subject = appointments[i].Subject,
                            teacher = appointments[i].Teacher
                        };

                        byte day = (byte)(verticesColors[i] % 6);
                        byte hour;

                        if (verticesColors[i] <= 5)
                        {
                            hour = 0;
                        }
                        else if (verticesColors[i] <= 11)
                        {
                            hour = 1;
                        }
                        else if (verticesColors[i] <= 17)
                        {
                            hour = 2;
                        }
                        else if (verticesColors[i] <= 23)
                        {
                            hour = 3;
                        }
                        else if (verticesColors[i] <= 29)
                        {
                            hour = 4;
                        }
                        else
                        {
                            hour = 5;
                        }

                        var lesson = new Lessоn(day, hour, theClass, theAppointment);
                        plan.AddLesson(lesson);
                    }

                    plans.Add(plan);
                }

                //GeneticScheduler.GeneticSchedulerMain(plans);
            }



            return(true);
        }
Exemple #4
0
 public Lessоn(TheClass theClass, TheAppointment appointment)
 {
     TheClass    = theClass;
     Appointment = appointment;
 }
Exemple #5
0
 public Lessоn(byte day, byte hour, TheClass theClass, TheAppointment appointment)
     : this(theClass, appointment)
 {
     Day  = day;
     Hour = hour;
 }
Exemple #6
0
 public void RemoveLesson(TheClass theClass, TheAppointment teacher)
 {
     ClassToTeacher.Remove(theClass);
     TeacherToClass.Remove(teacher);
 }