-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
82 lines (71 loc) · 3.79 KB
/
Main.py
File metadata and controls
82 lines (71 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
# @Time : 19/01/2021 21:49
# @Author : Miruna Andreea Gheata, Pablo González Maya, Mateu Jover Mulet
# @Email : miruna.gheata1@estudiant.uib.cat
# @File : Main.py
# @Software: PyCharm
import os
import Constants
from Application.GeneticAlgorithm import GeneticAlgorithm
from Infrastructure.Loader import Loader
from datetime import datetime
import Constants
from Application.GeneticAlgorithm import GeneticAlgorithm
from Infrastructure.Loader import Loader
from datetime import datetime
from Infrastructure.Writer import Writer
from Infrastructure.Graphs import Graphs
from Infrastructure.Writer import Writer
if __name__ == '__main__':
# Create Loader element to load the data from the Excel files
loader = Loader()
# Load timetable information (total days with classes,
# hours per day, first hour of class day, las hour of
# class day)
courses, class_days, hours_per_day, start_time, end_time = loader.load_timetable_info()
print(f"Loading problem information from {Constants.FILE_EXCEL_DATA}/{Constants.SHEET_INFO}...")
# Update project constants if necessary
if Constants.COURSES != courses:
Constants.COURSES = courses
if Constants.HOURS_PER_DAY != hours_per_day:
Constants.HOURS_PER_DAY = hours_per_day
if Constants.DAYS_PER_WEEK != class_days:
Constants.WEEK_DAYS = class_days
if Constants.HOUR_START_DAY != start_time:
Constants.HOUR_START_DAY = start_time
if Constants.HOUR_END_DAY != end_time:
Constants.HOUR_END_DAY = end_time
# Load teacher information (name, availability during the week)
teachers = loader.load_teachers(class_days, hours_per_day)
print(f"Loading teachers from {Constants.FILE_EXCEL_DATA}/{Constants.SHEET_TEACHER_INFO}...")
# Load classes (name, list of teachers teach this class)
classes = loader.load_classes(teachers)
print(f"Loading classes from {Constants.FILE_EXCEL_DATA}/{Constants.SHEET_CLASS_TEACHERS_INFO}...")
# Load courses (name, list of classes/course, hours per week);
# the time information of the classes is also loaded here (hours per class in the given course)
courses = loader.load_courses(classes)
print(f"Loading courses from {Constants.FILE_EXCEL_DATA}/{Constants.SHEET_COURSE_HOURS_INFO}...")
print(f"Launching genetic algorithm computation to find a possible timetable solution...")
print(f"Total generations to be computed: {Constants.MAXIMUM_GENERATIONS}\n")
geneticAlgorithm = GeneticAlgorithm(courses, classes, teachers)
# Define the parent selection algorithm
parent_selection_type = Constants.Parent_Selection_Type.TOURNAMENT
# Compute the timetable solution
solution, cost_evolution, constraints_evolution, generation_cost_evolution = geneticAlgorithm.find_solution(parent_selection_type, True)
# Initialize writer to save the results in an Excel file
writer = Writer()
now = datetime.now()
file_name = f"TOURNM_{Constants.MUTATION}_{now.strftime('%d_%m_%Y-%H_%M')}"
# Write the resulting timetables (each column represents one specific course)
writer.write_timetable(solution, file_name)
# Write the cost evolution over the different iterations
writer.write_evolution(cost_evolution, constraints_evolution, generation_cost_evolution, file_name)
# Graphs
if not os.path.exists(Constants.DIR_GRAPH_RESULTS):
os.mkdir(Constants.DIR_GRAPH_RESULTS)
visualizer = Graphs("Torunament selection", cost_evolution, generation_cost_evolution, constraints_evolution, file_name)
visualizer.best_ind_plot()
visualizer.generation_cost_plot()
for c in Constants.CONSTRAINTS:
visualizer.constraints_plot(c)
print(f"Done! Computed timetables can be found in file {Constants.FILE_EXCEL_RESULTS} and cost evolution in file {Constants.FILE_EXCEL_EVOLUTION}.")