def has_no_cycle(chain: Chain): graph, _ = as_nx_graph(chain) cycled = list(simple_cycles(graph)) if len(cycled) > 0: raise ValueError(f'{ERROR_PREFIX} Chain has cycles') return True
def has_no_isolated_components(chain: Chain): graph, _ = as_nx_graph(chain) ud_graph = nx.Graph() ud_graph.add_nodes_from(graph) ud_graph.add_edges_from(graph.edges) if not nx.is_connected(ud_graph): raise ValueError(f'{ERROR_PREFIX} Chain has isolated components') return True
def visualise(chain: Chain): try: chain.sort_nodes() graph, node_labels = as_nx_graph(chain=chain) pos = node_positions(graph.to_undirected()) plt.figure(figsize=(10, 16)) nx.draw(graph, pos=pos, with_labels=True, labels=node_labels, font_size=12, font_family='calibri', font_weight='bold', node_size=7000, width=2.0, node_color=colors_by_node_labels(node_labels), cmap='Set3') plt.show() except Exception as ex: print(f'Visualisation failed with {ex}')
def _visualise_chains(chains, fitnesses): fitnesses = deepcopy(fitnesses) last_best_chain = chains[0] prev_fit = fitnesses[0] for ch_id, chain in enumerate(chains): graph, node_labels = as_nx_graph(chain=chain) pos = node_positions(graph.to_undirected()) plt.rcParams['axes.titlesize'] = 20 plt.rcParams['axes.labelsize'] = 20 plt.rcParams['figure.figsize'] = [10, 10] plt.title('Current chain') nx.draw(graph, pos=pos, with_labels=True, labels=node_labels, font_size=12, font_family='calibri', font_weight='bold', node_size=scaled_node_size(chain.length), width=2.0, node_color=colors_by_node_labels(node_labels), cmap='Set3') path = f'{ComposerVisualiser.temp_path}ch_{ch_id}.png' plt.savefig(path, bbox_inches='tight') plt.cla() plt.clf() plt.close('all') path_best = f'{ComposerVisualiser.temp_path}best_ch_{ch_id}.png' if fitnesses[ch_id] > prev_fit: fitnesses[ch_id] = prev_fit else: last_best_chain = chain prev_fit = fitnesses[ch_id] best_graph, best_node_labels = as_nx_graph(chain=last_best_chain) pos = node_positions(best_graph.to_undirected()) plt.rcParams['axes.titlesize'] = 20 plt.rcParams['axes.labelsize'] = 20 plt.rcParams['figure.figsize'] = [10, 10] plt.title(f'Best chain after {round(ch_id)} evals') nx.draw(best_graph, pos=pos, with_labels=True, labels=best_node_labels, font_size=12, font_family='calibri', font_weight='bold', node_size=scaled_node_size(chain.length), width=2.0, node_color=colors_by_node_labels(best_node_labels), cmap='Set3') plt.savefig(path_best, bbox_inches='tight') plt.cla() plt.clf() plt.close('all')
def has_no_isolated_nodes(chain: Chain): graph, _ = as_nx_graph(chain) isolated = list(isolates(graph)) if len(isolated) > 0: raise ValueError(f'{ERROR_PREFIX} Chain has isolated nodes') return True