From e0395858c0552bbabbbd393ffa975acef9e8dc8f Mon Sep 17 00:00:00 2001 From: coolneng Date: Tue, 22 Jun 2021 10:00:44 +0200 Subject: [PATCH] Add execution module --- src/execution.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.py | 2 +- 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/execution.py diff --git a/src/execution.py b/src/execution.py new file mode 100644 index 0000000..f111a2b --- /dev/null +++ b/src/execution.py @@ -0,0 +1,84 @@ +from glob import glob +from subprocess import run +from sys import executable + +from numpy import mean, std +from pandas import DataFrame, ExcelWriter + + +def file_list(path): + file_list = [] + for fname in glob(path): + file_list.append(fname) + return file_list + + +def create_dataframes(): + return [DataFrame() for _ in range(2)] + + +def process_output(results): + distances = [] + time = [] + for line in results: + if line.startswith(bytes("Total distance:", encoding="utf-8")): + line_elements = line.split(sep=bytes(":", encoding="utf-8")) + distances.append(float(line_elements[1])) + if line.startswith(bytes("Execution time:", encoding="utf-8")): + line_elements = line.split(sep=bytes(":", encoding="utf-8")) + time.append(float(line_elements[1])) + return distances, time + + +def populate_dataframe(df, output_cmd, dataset): + distances, time = process_output(output_cmd) + data_dict = { + "dataset": dataset.removeprefix("data/"), + "media distancia": mean(distances), + "desviacion distancia": std(distances), + "media tiempo": mean(time), + "desviacion tiempo": std(time), + } + df = df.append(data_dict, ignore_index=True) + return df + + +def script_execution(filenames, df_list): + script = "src/main.py" + parameters = [["annealing"], ["multistart"]] + for dataset in filenames: + print(f"Running on dataset {dataset}") + for index, params in zip(range(4), parameters): + print(f"Running {params} algorithm") + output_cmd = run( + [executable, script, dataset, *params], capture_output=True + ).stdout.splitlines() + df_list[index] = populate_dataframe(df_list[index], output_cmd, dataset) + return df_list + + +def export_results(df_list): + dataframes = { + "Simulated annealing": df_list[0], + "Multi-start local search": df_list[1], + } + writer = ExcelWriter(path="docs/algorithm-results.xlsx", engine="xlsxwriter") + for name, df in dataframes.items(): + df.to_excel(writer, sheet_name=name, index=False) + worksheet = writer.sheets[name] + for index, column in enumerate(df): + series = df[column] + max_length = max(series.astype(str).str.len().max(), len(str(series.name))) + worksheet.set_column(index, index, width=max_length + 5) + writer.save() + + +def main(): + datasets = file_list(path="data/*.txt") + df_list = create_dataframes() + populated_df_list = script_execution(datasets, df_list) + export_results(populated_df_list) + + +if __name__ == "__main__": + main() diff --git a/src/main.py b/src/main.py index 97b96cb..1199df6 100755 --- a/src/main.py +++ b/src/main.py @@ -23,7 +23,7 @@ def show_results(solution, time_delta): def parse_arguments(): parser = ArgumentParser() parser.add_argument("file", help="dataset of choice") - parser.add_argument("algorithm", choices=["annealing", "multisearch"]) + parser.add_argument("algorithm", choices=["annealing", "multistart"]) return parser.parse_args()