Refactor execution script

This commit is contained in:
coolneng 2021-06-21 17:54:26 +02:00
parent 4e640ffc2d
commit 20aa6b2d1e
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 44 additions and 42 deletions

View File

@ -14,9 +14,7 @@ def file_list(path):
def create_dataframes(): def create_dataframes():
greedy = DataFrame() return [DataFrame() for _ in range(7)]
local = DataFrame()
return greedy, local
def process_output(results): def process_output(results):
@ -33,51 +31,55 @@ def process_output(results):
return distances, time return distances, time
def populate_dataframes(greedy, local, greedy_list, local_list, dataset): def populate_dataframes(df, output_list, dataset):
greedy_distances, greedy_time = process_output(greedy_list) df_distances, df_time = process_output(output_list)
local_distances, local_time = process_output(local_list) df_dict = {
greedy_dict = {
"dataset": dataset.removeprefix("data/"), "dataset": dataset.removeprefix("data/"),
"media distancia": mean(greedy_distances), "media distancia": mean(df_distances),
"desviacion distancia": std(greedy_distances), "desviacion distancia": std(df_distances),
"media tiempo": mean(greedy_time), "media tiempo": mean(df_time),
"desviacion tiempo": std(greedy_time), "desviacion tiempo": std(df_time),
} }
local_dict = { df = df.append(df_dict, ignore_index=True)
"dataset": dataset.removeprefix("data/"), return df
"media distancia": mean(local_distances),
"desviacion distancia": std(local_distances),
"media tiempo": mean(local_time),
"desviacion tiempo": std(local_time),
}
greedy = greedy.append(greedy_dict, ignore_index=True)
local = local.append(local_dict, ignore_index=True)
return greedy, local
def script_execution(filenames, greedy, local, iterations=3): def script_execution(filenames, df_list, iterations=2):
script = "src/main.py" script = "src/main.py"
parameters = [
["genetic", "uniform", "generational"],
["genetic", "position", "generational"],
["geneti,c" "uniform", "stationary"],
["genetic," "position", "stationary"],
["memetic", "all"],
["memetic", "random"],
["memetic", "best"],
]
populated_list = []
for dataset in filenames: for dataset in filenames:
print(f"Running on dataset {dataset}") print(f"Running on dataset {dataset}")
greedy_list = [] for df, params in zip(df_list, parameters):
local_list = [] print(f"Running {params} algorithm")
for _ in range(iterations): output_list = []
greedy_cmd = run( for _ in range(iterations):
[executable, script, dataset, "greedy"], capture_output=True output_cmd = run(
).stdout.splitlines() [executable, script, dataset, *params], capture_output=True
local_cmd = run( ).stdout.splitlines()
[executable, script, dataset, "local"], capture_output=True output_list.append(output_cmd)
).stdout.splitlines() populated_list.append(populate_dataframes(df, output_list, dataset))
greedy_list.append(greedy_cmd) return populated_list
local_list.append(local_cmd)
greedy, local = populate_dataframes(
greedy, local, greedy_list, local_list, dataset
)
return greedy, local
def export_results(greedy, local): def export_results(df_list):
dataframes = {"Greedy": greedy, "Local search": local} dataframes = {
"Generational uniform genetic": df_list[0],
"Generational position genetic": df_list[1],
"Stationary uniform genetic": df_list[2],
"Stationary position genetic": df_list[3],
"All genes memetic": df_list[4],
"Random genes memetic": df_list[5],
"Best genes memetic": df_list[6],
}
writer = ExcelWriter(path="docs/algorithm-results.xlsx", engine="xlsxwriter") writer = ExcelWriter(path="docs/algorithm-results.xlsx", engine="xlsxwriter")
for name, df in dataframes.items(): for name, df in dataframes.items():
df.to_excel(writer, sheet_name=name, index=False) df.to_excel(writer, sheet_name=name, index=False)
@ -91,9 +93,9 @@ def export_results(greedy, local):
def main(): def main():
datasets = file_list(path="data/*.txt") datasets = file_list(path="data/*.txt")
greedy, local = create_dataframes() df_list = create_dataframes()
populated_greedy, populated_local = script_execution(datasets, greedy, local) populated_df_list = script_execution(datasets, df_list)
export_results(populated_greedy, populated_local) export_results(populated_df_list)
if __name__ == "__main__": if __name__ == "__main__":