From f2551781624af97fe7016deab09336c125351953 Mon Sep 17 00:00:00 2001 From: Angelika Iskra Date: Wed, 1 Jun 2022 19:52:16 +0200 Subject: [PATCH] fix file not found error; --- algorithms/genetic/map_generator.py | 2 +- algorithms/genetic/map_importer_exporter.py | 13 ++++++++----- algorithms/genetic/population.py | 0 3 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 algorithms/genetic/population.py diff --git a/algorithms/genetic/map_generator.py b/algorithms/genetic/map_generator.py index e90488f..abe85b9 100644 --- a/algorithms/genetic/map_generator.py +++ b/algorithms/genetic/map_generator.py @@ -10,7 +10,7 @@ def main() -> None: print(example_genome.grid) islands = get_islands(example_genome.grid, example_genome.knights_red) print(f'Roots {islands} and islands count {len(islands)}') - # export_map(example_genome.grid) fixme: FileNotFoundError + export_map(example_genome.grid) if __name__ == '__main__': diff --git a/algorithms/genetic/map_importer_exporter.py b/algorithms/genetic/map_importer_exporter.py index 9293f75..a3b5034 100644 --- a/algorithms/genetic/map_importer_exporter.py +++ b/algorithms/genetic/map_importer_exporter.py @@ -2,12 +2,13 @@ import json import random import string from datetime import datetime - +from pathlib import Path import numpy import numpy.typing as npt from os import listdir from os.path import isfile, join +MAPS_FOLDER = Path("resources/maps/") # Save map to file @@ -16,8 +17,9 @@ def export_map(grid: npt.NDArray): now = datetime.now() file_name = "map_" + now.strftime("%Y_%m_%d_%H_%M_%S") + ".json" + file_to_open = MAPS_FOLDER / file_name - with open("resources/maps/" + file_name, "w") as write_file: + with open(file_to_open, "w") as write_file: json.dump(json_data, write_file) print("Saved map to file " + file_name) @@ -30,9 +32,10 @@ def import_random_map() -> object: # Read map from file -def import_map(file: string) -> object: - with open("resources/maps/" + file, "r") as read_file: - print("Reading map from file " + file) +def import_map(file_name: string) -> object: + file_to_open = MAPS_FOLDER / file_name + with open(file_to_open, "r") as read_file: + print("Reading map from file " + file_name) decoded_json = json.load(read_file) decoded_grid = numpy.asarray(decoded_json["map"]) diff --git a/algorithms/genetic/population.py b/algorithms/genetic/population.py new file mode 100644 index 0000000..e69de29