-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
162 lines (122 loc) · 4.81 KB
/
main.py
File metadata and controls
162 lines (122 loc) · 4.81 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import logging
import sys
import os
import time
from src.app.problem_instance.models import ProblemInstance, Sets
from src.app.validator.controller import ValidatorController
from src.core.factory.factory import Factory
from src.model.model import Model
from src.posprocessing.excel import Exporting
from src.utils.utils import read_sheet, save_sheet
logger = logging.getLogger(__name__)
def create_factory() -> Factory:
return Factory()
def get_parameters(factory: Factory, path_excel: str) -> tuple[dict, dict, dict, dict]:
input_importer = factory.get_model_import_controller()
logger.info("[BACKEND] Generating Parameters")
(
combination_dict,
disponibility_patients,
disponibility_professionals,
professional_hours,
local_list,
) = input_importer.handle_input(path_excel)
return (
combination_dict,
disponibility_patients,
disponibility_professionals,
professional_hours,
local_list,
)
def validate_input(factory: Factory, use_cases: dict, path: str):
validator_controller: ValidatorController = factory.get_validator_controller(
path=path, use_cases=use_cases
)
error_list = validator_controller.validate_input()
if "ERROR" in error_list:
logger.error(
"""
=====================================================================================================
\n\n
>>>>>>>>>> [VALIDATOR] THE INPUT FILE CONTAINS ERROR. PLEASE CHECK THE 'Inconsistência' tab.
\n\n
=====================================================================================================
"""
)
while True:
user_input = input("Press '0' to exit the application...\n")
if user_input == "0":
sys.exit()
def preprocess_data(factory: Factory, path_excel: str) -> ProblemInstance:
(
combination_dict,
disponibility_patients,
disponibility_professionals,
professional_hours,
local_list,
) = get_parameters(factory, path_excel)
problem_instance_controller: ProblemInstance = factory.problem_instance_controller(
local_list,
combination_dict,
disponibility_patients,
disponibility_professionals,
professional_hours,
)
sets: Sets = problem_instance_controller.get_sets(
use_case=factory.get_model_import_controller().read_xlsx(path_excel)
)
problem_instance: ProblemInstance = (
problem_instance_controller.get_problem_instance(sets)
)
return problem_instance
def run_model(problem_instance: ProblemInstance):
model = Model(problem_instance)
model.create_variables()
model.create_constraints()
model.solve()
model_data = model.export_result()
return model_data
def posprocessing(
model_data: list[list],
problem_instance: ProblemInstance,
use_cases: dict,
path_excel: str,
):
schedule_table = Exporting.create_table(
model_data, problem_instance.sets.hours, problem_instance.sets.days
)
summary = Exporting.create_summary(problem_instance, model_data)
# path_excel, _ = os.path.splitext(path_excel)
# path_excel = f"{path_excel}_output"
solution = Exporting.create_solution(model_data)
logger.info("[Results] Exporting Solution into Excel file ")
use_cases["Solução"] = solution
logger.info("[Results] Exporting Schedule table into Excel")
use_cases["Tabela de Horários"] = schedule_table
logger.info("[Results] Exporting KPI's table into Excel")
use_cases["KPI's"] = summary
save_sheet(path_excel, use_cases, ["Solução", "Tabela de Horários", "KPI's"])
def main(path_excel: str):
use_cases: dict = read_sheet(path_excel)
factory = create_factory()
validate_input(factory, use_cases, path_excel)
problem_instance: ProblemInstance = preprocess_data(
factory=factory, path_excel=path_excel
)
model_data = run_model(problem_instance=problem_instance)
posprocessing(model_data, problem_instance, use_cases, path_excel)
if __name__ == "__main__":
# logger setup
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[logging.FileHandler("debug.log"), logging.StreamHandler(sys.stdout)],
)
path_excel = input("Please provide the path to the Excel file: ")
# main function
main(path_excel)
while True:
user_input = input("Press '0' to exit the application...\n")
if user_input == "0":
break
logger.info("Execution finished")