diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..272833f --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Игнорируемые файлы по умолчанию +/shelf/ +/workspace.xml +# HTTP-клиент на основе редактора +/httpRequests/ diff --git a/.idea/2026-rff_mp.iml b/.idea/2026-rff_mp.iml new file mode 100644 index 0000000..c03f621 --- /dev/null +++ b/.idea/2026-rff_mp.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..590a59e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a150c6b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 7b46480..0000000 --- a/README.md +++ /dev/null @@ -1,54 +0,0 @@ -# 2026-MP - -Практика по курсам "Методы программирования" и "Программная инженерия" РФФ ННГУ - -[Презентация по курсу (обновляемая)](https://docs.google.com/presentation/d/1wmYjy5QDoYECEHi7NAAINPulU9pLsaIi-aLaUppspps/edit?usp=sharing) - -Для работы необходим python 3.11 и выше. Библиотеки: numpy, pandas, matplotlib, tensorflow, Pillow. Редактор любой. Из неплохих: IDLE (родной, идёт вместе с установщиком), Visual Studio Code, notepad++, PyCharm, vim (для любителей сначала страдать, потом наслаждаться). - -Работа с блокнотами онлайн, с возможностью подключения удалённых мощностей гугла (GPU, TPU): https://colab.research.google.com/ - -Мой контакт: nsmorozov@rf.unn.ru - -Внутри папки группы создать папку имени себя (фамилия и имя). В своей папке можете делать все что угодно, в чужие не залезать, в корневую тоже. Я буду ориентироваться на файлы, где в названии будет номер лабораторной. - -**Название пулл-реквеста должно начинаться с квадратных скобок, в которых перечислены номера сдаваемых лабораторных работ. Не больше одного активного реквеста, если надо довнести -- надо обновить текущий.** - -### Крайний срок приема работ 25.05.2026 до 14:00 - -## Задание 1 -- репозиторий - -0. Создай пользователя (логин — фамилия+инициалы слитно транслитом, как в терминал-классе). - -1. Зайди в этот репозиторий на Gitea, нажми кнопку **Форкнуть**, чтобы создать копию в своем аккаунте. - -2. **Клонирование:** Скопируй ссылку на свой форк и выполни: - ```bash - git clone <ссылка_на_ваш_форк> - cd <название_репозитория> - ``` - -3. **Создай ветку** (название — фамилия+инициалы слитно транслитом, буква в букву как логин): - ```bash - git checkout -b IvanovII - ``` - -4. **Создай папку** с таким же названием (`IvanovII`) и внутри неё — текстовый файл, названный номером вашей группы (например, `101.md`). - -5. **Сохрани изменения:** - ```bash - git add -A - git commit -m "[0] initial commit" - ``` - -6. Отправь ветку **в свой форк** на Gitea: - ```bash - git push origin IvanovII - ``` - -7. **Создай запрос на слияние (Pull Request):** На Gitea перейди в свой форк, выбери ветку `IvanovII`, нажмите **Запрос на слияние**. Убедитесь, что: - - Базовый репозиторий: **учебный** (преподавателя) - - Базовая ветка: **develop** - - Сравниваемая ветка: **свой форк / IvanovII** - -8. Отправь PR. \ No newline at end of file diff --git a/stepinim/lab1_structure/docs/data/lab1_graph.png b/stepinim/lab1_structure/docs/data/lab1_graph.png new file mode 100644 index 0000000..7989d6b Binary files /dev/null and b/stepinim/lab1_structure/docs/data/lab1_graph.png differ diff --git a/stepinim/lab1_structure/docs/data/lab1_results.csv b/stepinim/lab1_structure/docs/data/lab1_results.csv new file mode 100644 index 0000000..208876b --- /dev/null +++ b/stepinim/lab1_structure/docs/data/lab1_results.csv @@ -0,0 +1,55 @@ +Структура,Режим,Повтор,Операция,Время (сек) +LinkedList,shuffled,1,insert,0.5023735999711789 +LinkedList,shuffled,1,search,0.022223800013307482 +LinkedList,shuffled,1,delete,0.010106799949426204 +LinkedList,shuffled,2,insert,0.5151404999778606 +LinkedList,shuffled,2,search,0.023844500014092773 +LinkedList,shuffled,2,delete,0.010028599994257092 +LinkedList,shuffled,3,insert,0.5328615000471473 +LinkedList,shuffled,3,search,0.020557800016831607 +LinkedList,shuffled,3,delete,0.012162799946963787 +LinkedList,sorted,1,insert,0.4577932999818586 +LinkedList,sorted,1,search,0.017212599981576204 +LinkedList,sorted,1,delete,0.012185800005681813 +LinkedList,sorted,2,insert,0.43183969997335225 +LinkedList,sorted,2,search,0.01829650002764538 +LinkedList,sorted,2,delete,0.012130599992815405 +LinkedList,sorted,3,insert,0.436789300001692 +LinkedList,sorted,3,search,0.017460400005802512 +LinkedList,sorted,3,delete,0.012465099978726357 +HashTable,shuffled,1,insert,0.0032562999986112118 +HashTable,shuffled,1,search,9.469996439293027e-05 +HashTable,shuffled,1,delete,5.15999854542315e-05 +HashTable,shuffled,2,insert,0.0031429000082425773 +HashTable,shuffled,2,search,9.000004502013326e-05 +HashTable,shuffled,2,delete,4.360004095360637e-05 +HashTable,shuffled,3,insert,0.003212600015103817 +HashTable,shuffled,3,search,0.00010830000974237919 +HashTable,shuffled,3,delete,4.650000482797623e-05 +HashTable,sorted,1,insert,0.0030796999926678836 +HashTable,sorted,1,search,8.420000085607171e-05 +HashTable,sorted,1,delete,4.730001091957092e-05 +HashTable,sorted,2,insert,0.0030180999892763793 +HashTable,sorted,2,search,9.079999290406704e-05 +HashTable,sorted,2,delete,5.299999611452222e-05 +HashTable,sorted,3,insert,0.0029779999749734998 +HashTable,sorted,3,search,8.510000770911574e-05 +HashTable,sorted,3,delete,6.589997792616487e-05 +BST,shuffled,1,insert,0.011618499993346632 +BST,shuffled,1,search,0.00031289999606087804 +BST,shuffled,1,delete,0.0002456999500282109 +BST,shuffled,2,insert,0.021565500006545335 +BST,shuffled,2,search,0.00032350001856684685 +BST,shuffled,2,delete,0.0002101999707520008 +BST,shuffled,3,insert,0.011865400010719895 +BST,shuffled,3,search,0.0003497999859973788 +BST,shuffled,3,delete,0.0002114999806508422 +BST,sorted,1,insert,1.961912199971266 +BST,sorted,1,search,0.025325599999632686 +BST,sorted,1,delete,0.03309909999370575 +BST,sorted,2,insert,1.8450072000268847 +BST,sorted,2,search,0.025074300006963313 +BST,sorted,2,delete,0.03284020000137389 +BST,sorted,3,insert,1.8502263000118546 +BST,sorted,3,search,0.028948499995749444 +BST,sorted,3,delete,0.040639499959070235 diff --git a/stepinim/lab1_structure/docs/otchet_1lab.md b/stepinim/lab1_structure/docs/otchet_1lab.md new file mode 100644 index 0000000..6bf4dba --- /dev/null +++ b/stepinim/lab1_structure/docs/otchet_1lab.md @@ -0,0 +1,15 @@ +В ходе экспериментов было показано, что производительность структуры данных сильно зависит +от её внутреннего устройства и характера входных данных. + +BST работает быстро на случайных данных, но при отсортированном порядке деградирует почти до +связного списка, из-за чего время вставки и удаления резко увеличивается. Хеш-таблица +практически не зависит от порядка входных данных, так как доступ к элементам происходит через +хеш-функцию, поэтому она показала лучшие результаты при поиске и вставке. Связный список +оказался самым медленным при поиске, так как требует последовательного обхода элементов. + +Удаление также работает по-разному: в связном списке и BST сначала требуется поиск элемента, +а в хеш-таблице удаление обычно выполняется быстрее за счёт обращения к нужному бакету. + +На практике хеш-таблицы лучше подходят для частого поиска и вставки данных, BST — когда +важно хранить элементы в отсортированном виде, а связные списки полезны в более простых +задачах, где структура данных часто изменяется и не требуется быстрый поиск. \ No newline at end of file diff --git a/stepinim/lab1_structure/test.py b/stepinim/lab1_structure/test.py new file mode 100644 index 0000000..46d6350 --- /dev/null +++ b/stepinim/lab1_structure/test.py @@ -0,0 +1,433 @@ +import sys +sys.setrecursionlimit(30000) + +csv_path = '/stepinim/docs/data/lab1_results.csv' + +#Связный список +def ll_insert(head, name, phone): + new_node = {'name': name, 'phone': phone, 'next': None} + if head is None: + return new_node + + curr = head + prev = None + while curr is not None: + if curr['name'] == name: + curr['phone'] = phone + return head + prev = curr + curr = curr['next'] + prev['next'] = new_node + return head + +def ll_find(head, name): + curr = head + while curr: + if curr['name'] == name: + return curr['phone'] + curr = curr['next'] + return None + +def ll_delete(head, name): + if head is None: + return None + if head['name'] == name: + return head['next'] + curr = head + while curr['next']: + if curr['next']['name'] == name: + curr['next'] = curr['next']['next'] + return head + curr = curr['next'] + return head + +def ll_list_all(head): + result = [] + curr = head + while curr: + result.append((curr['name'], curr['phone'])) + curr = curr['next'] + result.sort(key=lambda x: x[0]) + return result + +#Хэш-таблица +HASH_SIZE = 1009 +def _hash_name(name): + return hash(name) % HASH_SIZE + +def ht_insert(buckets, name, phone): + idx = _hash_name(name) + buckets[idx] = ll_insert(buckets[idx], name, phone) + +def ht_find(buckets, name): + idx = _hash_name(name) + return ll_find(buckets[idx], name) + +def ht_delete(buckets, name): + idx = _hash_name(name) + buckets[idx] = ll_delete(buckets[idx], name) + +def ht_list_all(buckets): + all_entries = [] + for bucket in buckets: + if bucket is not None: + curr = bucket + while curr: + all_entries.append((curr['name'], curr['phone'])) + curr = curr['next'] + all_entries.sort(key=lambda x: x[0]) + return all_entries + +#Двоичное дерево поиска +def bst_insert(root, name, phone): + if root is None: + return {'name': name, 'phone': phone, 'left': None, 'right': None} + if name < root['name']: + root['left'] = bst_insert(root['left'], name, phone) + elif name > root['name']: + root['right'] = bst_insert(root['right'], name, phone) + else: + root['phone'] = phone + return root + +def bst_find(root, name): + curr = root + while curr: + if name == curr['name']: + return curr['phone'] + elif name < curr['name']: + curr = curr['left'] + else: + curr = curr['right'] + return None + +def bst_delete(root, name): + if root is None: + return None + if name < root['name']: + root['left'] = bst_delete(root['left'], name) + elif name > root['name']: + root['right'] = bst_delete(root['right'], name) + else: + if root['left'] is None: + return root['right'] + if root['right'] is None: + return root['left'] + min_node = root['right'] + while min_node['left']: + min_node = min_node['left'] + root['name'] = min_node['name'] + root['phone'] = min_node['phone'] + root['right'] = bst_delete(root['right'], min_node['name']) + return root + +def bst_list_all(root): + result = [] + def inorder(node): + if node: + inorder(node['left']) + result.append((node['name'], node['phone'])) + inorder(node['right']) + inorder(root) + return result + +# ============================================================ +# TECT +# ============================================================ + +import os +import random +import time +import csv +import pandas as pd +import matplotlib.pyplot as plt + +# ============================================================ +# ПОДГОТОВКА ПАПОК +# ============================================================ + +DATA_DIR = os.path.join("docs", "data") +os.makedirs(DATA_DIR, exist_ok=True) + +csv_path = os.path.join(DATA_DIR, "lab1_results.csv") +graph_path = os.path.join(DATA_DIR, "lab1_graph.png") + +# ============================================================ +# ТЕСТОВЫЕ ДАННЫЕ +# ============================================================ + +random.seed(42) + +N = 3000 + +base_records = [ + (f"User_{i:05d}", f"123-{i:05d}") + for i in range(N) +] + +records_shuffled = base_records.copy() +random.shuffle(records_shuffled) + +records_sorted = sorted(base_records, key=lambda x: x[0]) + +# Поиск +search_existing = [ + name for name, _ in random.sample(base_records, 100) +] + +search_nonexist = [ + f"None_{i}" + for i in range(10) +] + +# Удаление +delete_names = [ + name for name, _ in random.sample(base_records, 50) +] + +# ============================================================ +# СОЗДАНИЕ СТРУКТУР +# ============================================================ + +def build_structure(records, struct_type): + + if struct_type == "ll": + structure = None + + for name, phone in records: + structure = ll_insert(structure, name, phone) + + return structure + + elif struct_type == "ht": + structure = [None] * HASH_SIZE + + for name, phone in records: + ht_insert(structure, name, phone) + + return structure + + elif struct_type == "bst": + structure = None + + for name, phone in records: + structure = bst_insert(structure, name, phone) + + return structure + + +# ============================================================ +# INSERT +# ============================================================ + +def measure_insert(records, struct_type): + + start = time.perf_counter() + + build_structure(records, struct_type) + + end = time.perf_counter() + + return end - start + + +# ============================================================ +# SEARCH +# ============================================================ + +def measure_search(records, struct_type): + + structure = build_structure(records, struct_type) + + start = time.perf_counter() + + if struct_type == "ll": + for name in search_existing + search_nonexist: + ll_find(structure, name) + + elif struct_type == "ht": + for name in search_existing + search_nonexist: + ht_find(structure, name) + + elif struct_type == "bst": + for name in search_existing + search_nonexist: + bst_find(structure, name) + + end = time.perf_counter() + + return end - start + + +# ============================================================ +# DELETE +# ============================================================ + +def measure_delete(records, struct_type): + + structure = build_structure(records, struct_type) + + start = time.perf_counter() + + if struct_type == "ll": + for name in delete_names: + structure = ll_delete(structure, name) + + elif struct_type == "ht": + for name in delete_names: + ht_delete(structure, name) + + elif struct_type == "bst": + for name in delete_names: + structure = bst_delete(structure, name) + + end = time.perf_counter() + + return end - start + + +# ============================================================ +# ЗАМЕРЫ +# ============================================================ + +all_data = [] + +experiments = [ + ("LinkedList", "ll"), + ("HashTable", "ht"), + ("BST", "bst") +] + +modes = [ + ("shuffled", records_shuffled), + ("sorted", records_sorted) +] + +for struct_name, struct_type in experiments: + + for mode_name, records in modes: + + for rep in range(1, 4): + + insert_time = measure_insert(records, struct_type) + + search_time = measure_search(records, struct_type) + + delete_time = measure_delete(records, struct_type) + + all_data.append([ + struct_name, + mode_name, + rep, + "insert", + insert_time + ]) + + all_data.append([ + struct_name, + mode_name, + rep, + "search", + search_time + ]) + + all_data.append([ + struct_name, + mode_name, + rep, + "delete", + delete_time + ]) + +# ============================================================ +# CSV +# ============================================================ + +with open(csv_path, "w", newline="", encoding="utf-8") as f: + + writer = csv.writer(f) + + writer.writerow([ + "Структура", + "Режим", + "Повтор", + "Операция", + "Время (сек)" + ]) + + writer.writerows(all_data) + +print(f"CSV сохранён: {csv_path}") + +# ============================================================ +# ГРАФИК +# ============================================================ + +df = pd.read_csv(csv_path) + +df_avg = ( + df.groupby( + ["Структура", "Режим", "Операция"] + )["Время (сек)"] + .mean() + .reset_index() +) + +fig, ax = plt.subplots(figsize=(12, 6)) + +ops = ["insert", "search", "delete"] + +x = range(len(ops)) + +width = 0.12 + +configs = [ + ("LinkedList", "shuffled"), + ("LinkedList", "sorted"), + ("HashTable", "shuffled"), + ("HashTable", "sorted"), + ("BST", "shuffled"), + ("BST", "sorted") +] + +for i, (struct, mode) in enumerate(configs): + + subset = df_avg[ + (df_avg["Структура"] == struct) + & + (df_avg["Режим"] == mode) + ] + + times = [ + subset[ + subset["Операция"] == op + ]["Время (сек)"].values[0] + for op in ops + ] + + ax.bar( + [p + i * width for p in x], + times, + width, + label=f"{struct} ({mode})" + ) + +ax.set_xticks([p + 2.5 * width for p in x]) + +ax.set_xticklabels(ops) + +ax.set_ylabel("Среднее время (сек)") + +ax.set_title("Сравнение структур данных") + +ax.legend( + bbox_to_anchor=(1.05, 1), + loc="upper left" +) + +plt.tight_layout() + +plt.savefig(graph_path) + +print(f"График сохранён: {graph_path}") + +plt.show() \ No newline at end of file diff --git a/stepinim/lab2_oop/docs/data/chart_time_2lab.png b/stepinim/lab2_oop/docs/data/chart_time_2lab.png new file mode 100644 index 0000000..e2ace45 Binary files /dev/null and b/stepinim/lab2_oop/docs/data/chart_time_2lab.png differ diff --git a/stepinim/lab2_oop/docs/data/empty_2lab.txt b/stepinim/lab2_oop/docs/data/empty_2lab.txt new file mode 100644 index 0000000..db11fb1 --- /dev/null +++ b/stepinim/lab2_oop/docs/data/empty_2lab.txt @@ -0,0 +1,20 @@ +S + + + + + + + + + + + + + + + + + + + E diff --git a/stepinim/lab2_oop/docs/data/large_2lab.txt b/stepinim/lab2_oop/docs/data/large_2lab.txt new file mode 100644 index 0000000..a446b35 --- /dev/null +++ b/stepinim/lab2_oop/docs/data/large_2lab.txt @@ -0,0 +1,100 @@ +#################################################################################################### +#S # # ### ## ## # # ### # # ## # # ### ## #### # ## ## # ##### +# # # # ## ## # # # # # # # # # ##### # ## # # ## # ## # # +## # ## ### ## #### ## # # # ## #### # ## #### ## ### # # # ## # # ## # +### # ## ## ### # # #### # # # # ## ## ###### ## ## # # ### # ### +## # # # ### # # ## ### ### # # ## # # # ## # # ####### # # +###### ### ## # # # # # # ### ### ## ### ### ##### # # ## # # ## ## #### +#### # ## # # ## # ## ## ## # ## # # ## ## # # # # ## ### +# # # # # ###### # # ## #### # # # ## # # ### ##### ## # # +# ### # # # # ## ### # # ## #### # # # ###### # # ###### # ## # +## ## ## ### # # ### # # ## # ## ### ### ## ## # ### # # # +## # ## # # ## # ### # ## # ### ## # ### ### ### # ## ## # ### # # # +# # # # # ### # ## # # # ## # # # # # # # # # # ### ## # # ## # # # +## # # # #### ## # # # # # # ### # # ##### # # # # # # # # # +# ## ## # ## # # #### # # ## #### # ## # # # # # # # # # +# # ### # # # ## # # # # ## ## # # # # ## # # # # ## ## #### # ## # # # +# # ### # # ## # ## #### #### ## # #### # ### ## # # # # ## ## # +##### # # # # # ## # ## # ### # # # # # # # # # # ### ## # ## ## #### # # # +# # # #### ## ## ## # # # # # # # # # ## # # ## # ### # # ## # # # ## +### # ### ####### ### # # # # # ## ## # ## # ## # ## ### +# # ### ######## ### ## # # # # # # ## ## # ## ### # # # ### ### ### +# # ## ## # # ### ### ## # # ## # # # # # ## ## ### # # #### #### ### +# ### ## ### #### # ## # # ## # ## # ## ### # # # # ### # ## ### +#### ## # ### # # ## ### ## # # # # # # # ## # ## ## # # # # ## ## # +### ## # # #### ## ## ##### ## # # # ## # ## # # # # # # # # ## # ## # # +# # # # # # # ## ## # ### # ## ## # ## # # # ## # # ## ## # # # # ## # +# # ###### # # # # # # # # ## ## # # # ## # # # # # # ## # # ## # # ## +# # # # # ## # # # # # # # # ## # ## # #### # ### #### ## # # ## ## +## # # ## # ### # #### ## # ### ## # # # # ### # ## # # # # # +# # ### # ### # ## # ## ## # # # ### ##### ## ### # ## ### # # # ## ###### # +## # # # ## # # #### # # ## # ## # # # # # # # # # # # # # +## # # # ## #### # ### ### ## # ## ##### # # # # # # # # # # ##### # # ## +# ## ## # # # # ### ## # # ### ### ## # # # # ## # # ## ### # # # ### +# ## ## ## ### # # #### # ## ## ## ### ### # # ## ## +## # # ##### #### # # ## ## ### # # # # # # # # # # ### # #### # +# # # # # # #### # # ## # # # ## # # # # # # # ### # # ## +## ### ## ### # ## ## ### # ## # # # # # # # # # # #### ## ## +## # ### ## ## # # ### # # #### # # # ### # # # # # # #### # # +# # # ## ### # # # # # # ## ## # # ## ## # ## # # # # ### ## # # +# ### # ### ## # # # # # # ## # ###### # ## ## ## # ## ## ## # ## # # +# # # # # ## # # # # # # ## # # # ###### # # ### # # +# # ## # # ## # # ## ### # ## ## ## # # # # # # # # # ## # # # +## #### ## ### #### # # ### # # # # # ## # ## # # ##### ## ## # # ## # +## # # # ## # ## # # ## # #### # # ### # #### # ## # ##### # # # # +# # # # # ## #### # # ## # # # # ## # ## ## # # # ## # # ## ## ### # # +# # # ## # ## # # ## ## # ## # # ## # ## # # # ##### # +## #### ## ##### # ### # ### # # ## # ## # # # # # # # ## # ### # ## # +# # # # # ## # ## # ## ## # # ### # ####### # # ## # ### ## ## ## ## ## # +### ## # ## ## # ## # ### ### # ### # # # ###### ### ### # # # ## ### # # +## # ## ## # ## ### # # # # ## # ### # ### ## # ## # # # # # +# # ## ## ## # # # # ## # # ## # ## # # # ## # # # # # # # # ## # # # +# # # ## # ## ## ## #### ## ## ## ### ##### ###### ## ### # # # +# # ## # # # # ## # # # # # ## # #### ## # ## # ### # ## # +# # ## # # # ### # # # # ## ### ## # ## # # # # # ## # # # ### ## # # +# ## # # # ## # # # # # ## # # # # ## # # # # ###### ## ### # # +# ## ## # ### # # ## ####### ## # ####### # # # # ## # ### # ## # # # +## ### ## ### # ## # # ## ### # ### ### # # # ## # # ## # ### # # +# # # ### ## # # ## # ## ## # ### # # # ### ###### # # # # +## # ### ## # ### ## ## # ## # ## ## ### ## ### ### ## ## # ## # +# # #### ## # # ## ## # #### # ### # ## ## # # # # # ## # # +# # # # #### # # # ### ## # ## ## ## # # ## # ##### # # # # # ## # #### # # # +# # ### # # ## #### # # # # ## ## # ## ## ### # ## ## ## # ## # +## # ## # # # # # ## ## # ## ## ## # ## # #### ## # ## # +# # # ## # ## # # # # ### # # ## ## # # # # # ### # ### ## # # ## # +# # # ### # # ## ## # # ## # ### # # ### # ## # ### # ## ## ## # # +### # ## # # #### ### ## # ## # ## # ## #### # # ###### # # #### # ## # +# # # # # # ### # # # # ## #### ### # # # # # ## # # # #### # #### # # +## ## # ## # # # # # ## # ### #### # ## ### # ##### ### ## ### # # # +# # # ####### ## ### # ####### # # # ####### # # # ### # # ## # # # # +## # # ## ## ## # ### ## ## ### ## ## # # ## # ## ## # # # # # +### # # #### ### # # ## # ## # # # #### # # # # # # # # ## # ## # +# ##### # ## ### ## #### # # # ## #### # # # # ## # # ## # # +# # # # # ### # ## #### # # # ### # # # # ## # # # # # # # +# # ## ## #### # # ### ## ## ### ### ## ## # # # # # ## # ## ### # +## # # ## # # ## # ## # ### # ### # # # ## # ### #### # ## # +# ## # # # # ## ## # # # # ## # ### ## ###### ### # ## ### # # # # # # +### # # # # #### # # # # ### ##### # ## # ## # # ## # # # ## # +# # # # ##### # # #### # # # ## # ## # # ## ### # # ## # #### # +### ## # # # ## # # ## # # ## # # # ### ## ## # ### # # ## # # # # # +#### # # # # ## # # # ## ### # # # ### # ## # # ## # # # ## # # +# # # ## # ## # # ## ### # ## ## # ## # ## ## # # # ## # # ### ## ## ## # +# # # ## # # # ## # ## ## # ## # ## ## #### #### # ## # # # +## # # #### ### ## # #### ### ### # ## # # ## ## # ## # ### # +# ## ##### # # ## ### ### # # # # ### #### ## ## ## ## ### ## # # +# # ## # ## ## # # ### # # # # ## # ### ## ## ## ## ## # # # # +# #### # # #### # ## ### ###### # # # ## # # # # # # # # # #### ## # # # # +# #### # ### ##### # ## ## # # ## ## # ## ## # ## ### # # # # # # # +# # ## # # # ## # # ## ## # # # # ## # # # ## ### # ## # # +### # ## # # # ## # # # #### ##### # # ## # ## # # # # ### # # +# # # # ### # ## # # # # ## # # # ### # ## # # # # # # # # # # # # +## # # # # ## ## # #### ## # # ## # # # # # ### # # # #### # # ## ## # # # +## # # ## # #### # # # # ## # ### ## ## #### # ## ## # ## ## #### ## ### #### # +# ### ### ### # ### ### ## # # # ###### # # ### ## # # # # # # # # +## ### ## # ## # ## # ## ### # # # #### ### ### # # +# # ## # # # ## ## # # # # ### ### # ### ### # # # # ### ## # # +## # ## # # # # ###### ## ### # ### # # # ## # ## ## # ### # # # ## # ## # +# ## # ## # # # ## ## # ## ## ## # ## # ## # # # # ## # # # # +# ### # ## ## # #### # ##### # ## ## ## ### # # # # ### # +## # ## # # # # ##### # ## # # # ## # ### # # # ## # ## # # # ## E# +#################################################################################################### diff --git a/stepinim/lab2_oop/docs/data/medium_2lab.txt b/stepinim/lab2_oop/docs/data/medium_2lab.txt new file mode 100644 index 0000000..b6b2a97 --- /dev/null +++ b/stepinim/lab2_oop/docs/data/medium_2lab.txt @@ -0,0 +1,50 @@ +################################################## +#S ## ### # # # #### # ## # +# # ## # # # # ##### ### # ### # # +# ## #### # ### ### ## # # #### ### # # # +#### # # # # # ## # #### +# ## # #### ## # # ## # # # # +# ### # # # # ### # ## +# # # # ## # # ## # ## # # # +## # # # #### # # # # # ### ## +## # # ## ## ### # # ## # +## # # # # # # # # # # # # # +# # ## # ## ## ## ## ## # ### +# ### # # # ## ## # # # # ## # ## +## # # # # ## # # # # # +# # # # # ### # ### # # ## +# # # # # # # ##### # ### ## +# # # # ## ## # # # # ### #### # +## # # # ## # # ## # ### ## ### # # +## # ## # # # # # +# ##### ## ## # # # ## # ## # # # +# # # # ### ##### ### # # ## # +## # # # ## # # ## # # # # ## # +#### # # ## # # # ## ## # ## ## # +# # ### ### ## # ## #### # # +# # ### # ## ##### # # # # ## # # +## #### # # # # # # +# ## # # # # ## ## # # ## # # +# # ## # ### # ### ## # ## # +# # # # # ## ## ## # # +# # ## ### ## ## # # # # # ## # # +## # # #### # #### # ## ## # ## # +# # # ## # # # # # # # # # +# ### ### # # # # # # # # +# ## # # # ####### # # # # # # ### # +## # # # # # # # # # ## # ## # +# # # # ## ## # # ## ### # # # # # # # +# ## # ### # # # # # # # # +# # # # # # # ## # ### # # +# # ### # # # ### # ## # # # +# ### # # # # ## # # ## # # +# ## # ### # ## ## ### # # # # +# ## # # ## ## # # # ## # # +# # ## # # # # # # # # ## # +## # # # # # # # # # # # # +# # # # ### ## ### # ## # # # # +## ##### # # # ## # ## ### # # +# ## # ## ##### # # # ## # +# # # # # # ### # # # # # # +### # # # # # # ## ## ### ## #E# +################################################## diff --git a/stepinim/lab2_oop/docs/data/no_exit_2lab.txt b/stepinim/lab2_oop/docs/data/no_exit_2lab.txt new file mode 100644 index 0000000..9cc457c --- /dev/null +++ b/stepinim/lab2_oop/docs/data/no_exit_2lab.txt @@ -0,0 +1,15 @@ +############### +#S # # +# # +## # # +# # # # +## ## # # # # +# # # # +# # # +# # # ## # +## # # # +## # +## # # # # +# # # ## +# # #E# +############### diff --git a/stepinim/lab2_oop/docs/data/results_2lab.csv b/stepinim/lab2_oop/docs/data/results_2lab.csv new file mode 100644 index 0000000..ba69850 --- /dev/null +++ b/stepinim/lab2_oop/docs/data/results_2lab.csv @@ -0,0 +1,21 @@ +maze,strategy,time_ms,visited,path_length +small,BFS,0.1971,28,15 +small,DFS,0.062,16,15 +small,A*,0.1713,28,15 +small,Dijkstra,0.148,28,15 +medium,BFS,5.3354,1377,95 +medium,DFS,0.7772,282,151 +medium,A*,3.8703,500,95 +medium,Dijkstra,8.3548,1363,95 +large,BFS,16.9817,4391,195 +large,DFS,3.414,614,285 +large,A*,5.7519,559,195 +large,Dijkstra,31.018,4380,195 +empty,BFS,2.3012,400,39 +empty,DFS,1.4237,400,191 +empty,A*,3.6105,400,39 +empty,Dijkstra,2.9606,400,39 +no_exit,BFS,0.5791,136,0 +no_exit,DFS,0.5479,136,0 +no_exit,A*,0.9933,136,0 +no_exit,Dijkstra,0.8121,136,0 diff --git a/stepinim/lab2_oop/docs/data/small_2lab.txt b/stepinim/lab2_oop/docs/data/small_2lab.txt new file mode 100644 index 0000000..9fec091 --- /dev/null +++ b/stepinim/lab2_oop/docs/data/small_2lab.txt @@ -0,0 +1,10 @@ +########## +#S # +# ###### # +# # # # +# # ## # # +# # ## # # +# # # # +# ###### # +# E# +########## diff --git a/stepinim/lab2_oop/docs/otchet_2lab.md b/stepinim/lab2_oop/docs/otchet_2lab.md new file mode 100644 index 0000000..3d7029b --- /dev/null +++ b/stepinim/lab2_oop/docs/otchet_2lab.md @@ -0,0 +1,122 @@ +ОПИСАНИЕ ЗАДАЧИ И ВЫБРАННЫХ ПАТТЕРНОВ + +Цель работы — разработать систему поиска пути в лабиринте с использованием +оопп и паттернов проектирования. + +В работе были использованы следующие паттерны: + +Strategy — для реализации алгоритмов поиска пути (BFS, DFS, A*, Dijkstra). +Позволяет менять алгоритм без изменения кода основного класса MazeSolver. + +Builder — для создания лабиринта из текстового файла. +Отделяет логику загрузки данных от основной системы. +'''mermaid + +classDiagram +class Cell { ++x ++y ++is_wall ++is_start ++is_exit ++weight ++isPassable() +} + +class Maze { ++width ++height ++start ++exit ++getCell() ++getNeighbors() ++getWeightedNeighbors() +} + +class MazeBuilder { ++buildFromFile() +} + +class TextFileMazeBuilder +MazeBuilder <|-- TextFileMazeBuilder + +class PathFindingStrategy { ++findPath() +} + +class BFSStrategy +class DFSStrategy +class AStarStrategy +class DijkstraStrategy + +PathFindingStrategy <|-- BFSStrategy +PathFindingStrategy <|-- DFSStrategy +PathFindingStrategy <|-- AStarStrategy +PathFindingStrategy <|-- DijkstraStrategy + +class MazeSolver { ++setStrategy() ++solve() +} + +MazeSolver --> PathFindingStrategy +Maze --> Cell +''' +ЛИСТИНГИ КЛЮЧЕВЫХ КЛАССОВ + +В проекте реализованы основные классы: +Cell — хранение информации о клетке лабиринта +Maze — структура лабиринта и работа с соседями +MazeSolver — запуск поиска пути +PathFindingStrategy — интерфейс алгоритмов +BFSStrategy, DFSStrategy, AStarStrategy, DijkstraStrategy — реализации алгоритмов +TextFileMazeBuilder — загрузка лабиринта из файла +SearchStats — хранение статистики + +РЕЗУЛЬТАТЫ ЭКСПЕРИМЕНТОВ + +Алгоритмы тестировались на разных лабиринтах: small, medium, large, empty, no_exit. + +Сравнивались: + +время выполнения +количество посещённых клеток +длина найденного пути + +Результаты в общем виде: + +BFS — гарантирует кратчайший путь, но посещает много клеток +DFS — быстрый, но не гарантирует оптимальный путь +A* — самый быстрый в большинстве случаев за счёт эвристики +Dijkstra — стабильный, но медленнее A* на больших лабиринтах + +АНАЛИЗ ЭФФЕКТИВНОСТИ И ПАТТЕРНОВ + +Результаты показали, что A* является наиболее эффективным алгоритмом на больших данных, +так как использует эвристику и уменьшает количество проверяемых клеток. + +BFS всегда находит оптимальный путь, но работает медленнее из-за полного обхода пространства. + +DFS быстрее по времени, но не гарантирует лучший результат. + +Dijkstra корректно работает с весами, но в данной задаче часто уступает A*. + +Паттерн Strategy позволил легко переключать алгоритмы без изменения основной логики программы. +Паттерн Builder упростил создание лабиринтов и отделил загрузку данных от логики поиска. + +ВЫВОДЫ + +В ходе работы была создана гибкая система поиска пути в лабиринте с использованием ООП +и паттернов проектирования. Благодаря Strategy алгоритмы стали независимыми и легко +заменяемыми. Благодаря Builder упростилась работа с созданием и загрузкой лабиринтов. +В целом, архитектура получилась расширяемой: можно легко добавить новый алгоритм или тип +лабиринта без переписывания существующего кода. +Таким образом, наиболее сбалансированным алгоритмом для поиска пути в лабиринте является A*, +так как он обеспечивает: + +высокую скорость работы, +оптимальность результата, +минимальное количество исследуемых состояний. + +Алгоритмы BFS и Dijkstra гарантируют оптимальность, но проигрывают по производительности, +а DFS является самым быстрым, но не гарантирует качество решения. \ No newline at end of file diff --git a/stepinim/lab2_oop/poisk.py b/stepinim/lab2_oop/poisk.py new file mode 100644 index 0000000..92b8ec4 --- /dev/null +++ b/stepinim/lab2_oop/poisk.py @@ -0,0 +1,789 @@ +import time +from collections import deque +import heapq +import csv +import os +import random +import matplotlib.pyplot as plt + + +# ============================================================ +# ЭТАП 1. МОДЕЛЬ ЛАБИРИНТА +# ============================================================ + +class Cell: + def __init__(self, x, y, is_wall=False, is_start=False, is_exit=False): + self.x = x + self.y = y + self.is_wall = is_wall + self.is_start = is_start + self.is_exit = is_exit + self.weight = 1 + + def isPassable(self): + return not self.is_wall + + def __repr__(self): + return f"Cell({self.x},{self.y})" + + def __hash__(self): + return hash((self.x, self.y)) + + def __eq__(self, other): + return isinstance(other, Cell) and self.x == other.x and self.y == other.y + + +class Maze: + def __init__(self, width, height): + self.width = width + self.height = height + self.cells = [] + self.start = None + self.exit = None + + def getCell(self, x, y): + if 0 <= x < self.width and 0 <= y < self.height: + return self.cells[y][x] + return None + + def getNeighbors(self, cell): + neighbors = [] + + for dx, dy in [(0, -1), (0, 1), (-1, 0), (1, 0)]: + nx = cell.x + dx + ny = cell.y + dy + + neighbor = self.getCell(nx, ny) + + if neighbor and neighbor.isPassable(): + neighbors.append(neighbor) + + return neighbors + + def getWeightedNeighbors(self, cell): + return [(n, n.weight) for n in self.getNeighbors(cell)] + + +# ============================================================ +# ЭТАП 2. BUILDER +# ============================================================ + +class MazeBuilder: + def buildFromFile(self, filename): + raise NotImplementedError + + +class TextFileMazeBuilder(MazeBuilder): + + def buildFromFile(self, filename): + + with open(filename, 'r', encoding='utf-8') as f: + lines = [line.rstrip('\n') for line in f] + + height = len(lines) + width = max(len(line) for line in lines) + + maze = Maze(width, height) + + for y, line in enumerate(lines): + + row = [] + + for x, char in enumerate(line): + + if char == '#': + cell = Cell(x, y, is_wall=True) + + elif char == 'S': + cell = Cell(x, y, is_start=True) + maze.start = cell + + elif char == 'E': + cell = Cell(x, y, is_exit=True) + maze.exit = cell + + else: + cell = Cell(x, y) + + row.append(cell) + + while len(row) < width: + row.append(Cell(len(row), y, is_wall=True)) + + maze.cells.append(row) + + if maze.start is None or maze.exit is None: + raise ValueError("В лабиринте нет S или E") + + return maze + + +# ============================================================ +# ВОССТАНОВЛЕНИЕ ПУТИ +# ============================================================ + +def reconstruct_path(parents, end_cell): + + path = [] + + current = end_cell + + while current is not None: + path.append(current) + current = parents[current] + + path.reverse() + + return path + + +# ============================================================ +# ЭТАП 3. STRATEGY +# ============================================================ + +class PathFindingStrategy: + + @property + def name(self): + return "Unknown" + + def findPath(self, maze, start, exit): + raise NotImplementedError + + +# ============================================================ +# BFS +# ============================================================ + +class BFSStrategy(PathFindingStrategy): + + @property + def name(self): + return "BFS" + + def findPath(self, maze, start, exit): + + queue = deque([start]) + + visited = {start} + + parents = { + start: None + } + + visited_count = 1 + + while queue: + + current = queue.popleft() + + if current == exit: + path = reconstruct_path(parents, exit) + return path, visited_count + + for neighbor in maze.getNeighbors(current): + + if neighbor not in visited: + + visited.add(neighbor) + + parents[neighbor] = current + + visited_count += 1 + + queue.append(neighbor) + + return [], visited_count + + +# ============================================================ +# DFS +# ============================================================ + +class DFSStrategy(PathFindingStrategy): + + @property + def name(self): + return "DFS" + + def findPath(self, maze, start, exit): + + stack = [start] + + visited = {start} + + parents = { + start: None + } + + visited_count = 1 + + while stack: + + current = stack.pop() + + if current == exit: + path = reconstruct_path(parents, exit) + return path, visited_count + + for neighbor in maze.getNeighbors(current): + + if neighbor not in visited: + + visited.add(neighbor) + + parents[neighbor] = current + + visited_count += 1 + + stack.append(neighbor) + + return [], visited_count + + +# ============================================================ +# A* +# ============================================================ + +class AStarStrategy(PathFindingStrategy): + + @property + def name(self): + return "A*" + + def heuristic(self, a, b): + return abs(a.x - b.x) + abs(a.y - b.y) + + def findPath(self, maze, start, exit): + + counter = 0 + + open_set = [] + + heapq.heappush(open_set, (0, counter, start)) + + parents = { + start: None + } + + g_score = { + start: 0 + } + + visited = set() + + visited_count = 0 + + while open_set: + + _, _, current = heapq.heappop(open_set) + + if current in visited: + continue + + visited.add(current) + + visited_count += 1 + + if current == exit: + path = reconstruct_path(parents, exit) + return path, visited_count + + for neighbor in maze.getNeighbors(current): + + tentative_g = g_score[current] + 1 + + if neighbor not in g_score or tentative_g < g_score[neighbor]: + + g_score[neighbor] = tentative_g + + parents[neighbor] = current + + f_score = tentative_g + self.heuristic(neighbor, exit) + + counter += 1 + + heapq.heappush( + open_set, + (f_score, counter, neighbor) + ) + + return [], visited_count + + +# ============================================================ +# DIJKSTRA +# ============================================================ + +class DijkstraStrategy(PathFindingStrategy): + + @property + def name(self): + return "Dijkstra" + + def findPath(self, maze, start, exit): + + counter = 0 + + queue = [] + + heapq.heappush(queue, (0, counter, start)) + + distances = { + start: 0 + } + + parents = { + start: None + } + + visited = set() + + visited_count = 0 + + while queue: + + dist, _, current = heapq.heappop(queue) + + if current in visited: + continue + + visited.add(current) + + visited_count += 1 + + if current == exit: + path = reconstruct_path(parents, exit) + return path, visited_count + + for neighbor, weight in maze.getWeightedNeighbors(current): + + new_dist = dist + weight + + if neighbor not in distances or new_dist < distances[neighbor]: + + distances[neighbor] = new_dist + + parents[neighbor] = current + + counter += 1 + + heapq.heappush( + queue, + (new_dist, counter, neighbor) + ) + + return [], visited_count + + +# ============================================================ +# ЭТАП 4. STATS + SOLVER +# ============================================================ + +class SearchStats: + + def __init__( + self, + strategy_name, + time_ms, + visited_cells, + path_length, + path_found + ): + self.strategy_name = strategy_name + self.time_ms = time_ms + self.visited_cells = visited_cells + self.path_length = path_length + self.path_found = path_found + + +class MazeSolver: + + def __init__(self, maze, strategy=None): + self.maze = maze + self.strategy = strategy + + def setStrategy(self, strategy): + self.strategy = strategy + + def solve(self): + + if self.strategy is None: + raise ValueError("Стратегия не выбрана") + + start_time = time.perf_counter() + + path, visited = self.strategy.findPath( + self.maze, + self.maze.start, + self.maze.exit + ) + + end_time = time.perf_counter() + + elapsed_ms = (end_time - start_time) * 1000 + + return SearchStats( + self.strategy.name, + elapsed_ms, + visited, + len(path), + len(path) > 0 + ), path + + +# ============================================================ +# ВИЗУАЛИЗАЦИЯ +# ============================================================ + +def render(maze, path=None): + + path_set = set(path) if path else set() + + for y in range(maze.height): + + line = "" + + for x in range(maze.width): + + cell = maze.getCell(x, y) + + if cell == maze.start: + line += "S" + + elif cell == maze.exit: + line += "E" + + elif cell in path_set: + line += "." + + elif cell.is_wall: + line += "#" + + else: + line += " " + + print(line) + + print() + + +# ============================================================ +# ФАЙЛЫ И ПУТИ +# ============================================================ + +OUTPUT_DIR = os.path.join("docs", "data") + +PREFIX = "_2lab" + +os.makedirs(OUTPUT_DIR, exist_ok=True) + + +def get_path(filename): + + name, ext = os.path.splitext(filename) + + return os.path.join( + OUTPUT_DIR, + f"{name}{PREFIX}{ext}" + ) + + +# ============================================================ +# СОЗДАНИЕ ЛАБИРИНТА +# ============================================================ + +def create_test_maze(filename, lines): + + with open(filename, 'w', encoding='utf-8') as f: + + for line in lines: + f.write(line + '\n') + + return filename + + +# ============================================================ +# ГЕНЕРАЦИЯ +# ============================================================ + +def generate_maze(width, height, wall_density=0.3): + + grid = [[' ' for _ in range(width)] for _ in range(height)] + + for x in range(width): + grid[0][x] = '#' + grid[height - 1][x] = '#' + + for y in range(height): + grid[y][0] = '#' + grid[y][width - 1] = '#' + + x, y = 1, 1 + + path_cells = {(x, y)} + + while x < width - 2 or y < height - 2: + + if x < width - 2 and random.random() > 0.3: + x += 1 + + elif y < height - 2: + y += 1 + + else: + x += 1 + + path_cells.add((x, y)) + + for yy in range(1, height - 1): + + for xx in range(1, width - 1): + + if (xx, yy) not in path_cells: + + if random.random() < wall_density: + grid[yy][xx] = '#' + + grid[1][1] = 'S' + grid[height - 2][width - 2] = 'E' + + return [''.join(row) for row in grid] + + +def generate_empty_maze(size): + + lines = [" " * size for _ in range(size)] + + lines[0] = "S" + " " * (size - 1) + + lines[size - 1] = " " * (size - 1) + "E" + + return lines + + +def generate_no_exit_maze(size): + + lines = generate_maze(size, size, wall_density=0.2) + + for y, line in enumerate(lines): + + if 'E' in line: + + x = line.index('E') + + for dy, dx in [(-1, 0), (1, 0), (0, -1), (0, 1)]: + + ny = y + dy + nx = x + dx + + if 0 <= ny < size and 0 <= nx < size: + + if lines[ny][nx] == ' ': + + lines[ny] = ( + lines[ny][:nx] + + '#' + + lines[ny][nx + 1:] + ) + + return lines + + +# ============================================================ +# ЭКСПЕРИМЕНТЫ +# ============================================================ + +def run_experiments(): + + mazes = { + + "small": [ + "##########", + "#S #", + "# ###### #", + "# # # #", + "# # ## # #", + "# # ## # #", + "# # # #", + "# ###### #", + "# E#", + "##########" + ], + + "medium": generate_maze(50, 50, 0.35), + + "large": generate_maze(100, 100, 0.4), + + "empty": generate_empty_maze(20), + + "no_exit": generate_no_exit_maze(15) + } + + strategies = [ + BFSStrategy(), + DFSStrategy(), + AStarStrategy(), + DijkstraStrategy() + ] + + results = [] + + print("=" * 60) + print("ЭКСПЕРИМЕНТЫ") + print("=" * 60) + + for maze_name, lines in mazes.items(): + + filename = get_path(f"{maze_name}.txt") + + create_test_maze(filename, lines) + + maze = TextFileMazeBuilder().buildFromFile(filename) + + print(f"\nЛабиринт: {maze_name}") + print("-" * 60) + + for strategy in strategies: + + times = [] + visited_values = [] + + final_path_len = 0 + + for _ in range(5): + + solver = MazeSolver(maze) + + solver.setStrategy(strategy) + + stats, path = solver.solve() + + times.append(stats.time_ms) + + visited_values.append(stats.visited_cells) + + final_path_len = stats.path_length + + avg_time = sum(times) / len(times) + + avg_visited = sum(visited_values) / len(visited_values) + + results.append({ + "maze": maze_name, + "strategy": strategy.name, + "time_ms": round(avg_time, 4), + "visited": int(avg_visited), + "path_length": final_path_len + }) + + status = "найден" if final_path_len > 0 else "не найден" + + print( + f"{strategy.name:<10} | " + f"{avg_time:>8.4f} мс | " + f"{int(avg_visited):>5} клеток | " + f"путь {status}" + ) + + csv_path = get_path("results.csv") + + with open(csv_path, "w", newline="", encoding='utf-8') as f: + + writer = csv.DictWriter( + f, + fieldnames=[ + "maze", + "strategy", + "time_ms", + "visited", + "path_length" + ] + ) + + writer.writeheader() + + writer.writerows(results) + + print(f"\nCSV сохранён: {csv_path}") + + return results + + +# ============================================================ +# ГРАФИК +# ============================================================ + +def build_charts(results): + + mazes = list(dict.fromkeys(r["maze"] for r in results)) + + strategies = list(dict.fromkeys(r["strategy"] for r in results)) + + fig, ax = plt.subplots(figsize=(12, 6)) + + x = range(len(mazes)) + + width = 0.2 + + colors = { + 'BFS': '#3498db', + 'DFS': '#e74c3c', + 'A*': '#2ecc71', + 'Dijkstra': '#f39c12' + } + + for i, strategy in enumerate(strategies): + + times = [ + r["time_ms"] + for r in results + if r["strategy"] == strategy + ] + + ax.bar( + [j + i * width for j in x], + times, + width, + label=strategy, + color=colors.get(strategy, 'gray') + ) + + ax.set_xlabel("Лабиринт") + + ax.set_ylabel("Время (мс)") + + ax.set_title("Сравнение алгоритмов") + + ax.set_xticks([j + width * 1.5 for j in x]) + + ax.set_xticklabels(mazes) + + ax.legend() + + ax.grid(axis='y', alpha=0.3) + + plt.tight_layout() + + chart_path = get_path("chart_time.png") + + plt.savefig(chart_path, dpi=150, bbox_inches='tight') + + print(f"График сохранён: {chart_path}") + + plt.show() + + +# ============================================================ +# MAIN +# ============================================================ + +def main(): + + results = run_experiments() + + build_charts(results) + + +if __name__ == "__main__": + main() \ No newline at end of file