Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
a.hosseini committed May 19, 2024
1 parent f69611a commit fd20b98
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 41 deletions.
81 changes: 78 additions & 3 deletions Course Scheduler/Controllers/SchedulerController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ClosedXML.Excel;
using Course_Scheduler.Data;
using Course_Scheduler.Models;
using Course_Scheduler.Models.Enum;
using Course_Scheduler.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -46,21 +47,95 @@ public async Task<IActionResult> GeneticAlgorithm(int semesterId,int Count = 100
.Where(fc => fc.SemesterId == semesterId)
.ToList();
var coursePrerequisites = _context.CoursePrerequisites.ToList();

var corequisiteCourse = _context.CorequisitesCourses.ToList();
var schedules = new List<Schedule>();

GeneticAlgorithm ga = new GeneticAlgorithm(courses, courseToTeachers, coursePenaltys,
teachers, fixedCourses, coursePrerequisites);
teachers, fixedCourses, coursePrerequisites,
corequisiteCourse);

schedules.AddRange(await ga.CreateSchedules(Count,UnhealtyCount));

schedules = schedules.OrderBy(s => s.Penalty.TotalPenalty).ToList();






//using (var workbook = new XLWorkbook())
//{
// // Add a worksheet to the workbook
// var worksheet = workbook.Worksheets.Add("My Worksheet2");

// // Set headers


// // Populate data
// var cell = 0;
// int row = 0;
// int id = 0;
// foreach (var schedule in schedules)
// {
// worksheet.Cell(row, cell + 1).Value = "T1";
// worksheet.Cell(row, cell + 2).Value = "T2";
// worksheet.Cell(row, cell + 3).Value = "T3";
// worksheet.Cell(row, cell + 4).Value = "T4";
// worksheet.Cell(row, cell + 5).Value = "T5";

// worksheet.Cell(row+1, cell).Value = "sa";
// worksheet.Cell(row+2, cell).Value = "su";
// worksheet.Cell(row+3, cell).Value = "m";
// worksheet.Cell(row+4, cell).Value = "t";
// worksheet.Cell(row+5, cell).Value = "w";

// foreach (var CTT in schedule.CourseTeacherClassTimes)
// {
// foreach (var time in CTT.ClassTimes)
// {
// switch (time.ClassTime.ToString().Substring(0, time.ClassTime.ToString().IndexOf('T')))
// {
// case "Saturday":
// // کد مربوط به روز شنبه
// switch (time.ClassTime.ToString().Substring(time.ClassTime.ToString().IndexOf('T')), time.ClassTime.ToString().Length)
// {
// //case "":
// // break;
// }

// //worksheet.Cell(row+1,);
// break;
// case "Sunday":
// // کد مربوط به روز یکشنبه
// break;
// case "Monday":
// // کد مربوط به روز دوشنبه
// break;
// case "Tuesday":
// // کد مربوط به روز سه شنبه
// break;
// case "Wednesday":
// // کد مربوط به روز چهارشنبه
// break;
// case "Thursday":
// // کد مربوط به روز پنجشنبه
// break;
// case "Friday":
// // کد مربوط به روز جمعه
// break;
// default:
// // کد مربوط به زمانی که روز مشخص نیست
// break;
// }
// }
// }
// row++;
// }

// // Save the workbook
// workbook.SaveAs("TimeTable.xlsx");
//}


// Create a new Excel workbook
using (var workbook = new XLWorkbook())
{
Expand Down
11 changes: 11 additions & 0 deletions Course Scheduler/Models/CorequisiteCourse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ public class CorequisiteCourse : Base
public int CorequisiteCourseId { get; set; }
public Course Course { get; set; }



public CorequisiteCourse DeepCopy()
{
return new CorequisiteCourse
{
CourseId = this.CourseId,
CorequisiteCourseId = this.CorequisiteCourseId,
Course = this.Course.DeepCopy()
};
}
}
}
20 changes: 20 additions & 0 deletions Course Scheduler/Models/Course.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,25 @@ public Course()
public List<Group> Groups { get; set; }
public List<CorequisiteCourse> CorequisiteCourses { get; set; }
public List<CoursePrerequisites> Prerequisites { get; set; }





public Course DeepCopy()
{
var newCourse = new Course
{
Name = this.Name,
Credits = this.Credits,
CountOfClass = this.CountOfClass,
CourseCode = this.CourseCode,
Groups = this.Groups.Select(g => g.DeepCopy()).ToList(),
CorequisiteCourses = this.CorequisiteCourses.Select(cc => cc.DeepCopy()).ToList(),
Prerequisites = this.Prerequisites.Select(cp => cp.DeepCopy()).ToList()
};

return newCourse;
}
}
}
13 changes: 13 additions & 0 deletions Course Scheduler/Models/CoursePrerequisites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,18 @@ public class CoursePrerequisites : Base
public int CourseId { get; set; }
public int PrerequisiteCourseId { get; set; }
public Course Course { get; set; }




public CoursePrerequisites DeepCopy()
{
return new CoursePrerequisites
{
CourseId = this.CourseId,
PrerequisiteCourseId = this.PrerequisiteCourseId,
Course = this.Course.DeepCopy()
};
}
}
}
11 changes: 11 additions & 0 deletions Course Scheduler/Models/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,16 @@ public Group()
}
public string Name { get; set; }
public List<Course> Courses { get; set; }



public Group DeepCopy()
{
return new Group
{
Name = this.Name,
Courses = this.Courses.Select(c => c.DeepCopy()).ToList()
};
}
}
}
35 changes: 35 additions & 0 deletions Course Scheduler/Models/Schedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,40 @@ public Schedule()
}
public List<CourseTeacherClassTime> CourseTeacherClassTimes { get; set; }
public Penalty Penalty { get; set; }




public Schedule DeepCopy()
{
var newSchedule = new Schedule
{
Penalty = new Penalty
{
TotalPenalty = this.Penalty?.TotalPenalty ?? 0,
PenaltyOfOverlay = this.Penalty?.PenaltyOfOverlay ?? 0,
PenaltyOfTeacher = this.Penalty?.PenaltyOfTeacher ?? 0,
PenaltyOfMaximumCountOfClassInSection = this.Penalty?.PenaltyOfMaximumCountOfClassInSection ?? 0
}
};

foreach (var ctt in this.CourseTeacherClassTimes)
{
newSchedule.CourseTeacherClassTimes.Add(new CourseTeacherClassTime
{
Course = ctt.Course.DeepCopy(),
Teacher = ctt.Teacher.DeepCopy(),
SemesterId = ctt.SemesterId,
ClassTimes = ctt.ClassTimes.Select(ct => new EvenOddClassTime
{
ClassTime = ct.ClassTime,
EvenOdd = ct.EvenOdd,
CourseTeacherClassTimeId = ct.CourseTeacherClassTimeId
}).ToList()
});
}

return newSchedule;
}
}
}
27 changes: 27 additions & 0 deletions Course Scheduler/Models/Teacher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ public Teacher()
public List<TeacherClassTimeWithPenalties> PreferredTimes{ get; set; }
public int MaximumDayCount { get; set; }
public int PenaltyForEmptyTime { get; set; } = 1;

public Teacher DeepCopy()
{
return new Teacher
{
Name = this.Name,
TeacherCode = this.TeacherCode,
MaximumDayCount = this.MaximumDayCount,
PenaltyForEmptyTime = this.PenaltyForEmptyTime,
PreferredTimes = this.PreferredTimes.Select(pt => pt.DeepCopy()).ToList()
};
}

}
public class TeacherClassTimeWithPenalties : Base
{
Expand All @@ -22,4 +35,18 @@ public class TeacherClassTimeWithPenalties : Base
public EvenOdd? EvenOdd { get; set; }
public int TeacherId { get; set; }
public Teacher Teacher { get; set; }


public TeacherClassTimeWithPenalties DeepCopy()
{
return new TeacherClassTimeWithPenalties
{
PreferredTime = this.PreferredTime,
Penalty = this.Penalty,
EvenOdd = this.EvenOdd,
TeacherId = this.TeacherId,
Teacher = this.Teacher?.DeepCopy()
};
}

}
Loading

0 comments on commit fd20b98

Please sign in to comment.