Skip to content

Commit

Permalink
WIP(evo-animation): template for parent traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
donRumata03 committed Dec 1, 2023
1 parent 6faa462 commit 3841688
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 2 additions & 3 deletions examples/synthetic_graph_evolution/simple_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from golem.metrics.edit_distance import tree_edit_dist


def run_graph_search(size=16, timeout=1, visualize=True):
def run_graph_search(size=16, timeout=0.2, visualize=True):
# Generate target graph that will be sought by optimizer
node_types = ('a', 'b')
target_graph = generate_labeled_graph('tree', size, node_labels=node_types)
Expand Down Expand Up @@ -54,8 +54,7 @@ def run_graph_search(size=16, timeout=1, visualize=True):

if visualize:
# Restore the NetworkX graph back from internal Graph representation
archive_graphs = [graph_gen_params.adapter.restore(g[0].graph) for g in optimiser.history.archive_history]
animate_graph_evolution(target_graph, archive_graphs, "./")
animate_graph_evolution(target_graph, optimiser.history, graph_gen_params.adapter, "./")

optimiser.history.show.fitness_line()
return found_graphs
Expand Down
7 changes: 6 additions & 1 deletion examples/synthetic_graph_evolution/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import numpy as np
from matplotlib import cm

from golem.core.adapter import BaseOptimizationAdapter
from golem.core.adapter.nx_adapter import BaseNetworkxAdapter
from golem.core.optimisers.opt_history_objects.opt_history import OptHistory
from golem.utilities.data_structures import ensure_wrapped_in_sequence
Expand Down Expand Up @@ -124,10 +125,14 @@ def draw_graphs_subplots(*graphs: nx.Graph,
plt.savefig(os.path.join(path_to_save, 'graphs_subplots.png'))


def animate_graph_evolution(target_graph: nx.Graph, evolution_history: List[nx.Graph], dir_to_save_gif: str):
def animate_graph_evolution(target_graph: nx.Graph, evolution_history: OptHistory, adapter: BaseOptimizationAdapter, dir_to_save_gif: str):
archive_graphs = [adapter.restore(g[0].graph) for g in evolution_history.archive_history]
print(repr(adapter.restore(evolution_history.archive_history[-1][0].parents[0])))

target_frames = 10
target_time_s = 3.

# TODO: Make work for len(evolution_history) smaller than target frames, analyze typical situation
evolution_history = evolution_history[::len(evolution_history) // target_frames]

fig, (target_ax, evo_ax) = plt.subplots(1, 2)
Expand Down

0 comments on commit 3841688

Please sign in to comment.