Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add layout database #852

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

WIP: Add layout database #852

wants to merge 4 commits into from

Conversation

jbdyn
Copy link

@jbdyn jbdyn commented Mar 6, 2025

This PR adds a Python script for generating the database for Pelita mazes.

@Debilski
Copy link
Member

Debilski commented Mar 7, 2025

I refined the existing walls_to_graph implementation in #853 so that it can be reused here.

@jbdyn
Copy link
Author

jbdyn commented Mar 7, 2025

I refined the existing walls_to_graph implementation in #853 so that it can be reused here.

Oh, sorry, I didn't see that. I also reworked the code in create_db.walls_to_graph.

@otizonaizit
Copy link
Member

I refined the existing walls_to_graph implementation in #853 so that it can be reused here.

Oh, sorry, I didn't see that. I also reworked the code in create_db.walls_to_graph.

do you have a profiling for this? How much faster is it to start with a fully connected graph and remove the nodes?

@Debilski
Copy link
Member

Debilski commented Mar 7, 2025

I just profiled it. @jbdyn’s new variant is twice as fast as ours on the normal mazes. Interestingly, nx.grid_2d_graph does not apply any magic at all (if fills the whole thing with Python for loops), so this is probably a case of simply choosing the right iterator for the right data structure. (see: https://networkx.org/documentation/stable/_modules/networkx/generators/lattice.html#grid_2d_graph )

@Debilski
Copy link
Member

Debilski commented Mar 7, 2025

def find_chambers(G: nx.Graph, shape):
    w, h = shape

    main_chamber = set()
    chamber_tiles = set()

    for chamber in nx.biconnected_components(G):
        max_x = max(chamber, key=lambda n: n[0])[0]
        min_x = min(chamber, key=lambda n: n[0])[0]
        if min_x < w // 2 <= max_x:
            # only the main chamber covers both sides
            # our own mazes should only have one central chamber
            # but other configurations could have more than one
            main_chamber.update(chamber)
            continue
        else:
            chamber_tiles.update(set(chamber))

    # remove shared articulation points with the main chamber
    chamber_tiles -= main_chamber

    # combine connected subgraphs
    subgraphs = G.subgraph(chamber_tiles)
    chambers = list(nx.connected_components(subgraphs))

    return chambers, chamber_tiles

Using this instead of paint_chambers reduces the time from 9.3s to 6.5s on my system. (We can now also skip searching for the cuts before.)

@otizonaizit
Copy link
Member

@jbdyn : is it OK if we push to this branch to move it forward?

@jbdyn
Copy link
Author

jbdyn commented Mar 10, 2025

is it OK if we push to this branch to move it forward?

Sure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants