Ejemplo n.º 1
0
 public void RunAlgorithm(DBManager db, int days, int MaximumHours, AlgorithmParameters Params)
 {
     this.ga = new MalvinaAlgorithm();
     ga.BeginInit();
     ga.Days = days;
     ga.PeriodsCount = MaximumHours;
     List<Curriculum> data = db.Lessons;
     ga.Data = data;
     ga.SubjectsDifficulty = Params;
     try
     {
         ga.EndInit();
     }
     catch (GeneticAlgorithmNotInitializedException e)
     {
         if (e.InnerException is GeneticAlgorithmDataIsEmptyException)
         {
             this.Interface.MainForm.SendMessage("Моля, въведете уроци.");
         }
         else if (e.InnerException is GeneticAlgorithmDataIsNullException)
         {
             this.Interface.MainForm.SendMessage("");
         }
         else if (e.InnerException is GeneticAlgorithmDaysNegativeNumberException)
         {
             this.Interface.MainForm.SendMessage("");
         }
         else if (e.InnerException is GeneticAlgorithmDifficultyPatternIsNullException)
         {
             this.Interface.MainForm.SendMessage("");
         }
         else if (e.InnerException is GeneticAlgorithmPeriodsNegativeNumberException)
         {
             this.Interface.MainForm.SendMessage("");
         }
     }
     algoThr = new System.Threading.Thread(delegate()
     {
         ga.Generate();
         ReportGenerationAlgorithmFinished();
     });
     algoThr.Start();
 }
Ejemplo n.º 2
0
        public Schedule(Schedule firstParent, Schedule secondParent, AlgorithmParameters parameters)
            : this()
        {
            this.DifficultyPattern = parameters;
            if (firstParent.Timetable.Count != secondParent.Timetable.Count)
            {
                throw new GeneticAlgorithmException("Passed pair of parents with different count of days");
            }

            #region Lessons combination

            List<Day> d = new List<Day>{};
            Dictionary<Tuple<Lesson, Parent>, List<Index>> dict = new Dictionary<Tuple<Lesson, Parent>, List<Index>>{ };
            Dictionary<Index, List<Tuple<Lesson, Parent>>> indexesDict = new Dictionary<Index,List<Tuple<Lesson,Parent>>>();
            for (int day = 0; day < firstParent.Timetable.Count; ++day)
            {
                if (firstParent.Timetable[day].Classes.Count != secondParent.Timetable[day].Classes.Count)
                {
                    throw new GeneticAlgorithmException("Passed pair of parents with different classes for a given day");
                }
                List<ClassSchedule> schedules = new List<ClassSchedule>{ };
                for (int classN = 0; classN < firstParent.Timetable[day].Classes.Count; ++classN)
                {
                    List<Lesson> lessons = new List<Lesson>{ };
                    for (int lesson = 0; lesson < Math.Max(firstParent.Timetable[day].Classes[classN].Lessons.Count,
                                                           secondParent.Timetable[day].Classes[classN].Lessons.Count); ++lesson)
                    {
                        bool should = false;
                        /*if (firstParent.Timetable[day].Classes[classN].Lessons[lesson].Equals(secondParent.Timetable[day].Classes[classN].Lessons[lesson]))
                        {
                            lessons.Add(firstParent.Timetable[day].Classes[classN].Lessons[lesson]);
                        }
                        else
                        {
                            lessons.Add(null);

                        }*/
                        if (firstParent.Timetable[day].Classes[classN].Lessons[lesson] == null && secondParent.Timetable[day].Classes[classN].Lessons[lesson] == null)
                        {
                            lessons.Add(null);
                        }
                        else
                        {
                            if (firstParent.Timetable[day].Classes[classN].Lessons[lesson] != null &&
                                secondParent.Timetable[day].Classes[classN].Lessons[lesson] != null)
                            {
                                if (firstParent.Timetable[day].Classes[classN].Lessons[lesson].Equals(secondParent.Timetable[day].Classes[classN].Lessons[lesson]))
                                {
                                    lessons.Add(secondParent.Timetable[day].Classes[classN].Lessons[lesson]);
                                }
                                else
                                {
                                    should = true;
                                    lessons.Add(null);
                                }
                            }
                            else
                            {
                                should = true;
                                lessons.Add(null);
                            }
                        }
                        if (should)
                        {
                            #region dictionary
                            Index shit = new Index(day, classN, lesson);
                            Tuple<Lesson, Parent> t =
                                new Tuple<Lesson, Parent>(firstParent.Timetable[day].Classes[classN].Lessons[lesson], Parent.FirstParent);
                            indexesDict.Add(shit, new List<Tuple<Lesson,Parent>>{ t });
                            if (dict.ContainsKey(t)) dict[t].Add(shit);
                            else dict.Add(t, new List<Index> { shit });
                            t = new Tuple<Lesson, Parent>(secondParent.Timetable[day].Classes[classN].Lessons[lesson], Parent.SecondParent);
                            indexesDict[shit].Add(t);
                            if (dict.ContainsKey(t)) dict[t].Add(shit);
                            else dict.Add(t, new List<Index> { shit });
                            #endregion
                        }
                    }
                    ClassSchedule cs = new ClassSchedule(firstParent.Timetable[day].Classes[classN].Class, lessons);
                    schedules.Add(cs);
                }
                Day current = new Day(schedules);
                d.Add(current);
            }
            this.mDays = d;
            Tuple<Lesson, Parent>[] a = new Tuple<Lesson,Parent>[dict.Keys.Count];
            dict.Keys.CopyTo(a, 0);
            int rand;
            Random rnd = new Random();
            #region Old resolving
            /*
            int left = dict.Count;
            while (dict.Count != 0 && left > 0)
            {
                rand = rnd.Next(left);
                Tuple<Lesson, Parent> t = a[rand];
                if (!dict.ContainsKey(t))
                {
                    goto end;
                }
                List<Index> li = dict[t];
                Index inx = li[li.Count - 1];
                li.RemoveAt(li.Count - 1);
                if (li.Count == 0)
                {
                    if (t.Item2 == Parent.FirstParent)
                    {
                        dict.Remove(new Tuple<Lesson,Parent>(t.Item1, Parent.SecondParent));
                    }
                    else
                    {
                        dict.Remove(new Tuple<Lesson,Parent>(t.Item1, Parent.FirstParent));
                    }
                    dict.Remove(t);
                }
                if (t.Item2 == Parent.FirstParent)
                {
                    d[inx.Day].Classes[inx.ClassN].Lessons[inx.Period] = firstParent.Timetable[inx.Day].Classes[inx.ClassN].Lessons[inx.Period];
                }
                else
                {
                    d[inx.Day].Classes[inx.ClassN].Lessons[inx.Period] = secondParent.Timetable[inx.Day].Classes[inx.ClassN].Lessons[inx.Period];
                }
            end:
                left--;
                Tuple<Lesson, Parent> swap = a[rand];
                a[rand] = a[left];
                a[left] = swap;
            }*/
            #endregion
            int ConflictsCount = 0;
            #region New Resolving - up-to-date at 21 March of 2011
            foreach (var item in dict)
            {
                ConflictsCount += item.Value.Count;
            }
            ConflictsCount /= 2;
            int ConflictsResolved = 0;
            try
            {
                while (ConflictsResolved < ConflictsCount)
                {
                    #region build indexesDict
                    indexesDict = new Dictionary<Index, List<Tuple<Lesson, Parent>>>();
                    for (int day = 0; day < firstParent.Timetable.Count; ++day)
                    {
                        if (firstParent.Timetable[day].Classes.Count != secondParent.Timetable[day].Classes.Count)
                        {
                            throw new GeneticAlgorithmException("Passed pair of parents with different classes for a given day");
                        }
                        for (int classN = 0; classN < firstParent.Timetable[day].Classes.Count; ++classN)
                        {
                            for (int lesson = 0; lesson < Math.Max(firstParent.Timetable[day].Classes[classN].Lessons.Count,
                                                                   secondParent.Timetable[day].Classes[classN].Lessons.Count); ++lesson)
                            {
                                bool should = false;
                                if (firstParent.Timetable[day].Classes[classN].Lessons[lesson] == null && secondParent.Timetable[day].Classes[classN].Lessons[lesson] == null)
                                {
                                    should = false;
                                }
                                else
                                {
                                    if (firstParent.Timetable[day].Classes[classN].Lessons[lesson] != null &&
                                        secondParent.Timetable[day].Classes[classN].Lessons[lesson] != null)
                                    {
                                        if (firstParent.Timetable[day].Classes[classN].Lessons[lesson].Equals(secondParent.Timetable[day].Classes[classN].Lessons[lesson]))
                                        {
                                            should = false;
                                        }
                                        else
                                        {
                                            should = true;
                                        }
                                    }
                                    else
                                    {
                                        should = true;
                                    }
                                }
                                if (this.Timetable[day].Classes[classN].Lessons[lesson] != null)
                                    if (should)
                                    {
                                        #region dictionary
                                        Index shit = new Index(day, classN, lesson);
                                        Tuple<Lesson, Parent> t =
                                            new Tuple<Lesson, Parent>(firstParent.Timetable[day].Classes[classN].Lessons[lesson], Parent.FirstParent);
                                        indexesDict.Add(shit, new List<Tuple<Lesson, Parent>> { t });
                                        t = new Tuple<Lesson, Parent>(secondParent.Timetable[day].Classes[classN].Lessons[lesson], Parent.SecondParent);
                                        indexesDict[shit].Add(t);
                                        #endregion
                                    }
                            }
                        }
                    }
                    #endregion
                    if (dict.Count == 0)
                    {
                        throw new GeneticAlgorithmException();
                    }
                    dict.Keys.CopyTo(a, 0);
                    rand = rnd.Next(dict.Keys.Count);
                    var key = a[rand];
                    #region Opposite construction
                    Parent oppositeParent = Parent.FirstParent;
                    if (key.Item2 == Parent.FirstParent)
                    {
                        oppositeParent = Parent.SecondParent;
                    }
                    else
                    {
                        oppositeParent = Parent.FirstParent;
                    }
                    #endregion
                    var oppositeKey = new Tuple<Lesson, Parent>(key.Item1, oppositeParent);
                    Index ind = dict[key][dict[key].Count - 1];
                    if (dict.ContainsKey(oppositeKey))
                    {
                        Index oppositeIndex = dict[oppositeKey][dict[oppositeKey].Count - 1];
                        this.Timetable[ind.Day].Classes[ind.ClassN].Lessons[ind.Period] = key.Item1;
                        #region Dictionary update

                        var found = indexesDict[ind];
                        Tuple<Lesson, Parent> keyToChange;
                        if (found[0].Item2 != key.Item2)
                        {
                            keyToChange = found[0];
                        }
                        else
                        {
                            keyToChange = found[1];
                        }
                        var swap = indexesDict[ind][(int)oppositeParent];
                        indexesDict[oppositeIndex][(int)oppositeParent] = indexesDict[ind][(int)oppositeParent];
                        indexesDict[ind][(int)oppositeParent] = swap;

                        for (int i = 0; i < dict[keyToChange].Count; ++i)
                        {
                            if (dict[keyToChange][i].Equals(ind))
                            {
                                dict[keyToChange][i] = new Index(ind.Day, ind.ClassN, ind.Period);
                            }
                        }

                        dict[key].RemoveAt(dict[key].Count - 1);
                        if (dict[key].Count == 0) dict.Remove(key);
                        dict[oppositeKey].RemoveAt(dict[oppositeKey].Count - 1);
                        if (dict[oppositeKey].Count == 0) dict.Remove(oppositeKey);

                        ConflictsResolved++;
                        #endregion
                    }
                    else throw new GeneticAlgorithmException();
                }
            }
            catch (KeyNotFoundException e)
            {
                goto end;
            }
            if (dict.Count != 0)
            {
                throw new Exception();
            }

            #endregion

            #endregion

            #region Classrooms switching

            Dictionary<Classroom, bool> classrooms = new Dictionary<Classroom, bool>();

            for (int day = 0; day < d.Count; ++day)
            {
                for (int lesson = 0; lesson < d[day].Classes[0].Lessons.Count; ++lesson)
                {
                    classrooms.Clear();
                    for (int classN = 0; classN < d[day].Classes.Count; ++classN)
                    {
                        Lesson l = d[day].Classes[classN].Lessons[lesson];
                        if (l == null) continue;
                        foreach (Group g in l.Groups)
                        {
                            if (g.Room != null)
                            {
                                if (classrooms.ContainsKey(g.Room))
                                {
                                    bool broken = false;
                                    for (int i = 0; i < g.AlternativeRooms.Count; ++i)
                                    {
                                        if (!classrooms.ContainsKey(g.AlternativeRooms[i]))
                                        {
                                            g.Room = g.AlternativeRooms[i];
                                            classrooms.Add(g.Room, true);
                                            broken = true;
                                            break;
                                        }
                                    }
                                    if (!broken)
                                    {
                                        g.Room = g.DesiredRoom;
                                    }
                                }
                                else
                                {
                                    classrooms.Add(g.Room, true);
                                }
                            }
                        }
                    }
                }
            }

            #endregion
            end:
            this.mDays = d;
            this.mRating = CalculateRating();
        }
Ejemplo n.º 3
0
 public Schedule(List<Day> timetable, AlgorithmParameters parameters)
 {
     this.DifficultyPattern = parameters;
     SetTimetable(timetable);
 }
Ejemplo n.º 4
0
        void LoadParametersToCheckboxes(AlgorithmParameters param, int numberOfHours)
        {
            try
            {
                if (numberOfHours == 4)
                {
                    foreach (var p in param.FourHoursPerDay)
                    {
                        int row = 0;
                        foreach (var diff in param.FourHoursPerDay)
                        {
                            SetControlsChecked(row, diff);
                            row++;
                        }

                    }
                }
                if (numberOfHours == 5)
                {
                    foreach (var p in param.FiveHoursPerDay)
                    {
                        int row = 0;
                        foreach (var diff in param.FiveHoursPerDay)
                        {
                            SetControlsChecked(row, diff);
                            row++;
                        }
                    }
                }
                if (numberOfHours == 6)
                {
                    foreach (var p in param.FourHoursPerDay)
                    {
                        int row = 0;
                        foreach (var diff in param.SixHoursPerDay)
                        {
                            SetControlsChecked(row, diff);
                            row++;
                        }
                    }
                }
                if (numberOfHours == 7)
                {
                    int row = 0;
                    foreach (var diff in param.SevenHoursPerDay)
                    {
                        SetControlsChecked(row, diff);
                        row++;
                    }
                }
            }
            catch { }
        }