Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Drop heuristic to improve runtime #2

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

Closed
ahans wants to merge 1 commit into maneatingape:main from ahans:day17-drop-heuristic

Conversation

@ahans
Copy link

@ahans ahans commented Jan 7, 2024

This drops the heuristic, since using it is a net negative. Improves the runtime by ~30% on Intel Core i7-12900k.

This drops the heuristic, since using it is a net negative. Improves the
runtime by ~30% on Intel Core i7-12900k.
Copy link
Owner

Thanks for the PR!

That's an interesting result about the heuristic. On my input it results in a 15% speedup from eliminating branches.

Loop repetitions Part One Part Two
Heuristic 54784 72387
No heuristic 63926 85068

Would you mind checking if the speedup is from the structure of your input?

I modified the code to add a simple counter:

 let mut counter = 0;
 loop {
 while let Some((x, y, direction)) = todo[index % 100].pop() {
 <...snip...>
 counter += 1;
 // Check if we've reached the end.
 if x == width - 1 && y == height - 1 {
 println!("{counter}");
 return steps;
 }

Copy link
Author

ahans commented Jan 7, 2024

Good to see those numbers. Also explains the runtime differences somewhat. I find it quite surprising that there are such differences in the inputs. This is the result I get:

Loop repetitions Part One Part Two
Heuristic 63452 145385
No Heuristic 68011 89913

So it looks like for my input the heuristic actually makes it a lot worse.

Anyway, I would find it reasonable for you to optimize for your input, so feel free to close the PR.

Copy link
Owner

I'm curious what could cause such a difference. Let's leave the PR open for now until I can dig into it a little.

Visualizing the path taken may provide some insight.

@maneatingape maneatingape force-pushed the main branch 5 times, most recently from 98bb73e to 1b240a6 Compare May 18, 2024 01:21
@maneatingape maneatingape force-pushed the main branch 5 times, most recently from 3a27b90 to e3296a3 Compare July 15, 2024 14:50
@maneatingape maneatingape force-pushed the main branch 10 times, most recently from ca1bb0d to d7b0a50 Compare August 21, 2024 23:14
Copy link
Owner

maneatingape commented Sep 15, 2024
edited
Loading

Following up nine months later... 🙂

Implemented an animation visualizing the frontier in order to debug. With your input and the heuristic set as the Manhattan distance towards the bottom 2 * size - x - y, a second cheaper frontier occurs because a move up or left near the top left origin is cheaper. This causes double the work as two frontiers propagate towards the goal simultaneously

Tweaked the heuristic from 2 * size - x - y to (2 * size - x - y).min(size + size / 2). This "flattens" the heuristic in the top left quadrant, preferring all directions equally. This performs almost as well as the original and improves performance for your input.

Loop repetitions Part One Part Two
Heuristic 54784 72387
Modified Heuristic 55210 73286

Copy link
Owner

maneatingape commented Sep 15, 2024
edited
Loading

Tweak heuristic to fix performance regression on some inputs. Thanks for flagging this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /