feat: add greedy lookahead algorithm with rolling horizon for hyperpixel building#9
feat: add greedy lookahead algorithm with rolling horizon for hyperpixel building#9dhalmazna wants to merge 6 commits intofeat/algorithm-utilsfrom
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip CodeRabbit can use OpenGrep to find security vulnerabilities and bugs across 17+ programming languages.OpenGrep is compatible with Semgrep configurations. Add an |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request establishes a foundational greedy lookahead algorithm for hyperpixel construction, employing a rolling horizon approach. This new algorithm provides a crucial baseline for evaluating more complex Monte Carlo search methods that will be integrated in the future, ensuring a structured progression in the development of hyperpixel building strategies. Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a greedy lookahead algorithm, which is a solid addition as a baseline for hyperpixel construction. The implementation is well-structured, with a clear separation between the main building loop and the candidate generation logic. The use of a rolling horizon strategy is correctly implemented. My main suggestion is to use TypedDict for the return type to improve type safety and code clarity, which is a lightweight improvement over the current dict[str, object] while you plan for a future refactor to a dataclass.
There was a problem hiding this comment.
Pull request overview
This PR adds a greedy lookahead algorithm for building hyperpixels using a rolling horizon strategy (BFS lookahead + commit-one-step), along with a HyperpixelResult TypedDict to formalize the return contract of hyperpixel builders.
Changes:
- New
ciao/algorithm/lookahead.pyimplementing BFS-based greedy lookahead with rolling horizon for hyperpixel construction. - New
HyperpixelResultTypedDict inciao/scoring/hyperpixel.pyand migration ofselect_top_hyperpixelsto use it (including renaming the sort key fromhyperpixel_scoretoscore). - README and
__init__.pyupdates to reflect the new module.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
ciao/algorithm/lookahead.py |
New greedy lookahead builder using BFS candidate generation and rolling horizon commit strategy |
ciao/scoring/hyperpixel.py |
Added HyperpixelResult TypedDict; updated select_top_hyperpixels to use it |
ciao/algorithm/__init__.py |
Re-exports the new build_hyperpixel_greedy_lookahead function |
README.md |
Updated algorithm description and file tree to reflect the renamed module |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| def _generate_lookahead_candidates( | ||
| current_mask: int, | ||
| adj_masks: tuple[int, ...], | ||
| used_mask: int, | ||
| lookahead_distance: int, | ||
| desired_length: int, | ||
| ) -> dict[int, int]: | ||
| """Generate all connected supersets up to lookahead_distance steps via BFS. | ||
|
|
||
| Returns: | ||
| Dict mapping candidate_mask -> first_step_segment_id | ||
| """ | ||
| candidates: dict[int, int] = {} # mask -> first_step | ||
|
|
||
| # Queue stores tuples of: (current_mask, first_step_that_led_here, current_depth) | ||
| queue: deque[tuple[int, int | None, int]] = deque([(current_mask, None, 0)]) | ||
| visited = {current_mask} | ||
|
|
||
| while queue: | ||
| mask, first_step, depth = queue.popleft() | ||
|
|
||
| # Store valid candidates (depth > 0) | ||
| if depth > 0 and first_step is not None and mask not in candidates: | ||
| # Only add if not already seen (shortest path wins) | ||
| candidates[mask] = first_step | ||
|
|
||
| # Stop expanding if we reached the lookahead limit or maximum size | ||
| if depth >= lookahead_distance or mask.bit_count() >= desired_length: | ||
| continue | ||
|
|
||
| frontier = get_frontier(mask, adj_masks, used_mask) | ||
| for seg_id in iter_bits(frontier): | ||
| new_mask = add_node(mask, seg_id) | ||
|
|
||
| if new_mask not in visited: | ||
| visited.add(new_mask) | ||
| # If at the first layer (depth 0), this seg_id is our first_step. | ||
| # Otherwise, pass along the first_step inherited from the parent. | ||
| next_first_step = seg_id if depth == 0 else first_step | ||
| queue.append((new_mask, next_first_step, depth + 1)) | ||
|
|
||
| return candidates |
There was a problem hiding this comment.
That's the user's problem if he sets the lookahead_distance too large :D
Context:
This PR introduces the
lookahead.pymodule, which implements a greedy search algorithm with a rolling horizon strategy for constructing hyperpixels. This serves as a baseline algorithm before we introduce full Monte Carlo search methods.What's Changed / Added:
ciao/algorithm/lookahead.py: Added the implementation of the greedy lookahead builder. It uses bitwise operations and BFS to explore paths.ciao/scoring/hyperpixel.py: Introduced theHyperpixelResultusingTypedDictto establish a clear, strict contract for what a built hyperpixel returns.How it works:
lookahead_distancesteps away.