Add execution module

This commit is contained in:
coolneng 2021-06-22 10:00:44 +02:00
parent 44d4cefa98
commit e0395858c0
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
2 changed files with 85 additions and 1 deletions

84
src/execution.py Normal file
View File

@ -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()

View File

@ -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()