Exemplo n.º 1
0
 public void Vendos(List <int> pre, Curriculum _c)
 {
     if (pre.Count == 1)
     {
         Course c = courses.FirstOrDefault(a => a.ID == pre[0]);
         if (c.prereq != null && c.prereq.Count > 0)
         {
             Vendos(c.prereq, _c);
         }
         else
         //check where can be placed
         {
             int perioda = 1;
             for (int _i = 1; _i < 9; _i++)
             {
                 int periodSum = courses.Where(a => a.period == _i).Sum(a => a.credit);
                 if (c.period == -1 && (periodSum + c.credit < _c.maxCredits && courses.Count(a => a.period == _i) + 1 < _c.maxCourses))
                 {
                     perioda = _i;
                     break;
                 }
             }
             if (c.period == -1)
             {
                 c.period = perioda;
             }
         }
     }
     else
     {
         for (int _a = 0; _a < pre.Count; _a++)
         {
             if (courses.FirstOrDefault(a => a.ID == pre[_a]).prereq.Count == 0)
             {
                 //check where can be placed
                 int perioda = 1;
                 for (int _i = 1; _i < 9; _i++)
                 {
                     int periodSum = courses.Where(a => a.period == _i).Sum(a => a.credit);
                     if (courses.FirstOrDefault(a => a.ID == pre[_a]).period == -1 && (periodSum + courses.FirstOrDefault(a => a.ID == pre[_a]).credit < _c.maxCredits || courses.Count(a => a.period == _i) + 1 < _c.maxCourses))
                     //if (periodSum + courses.FirstOrDefault(a => a.ID == pre[_a]).credit < _c.maxCredits)
                     {
                         perioda = _i;
                         break;
                     }
                 }
                 if (courses.FirstOrDefault(a => a.ID == pre[_a]).period == -1)
                 {
                     courses.FirstOrDefault(a => a.ID == pre[_a]).period = perioda;
                 }
             }
             else
             {
                 Vendos(courses.FirstOrDefault(a => a.ID == pre[_a]).prereq, _c);
             }
         }
     }
 }
Exemplo n.º 2
0
        public static List <int> getPrerequringCourses(Curriculum _c, int CourseID)
        {
            List <int> result = new List <int>();

            foreach (Course c in _c.courses)
            {
                if (c.ID != CourseID && c.RequiredCourses != null)
                {
                    if (c.RequiredCourses.Contains(CourseID))
                    {
                        result.Add(c.ID);
                    }
                }
            }
            return(result);
        }
        private int[] getShiftCandidateOrigine(Individ _parent, Curriculum _c)
        {
            int[]      shiftCandidate = new int[2];
            List <int> periodList     = BasicFunctions.getRandomPeriodList(_c.noPeriods);
            //List<int> periodList = BasicFunctions.getCreditLoadSortedPeriodList(parentA.PeriodCreditLoad);
            int candidatePeriod = -1;
            int candidateCourse = -1;

            while (periodList.Count > 0)
            {
                //Console.WriteLine("periodList.Count: " + periodList.Count);
                candidatePeriod = periodList[0];
                int candidatePeriodCourseLoad = _parent.Representation[candidatePeriod].Count;

                if (candidatePeriodCourseLoad - 1 >= _c.minCourses)
                {
                    int candidatePeriodCreditLoad = _parent.PeriodCreditLoad[candidatePeriod];
                    //choosing randomly a course from this period, check minPeriodCreditLoad
                    List <int> RandomListOfCourses = BasicFunctions.getRandomCourseList(_parent.Representation[candidatePeriod]);
                    int        CourseIndex;
                    for (CourseIndex = 0; CourseIndex < RandomListOfCourses.Count; CourseIndex++)
                    {
                        int tempCourseCredit = _c.courses.FirstOrDefault(a => a.ID == RandomListOfCourses[CourseIndex]).credit;
                        if ((candidatePeriodCreditLoad - tempCourseCredit) >= _c.minCredits)
                        {
                            candidateCourse = _c.courses.FirstOrDefault(a => a.ID == RandomListOfCourses[CourseIndex]).ID;
                            return(new int[] { candidatePeriod, candidateCourse });
                        }
                    }
                    if (CourseIndex == RandomListOfCourses.Count)
                    {
                        periodList.RemoveAt(0);
                    }
                }
                else
                {
                    periodList.RemoveAt(0);
                }
            }

            return(new int[] { -1, -1 });
        }
Exemplo n.º 4
0
        public static bool checkPrerequisites(List <int>[] individRepresentation, Curriculum _c)
        {
            List <int> prerequisiteCoursesPeriod;
            int        coursePeriod;

            foreach (Course c in _c.courses)
            {
                if (c.RequiredCourses != null)
                {
                    coursePeriod = CoursePeriod(individRepresentation, c.ID);
                    prerequisiteCoursesPeriod = PrerequisitesPeriods(individRepresentation, c);
                    foreach (int i in prerequisiteCoursesPeriod)
                    {
                        if (i > coursePeriod)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Exemplo n.º 5
0
        public Individ getRandomIndivid(Curriculum _c)
        {
            int t0 = DateTime.Now.Millisecond;

            courses = _c.courses;
            Individ       individi = new Individ(_c.noPeriods);
            List <Course> periodCourses = new List <Course>();
            int           noPeriods = 8;
            int           minCredits = _c.minCredits, minCourses = _c.minCourses;
            int           maxCredits = _c.maxCredits, maxCourses = _c.maxCourses;


            for (int i = 0; i < courses.Count; i++)
            {
                if (courses[i].prereq != null && courses[i].prereq.Count > 0)
                {
                    coursesDependent.Add(courses[i]);
                }
            }
            int al = 0;

            for (int k = 0; k < coursesDependent.Count; k++)
            {
                Vendos(coursesDependent[k].prereq, _c);
                int max = courses.FirstOrDefault(a => a.ID == coursesDependent[k].prereq[0]).period;// coursesDependent[k].prereq[0].period;
                for (int l = 1; l < coursesDependent[k].prereq.Count; l++)
                {
                    int temp = courses.FirstOrDefault(a => a.ID == coursesDependent[k].prereq[l]).period;
                    if (temp > max)
                    {
                        max = temp;
                    }
                }
                max++;
                int periodSumi = courses.Where(a => a.period == max).Sum(a => a.credit);
                while (periodSumi + coursesDependent[k].credit > _c.maxCredits ||
                       courses.Count(a => a.period == max) + 1 > _c.maxCourses)
                {
                    max++;
                    periodSumi = courses.Where(a => a.period == max).Sum(a => a.credit);
                }
                Random rand = new Random(DateTime.Now.Millisecond);
                max = rand.Next(max, max + 1);
                //max = (max > 8 ? max - (max % 8) : max);
                courses.FirstOrDefault(a => a.ID == coursesDependent[k].ID).period = max;
            }

            Random        rnd;
            int           _i;
            int           periodSum;
            List <Course> inDependent = new List <Course>();

            for (int i = 0; i < courses.Count; i++)
            {
                if (courses[i].period == -1)
                {
                    inDependent.Add(courses[i]);
                }
            }
            for (int i = 1; i <= 8; i++)
            {
                while (courses.Count(a => a.period == i) < _c.minCourses)
                {
                    do
                    {
                        rnd       = new Random(DateTime.Now.Millisecond);
                        _i        = rnd.Next(0, inDependent.Count - 1);
                        periodSum = courses.Where(a => a.period == i).Sum(a => a.credit);
                    }while (periodSum + courses[i].credit > _c.maxCredits || courses.Count(a => a.period == i) + 1 > _c.maxCourses);
                    courses.FirstOrDefault(a => a.ID == inDependent[_i].ID).period = i;
                    inDependent.RemoveAt(_i);
                }
            }

            for (int i = 0; i < courses.Count; i++)
            {
                if (courses[i].period == -1)
                {
                    do
                    {
                        rnd       = new Random(DateTime.Now.Millisecond);
                        _i        = rnd.Next(6, 9);
                        periodSum = courses.Where(a => a.period == _i).Sum(a => a.credit);
                    }while (periodSum + courses[i].credit > _c.maxCredits || courses.Count(a => a.period == _i) + 1 > _c.maxCourses);
                    courses[i].period = _i;
                }
            }

            int t1 = DateTime.Now.Millisecond;


            for (int i = 0; i < courses.Count; i++)
            {
                individi.Representation[courses[i].period - 1].Add(courses[i].ID);
            }

            for (int p = 0; p < individi.Representation.Length; p++)
            {
                int sum = 0;
                for (int j = 0; j < individi.Representation[p].Count; j++)
                {
                    sum += courses.FirstOrDefault(a => a.ID == (individi.Representation[p])[j]).credit;
                }
                individi.PeriodCreditLoad[p] = sum;
            }

            return(individi);
        }
        public Individ getRandomIndivid(Curriculum _c)
        {
            //All Courses read from File
            var Courses = new List <Course>();

            Courses = _c.courses;

            //Util list to track Prereq and courses that has as a Prereq CandidateCourse
            var TempCourseList = new List <Course>();

            int t0 = DateTime.Now.Millisecond;

            #region 1. First set MoveDownIndex/MoveUpIndex to evaluate each course values for these indexes

            Courses.ForEach(a =>
            {
                if (Courses.Any(b => b.RequiredCourses != null && b.RequiredCourses.Contains(a.ID)))
                {
                    //Take courses which has this course(a) as prereq
                    TempCourseList = Courses.Where(b => b.RequiredCourses != null && b.RequiredCourses.Contains(a.ID)).ToList();

                    while (TempCourseList.Count > 0)
                    {
                        //Index that shows upper-limit of Period (NumberOfPeriods-MoveDownIndex)
                        a.MoveDownIndex++;

                        //Track courses as a prereq of other courses(in-depth search)
                        TempCourseList = Courses.Where(b => b.RequiredCourses != null && TempCourseList.Any(c => b.RequiredCourses.Contains(c.ID))).ToList();
                    }
                }

                //If this course has Prereq courses
                if (a.RequiredCourses != null && a.RequiredCourses.Count > 0)
                {
                    //Take prereq courses
                    TempCourseList = Courses.Where(b => a.RequiredCourses.Contains(b.ID)).ToList();

                    while (TempCourseList.Count > 0)
                    {
                        //Index that shows down-limit of Period (MoveUpIndex+1)
                        a.MoveUpIndex++;

                        //Take prereq of prereq courses(In-depth search)
                        TempCourseList = Courses.Where(b => TempCourseList.Any(c => c.RequiredCourses != null && c.RequiredCourses.Contains(b.ID))).ToList();
                    }
                }
            });

            #endregion

            #region Vars which we need

            int NumberOfPeriods        = _c.noPeriods,
                NumberOfMinimalCourses = _c.minCourses,
                NumberOfMaximalCourses = _c.maxCourses,
                NumberOfMinimalCredits = _c.minCredits,
                NumberOfMaximalCredits = _c.maxCredits;
            Course CandidateCourse     = null;
            int    Period      = 1;
            int    randomIndex = 0; //Planning to put some crazy randomness

            #endregion

            //Util list to ensure the conditions about CourseLoad & CreditLoad are achieved
            var PeriodTracker = new List <PeriodTracker>();
            for (int i = 1; i <= NumberOfPeriods; i++)
            {
                PeriodTracker.Add(new PeriodTracker {
                    Done = false, period = i
                });
            }
            //Split courses without any dependency in a list
            var FreeCourses = Courses.Where(a => a.MoveUpIndex == 0 && a.MoveDownIndex == 0).ToList();
            //Split courses with any dependency(prereqOf or hasPrereq) in a list
            var  DependentCourses = Courses.Where(a => a.MoveDownIndex != 0 || a.MoveUpIndex != 0).ToList();
            var  TempPeriodList = new List <PeriodTracker>();
            bool CourseLoad = false, CreditLoad = false;

            #region 2. Manage DependentCourses firstly

            while (DependentCourses.Count > 0)
            {
                //Courses without any prereq proceed first
                if (DependentCourses.Count(a => a.RequiredCourses == null) > 0)
                {
                    CandidateCourse = DependentCourses.FirstOrDefault(a => a.RequiredCourses == null);
                }
                else
                {
                    CandidateCourse = DependentCourses.Where(a => a.RequiredCourses != null).FirstOrDefault();
                }

                if (PeriodTracker.Any(a => !a.Done && a.period >= (CandidateCourse.MoveUpIndex + 1) && a.period <= (NumberOfPeriods - CandidateCourse.MoveDownIndex)))
                {
                    //For the sake of creditBalance and courseLoad we proceed with FirstOrDefault always
                    Period = PeriodTracker.FirstOrDefault(a => !a.Done && a.period >= (CandidateCourse.MoveUpIndex + 1) && a.period <= (NumberOfPeriods - CandidateCourse.MoveDownIndex)).period;
                }
                else
                {
                    //Put some randomness here
                    Period = BasicFunctions.randomGenerator.Next(CandidateCourse.MoveUpIndex + 1, NumberOfPeriods - CandidateCourse.MoveDownIndex + 1);
                }

                CourseLoad = BasicFunctions.CheckCourseLoad(Courses, Period, NumberOfMinimalCourses, NumberOfMaximalCourses);
                CreditLoad = BasicFunctions.CheckCreditLoad(Courses, Period, NumberOfMinimalCredits, NumberOfMaximalCredits, CandidateCourse.credit);
                //Check to see if constraints aboud CourseLoad and CreditLoad are met
                if (CourseLoad && CreditLoad)
                {
                    //Update Period of this course in the full Courses list
                    Courses.FirstOrDefault(a => a.ID == CandidateCourse.ID).period = Period;

                    PeriodTracker.FirstOrDefault(a => a.period == Period).Done = ((Courses.Count(a => a.period == Period) >= NumberOfMinimalCourses) &&
                                                                                  (Courses.Where(a => a.period == Period).Sum(a => a.credit) >= NumberOfMinimalCredits));

                    //Look at the courses that have this CandidateCourse as a Prereq and update their MoveUpIndex to this Period value
                    //because the down-limit of their Period Value must be greater than this CandidateCourse's Period value
                    //Note! The update must occur only if MoveUpIndex is less then CandidateCourse's Period value
                    Courses.Where(a => a.RequiredCourses != null && a.RequiredCourses.Contains(CandidateCourse.ID) && a.MoveUpIndex < Period)
                    .ToList()
                    .ForEach(a =>
                    {
                        a.MoveUpIndex = Period;
                    });

                    //Remove CandidateCourse from this list, so we can reduce this list until the last member
                    DependentCourses.Remove(CandidateCourse);
                }
            }

            #endregion

            #region 3. Manage FreeCourses

            //Now we deal with Free Courses which have no dependencies
            while (FreeCourses.Count > 0)
            {
                randomIndex     = BasicFunctions.randomGenerator.Next(0, FreeCourses.Count);
                CandidateCourse = FreeCourses[randomIndex];

                if (PeriodTracker.Any(a => !a.Done && a.period >= (CandidateCourse.MoveUpIndex + 1) && a.period <= (NumberOfPeriods - CandidateCourse.MoveDownIndex)))
                {
                    //Again, randomness
                    TempPeriodList = PeriodTracker.Where(a => !a.Done && a.period >= (CandidateCourse.MoveUpIndex + 1) && a.period <= (NumberOfPeriods - CandidateCourse.MoveDownIndex)).ToList();

                    Period = TempPeriodList.ElementAt(BasicFunctions.randomGenerator.Next(TempPeriodList.Count)).period;
                }
                else
                {
                    //Randomn...ess here too
                    Period = BasicFunctions.randomGenerator.Next(CandidateCourse.MoveUpIndex + 1, NumberOfPeriods - CandidateCourse.MoveDownIndex);
                }

                CourseLoad = BasicFunctions.CheckCourseLoad(Courses, Period, NumberOfMinimalCourses, NumberOfMaximalCourses);
                CreditLoad = BasicFunctions.CheckCreditLoad(Courses, Period, NumberOfMinimalCredits, NumberOfMaximalCredits, CandidateCourse.credit);
                if (CourseLoad && CreditLoad)
                {
                    Courses.FirstOrDefault(a => a.ID == CandidateCourse.ID).period = Period;

                    PeriodTracker.FirstOrDefault(a => a.period == Period).Done = ((Courses.Count(a => a.period == Period) >= NumberOfMinimalCourses) &&
                                                                                  (Courses.Where(a => a.period == Period).Sum(a => a.credit) >= NumberOfMinimalCredits));

                    FreeCourses.Remove(CandidateCourse);
                }
            }

            #endregion

            Individ individi = new Individ(NumberOfPeriods);
            for (int i = 0; i < Courses.Count; i++)
            {
                individi.Representation[Courses[i].period - 1].Add(Courses[i].ID);
            }

            for (int p = 0; p < individi.Representation.Length; p++)
            {
                int sum = 0;
                for (int j = 0; j < individi.Representation[p].Count; j++)
                {
                    sum += Courses.FirstOrDefault(a => a.ID == (individi.Representation[p])[j]).credit;
                }
                individi.PeriodCreditLoad[p] = sum;
            }

            return(individi);
        }
        private void btnReadFromFile_Click(object sender, EventArgs e)
        {
            int NumberOfPeriods        = (txtNoPeriods.Text.Length > 0 ? int.Parse(txtNoPeriods.Text) : 4),
                NumberOfMinimalCourses = (txtMinCourses.Text.Length > 0 ? int.Parse(txtMinCourses.Text) : 2),
                NumberOfMaximalCourses = (txtMaxCourses.Text.Length > 0 ? int.Parse(txtMaxCourses.Text) : 4),
                NumberOfMinimalCredits = (txtMinCredits.Text.Length > 0 ? int.Parse(txtMinCredits.Text) : 3),
                NumberOfMaximalCredits = (txtMaxCredits.Text.Length > 0 ? int.Parse(txtMaxCredits.Text) : 15);

            Data objData = new Data();

            dgvData.Rows.Clear();

            if (txtPathFile.Text.Trim().Length > 0)
            {
                //Fill dataset automatically from FILE
                objCurriculum     = objData.GetDataFromFile(txtPathFile.Text);
                txtNoPeriods.Text = objCurriculum.noPeriods.ToString();
                NumberOfPeriods   = objCurriculum.noPeriods;

                txtMinCredits.Text     = objCurriculum.minCredits.ToString();
                NumberOfMinimalCredits = objCurriculum.minCredits;

                txtMaxCredits.Text     = objCurriculum.maxCredits.ToString();
                NumberOfMaximalCredits = objCurriculum.maxCredits;

                txtMinCourses.Text     = objCurriculum.minCourses.ToString();
                NumberOfMinimalCourses = objCurriculum.minCourses;

                txtMaxCourses.Text     = objCurriculum.maxCourses.ToString();
                NumberOfMaximalCourses = objCurriculum.maxCourses;

                EnableDisableFields(false);
            }
            else
            {
                //Fill dataset manually
                EnableDisableFields(true);

                txtNoPeriods.Text  = NumberOfPeriods.ToString();
                txtMaxCourses.Text = NumberOfMaximalCourses.ToString();
                txtMinCourses.Text = NumberOfMinimalCourses.ToString();
                txtMaxCredits.Text = NumberOfMaximalCredits.ToString();
                txtMinCredits.Text = NumberOfMinimalCredits.ToString();

                objCurriculum = objData.getData(NumberOfPeriods,
                                                NumberOfMinimalCredits,
                                                NumberOfMaximalCredits,
                                                NumberOfMinimalCourses,
                                                NumberOfMaximalCourses);
            }

            //All Courses read from File
            var Courses = new List <Course>();

            Courses = objCurriculum.courses;

            if (dgvData.Columns.Count == 0)
            {
                dgvData.Columns.Add("Id", "Id");
                dgvData.Columns.Add("Name", "Name");
                dgvData.Columns.Add("Prereq", "Prereq");
                dgvData.Columns.Add("Credit", "Credit");
                dgvData.Columns.Add("Description", "Description");
            }

            foreach (var course in Courses)
            {
                dgvData.Rows.Add(course.ID,
                                 course.name,
                                 course.RequiredCourses != null ? string.Join(", ", Courses.Where(a => course.RequiredCourses.Contains(a.ID)).Select(a => a.name)) : "",
                                 course.credit,
                                 course.description);
            }
        }
        private List <int> getShiftingPeriodCandidates(
            Curriculum _c, List <int>[] Representation, int[] PeriodCreditLoad,
            int _currentPeriod, Course _candidateCourse)
        {
            List <int> LeftPeriodList      = new List <int>();
            List <int> RightPeriodList     = new List <int>();
            List <int> resultingPeriodList = new List <int>();
            List <int> RequiringCourses    = _c.courses[_candidateCourse.ID - 1].RequiringCourses;
            List <int> RequiredCourses     = _c.courses[_candidateCourse.ID - 1].RequiredCourses;

            for (int PeriodIndex = 0; PeriodIndex < Representation.Length; PeriodIndex++)
            {
                if (PeriodIndex != _currentPeriod)
                {
                    List <int> PeriodCourseList = Representation[PeriodIndex];

                    //Check max courses and Check max credits
                    if ((Representation[PeriodIndex].Count + 1 > _c.maxCourses) ||
                        (PeriodCreditLoad[PeriodIndex] + _candidateCourse.credit > _c.maxCredits))
                    {
                        bool ItNeededToContinueWithRemainingPeriods = true;
                        foreach (int cp in RequiringCourses)
                        {
                            if (PeriodCourseList.Contains(cp))
                            {
                                ItNeededToContinueWithRemainingPeriods = false;
                                break;
                            }
                        }

                        foreach (int cp in RequiredCourses)
                        {
                            if (PeriodCourseList.Contains(cp))
                            {
                                LeftPeriodList.Clear();
                                ItNeededToContinueWithRemainingPeriods = true;
                                break;
                            }
                        }

                        if (ItNeededToContinueWithRemainingPeriods)
                        {
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }

                    //Check prerequisites
                    bool CurrenPeriodCanBeUsedForShifting = true;
                    if (PeriodIndex < _currentPeriod)
                    {
                        foreach (int cp in RequiredCourses)
                        {
                            if (PeriodCourseList.Contains(cp))
                            {
                                LeftPeriodList.Clear();
                                CurrenPeriodCanBeUsedForShifting = false;
                                break;
                            }
                        }
                        if (CurrenPeriodCanBeUsedForShifting)
                        {
                            LeftPeriodList.Add(PeriodIndex);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        foreach (int cp in RequiringCourses)
                        {
                            if (PeriodCourseList.Contains(cp))
                            {
                                CurrenPeriodCanBeUsedForShifting = false;
                                break;
                            }
                        }
                        if (CurrenPeriodCanBeUsedForShifting)
                        {
                            RightPeriodList.Add(PeriodIndex);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            resultingPeriodList.AddRange(LeftPeriodList);
            resultingPeriodList.AddRange(RightPeriodList);
            return(resultingPeriodList);
        }
        public Individ Apply(Curriculum objCurriculum)
        {
            Individ Best = new Individ(objCurriculum.noPeriods);

            Individ[] P = Enumerable.Repeat(
                new Individ(objCurriculum.noPeriods), Parameters.popSize).ToArray();
            Individ[] Q = Enumerable.Repeat(
                new Individ(objCurriculum.noPeriods), Parameters.popSize).ToArray();
            for (int i = 0; i < Parameters.popSize; i++)
            {
                Individ currentIndivid = getRandomIndivid(objCurriculum);
                //Individ currentIndivid = iah.getRandomIndivid(objCurriculum);
                //Individ currentIndivid = ifa.getRandomIndivid(objCurriculum);

                Boolean preReq            = BasicFunctions.checkPrerequisites(currentIndivid.Representation, objCurriculum);
                Boolean ChecMinMaxCredits = BasicFunctions.checkMinMaxCredit(currentIndivid.PeriodCreditLoad, objCurriculum.maxCredits, objCurriculum.minCredits);
                Boolean ChecMinMaxCourses = BasicFunctions.checkMinMaxCourse(currentIndivid.Representation, objCurriculum.maxCourses, objCurriculum.minCourses);
                Boolean CheckEvaluation   = BasicFunctions.checkFitnessMaximumLoad(currentIndivid.Representation, objCurriculum, currentIndivid.Evaluation);
                P[i] = (Individ)currentIndivid.Clone();
            }

            Best = (Individ)P[0].Clone();

            int CurrentGeneration = 1;

            do
            {
                for (int i = 0; i < Parameters.popSize; i++)
                {
                    Boolean preReq            = BasicFunctions.checkPrerequisites(P[i].Representation, objCurriculum);
                    Boolean ChecMinMaxCredits = BasicFunctions.checkMinMaxCredit(P[i].PeriodCreditLoad, objCurriculum.maxCredits, objCurriculum.minCredits);
                    Boolean ChecMinMaxCourses = BasicFunctions.checkMinMaxCourse(P[i].Representation, objCurriculum.maxCourses, objCurriculum.minCourses);
                    Boolean CheckEvaluation   = BasicFunctions.checkFitnessMaximumLoad(P[i].Representation, objCurriculum, P[i].Evaluation);

                    if (P[i].Evaluation < Best.Evaluation)
                    {
                        Best = (Individ)P[i].Clone();;
                        Console.WriteLine("Current generation: " + CurrentGeneration);
                        Console.WriteLine(BasicFunctions.getSolutionDetails(Best));
                    }
                }
                for (int i = 0; i < Parameters.popSize; i += 2)
                {
                    Individ ParentA = BasicFunctions.GetBestParent(P);
                    Individ ParentB = BasicFunctions.GetBestParent(P);

                    Individ childA, childB;
                    if (SwapMutationOn)
                    {
                        childA = objSwapMutate.GetChild(ParentA, objCurriculum);
                        childB = objSwapMutate.GetChild(ParentB, objCurriculum);
                    }
                    else
                    {
                        Boolean preReq            = BasicFunctions.checkPrerequisites(ParentA.Representation, objCurriculum);
                        Boolean ChecMinMaxCredits = BasicFunctions.checkMinMaxCredit(ParentA.PeriodCreditLoad, objCurriculum.maxCredits, objCurriculum.minCredits);
                        Boolean ChecMinMaxCourses = BasicFunctions.checkMinMaxCourse(ParentA.Representation, objCurriculum.maxCourses, objCurriculum.minCourses);
                        Boolean CheckEvaluation   = BasicFunctions.checkFitnessMaximumLoad(ParentA.Representation, objCurriculum, ParentA.Evaluation);

                        childA = objShiftMutate.GetChild(ParentA, objCurriculum);

                        Boolean preReq2            = BasicFunctions.checkPrerequisites(childA.Representation, objCurriculum);
                        Boolean ChecMinMaxCredits2 = BasicFunctions.checkMinMaxCredit(childA.PeriodCreditLoad, objCurriculum.maxCredits, objCurriculum.minCredits);
                        Boolean ChecMinMaxCourses2 = BasicFunctions.checkMinMaxCourse(childA.Representation, objCurriculum.maxCourses, objCurriculum.minCourses);
                        Boolean CheckEvaluation2   = BasicFunctions.checkFitnessMaximumLoad(childA.Representation, objCurriculum, childA.Evaluation);

                        Boolean preReq3            = BasicFunctions.checkPrerequisites(ParentB.Representation, objCurriculum);
                        Boolean ChecMinMaxCredits3 = BasicFunctions.checkMinMaxCredit(ParentB.PeriodCreditLoad, objCurriculum.maxCredits, objCurriculum.minCredits);
                        Boolean ChecMinMaxCourses3 = BasicFunctions.checkMinMaxCourse(ParentB.Representation, objCurriculum.maxCourses, objCurriculum.minCourses);
                        Boolean CheckEvaluation3   = BasicFunctions.checkFitnessMaximumLoad(ParentB.Representation, objCurriculum, ParentB.Evaluation);

                        childB = objShiftMutate.GetChild(ParentB, objCurriculum);

                        Boolean preReq4            = BasicFunctions.checkPrerequisites(childB.Representation, objCurriculum);
                        Boolean ChecMinMaxCredits4 = BasicFunctions.checkMinMaxCredit(childB.PeriodCreditLoad, objCurriculum.maxCredits, objCurriculum.minCredits);
                        Boolean ChecMinMaxCourses4 = BasicFunctions.checkMinMaxCourse(childB.Representation, objCurriculum.maxCourses, objCurriculum.minCourses);
                        Boolean CheckEvaluation4   = BasicFunctions.checkFitnessMaximumLoad(childB.Representation, objCurriculum, childB.Evaluation);
                    }


                    Q[i]     = (Individ)childA.Clone();
                    Q[i + 1] = (Individ)childB.Clone();
                }

                P = (Individ[])Q.Clone();

                CurrentGeneration++;
                if (CurrentGeneration % (Parameters.MutationAlternationFrequency * Parameters.MaxGeneration) == 0)
                {
                    SwapMutationOn = !SwapMutationOn;
                }
            }while (CurrentGeneration <= Parameters.MaxGeneration);

            MessageBox.Show("Best solution details:\n" + BasicFunctions.getSolutionDetails(Best));

            return(Best);
        }
Exemplo n.º 10
0
        public Individ SwapCourses(int CurrentPeriod, int MinMaxCreditLoadPeriod,
                                   Curriculum _c, Individ _individ)
        {
            Individ    ResultingIndivid = (Individ)_individ.Clone();
            int        LeftPeriodIndex, RightPeriodIndex;
            List <int> LeftPeriodSwapCandidates, RightPeriodSwapCandidates;

            if (CurrentPeriod < MinMaxCreditLoadPeriod)
            {
                LeftPeriodIndex  = CurrentPeriod;
                RightPeriodIndex = MinMaxCreditLoadPeriod;
            }
            else
            {
                LeftPeriodIndex  = MinMaxCreditLoadPeriod;
                RightPeriodIndex = CurrentPeriod;
            }
            LeftPeriodSwapCandidates =
                this.getLeftPeriodSwapCandidates(_c, ResultingIndivid.Representation,
                                                 LeftPeriodIndex, RightPeriodIndex);
            RightPeriodSwapCandidates =
                this.getRightPeriodSwapCandidates(_c, ResultingIndivid.Representation,
                                                  LeftPeriodIndex, RightPeriodIndex);
            if (LeftPeriodSwapCandidates.Count > 0 && RightPeriodSwapCandidates.Count > 0)
            {
                List <int> RandomLeftList  = BasicFunctions.getRandomCourseList(LeftPeriodSwapCandidates);
                List <int> RandomRightList = BasicFunctions.getRandomCourseList(RightPeriodSwapCandidates);
                for (int IL = 0; IL < RandomLeftList.Count; IL++)
                {
                    int LeftListCourseID = RandomLeftList[IL];
                    for (int IR = 0; IR < RandomRightList.Count; IR++)
                    {
                        int RightListCourseID = RandomRightList[IR];

                        int LeftPeriodForeseenCreditLoad = ResultingIndivid.PeriodCreditLoad[LeftPeriodIndex] +
                                                           _c.courses[RightListCourseID - 1].credit - _c.courses[LeftListCourseID - 1].credit;
                        int RightPeriodForeseenCreditLoad = ResultingIndivid.PeriodCreditLoad[RightPeriodIndex] +
                                                            _c.courses[LeftListCourseID - 1].credit - _c.courses[RightListCourseID - 1].credit;

                        bool LeftPeriodCreditLoadFeasible  = LeftPeriodForeseenCreditLoad >= _c.minCredits && LeftPeriodForeseenCreditLoad <= _c.maxCredits;
                        bool RightPeriodCreditLoadFeasible = RightPeriodForeseenCreditLoad >= _c.minCredits && RightPeriodForeseenCreditLoad <= _c.maxCredits;
                        if (LeftPeriodCreditLoadFeasible && RightPeriodCreditLoadFeasible)
                        {
                            ResultingIndivid.Representation[LeftPeriodIndex].Remove(LeftListCourseID);
                            ResultingIndivid.Representation[LeftPeriodIndex].Add(RightListCourseID);
                            ResultingIndivid.Representation[RightPeriodIndex].Remove(RightListCourseID);
                            ResultingIndivid.Representation[RightPeriodIndex].Add(LeftListCourseID);

                            ResultingIndivid.PeriodCreditLoad[LeftPeriodIndex] +=
                                (_c.courses[RightListCourseID - 1].credit -
                                 _c.courses[LeftListCourseID - 1].credit);

                            ResultingIndivid.PeriodCreditLoad[RightPeriodIndex] +=
                                (_c.courses[LeftListCourseID - 1].credit -
                                 _c.courses[RightListCourseID - 1].credit);
                            goto ReturnIndivid;
                        }
                    }
                }
            }

ReturnIndivid:
            ResultingIndivid.Evaluation = BasicFunctions.AssessFitnessMaximumLoad(ResultingIndivid);
            return(ResultingIndivid);
        }
Exemplo n.º 11
0
        public Individ getRandomIndivid(Curriculum _c)
        {
            Individ RandomIndivid = new Individ(_c.noPeriods);

            // Step 1: Consider Prerequisits and MinMaxCourses constraints
            int[] InitialNoCoursePerPeriod = this.getInitialNoCoursePerPeriod(_c.courses.Count, _c.noPeriods);
            int   CourseID, PeriodStartIndex = 0;

            for (int CourseIndex = 0; CourseIndex < _c.courses.Count; CourseIndex++)
            {
                CourseID = _c.courses[CourseIndex].ID;
                List <int> RequiredCourses = _c.courses[CourseIndex].RequiredCourses;
                for (int PeriodIndex = PeriodStartIndex; PeriodIndex < _c.noPeriods; PeriodIndex++)
                {
                    if (RandomIndivid.Representation[PeriodIndex].Count < InitialNoCoursePerPeriod[PeriodIndex])
                    {
                        if (
                            RandomIndivid.PeriodCreditLoad[PeriodIndex] + _c.courses[CourseIndex].credit <= _c.maxCredits &&
                            !ContainsGroup(RandomIndivid.Representation[PeriodIndex], RequiredCourses)
                            )
                        {
                            RandomIndivid.Representation[PeriodIndex].Add(CourseID);
                            RandomIndivid.PeriodCreditLoad[PeriodIndex] += _c.courses[CourseIndex].credit;
                            break;
                        }
                    }
                    else
                    {
                        PeriodStartIndex++;
                    }
                }
            }

            //Step 2 :Incorporate randomness
            int PeriodA, PeriodB;

            for (int RanInensityIndex = 0; RanInensityIndex < Parameters.InitialSolutionRandomnessIntensity;
                 RanInensityIndex++)
            {
                PeriodA       = BasicFunctions.randomGenerator.Next(0, _c.noPeriods);
                PeriodB       = this.getRandomPeriod(PeriodA, _c.noPeriods);
                RandomIndivid = this.SwapCourses(PeriodA, PeriodB, _c, RandomIndivid);
            }

            //Step 3 :Consider MinMaxCredits constraint
            Boolean MinMaxCreditsConstraintFulfilled = false;

            while (!MinMaxCreditsConstraintFulfilled)
            {
                int PeriodCount = 0, RandomPeriod;
                for (int CurrentPeriod = 0; CurrentPeriod < _c.noPeriods; CurrentPeriod++)
                {
                    if (RandomIndivid.PeriodCreditLoad[CurrentPeriod] > _c.maxCredits ||
                        RandomIndivid.PeriodCreditLoad[CurrentPeriod] < _c.minCredits)
                    {
                        RandomPeriod  = this.getRandomPeriod(CurrentPeriod, _c.noPeriods);
                        RandomIndivid = this.SwapCourses(CurrentPeriod, RandomPeriod, _c, RandomIndivid);
                        break;
                    }
                    PeriodCount++;
                }
                if (PeriodCount == _c.noPeriods)
                {
                    MinMaxCreditsConstraintFulfilled = true;
                }
            }

            RandomIndivid.Evaluation = BasicFunctions.AssessFitnessMaximumLoad(RandomIndivid);

            Boolean preReq            = BasicFunctions.checkPrerequisites(RandomIndivid.Representation, _c);
            Boolean ChecMinMaxCredits = BasicFunctions.checkMinMaxCredit(RandomIndivid.PeriodCreditLoad, _c.maxCredits, _c.minCredits);
            Boolean ChecMinMaxCourses = BasicFunctions.checkMinMaxCourse(RandomIndivid.Representation, _c.maxCourses, _c.minCourses);
            Boolean CheckEvaluation   = BasicFunctions.checkFitnessMaximumLoad(RandomIndivid.Representation, _c, RandomIndivid.Evaluation);

            return(RandomIndivid);
        }
Exemplo n.º 12
0
        public Individ GetChild2(Individ parentA, Curriculum c)
        {
            //step 1: construct list of period loads
            int[] tempPeriodList        = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
            int[] tempPeriodListCredits = (int[])parentA.PeriodCreditLoad.Clone();
            for (int i = 0; i < tempPeriodListCredits.Length - 1; i++)
            {
                for (int j = i + 1; j < tempPeriodListCredits.Length; j++)
                {
                    if (tempPeriodListCredits[i] < tempPeriodListCredits[j])
                    {
                        int temp = tempPeriodListCredits[i];
                        tempPeriodListCredits[i] = tempPeriodListCredits[j];
                        tempPeriodListCredits[j] = temp;

                        temp = tempPeriodList[i];
                        tempPeriodList[i] = tempPeriodList[j];
                        tempPeriodList[j] = temp;
                    }
                }
            }
            //start swaping by most and least loaded period
            int        left = 0, right = tempPeriodList.Length - 1;
            List <int> candidatePeriod1 = parentA.Representation[tempPeriodList[left]];
            List <int> candidatePeriod2 = parentA.Representation[tempPeriodList[right]];

            for (int i = 0; i < candidatePeriod1.Count; i++)
            {
                //krahasimi me mesataren e period load
                if (parentA.PeriodCreditLoad[tempPeriodList[right]] > parentA.PeriodCreditLoad[tempPeriodList[left]])
                {
                    break;
                }

                int candidateCourse = candidatePeriod1[i];
                parentA.Representation[tempPeriodList[left]].Remove(candidateCourse);
                parentA.Representation[tempPeriodList[right]].Add(candidateCourse);
                if (!BasicFunctions.checkPrerequisites(parentA.Representation, c) || !BasicFunctions.checkMinMaxCourse(parentA.Representation, c.maxCourses, c.minCourses) ||
                    !BasicFunctions.checkMinMaxCredit(parentA.PeriodCreditLoad, c.maxCredits, c.minCredits))
                {
                    parentA.Representation[tempPeriodList[right]].Remove(candidateCourse);
                    parentA.Representation[tempPeriodList[left]].Add(candidateCourse);
                }

                //update periodcreditload
                int s = 0;
                for (int k = 0; k < parentA.Representation[tempPeriodList[right]].Count; k++)
                {
                    s += c.courses[parentA.Representation[tempPeriodList[right]][k] - 1].credit;
                }
                parentA.PeriodCreditLoad[tempPeriodList[right]] = s;

                s = 0;
                for (int k = 0; k < parentA.Representation[tempPeriodList[left]].Count; k++)
                {
                    s += c.courses[parentA.Representation[tempPeriodList[left]][k] - 1].credit;
                }
                parentA.PeriodCreditLoad[tempPeriodList[left]] = s;
            }

            return(parentA);
        }