-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.py
179 lines (151 loc) · 7.16 KB
/
file.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from Classtab import Tab
import os
import contextlib
def input_file():
"""
Ask the user to choose a constraint table
:return num_file: the number of the file to read
"""
num_file = 0
while num_file < 1 or num_file > 12:
try:
num_file = int(input("Veuillez choisir une table de contrainte de "
"1 à 12 en inscrivant le numéro de la table : "))
if num_file < 1 or num_file > 12:
print("Veuillez entrer un numéro de table valide")
except ValueError:
print("Invalid input. Please enter a number.")
return num_file
def read_file(filename):
"""
Read file, remove backslash n, and return a list of steps from the constraint table
:param filename: the name of the file
:return steps: a list of steps from the constraint table
"""
with open(filename, 'r') as f:
data = f.readlines()
# removing backslash n from the end of the line
for i in range(0, len(data)):
data[i] = remove_backslashn(data[i])
# splitting the line by comma
steps = []
for i in range(0, len(data)):
steps.append(data[i].split(','))
# splitting each steps by space
steps[i] = steps[i][0].split(' ')
# recursive function to remove empty string and counter out of range
steps[i] = remove_empty_strings(steps[i])
# converting the second element (weight) of each step to int
for i in range(0, len(steps)):
for j in range(0, len(steps[i])):
steps[i][j] = int(steps[i][j])
return steps
def remove_backslashn(step):
"""
Remove backslash n from the end of line
:param step: The line with a backslash n
:return step: The line without backslash n
"""
if step[-1] == "\n":
step = step[:-1]
return step
def remove_empty_strings(lst, index=0):
"""
Remove empty strings from a list recursively to avoid out of range
:param lst: The list to remove empty strings from
:param index: The current index (default is 0)
:return: The list without empty strings
"""
if index == len(lst):
return lst
if lst[index] == '':
lst.pop(index)
return remove_empty_strings(lst, index)
else:
return remove_empty_strings(lst, index + 1)
def problem_initialization(num_file):
"""
Initialize the problem with the constraint table
:param num_file: number of the file to read
:return problem_table: the problem table
"""
# read file
constraint_table = read_file("./constraint_tables/table_" + str(num_file) + ".txt")
# initialize problem table
tab_problem = Tab()
# add cout to the problem table
# len(constraint_table) - 1 to avoid the last line which is specific to command
for i in range(0, len(constraint_table) - 1):
temporary_list = []
# len(constraint_table[i]) - 1 to avoid the last element which is specific to provider
for j in range(0, len(constraint_table[i]) - 1):
temporary_list.append(constraint_table[i][j])
# append the temporary list of cost to the problem table cost section
tab_problem.cout.append(temporary_list)
# add command to the problem table
# len(constraint_table[-1]) to get the last line which is the total of a specific command
for i in range(0, len(constraint_table[-1])):
tab_problem.command.append(constraint_table[-1][i])
# add provider to the problem table
# len(constraint_table) - 1 to avoid the last line (command) and get all the providers
for i in range(0, len(constraint_table) - 1):
tab_problem.provider.append(constraint_table[i][-1])
return tab_problem
def traces_execution():
"""
Test all the functions and write all the result in txt file
:param: None
"""
# Check if the directory contain all the test files : to know if the file traces.txt already exists
# path of the directory
path = "B2_traces_execution"
# Check if the directory exists
if not os.path.exists(path):
os.makedirs(path)
# Getting the list of directories
dir_traces = os.listdir(path)
# Checking if the list of directories contains all the test files
if len(dir_traces) < 24:
# Running the function for each file
for num_file in range(1, 13):
problem_table = problem_initialization(num_file)
with open(f"B2_traces_execution/B2_trace{num_file}_no.txt", "w", encoding="utf-8") as f:
f.write("----------- Etape 1 : Lecture & affichage de la table de contrainte -----------\n")
# Number of providers
f.write(f"Nombre de producteurs : {len(problem_table.provider)}\n")
# Number of commands
f.write(f"Nombre de commandes : {len(problem_table.command)}\n")
f.write("\n-------------------- Etape 2 : Nord-Ouest --------------------\n")
# Redirect the std output to the file
with contextlib.redirect_stdout(f):
print("Proposition initiale par la méthode du nord-ouest :\n")
problem_table.nord_ouest()
problem_table.print_tab_traces()
print("\nMéthode du marche pied à partir de la proposition du nord-ouest :\n")
potential_cost, marginal_cost = problem_table.stepping_stone()
print(f"Marche pied : Flot(coût)")
problem_table.print_tab_traces()
total_cost = problem_table.total_cost()
print(f"Coût total : {total_cost}")
print("\nCoûts potentiels et marginaux (avant étape finale) :\n")
problem_table.print_tab_cout(potential_cost, marginal_cost)
with open(f"B2_traces_execution/B2_trace{num_file}_bh.txt", "w", encoding="utf-8") as f:
f.write("----------- Etape 1 : Lecture & affichage de la table de contrainte -----------\n")
# Number of providers
f.write(f"Nombre de producteurs : {len(problem_table.provider)}\n")
# Number of commands
f.write(f"Nombre de commandes : {len(problem_table.command)}\n")
f.write("\n-------------------- Etape 2 : Balas-Hammer --------------------\n")
# Redirect the std output to the file
with contextlib.redirect_stdout(f):
print("Proposition initiale par la méthode de Balas-Hammer :\n")
problem_table.balas_hammer()
problem_table.print_tab_traces()
print("\nMéthode du marche pied à partir de la proposition de Balas-Hammer :\n")
potential_cost, marginal_cost = problem_table.stepping_stone()
print(f"Marche pied : Flot(coût)")
problem_table.print_tab_traces()
total_cost = problem_table.total_cost()
print(f"Coût total : {total_cost}")
print("\nCoûts potentiels et marginaux (avant étape finale) :\n")
problem_table.print_tab_cout(potential_cost, marginal_cost)