Skip to content

Fix #572: [Model] GeneralizedHex#686

Merged
zazabap merged 5 commits intomainfrom
issue-572-generalizedhex
Mar 19, 2026
Merged

Fix #572: [Model] GeneralizedHex#686
zazabap merged 5 commits intomainfrom
issue-572-generalizedhex

Conversation

@GiggleLiu
Copy link
Copy Markdown
Contributor

@GiggleLiu GiggleLiu commented Mar 16, 2026

Summary

Add the GeneralizedHex graph model with a zero-variable memoized minimax evaluator, CLI creation support (--source/--sink and --random), focused unit coverage, the canonical example fixture, and the corresponding paper/reference updates.

Fixes #572

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 16, 2026

Codecov Report

❌ Patch coverage is 95.52846% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.38%. Comparing base (3920752) to head (60a61e9).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/models/graph/generalized_hex.rs 93.75% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #686      +/-   ##
==========================================
- Coverage   97.39%   97.38%   -0.02%     
==========================================
  Files         339      341       +2     
  Lines       43413    43659     +246     
==========================================
+ Hits        42284    42519     +235     
- Misses       1129     1140      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@GiggleLiu
Copy link
Copy Markdown
Contributor Author

Implementation Summary

Changes

  • Added GeneralizedHex as a graph model in src/models/graph/generalized_hex.rs and exported it through the graph/model preludes.
  • Added focused unit coverage in src/unit_tests/models/graph/generalized_hex.rs.
  • Extended CLI creation/help paths for GeneralizedHex, using --source and --sink for the terminal vertices.
  • Regenerated the example-db fixture and documented the model in the paper with supporting references.

Deviations from Plan

  • Implemented GeneralizedHex as a zero-variable SatisfactionProblem whose evaluate([]) runs memoized minimax over playable-vertex claim states. This preserves alternating-play semantics more directly than encoding move choices as static variables.
  • Corrected the issue's supplied YES example after independent minimax verification showed it is actually losing for the first player under optimal play. The paper/example now use a different winning instance.

Open Questions

  • None.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new graph game model, GeneralizedHex, to the problemreductions model registry and wires it through the CLI, examples/fixtures, and paper docs so it can be created, serialized, and evaluated like existing graph problems.

Changes:

  • Introduces the GeneralizedHex model (memoized minimax evaluator; dims() = []) with schema/variant registration and a canonical example.
  • Adds CLI support for pred create GeneralizedHex (including --source/--sink and --random generation) plus CLI tests.
  • Adds unit tests, example-db fixture entry, and paper documentation/references for the new model.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/models/graph/generalized_hex.rs New GeneralizedHex model, schema registration, variants, and example-db hook
src/unit_tests/models/graph/generalized_hex.rs Unit tests covering construction, evaluation, solver behavior, and serde round-trip
src/models/graph/mod.rs Registers the new module/export and includes its canonical example spec
src/models/mod.rs Re-exports GeneralizedHex from the top-level models module
src/lib.rs Exposes GeneralizedHex in the crate prelude
problemreductions-cli/src/commands/create.rs Adds create + random-generation support for GeneralizedHex and CLI tests
problemreductions-cli/src/cli.rs Updates CLI help text to list GeneralizedHex flags and an example command
src/example_db/fixtures/examples.json Adds GeneralizedHex example fixture (and modifies other existing fixtures)
docs/paper/references.bib Adds citations relevant to Generalized Hex / Shannon switching game
docs/paper/reductions.typ Adds a paper section describing GeneralizedHex and its canonical instance

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 409 to 415
if canonical == "LengthBoundedDisjointPaths" && field_name == "max_length" {
return "bound".to_string();
}
if canonical == "GeneralizedHex" && field_name == "target" {
return "sink".to_string();
}
field_name.replace('_', "-")
Comment on lines +253 to +260
if !config.is_empty() {
return false;
}
let playable_vertices = self.playable_vertices();
let vertex_to_state_index = self.vertex_to_state_index(&playable_vertices);
let mut state = vec![ClaimState::Unclaimed; playable_vertices.len()];
let mut memo = HashMap::new();
self.first_player_wins(&mut state, &vertex_to_state_index, &mut memo)
{"source":{"problem":"LongestCommonSubsequence","variant":{},"instance":{"strings":[[65,66,65,67],[66,65,67,65]]}},"target":{"problem":"ILP","variant":{"variable":"bool"},"instance":{"constraints":[{"cmp":"Le","rhs":1.0,"terms":[[0,1.0],[1,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[2,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[3,1.0],[4,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[5,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[2,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[0,1.0],[3,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[5,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[1,1.0],[4,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[0,1.0],[2,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[1,1.0],[2,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[1,1.0],[3,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[1,1.0],[5,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[4,1.0],[5,1.0]]}],"num_vars":6,"objective":[[0,1.0],[1,1.0],[2,1.0],[3,1.0],[4,1.0],[5,1.0]],"sense":"Maximize"}},"solutions":[{"source_config":[0,1,1,1],"target_config":[0,0,1,1,0,1]}]},
{"source":{"problem":"MaxCut","variant":{"graph":"SimpleGraph","weight":"i32"},"instance":{"edge_weights":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],"graph":{"inner":{"edge_property":"undirected","edges":[[0,1,null],[0,4,null],[0,5,null],[1,2,null],[1,6,null],[2,3,null],[2,7,null],[3,4,null],[3,8,null],[4,9,null],[5,7,null],[5,8,null],[6,8,null],[6,9,null],[7,9,null]],"node_holes":[],"nodes":[null,null,null,null,null,null,null,null,null,null]}}}},"target":{"problem":"SpinGlass","variant":{"graph":"SimpleGraph","weight":"i32"},"instance":{"couplings":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],"fields":[0,0,0,0,0,0,0,0,0,0],"graph":{"inner":{"edge_property":"undirected","edges":[[0,1,null],[0,4,null],[0,5,null],[1,2,null],[1,6,null],[2,3,null],[2,7,null],[3,4,null],[3,8,null],[4,9,null],[5,7,null],[5,8,null],[6,8,null],[6,9,null],[7,9,null]],"node_holes":[],"nodes":[null,null,null,null,null,null,null,null,null,null]}}}},"solutions":[{"source_config":[0,1,0,1,0,1,0,0,0,1],"target_config":[0,1,0,1,0,1,0,0,0,1]}]},
{"source":{"problem":"MaxCut","variant":{"graph":"SimpleGraph","weight":"i32"},"instance":{"edge_weights":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],"graph":{"inner":{"edge_property":"undirected","edges":[[0,1,null],[0,4,null],[0,5,null],[1,2,null],[1,6,null],[2,3,null],[2,7,null],[3,4,null],[3,8,null],[4,9,null],[5,7,null],[5,8,null],[6,8,null],[6,9,null],[7,9,null]],"node_holes":[],"nodes":[null,null,null,null,null,null,null,null,null,null]}}}},"target":{"problem":"SpinGlass","variant":{"graph":"SimpleGraph","weight":"i32"},"instance":{"couplings":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],"fields":[0,0,0,0,0,0,0,0,0,0],"graph":{"inner":{"edge_property":"undirected","edges":[[0,1,null],[0,4,null],[0,5,null],[1,2,null],[1,6,null],[2,3,null],[2,7,null],[3,4,null],[3,8,null],[4,9,null],[5,7,null],[5,8,null],[6,8,null],[6,9,null],[7,9,null]],"node_holes":[],"nodes":[null,null,null,null,null,null,null,null,null,null]}}}},"solutions":[{"source_config":[1,0,1,0,0,0,0,0,1,1],"target_config":[1,0,1,0,0,0,0,0,1,1]}]},
{"source":{"problem":"MaximumClique","variant":{"graph":"SimpleGraph","weight":"i32"},"instance":{"graph":{"inner":{"edge_property":"undirected","edges":[[0,1,null],[0,2,null],[0,3,null],[0,4,null],[1,2,null],[1,3,null],[1,5,null],[2,4,null],[2,5,null],[3,4,null],[3,5,null],[4,5,null]],"node_holes":[],"nodes":[null,null,null,null,null,null]}},"weights":[1,1,1,1,1,1]}},"target":{"problem":"ILP","variant":{"variable":"bool"},"instance":{"constraints":[{"cmp":"Le","rhs":1.0,"terms":[[0,1.0],[5,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[1,1.0],[4,1.0]]},{"cmp":"Le","rhs":1.0,"terms":[[2,1.0],[3,1.0]]}],"num_vars":6,"objective":[[0,1.0],[1,1.0],[2,1.0],[3,1.0],[4,1.0],[5,1.0]],"sense":"Maximize"}},"solutions":[{"source_config":[1,1,1,0,0,0],"target_config":[1,1,1,0,0,0]}]},
Comment on lines +1 to +5
//! Generalized Hex problem implementation.
//!
//! Generalized Hex asks whether the first player has a forced win in the
//! vertex-claiming Shannon switching game on an undirected graph.

@GiggleLiu
Copy link
Copy Markdown
Contributor Author

Agentic Review Report

Structural Check

# Check Status
1 Model file exists (src/models/graph/generalized_hex.rs) PASS
2 inventory::submit! present PASS
3 #[derive(...Serialize, Deserialize)] on struct PASS
4 Problem trait impl PASS
5 SatisfactionProblem impl PASS
6 #[cfg(test)] + #[path = "..."] test link PASS
7 Test file exists (src/unit_tests/models/graph/generalized_hex.rs) PASS
8 Test file has >= 3 test functions PASS (9 test functions)
9 Registered in graph/mod.rs PASS
10 Re-exported in models/mod.rs PASS
11 declare_variants! entry exists PASS (default sat GeneralizedHex<SimpleGraph> => "3^num_playable_vertices")
12 CLI resolve_alias entry PASS (via inventory::submit! registry)
13 CLI create support PASS
14 Canonical model example registered PASS
15 Paper display-name entry PASS
16 Paper problem-def block PASS

Build: CI passing, patch coverage 95.42%, project coverage 97.03%.

Whitelist: FAIL — references.bib, cli.rs, create.rs, examples.json, lib.rs are outside the model whitelist but are standard supporting files for a model PR.

Semantic Review:

  • evaluate() correctness: OK — zero-variable satisfaction design; evaluate(&[]) runs memoized minimax game-tree search. Terminal states correctly handled via BFS path checks before recursion.
  • Minimax correctness: OK — Turn determination by counting claimed vertices is correct. Early termination on winning/blocking moves is correct.
  • BFS path check: OK — Source and target terminals always allowed; internal vertices must satisfy claim predicate.
  • is_valid_solution duplication: ISSUE — is_valid_solution (lines 98-107) is an exact duplicate of evaluate (lines 252-261). Should delegate to evaluate instead.
  • Module doc comment: ISSUE — src/models/graph/mod.rs does not list GeneralizedHex among graph problems.

Issue Compliance:

# Check Status
1 Problem name matches issue OK
2 Mathematical definition matches OK
3 Problem type (opt/sat) matches OK
4 Type parameters match OK
5 Configuration space DEVIATION — Issue specifies n-2 binary variables; implementation uses dims() = [] (zero variables). Intentional design choice preserving game semantics.
6 Feasibility/objective matches OK
7 Complexity matches OK — 3^num_playable_vertices

Quality Check

Design Principles:

  • DRY: ISSUE — is_valid_solution (generalized_hex.rs:98-107) duplicates evaluate (generalized_hex.rs:252-261) byte-for-byte and has no callers. Should delegate or be removed.
  • KISS: OK — Minimax with memoization is the natural algorithm. Helper decomposition is clean.
  • HC/LC: OK — No mixed concerns.

HCI (CLI changed):

  • Error messages: OK — Clear messages with usage hints.
  • Discoverability: OK — --help text updated correctly.
  • Consistency: OK — Uses --source/--sink flag naming matching LengthBoundedDisjointPaths.

Test Quality Issues:

  1. Important: test_generalized_hex_paper_example (line 99-103) is entirely redundant with test_generalized_hex_forced_win_on_bottleneck_example and test_generalized_hex_solver_returns_empty_config_for_win.
  2. Important: No test verifies BruteForce::find_satisfying returns None on a losing instance.
  3. Important: is_valid_solution has zero test coverage (never called in any test).
  4. Minor: No test exercises evaluate(&[0]) returning false (non-empty config guard at line 253-255).
  5. Minor: Unrelated Vec<u64> match arm cleanup in create.rs mixed into this PR.

Agentic Feature Tests

# Command Expected Actual Result
1 pred list GeneralizedHex in catalog Listed as GeneralizedHex/SimpleGraph * with complexity O(3^num_playable_vertices) PASS
2 pred show GeneralizedHex Shows description, fields, complexity 3 fields (graph, source, target), description correct, 0 reductions PASS
3 pred create --example GeneralizedHex Valid JSON instance 6-vertex graph, source=0, target=5 PASS
4 pred solve <example> (default ILP) Graceful error "No reduction path from GeneralizedHex to ILP. Try --solver brute-force" PASS
5 pred solve --solver brute-force <example> Satisfying solution {"evaluation": "true", "solution": []} PASS
6 pred create GeneralizedHex --graph 0-1,1-2 --source 0 --sink 2 Custom instance Valid 3-vertex path graph PASS
7 Solve path 0-1-2 Player 1 wins true PASS
8 Disconnected graph Player 1 loses "No solution found" PASS
9 Path 0-1-2-3 Player 1 loses (Red blocks) "No solution found" PASS
10 Random instance (--random --num-vertices 5 --seed 42) Valid random instance Valid 5-vertex graph PASS
11 Serialization round-trip Correct results Works correctly PASS
12 Unit tests (cargo test --lib generalized_hex) All pass 9/9 pass PASS
13 Example DB tests All pass 3/3 pass PASS

Issues:

# Severity Description
1 Low (cosmetic) pred create GeneralizedHex help text: Parameters section lists --target (from FieldInfo) but the actual CLI flag is --sink. Using --target errors with "GeneralizedHex requires --sink".

Generated by review-pipeline

Resolve conflicts: keep GeneralizedHex alongside newly added models
(ConjunctiveQueryFoldability, ResourceConstrainedScheduling,
PartitionIntoPathsOfLength2, etc.). Adapt ModelExampleSpec to new API.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@zazabap
Copy link
Copy Markdown
Collaborator

zazabap commented Mar 19, 2026

Final review notes:

  • Merged main, resolved conflicts (ConjunctiveQueryFoldability, ConjunctiveBooleanQuery, ResourceConstrainedScheduling, PartitionIntoPathsOfLength2, PartiallyOrderedKnapsack, RectilinearPictureCompression)
  • Adapted ModelExampleSpec to the new API (instance/optimal_config/optimal_value fields)
  • Deleted legacy examples.json fixture (now gitignored build artifact)
  • Reviewed all 4 inline comments:
    1. help_flag_for_create correctly maps targetsink — help output verified: shows --sink
    2. is_valid_solution duplication — already resolved (not present in current code)
    3. examples.json churn — moot (file deleted on main)
    4. PR description scope — cosmetic, not blocking

All local checks pass (make check green). Patch coverage 95.4%, project coverage 97.0%.

Copy link
Copy Markdown
Collaborator

@zazabap zazabap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final review passed. All conflicts resolved, inline comments addressed, coverage meets threshold. LGTM.

- Fix GeneralizedHex: graph.inner.edges -> graph.edges (no inner wrapper
  in serialized SimpleGraph)
- Fix ConjunctiveBooleanQuery: x.optimal.at(0).config -> x.optimal_config
  (adapted to new ModelExampleSpec API)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@zazabap zazabap merged commit 4635179 into main Mar 19, 2026
5 checks passed
@GiggleLiu GiggleLiu deleted the issue-572-generalizedhex branch April 12, 2026 00:53
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.

[Model] GeneralizedHex

3 participants