Skip to content

Commit

Permalink
fix: patch x/y in plots
Browse files Browse the repository at this point in the history
  • Loading branch information
squillero committed Aug 12, 2024
1 parent 2d2ee26 commit 79b0444
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 387 deletions.
39 changes: 9 additions & 30 deletions _experiments/t.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,14 @@

import byron

reg8 = byron.f.choice_parameter(['ah', 'bh', 'ch', 'dh', 'al', 'bl', 'cl', 'dl'])
reg16 = byron.f.choice_parameter(['ax', 'bx', 'cx', 'dx'])
int8 = byron.f.integer_parameter(0, 2**8)
int16 = byron.f.integer_parameter(0, 2**16)
macro_foo = byron.f.macro('foo #{num:04x}', num=byron.f.integer_parameter(0, 2**16))
macro_bar = byron.f.macro('bar {num:+0.2g}', num=byron.f.float_parameter(-1, 1))

opcodes2 = byron.f.choice_parameter(['mov', 'add', 'sub', 'or', 'and'])
opcodes1 = byron.f.choice_parameter(['not', 'neg', 'inc', 'dec'])
opcodes0 = byron.f.choice_parameter(['nop', 'hlt'])
foo_bunch = byron.f.bunch([macro_foo], size=5)
macro_ref = byron.f.macro('global_ref: {ref}', ref=byron.f.global_reference(foo_bunch))
ref_bunch = byron.f.bunch([macro_ref], size=3)
program = byron.f.sequence([foo_bunch, ref_bunch])

inst8rr = byron.f.macro("{op} {r1}, {r2}", op=opcodes2, r1=reg8, r2=reg8)
inst8ri = byron.f.macro("{op} {r}, {i:#x}", op=opcodes2, r=reg8, i=int8)
inst8r = byron.f.macro("{op} {r}", op=opcodes1, r=reg8)

inst16rr = byron.f.macro("{op} {r1}, {r2}", op=opcodes2, r1=reg16, r2=reg16)
inst16ri = byron.f.macro("{op} {r}, {i:#x}", op=opcodes2, r=reg16, i=int16)
inst16r = byron.f.macro("{op} {r}", op=opcodes1, r=reg16)

sub_entry = byron.f.macro("{_node} proc")
sub_exit = byron.f.macro("ret\n")
sub_body = byron.f.bunch([inst16rr, inst16ri, inst16r, inst8rr, inst8ri, inst8r], 3)
sub = byron.f.sequence([sub_entry, sub_body, sub_exit], name="sub")

call = byron.f.macro("call {proc}", proc=byron.f.global_reference(sub, first_macro=True, creative_zeal=1))

main_prologue = "global _start\n_start:"
main_body = byron.f.bunch([inst16rr, inst16ri, inst16r, inst8rr, inst8ri, inst8r, call], 100)
init = byron.f.macro("mov ax, {v:#x}\nmov bx, {v:#x}\nmov cx, {v:#x}\nmov dx, {v:#x}", v=int16)

full_prog = byron.f.sequence([main_prologue, init, main_body])

print(byron.f.as_text(full_prog))
pass
byron.f.as_text(program)
byron.f.as_forest(program)
byron.f.as_lgp(program)
33 changes: 30 additions & 3 deletions byron/classes/_individual_as.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def _draw_forest(self, zoom):

T.remove_node(0)
pos = nx.multipartite_layout(T, subset_key="depth", align="horizontal")
pos = {node: (-x, -y) for (node, (x, y)) in pos.items()}
patch_pos(self.structure_tree, pos)
colors = get_node_color_dict(self.G)

# draw structure
Expand Down Expand Up @@ -240,9 +240,11 @@ def _draw_multipartite(self, zoom: int) -> None:
G.add_edge(n1, n2)
for n in nodes:
G.nodes[n]["subset"] = s

colors = get_node_color_dict(self.G)
pos = nx.multipartite_layout(G)
# pos = {node: (-x, y) for (node, (x, y)) in pos.items()}
colors = get_node_color_dict(self._genome)
patch_pos(self.structure_tree, pos)

nodelist = list(G.nodes)

# figsize
Expand Down Expand Up @@ -316,3 +318,28 @@ def _draw_multipartite(self, zoom: int) -> None:
plt.tight_layout()

return fig


def patch_pos(
G: nx.DiGraph,
pos: dict,
):
pos_order = [n for n in list(nx.dfs_preorder_nodes(G, NODE_ZERO)) if n in pos]
swap_x, swap_y = 0, 0
for i in range(len(pos_order) - 1):
if pos[pos_order[i]][0] > pos[pos_order[i + 1]][0]:
swap_x -= 1
elif pos[pos_order[i]][0] > pos[pos_order[i + 1]][0]:
swap_x += 1
if pos[pos_order[i]][1] > pos[pos_order[i + 1]][1]:
swap_y += 1
elif pos[pos_order[i]][1] > pos[pos_order[i + 1]][1]:
swap_y -= 1
# ic(swap_x, swap_y)
if swap_x > 0:
for n in pos:
pos[n][0] = -pos[n][0]
if swap_y >= 0:
for n in pos:
pos[n][1] = -pos[n][1]
pass
Loading

0 comments on commit 79b0444

Please sign in to comment.