Hi All,
I am trying to create a GA to solve timetabling for exams, so far I have created a array of exams e.g. [2,3,5,4] means exam 2 is held at time slot 1 and exam 3 is at time slot 2.
//This is the testChromosome.ChromosomeArray is the sample code.
I have also created student data which hold exams a student takes for example [1,3,5,4] this means student takes exams 1,3,5,4.
//This is testStudent in the sample code.
I want my code to add a punishment to the testChromosome each time a students exams are in consecutive time slots. e.g. the example above will be punished for 3, 5 next to each other and 5,4 next to each other. The Chromosome also gets punished if the students exam is not timetabled. The code I have wrote seems to work ok, but sometime I get a extra 5 or 10 punishment especially when exams are timetabled twice e.g testChromosome = [1,2,2].
I think it is a simple fix I just can't seem to see it.
//-----------------------------Sample Code-------------------------------
public void consecutiveExams()
{
foreach (Student testStudent in testStudentData.SPopulation)//-- For each student in the SPopulation.
{
for (int i = 0; i < testStudent.StudentsNumberOfExams; i++) //-- for each exam within the test student
{
int testexam1 = testStudent.ExamStudentTakes[i]; //-- set testexam1 to equal the ith element of exams students takes.
if (testChromosome.ChromosomeArray.Contains(testexam1))
{
for (int j = 0; j < testChromosome.ChromosomeArray.Count(); j++) //-- for loop to find the location of the test exam in chromosome.
{
if (testChromosome.ChromosomeArray.ElementAt(j) == testexam1) //-- if the jth element in the chromosomeArray is equal to testexam return true.
{
for (int k = 0; k < testStudent.StudentsNumberOfExams; k++) //-- for each element in teststudent if the j-1 and j+1 chromosomeArray
//elements are within the students exam array then the chromosome gets punished by 5.
{
if (testChromosome.ChromosomeArray.ElementAtOrDefault((j + 1)) == testStudent.ExamStudentTakes[k] && ((j + 1) < testChromosome.ChromosomeArray.Count()))
{
punishment += 5; //--Punishment 5 for each consecutive exams.
}
if (testChromosome.ChromosomeArray.ElementAtOrDefault((j - 1)) == testStudent.ExamStudentTakes[k] && ((j - 1) >= 0))
{
punishment += 5; //--Punishment 5 for each consecutive exams.
}
}
}
}
}
else
{
//punishment += 100; //Chromosome gets punished 100 if the students exam is not time within the timetable.
}
}
}
}
Kind Regards
FlippA