Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/solverforge-macros/WIREFRAME.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ coherent stock bundle. Overriding `distance_meter`, `intra_distance_meter`,
`solution_trait`, `route_hooks`, `savings_hooks`, or `savings_metric_class_fn`
is rejected unless the override repeats the exact stock path. Custom routing
semantics should omit `domain = "cvrp"` and declare explicit hook paths instead.
The profile also declares savings feasibility shared by metric class, so
Clarke-Wright asks one owner per class; explicit hook paths keep asking every
owner.

**Generated code:**
- `impl PlanningSolution for T` — `type Score`, `score()`, `set_score()`, `HAS_CONSTRAINT_WEIGHTS = true` plus `constraint_weights()` returning `Some(&self.field)` when a `#[constraint_weights]` field exists (otherwise both keep their unweighted defaults), plus `update_entity_shadows()` / `update_all_shadows()` / `list_shadow_elements()` / `clear_list_shadows()` / `list_shadow_source()` delegation to the manifest-owned support implementation when `#[shadow_variable_updates(...)]` configures list shadows.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ pub(super) fn generate_list_metadata(
field,
)?;
let solution_trait_bound = parse_solution_trait_bound(solution_trait, field)?;
// The stock CVRP savings feasibility reads only the owner's problem data,
// and the stock metric class groups owners by that data.
let savings_feasible_shared_by_metric_class = matches!(
ListVariableDomain::from_attr(attr, field)?,
Some(ListVariableDomain::Cvrp)
);
let element_owner_path = parse_optional_path(
parse_attribute_string(attr, "element_owner_fn"),
"element_owner_fn",
Expand Down Expand Up @@ -159,6 +165,7 @@ pub(super) fn generate_list_metadata(
#savings_feasible,
)
.with_element_owner_fn(#element_owner)
.with_savings_feasible_shared_by_metric_class(#savings_feasible_shared_by_metric_class)
}

})
Expand Down
33 changes: 33 additions & 0 deletions crates/solverforge-macros/src/planning_entity_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,39 @@ fn list_metadata_forwards_element_owner_fn() {
assert!(expanded.contains("__solverforge_list_element_owner :: < Solution >"));
}

#[test]
fn list_metadata_declares_shared_savings_feasibility_only_for_the_cvrp_profile() {
let expand = |input| {
expand_derive(input)
.expect("entity expansion should succeed")
.to_string()
.replace(' ', "")
};
let cvrp = expand(parse_quote! {
struct Route {
#[planning_id]
id: String,
#[planning_list_variable(element_collection = "visits", domain = "cvrp")]
visits: Vec<usize>,
}
});
let custom = expand(parse_quote! {
struct Route {
#[planning_id]
id: String,
#[planning_list_variable(
element_collection = "visits",
savings_hooks = "crate::hooks",
savings_metric_class_fn = "crate::hooks::metric_class"
)]
visits: Vec<usize>,
}
});

assert!(cvrp.contains(".with_savings_feasible_shared_by_metric_class(true)"));
assert!(custom.contains(".with_savings_feasible_shared_by_metric_class(false)"));
}

#[test]
fn entity_descriptor_includes_index_shadow_metadata() {
let input = parse_quote! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ pub(super) fn generate_runtime_phase_support(
metadata.savings_feasible_fn,
)
.with_element_owner_fn(metadata.element_owner_fn)
.with_savings_feasible_shared_by_metric_class(
metadata.savings_feasible_shared_by_metric_class,
)
)
)
);
Expand Down
15 changes: 12 additions & 3 deletions crates/solverforge-solver/WIREFRAME.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,11 @@ src/
│ │ ├── list_clarke_wright.rs — ListClarkeWrightPhase
│ │ ├── list_clarke_wright/kernel.rs — Canonical savings merge and route assignment (savings.rs generates and sorts savings) shared by public/static/dynamic adapters
│ │ ├── list_clarke_wright/tests.rs — Tests
│ │ ├── list_clarke_wright/owner_assignment.rs — Owner-specific route assignment, buffered-merge publication on interruption, and preservation helpers
│ │ ├── list_clarke_wright/route_state.rs — Route state and merge bookkeeping
│ │ ├── list_clarke_wright/owner_admission.rs — Incremental merge admission: metric-class counts, per-route ownership and owner sets updated for the merged route only
│ │ ├── list_clarke_wright/owner_assignment.rs — Owner-specific route assignment, class-shared savings feasibility, buffered-merge publication on interruption, and preservation helpers
│ │ ├── list_clarke_wright/route_state.rs — Route state and reusable route-buffer helpers
│ │ ├── list_clarke_wright/savings.rs — Metric-class savings computation
│ │ ├── list_clarke_wright/tests/admission_oracle.rs — Incremental admission against whole-set recomputation on seeded instances
│ │ ├── list_clarke_wright/tests/metric_class.rs — Shared metric-class and owner-feasibility tests
│ │ ├── list_clarke_wright/tests/owner_binding.rs — Owner-specific hook binding tests
│ │ ├── list_k_opt.rs — ListKOptPhase
Expand Down Expand Up @@ -1020,7 +1022,14 @@ Runtime helpers:
optional savings depot, metric-class, distance, and feasibility callbacks;
and an optional partial fixed-owner callback. `new(...)` accepts every field
except the owner callback, which starts absent and is attached with
`with_element_owner_fn(...)`.
`with_element_owner_fn(...)`, and `savings_feasible_shared_by_metric_class`,
which starts `false` and is declared with
`with_savings_feasible_shared_by_metric_class(bool)`. Declared `true`,
Clarke-Wright asks one owner per savings metric class for savings
feasibility and reuses the answer for the class; the `domain = "cvrp"`
profile declares it because the stock hook reads only the owner's
`ProblemData`, which is what the stock metric class groups by.
`ListVariableSlot` carries the same field and builder.
- `ListVariableEntity<S>` — list-variable accessors plus `HAS_LIST_VARIABLE`, `LIST_VARIABLE_NAME`, and `LIST_ELEMENT_SOURCE`
- `runtime/compiler/` — compiles one value-owned `RuntimeModel` into an immutable graph, prepares solve-owned sources, and runs every configured or default phase through `CompiledRuntimePhaseRunner`
- `PlanningModelSupport` — `#[doc(hidden)]` support trait (and `#[doc(hidden)]`
Expand Down
6 changes: 4 additions & 2 deletions crates/solverforge-solver/public-api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ struct ExhaustiveSearchPhase: pub struct ExhaustiveSearchPhase<Dec> { /* private
struct FunctionalPartitioner: pub struct FunctionalPartitioner<S, PF, MF> where S: PlanningSolution, PF: Fn(&S) -> Vec<S> + Send + Sync, MF: Fn(&S, Vec<S>) -> S + Send + Sync,{ /* private fields */ }
FunctionalPartitioner: pub fn new(partition_fn: PF, merge_fn: MF) -> Self
FunctionalPartitioner: pub fn with_recommended_count(self, count: NonZeroUsize) -> Self
struct ListVariableMetadata: pub struct ListVariableMetadata<S, DM, IDM> { pub cross_distance_meter: DM, pub intra_distance_meter: IDM, pub route_get_fn: Option<fn(&S, usize) -> Vec<usize>>, pub route_set_fn: Option<fn(&mut S, usize, Vec<usize>)>, pub route_depot_fn: Option<fn(&S, usize) -> usize>, pub route_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, pub route_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, pub savings_depot_fn: Option<fn(&S, usize) -> usize>, pub savings_metric_class_fn: Option<fn(&S, usize) -> usize>, pub savings_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, pub savings_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, pub element_owner_fn: Option<fn(&S, &usize) -> Option<usize>>, /* private fields */ }
struct ListVariableMetadata: pub struct ListVariableMetadata<S, DM, IDM> {Show 13 fields pub cross_distance_meter: DM, pub intra_distance_meter: IDM, pub route_get_fn: Option<fn(&S, usize) -> Vec<usize>>, pub route_set_fn: Option<fn(&mut S, usize, Vec<usize>)>, pub route_depot_fn: Option<fn(&S, usize) -> usize>, pub route_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, pub route_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, pub savings_depot_fn: Option<fn(&S, usize) -> usize>, pub savings_metric_class_fn: Option<fn(&S, usize) -> usize>, pub savings_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, pub savings_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, pub savings_feasible_shared_by_metric_class: bool, pub element_owner_fn: Option<fn(&S, &usize) -> Option<usize>>, /* private fields */ }
ListVariableMetadata: pub fn new( cross_distance_meter: DM, intra_distance_meter: IDM, route_get_fn: Option<fn(&S, usize) -> Vec<usize>>, route_set_fn: Option<fn(&mut S, usize, Vec<usize>)>, route_depot_fn: Option<fn(&S, usize) -> usize>, route_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, route_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, savings_depot_fn: Option<fn(&S, usize) -> usize>, savings_metric_class_fn: Option<fn(&S, usize) -> usize>, savings_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, savings_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, ) -> Self
ListVariableMetadata: pub fn with_savings_feasible_shared_by_metric_class(self, shared: bool) -> Self
ListVariableMetadata: pub fn with_element_owner_fn( self, element_owner_fn: Option<fn(&S, &usize) -> Option<usize>>, ) -> Self
struct ListVariableSlot: pub struct ListVariableSlot<S, V, DM, IDM> {Show 33 fields pub entity_type_name: &'static str, pub element_count: fn(&S) -> usize, pub assigned_elements: fn(&S) -> Vec<V>, pub list_len: fn(&S, usize) -> usize, pub list_remove: fn(&mut S, usize, usize) -> Option<V>, pub list_insert: fn(&mut S, usize, usize, V), pub list_get: fn(&S, usize, usize) -> Option<V>, pub list_set: fn(&mut S, usize, usize, V), pub list_reverse: fn(&mut S, usize, usize, usize), pub sublist_remove: fn(&mut S, usize, usize, usize) -> Vec<V>, pub sublist_insert: fn(&mut S, usize, usize, Vec<V>), pub ruin_remove: fn(&mut S, usize, usize) -> V, pub ruin_insert: fn(&mut S, usize, usize, V), pub index_to_element: fn(&S, usize) -> V, pub element_source_key: fn(&S, &V) -> usize, pub entity_count: fn(&S) -> usize, pub cross_distance_meter: DM, pub intra_distance_meter: IDM, pub variable_name: &'static str, pub descriptor_index: usize, pub route_get_fn: Option<fn(&S, usize) -> Vec<usize>>, pub route_set_fn: Option<fn(&mut S, usize, Vec<usize>)>, pub route_depot_fn: Option<fn(&S, usize) -> usize>, pub route_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, pub route_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, pub savings_depot_fn: Option<fn(&S, usize) -> usize>, pub savings_metric_class_fn: Option<fn(&S, usize) -> usize>, pub savings_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, pub savings_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, pub element_owner_fn: Option<fn(&S, &V) -> Option<usize>>, pub construction_element_order_key: Option<fn(&S, V) -> i64>, pub precedence_duration_fn: Option<fn(&S, V) -> usize>, pub precedence_successors_fn: Option<fn(&S, V, &mut Vec<V>)>, /* private fields */ }
struct ListVariableSlot: pub struct ListVariableSlot<S, V, DM, IDM> {Show 34 fields pub entity_type_name: &'static str, pub element_count: fn(&S) -> usize, pub assigned_elements: fn(&S) -> Vec<V>, pub list_len: fn(&S, usize) -> usize, pub list_remove: fn(&mut S, usize, usize) -> Option<V>, pub list_insert: fn(&mut S, usize, usize, V), pub list_get: fn(&S, usize, usize) -> Option<V>, pub list_set: fn(&mut S, usize, usize, V), pub list_reverse: fn(&mut S, usize, usize, usize), pub sublist_remove: fn(&mut S, usize, usize, usize) -> Vec<V>, pub sublist_insert: fn(&mut S, usize, usize, Vec<V>), pub ruin_remove: fn(&mut S, usize, usize) -> V, pub ruin_insert: fn(&mut S, usize, usize, V), pub index_to_element: fn(&S, usize) -> V, pub element_source_key: fn(&S, &V) -> usize, pub entity_count: fn(&S) -> usize, pub cross_distance_meter: DM, pub intra_distance_meter: IDM, pub variable_name: &'static str, pub descriptor_index: usize, pub route_get_fn: Option<fn(&S, usize) -> Vec<usize>>, pub route_set_fn: Option<fn(&mut S, usize, Vec<usize>)>, pub route_depot_fn: Option<fn(&S, usize) -> usize>, pub route_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, pub route_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, pub savings_depot_fn: Option<fn(&S, usize) -> usize>, pub savings_metric_class_fn: Option<fn(&S, usize) -> usize>, pub savings_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, pub savings_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, pub savings_feasible_shared_by_metric_class: bool, pub element_owner_fn: Option<fn(&S, &V) -> Option<usize>>, pub construction_element_order_key: Option<fn(&S, V) -> i64>, pub precedence_duration_fn: Option<fn(&S, V) -> usize>, pub precedence_successors_fn: Option<fn(&S, V, &mut Vec<V>)>, /* private fields */ }
ListVariableSlot: pub fn new( entity_type_name: &'static str, element_count: fn(&S) -> usize, assigned_elements: fn(&S) -> Vec<V>, list_len: fn(&S, usize) -> usize, list_remove: fn(&mut S, usize, usize) -> Option<V>, list_insert: fn(&mut S, usize, usize, V), list_get: fn(&S, usize, usize) -> Option<V>, list_set: fn(&mut S, usize, usize, V), list_reverse: fn(&mut S, usize, usize, usize), sublist_remove: fn(&mut S, usize, usize, usize) -> Vec<V>, sublist_insert: fn(&mut S, usize, usize, Vec<V>), ruin_remove: fn(&mut S, usize, usize) -> V, ruin_insert: fn(&mut S, usize, usize, V), index_to_element: fn(&S, usize) -> V, element_source_key: fn(&S, &V) -> usize, entity_count: fn(&S) -> usize, cross_distance_meter: DM, intra_distance_meter: IDM, variable_name: &'static str, descriptor_index: usize, route_get_fn: Option<fn(&S, usize) -> Vec<usize>>, route_set_fn: Option<fn(&mut S, usize, Vec<usize>)>, route_depot_fn: Option<fn(&S, usize) -> usize>, route_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, route_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, savings_depot_fn: Option<fn(&S, usize) -> usize>, savings_metric_class_fn: Option<fn(&S, usize) -> usize>, savings_distance_fn: Option<fn(&S, usize, usize, usize) -> i64>, savings_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>, ) -> Self
ListVariableSlot: pub fn with_savings_feasible_shared_by_metric_class(self, shared: bool) -> Self
ListVariableSlot: pub fn with_element_owner_fn( self, element_owner_fn: Option<fn(&S, &V) -> Option<usize>>, ) -> Self
ListVariableSlot: pub fn with_construction_element_order_key( self, construction_element_order_key: Option<fn(&S, V) -> i64>, ) -> Self
ListVariableSlot: pub fn with_precedence_hooks( self, duration_fn: Option<fn(&S, V) -> usize>, successors_fn: Option<fn(&S, V, &mut Vec<V>)>, ) -> Self
Expand Down
12 changes: 12 additions & 0 deletions crates/solverforge-solver/src/builder/context/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ pub struct ListVariableSlot<S, V, DM, IDM> {
/// Whether a candidate route for an entity is feasible during savings
/// construction.
pub savings_feasible_fn: Option<fn(&S, usize, &[usize]) -> bool>,
/// Whether `savings_feasible_fn` answers the same for every entity of one
/// savings metric class; Clarke-Wright then asks one entity per class.
pub savings_feasible_shared_by_metric_class: bool,
/// The entity whose list must hold an element, when the model restricts
/// ownership; `None` from the hook means any entity.
pub element_owner_fn: Option<fn(&S, &V) -> Option<usize>>,
Expand Down Expand Up @@ -127,6 +130,7 @@ impl<S, V, DM: Clone, IDM: Clone> Clone for ListVariableSlot<S, V, DM, IDM> {
savings_metric_class_fn: self.savings_metric_class_fn,
savings_distance_fn: self.savings_distance_fn,
savings_feasible_fn: self.savings_feasible_fn,
savings_feasible_shared_by_metric_class: self.savings_feasible_shared_by_metric_class,
element_owner_fn: self.element_owner_fn,
construction_element_order_key: self.construction_element_order_key,
precedence_duration_fn: self.precedence_duration_fn,
Expand Down Expand Up @@ -202,6 +206,7 @@ impl<S, V, DM, IDM> ListVariableSlot<S, V, DM, IDM> {
savings_metric_class_fn,
savings_distance_fn,
savings_feasible_fn,
savings_feasible_shared_by_metric_class: false,
element_owner_fn: None,
construction_element_order_key: None,
precedence_duration_fn: None,
Expand All @@ -210,6 +215,13 @@ impl<S, V, DM, IDM> ListVariableSlot<S, V, DM, IDM> {
}
}

/// Declares whether `savings_feasible_fn` answers the same for every
/// entity of one savings metric class.
pub fn with_savings_feasible_shared_by_metric_class(mut self, shared: bool) -> Self {
self.savings_feasible_shared_by_metric_class = shared;
self
}

/// Sets the element ownership hook.
pub fn with_element_owner_fn(
mut self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ impl<S, V, DM, IDM> RuntimeListSlot<S, V, DM, IDM> {
}
}

/// Whether savings feasibility is declared shared by every owner of one
/// savings metric class. Host-language metadata never declares it.
pub(crate) fn savings_feasible_shared_by_metric_class(&self) -> bool {
match self {
Self::Static { route_bindings, .. } => {
route_bindings.savings_feasible_shared_by_metric_class
}
Self::Dynamic(_) => false,
}
}

pub(crate) fn route_read_policy(&self) -> RouteReadPolicy {
match self {
Self::Static { route_bindings, .. } => route_bindings.read_policy,
Expand Down
Loading
Loading