Skip to main content

problemreductions/
traits.rs

1//! Core traits for problem definitions.
2
3/// Minimal problem trait — a problem is a function from configuration to value.
4///
5/// This trait defines the interface for computational problems that can be
6/// solved by enumeration or reduction to other problems.
7pub trait Problem: Clone {
8    /// Base name of this problem type (e.g., "MaximumIndependentSet").
9    const NAME: &'static str;
10    /// The evaluation value type.
11    type Value: Clone;
12    /// Configuration space dimensions. Each entry is the cardinality of that variable.
13    fn dims(&self) -> Vec<usize>;
14    /// Evaluate the problem on a configuration.
15    fn evaluate(&self, config: &[usize]) -> Self::Value;
16    /// Number of variables (derived from dims).
17    fn num_variables(&self) -> usize {
18        self.dims().len()
19    }
20    /// Returns variant attributes derived from type parameters.
21    ///
22    /// Used for generating variant IDs in the reduction graph schema.
23    /// Returns pairs like `[("graph", "SimpleGraph"), ("weight", "i32")]`.
24    fn variant() -> Vec<(&'static str, &'static str)>;
25
26    /// Look up this problem's catalog entry.
27    ///
28    /// Returns the full [`ProblemType`] metadata from the catalog registry.
29    /// The default implementation uses `Self::NAME` to perform the lookup.
30    fn problem_type() -> crate::registry::ProblemType {
31        crate::registry::find_problem_type(Self::NAME)
32            .unwrap_or_else(|| panic!("no catalog entry for Problem::NAME = {:?}", Self::NAME))
33    }
34}
35
36/// Marker trait for explicitly declared problem variants.
37///
38/// Implemented automatically by [`declare_variants!`] for each concrete type.
39/// The [`#[reduction]`] proc macro checks this trait at compile time to ensure
40/// all reduction source/target types have been declared.
41pub trait DeclaredVariant {}
42
43#[cfg(test)]
44#[path = "unit_tests/traits.rs"]
45mod tests;