/
timshk
/
Shalaev_Stepik
Обзор
Документация
Войти
/
timshk
/
Shalaev_Stepik
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Task4/src/visualization.py
73 строки
3 KB
timshk
Stepik задания
01 июн 2026, 18:58
Верифицирован
01 июн 2026, 18:58
2e75834
Код
Авторство
О чём код?
""" Визуализация графа и результатов """ import networkx as nx import matplotlib.pyplot as plt import numpy as np def plot_graph(incompatibility_matrix, solution=None, title=None): """ Визуализирует граф несовместимости с раскраской решения. Args: incompatibility_matrix: матрица несовместимости solution: список битов (0/1) для каждого узла title: заголовок графика """ n_nodes = len(incompatibility_matrix) # Строим граф G = nx.Graph() for i in range(n_nodes): G.add_node(i, label=f"Груз {i+1}") for i in range(n_nodes): for j in range(i+1, n_nodes): if incompatibility_matrix[i][j] > 0: G.add_edge(i, j, weight=incompatibility_matrix[i][j]) pos = nx.circular_layout(G) plt.figure(figsize=(6, 5)) # Цвета узлов в зависимости от решения if solution is not None: colors = ['lightcoral' if solution[i] == 0 else 'lightgreen' for i in range(n_nodes)] else: colors = ['lightblue' for _ in range(n_nodes)] nx.draw_networkx_nodes(G, pos, node_color=colors, node_size=500) nx.draw_networkx_labels(G, pos, labels={i: f"Груз {i+1}" for i in range(n_nodes)}) nx.draw_networkx_edges(G, pos) edge_labels = {(u, v): f"{w['weight']}" for u, v, w in G.edges(data=True)} nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels) # Вычисляем вес разреза if solution is not None: cut_weight = 0 for i in range(n_nodes): for j in range(i+1, n_nodes): if solution[i] != solution[j]: cut_weight += incompatibility_matrix[i][j] plt.title(f"{title or 'Граф несовместимости'}\nВес разреза = {cut_weight}") else: plt.title(title or "Граф несовместимости") plt.axis('off') plt.tight_layout() plt.show() def plot_results_table(assignments, total_weight, capacity): """Выводит таблицу назначений.""" print("\n" + "="*50) print("ПЛАН ЗАГРУЗКИ") print("="*50) for machine_id, cargo_list in assignments.items(): weight = sum(cargo_list) status = "✅" if weight <= capacity else "❌ ПЕРЕГРУЗ!" print(f"Машина {machine_id}: грузы {cargo_list}, вес {weight}кг {status}")