public Individ GetChild(Individ _parent, Curriculum _c) { Individ ResultingChild = (Individ)_parent.Clone(); int PeriodA, PeriodB; for (int RanInensityIndex = 0; RanInensityIndex < Parameters.SwapMutateIntensity; RanInensityIndex++) { PeriodA = BasicFunctions.randomGenerator.Next(0, _c.noPeriods); PeriodB = this.getRandomPeriod(PeriodA, _c.noPeriods); Boolean preReq = BasicFunctions.checkPrerequisites(_parent.Representation, _c); Boolean ChecMinMaxCredits = BasicFunctions.checkMinMaxCredit(_parent.PeriodCreditLoad, _c.maxCredits, _c.minCredits); Boolean ChecMinMaxCourses = BasicFunctions.checkMinMaxCourse(_parent.Representation, _c.maxCourses, _c.minCourses); Boolean CheckEvaluation = BasicFunctions.checkFitnessMaximumLoad(_parent.Representation, _c, _parent.Evaluation); //Console.WriteLine(BasicFunctions.getSolutionDetails(_parent)); ResultingChild = this.SwapCourses(PeriodA, PeriodB, _c, _parent); //Console.WriteLine(BasicFunctions.getSolutionDetails(ResultingChild)); Boolean preReq2 = BasicFunctions.checkPrerequisites(ResultingChild.Representation, _c); Boolean ChecMinMaxCredits2 = BasicFunctions.checkMinMaxCredit(ResultingChild.PeriodCreditLoad, _c.maxCredits, _c.minCredits); Boolean ChecMinMaxCourses2 = BasicFunctions.checkMinMaxCourse(ResultingChild.Representation, _c.maxCourses, _c.minCourses); Boolean CheckEvaluation2 = BasicFunctions.checkFitnessMaximumLoad(ResultingChild.Representation, _c, ResultingChild.Evaluation); } return(ResultingChild); }
private int[] getShiftingSolution(Curriculum _c, Individ _individ) { int[] ResultingShiftingSolution = new int[3]; int[] ShiftCandidate = new int[2]; int candidateOriginePeriod, candidateOrigineCourseID; List <int> ShiftingPeriodCandidates; ShiftCandidate = this.getShiftCandidateOrigine(_individ, _c); candidateOriginePeriod = ShiftCandidate[0]; candidateOrigineCourseID = ShiftCandidate[1]; Course candidateCourse = _c.courses[candidateOrigineCourseID - 1]; ShiftingPeriodCandidates = this.getShiftingPeriodCandidates(_c, _individ.Representation, _individ.PeriodCreditLoad, candidateOriginePeriod, candidateCourse); //Console.WriteLine("ShiftingPeriodCandidates.Count" + ShiftingPeriodCandidates.Count); if (ShiftingPeriodCandidates.Count != 0) { int RandomListIndex = BasicFunctions.randomGenerator.Next(0, ShiftingPeriodCandidates.Count - 1); ResultingShiftingSolution[0] = candidateOriginePeriod; ResultingShiftingSolution[1] = candidateOrigineCourseID; ResultingShiftingSolution[2] = ShiftingPeriodCandidates[RandomListIndex]; } return(ResultingShiftingSolution); }
public Individ GetChild(Individ _parent, Curriculum _c) { Individ ResultingChild = (Individ)_parent.Clone(); for (int RanInensityIndex = 0; RanInensityIndex < Parameters.ShiftMutateIntensity; RanInensityIndex++) { //Boolean preReq4 = BasicFunctions.checkPrerequisites(ResultingChild.Representation, _c); int[] ShiftingSolution = this.getShiftingSolution(_c, ResultingChild); int OriginePeriod = ShiftingSolution[0]; int CourseID = ShiftingSolution[1]; int DestinationPeriod = ShiftingSolution[2]; if (CourseID != 0) { ResultingChild.Representation[OriginePeriod].Remove(CourseID); ResultingChild.Representation[DestinationPeriod].Add(CourseID); ResultingChild.PeriodCreditLoad[OriginePeriod] -= _c.courses[CourseID - 1].credit; ResultingChild.PeriodCreditLoad[DestinationPeriod] += _c.courses[CourseID - 1].credit; ResultingChild.Evaluation = BasicFunctions.AssessFitnessMaximumLoad(ResultingChild); //Boolean preReq5 = BasicFunctions.checkPrerequisites(ResultingChild.Representation, _c); } } return(ResultingChild); }
public static double AssessFitnessSquareDeviation(Individ _Individ) { double averagePeriodLoad = AveragePeriodLoad(_Individ.PeriodCreditLoad); double stDev = 0; for (int i = 0; i < _Individ.PeriodCreditLoad.Length; i++) { stDev += Math.Pow(_Individ.PeriodCreditLoad[i] - averagePeriodLoad, 2); } return(stDev); }
public Individ Clone() { Individ ClonedIndivid = new Individ(PeriodCreditLoad.Length); for (int i = 0; i < this.Representation.Length; i++) { ClonedIndivid.Representation[i].AddRange(this.Representation[i]); } Array.Copy(this.PeriodCreditLoad, ClonedIndivid.PeriodCreditLoad, this.PeriodCreditLoad.Length); ClonedIndivid.Evaluation = this.Evaluation; return(ClonedIndivid); }
public Individ GetChild(Individ parentA, Individ parentB) { //step1: select randomly course Ca from parent A int RandomPeriodIndex = BasicFunctions.randomGenerator.Next(0, parentA.Representation.Length); List <int> ParentAselectedPeriod = parentA.Representation[RandomPeriodIndex]; int RandomCourseIndex = BasicFunctions.randomGenerator.Next(0, ParentAselectedPeriod.Count); int CourseID = ParentAselectedPeriod[RandomCourseIndex]; //step 2: remove course Ca from parent B Individ TempChildA = RemoveCourse(parentB, CourseID); List <int> randomPeriodList = BasicFunctions.getRandomPeriodList(TempChildA.Representation.Length); return(null); }
public Individ RemoveCourse(Individ Ind, int CourseID) { for (int i = 0; i < Ind.Representation.Length; i++) { List <int> CurrentPeriod = Ind.Representation[i]; if (CurrentPeriod.Contains(CourseID)) { CurrentPeriod.Remove(CourseID); break; } } return(Ind); }
public static int AssessFitnessMaximumLoad(Individ _Individ) { int MaxLoad = _Individ.PeriodCreditLoad[0]; for (int i = 1; i < _Individ.PeriodCreditLoad.Length; i++) { if (MaxLoad < _Individ.PeriodCreditLoad[i]) { MaxLoad = _Individ.PeriodCreditLoad[i]; } } return(MaxLoad); }
public static String getSolutionDetails(Individ S) { String result; result = "Solution quality is " + S.Evaluation + "\n"; for (int i = 0; i < S.PeriodCreditLoad.Length; i++) { result = result + "Period " + (i + 1) + " with credit load " + S.PeriodCreditLoad[i] + " has the following courses : "; for (int j = 0; j < S.Representation[i].Count; j++) { result = result + S.Representation[i][j] + " "; } result = result + "\n"; } 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 }); }
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 btnRun_Click(object sender, EventArgs e) { GeneticAlgorithm ga = new GeneticAlgorithm(); Individ BestSolution = ga.Apply(objCurriculum); }
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); }
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); }
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); }
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 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); }