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
8 changes: 6 additions & 2 deletions crates/solverforge-solver/WIREFRAME.md
Original file line number Diff line number Diff line change
Expand Up @@ -974,9 +974,13 @@ carry a `reason` attribute naming the model declaration that selected them
(`savings_hooks_declared`, `precedence_construction_order_and_ownership_declared`,
`precedence_and_construction_order_declared`, `no_specialized_list_policy_declared`,
`required_assignments_remain`, `only_optional_assignments_remain`,
`scalar_slot_outside_assignment_groups`, `route_hooks_declared_and_route_non_empty`).
`scalar_slot_outside_assignment_groups`, `route_hooks_declared_and_route_non_empty`,
`element_groups_declared`, `list_elements_remain_unassigned`). The optional
assignment step of a time-limited solve also carries `time_budget_seconds`,
the share of the time limit it was allowed.
The default local-search node carries acceptor, forager,
`forager_accepted_count_limit`, `selection_order`, `weighting` and
`forager_accepted_count_limit`, `selection_order`, `weighting` (`equal`, or
`fixed:[...]` with one cost weight per selector) and
`excluded_families` (`family@slot:missing=capability+...`, or `none`); an
explicit local-search node carries the configured or inherited acceptor and
forager plus the effective `forager_accepted_count_limit` (`none` for variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,16 @@ fn run_clarke_wright_in_phase<S, A, D, BestCb>(
};

if matched_count < assignable_routes.len() && completion_routes.is_none() {
tracing::warn!(
// The matched routes stay: a route the fleet cannot take leaves its
// elements unassigned for a completing construction, instead of
// discarding every route that did fit.
tracing::info!(
constructed_routes = constructed_route_count,
owner_ineligible_routes,
available_slots = available_entity_slots.len(),
matched_routes = matched_count,
"ListClarkeWright could not match every constructed route to a feasible empty entity"
"ListClarkeWright committed the routes it could match to a feasible empty entity"
);
pending_move_telemetry.record_discarded(phase_scope);
phase_scope.calculate_score();
return;
}

if let Some(completed_routes) = completion_routes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ pub(crate) enum DefaultLocalSearchSelectorFamily {
}

impl DefaultLocalSearchSelectorFamily {
/// Share of the default union's candidate draws. Ruin-and-recreate and
/// unbounded k-opt rebuild or scan whole routes per candidate (48 ms per
/// ruin move at 10k stops), so they draw a quarter as often as the
/// families whose candidates touch a few positions.
pub(crate) fn cost_weight(self) -> u64 {
match self {
Self::UnboundedKOpt | Self::ListRuin => 1,
_ => 4,
}
}

pub(crate) fn trace_label(self) -> &'static str {
match self {
Self::ListGroupChange => "list_group_change",
Expand Down Expand Up @@ -176,6 +187,22 @@ pub(crate) struct DefaultLocalSearchPlan {
pub excluded: Vec<DefaultExcludedFamily>,
}

impl DefaultLocalSearchPlan {
/// One cost weight per selector, or `None` when every family weighs the
/// same and the union stays equally weighted.
pub(crate) fn family_weights(&self) -> Option<Vec<u64>> {
let weights: Vec<u64> = self
.selectors
.iter()
.map(|selector| selector.family.cost_weight())
.collect();
weights
.windows(2)
.any(|pair| pair[0] != pair[1])
.then_some(weights)
}
}

pub(super) fn target_for(slot: &RuntimeScalarSlotId) -> VariableTargetConfig {
VariableTargetConfig {
entity_class: Some(slot.entity_class.to_string()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ mod list;
mod scalar;

const DEFAULT_LOCAL_SEARCH_LATE_ACCEPTANCE_SIZE: usize = 400;
const DEFAULT_LOCAL_SEARCH_ACCEPTED_COUNT: usize = 256;
/// Accepted moves a list-variable step collects before it picks one. Few
/// accepted moves per step buy many more steps: on the CVRP bench, 4 beats
/// 32 and 256 at every instance size.
const DEFAULT_LIST_ACCEPTED_COUNT: usize = 4;
/// Accepted moves a scalar step with grouped, nearby or repair moves
/// collects. On the rostering bench, 32 beats both 4 and 256.
const DEFAULT_SCALAR_ACCEPTED_COUNT: usize = 32;
/// The accepted-count ceiling under the precedence forager: on job shop, 32
/// beats both 4 and 256.
const DEFAULT_PRECEDENCE_ACCEPTED_COUNT: usize = 32;
const DEFAULT_SIMULATED_ANNEALING_DECAY_RATE: f64 = 0.999_985;

pub(crate) fn compile_default_local_search_components<S, V, DM, IDM>(
Expand Down Expand Up @@ -63,16 +72,18 @@ where

let forager = if has_groups && !has_lists {
DefaultLocalSearchForagerPolicy::AcceptedCount {
limit: DEFAULT_LOCAL_SEARCH_ACCEPTED_COUNT,
limit: DEFAULT_SCALAR_ACCEPTED_COUNT,
}
} else if has_precedence {
DefaultLocalSearchForagerPolicy::FirstLastStepScoreImproving {
accepted_count_limit: Some(DEFAULT_LOCAL_SEARCH_ACCEPTED_COUNT),
accepted_count_limit: Some(DEFAULT_PRECEDENCE_ACCEPTED_COUNT),
}
} else {
DefaultLocalSearchForagerPolicy::AcceptedCount {
limit: if has_lists || has_nearby_scalar || has_conflict_repairs {
DEFAULT_LOCAL_SEARCH_ACCEPTED_COUNT
limit: if has_lists {
DEFAULT_LIST_ACCEPTED_COUNT
} else if has_nearby_scalar || has_conflict_repairs {
DEFAULT_SCALAR_ACCEPTED_COUNT
} else {
1
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ impl DefaultLocalSearchPlan {
selection_order_label(self.selection_order).to_string(),
),
("selector_count", self.selectors.len().to_string()),
// Every default union weighs its families equally.
("weighting", "equal".to_string()),
(
"weighting",
self.family_weights().map_or_else(
|| "equal".to_string(),
|weights| format!("fixed:{weights:?}"),
),
),
("excluded_families", self.excluded_families_signature()),
];
match self.components.acceptor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ fn typed_and_dynamic_scalar_capabilities_have_identical_default_policy() {
);
assert_eq!(
typed.components.forager,
DefaultLocalSearchForagerPolicy::AcceptedCount { limit: 256 }
DefaultLocalSearchForagerPolicy::AcceptedCount { limit: 32 }
);
assert_eq!(
trace_value(&typed, "simulated_annealing_decay_rate_bits"),
Expand All @@ -392,7 +392,7 @@ fn typed_and_dynamic_scalar_capabilities_have_identical_default_policy() {
);
assert_eq!(
trace_value(&typed, "forager_accepted_count_limit"),
Some("256".to_string())
Some("32".to_string())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn typed_and_dynamic_assignment_groups_hide_scalar_leaves_and_keep_group_policy(
);
assert_eq!(
plan.components.forager,
DefaultLocalSearchForagerPolicy::AcceptedCount { limit: 256 }
DefaultLocalSearchForagerPolicy::AcceptedCount { limit: 32 }
);
assert!(plan.candidate_trace_plan().is_complete());
}
Expand Down
2 changes: 2 additions & 0 deletions crates/solverforge-solver/src/runtime/compiler/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ use super::default_local_search::{
use super::slots::{matching_list_slots, resolved_scalar_bindings};
use super::types::{CompiledListSlot, CompiledScalarSlot, RuntimeCompileError};

mod list_completion;
mod stages;

pub(crate) use list_completion::resolve_default_list_completion;
pub(crate) use stages::{
resolve_default_postconstruction_kopt, resolve_default_preconstruction_stage,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//! The list completion stage of the omitted-construction profile.

use std::fmt;

use solverforge_config::{ConstructionHeuristicConfig, ConstructionHeuristicType};
use solverforge_core::domain::PlanningSolution;

use crate::heuristic::selector::nearby_list_change::CrossEntityDistanceMeter;

use super::super::graph::{CompiledConstruction, ListConstructionKind};
use super::super::types::CompiledListSlot;
use super::stages::{
list_config, list_policies, DefaultConstructionReason, DefaultConstructionStage,
DefaultConstructionStepKind, DefaultPreconstructionStage, ResolvedDefaultConstructionPlan,
ResolvedDefaultConstructionStep,
};
use super::DefaultRuntimeBindings;

/// Resolves the list completion stage: one list cheapest insertion for every
/// list slot whose `needs_completion` entry is true, in binding order.
pub(crate) fn resolve_default_list_completion<S, V, DM, IDM>(
bindings: &DefaultRuntimeBindings<S, V, DM, IDM>,
needs_completion: &[bool],
) -> ResolvedDefaultConstructionPlan<S, V, DM, IDM>
where
S: PlanningSolution + Clone + Send + Sync + 'static,
V: Clone + PartialEq + Send + Sync + fmt::Debug + 'static,
DM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
IDM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
{
let steps = bindings
.list_slots
.iter()
.zip(needs_completion)
.filter(|(_, &needs)| needs)
.map(|(slot, _)| list_completion_step(slot))
.collect();
ResolvedDefaultConstructionPlan {
stage: DefaultConstructionStage::Preconstruction(
DefaultPreconstructionStage::ListCompletion,
),
steps,
}
}

fn list_completion_step<S, V, DM, IDM>(
slot: &CompiledListSlot<S, V, DM, IDM>,
) -> ResolvedDefaultConstructionStep<S, V, DM, IDM>
where
S: PlanningSolution + Clone + Send + Sync + 'static,
V: Clone + PartialEq + Send + Sync + fmt::Debug + 'static,
DM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
IDM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
{
let heuristic = ConstructionHeuristicType::ListCheapestInsertion;
let target = slot.identity();
ResolvedDefaultConstructionStep {
kind: DefaultConstructionStepKind::ListCompletion,
reason: DefaultConstructionReason::ListElementsRemainUnassigned,
construction: CompiledConstruction::List {
kind: ListConstructionKind::CheapestInsertion,
config: ConstructionHeuristicConfig {
max_group_owners: slot
.element_group_policy()
.is_explicit()
.then_some(crate::list_groups::DEFAULT_GROUP_OWNERS),
..list_config(heuristic, &target)
},
slots: vec![slot.clone()],
},
required_only: false,
target: Some(target),
list_policies: Some(list_policies(slot)),
}
}
37 changes: 34 additions & 3 deletions crates/solverforge-solver/src/runtime/compiler/defaults/stages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ use super::{DefaultAssignmentBinding, DefaultRuntimeBindings, DefaultScalarBindi
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum DefaultPreconstructionStage {
ListConstruction,
/// Places the list elements the primary list construction left
/// unassigned. Resolved by the executor, which knows each slot's
/// remaining source work.
ListCompletion,
AssignmentRequired,
AssignmentOptional,
ScalarFirstFit,
}

impl DefaultPreconstructionStage {
pub(crate) const ORDERED: [Self; 4] = [
pub(crate) const ORDERED: [Self; 5] = [
Self::ListConstruction,
Self::ListCompletion,
Self::AssignmentRequired,
Self::AssignmentOptional,
Self::ScalarFirstFit,
Expand All @@ -57,6 +62,9 @@ pub(crate) enum DefaultConstructionStage {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum DefaultConstructionStepKind {
ListConstruction(ConstructionHeuristicType),
/// List cheapest insertion over the elements the primary list
/// construction left unassigned.
ListCompletion,
AssignmentRequired,
AssignmentOptional,
ScalarFirstFit,
Expand All @@ -77,6 +85,8 @@ pub(crate) enum DefaultConstructionReason {
PrecedenceAndOrder,
/// The list slot declares none of the specialized construction policies.
NoSpecializedListPolicy,
/// List elements remain unassigned after the primary list construction.
ListElementsRemainUnassigned,
/// The assignment group still has required rows unassigned.
RequiredAssignmentsRemain,
/// Every required row is assigned; optional rows remain.
Expand All @@ -97,6 +107,7 @@ impl DefaultConstructionReason {
}
Self::PrecedenceAndOrder => "precedence_and_construction_order_declared",
Self::NoSpecializedListPolicy => "no_specialized_list_policy_declared",
Self::ListElementsRemainUnassigned => "list_elements_remain_unassigned",
Self::RequiredAssignmentsRemain => "required_assignments_remain",
Self::OnlyOptionalAssignmentsRemain => "only_optional_assignments_remain",
Self::UngroupedScalarSlot => "scalar_slot_outside_assignment_groups",
Expand Down Expand Up @@ -154,6 +165,9 @@ where
.iter()
.map(list_construction_step)
.collect(),
DefaultPreconstructionStage::ListCompletion => {
unreachable!("the executor resolves list completion from each slot's source work")
}
DefaultPreconstructionStage::AssignmentRequired => bindings
.assignment_groups
.iter()
Expand Down Expand Up @@ -236,6 +250,7 @@ where
config: ConstructionHeuristicConfig {
max_group_owners: (reason == DefaultConstructionReason::ElementGroupsDeclared)
.then_some(crate::list_groups::DEFAULT_GROUP_OWNERS),
savings_neighbors: default_savings_neighbors(slot, heuristic),
..list_config(heuristic, &target)
},
slots: vec![slot.clone()],
Expand Down Expand Up @@ -306,6 +321,22 @@ where
(0..slot.entity_count(solution)).any(|entity| slot.list_len(solution, entity) > 0)
}

/// Savings each element keeps in a default Clarke-Wright construction whose
/// slot declares a savings neighbour source: the savings list then grows
/// linearly with the element count instead of with its square.
const DEFAULT_SAVINGS_NEIGHBORS: std::num::NonZeroU32 = std::num::NonZeroU32::new(40).unwrap();

fn default_savings_neighbors<S, V, DM, IDM>(
slot: &CompiledListSlot<S, V, DM, IDM>,
heuristic: ConstructionHeuristicType,
) -> Option<std::num::NonZeroU32> {
use crate::builder::context::list_access::ListAccessCapability;

(heuristic == ConstructionHeuristicType::ListClarkeWright
&& slot.supports(ListAccessCapability::SavingsNeighbors))
.then_some(DEFAULT_SAVINGS_NEIGHBORS)
}

fn default_list_construction_heuristic<S, V, DM, IDM>(
slot: &CompiledListSlot<S, V, DM, IDM>,
) -> (ConstructionHeuristicType, DefaultConstructionReason) {
Expand Down Expand Up @@ -395,7 +426,7 @@ fn scalar_first_fit_config(slot: &RuntimeScalarSlotId) -> ConstructionHeuristicC
}
}

fn list_config(
pub(super) fn list_config(
heuristic: ConstructionHeuristicType,
slot: &RuntimeScalarSlotId,
) -> ConstructionHeuristicConfig {
Expand All @@ -413,7 +444,7 @@ fn target_config(slot: &RuntimeScalarSlotId) -> VariableTargetConfig {
}
}

fn list_policies<S, V, DM, IDM>(
pub(super) fn list_policies<S, V, DM, IDM>(
slot: &CompiledListSlot<S, V, DM, IDM>,
) -> DefaultListPolicyProvenance {
DefaultListPolicyProvenance {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ fn equivalent_typed_and_dynamic_cvrp_capabilities_freeze_the_same_default_local_
);
assert_eq!(
typed.components.forager,
DefaultLocalSearchForagerPolicy::AcceptedCount { limit: 256 }
DefaultLocalSearchForagerPolicy::AcceptedCount { limit: 4 }
);
assert_eq!(
typed.family_weights(),
Some(vec![4, 4, 4, 4, 4, 4, 1]),
"ruin keeps its family but draws a quarter as often"
);
let typed_excluded = typed
.excluded
Expand Down
Loading
Loading