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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ struct CartesianPair {
tabu_signature: MoveTabuSignature,
}

/// How many rows and pairs a cartesian cursor held when it closed.
#[derive(Clone, Copy, Default)]
pub(super) struct CartesianSizes {
rows: usize,
pairs: usize,
}

/// Cartesian payloads own selected child carriers; graph edges are stable IDs.
/// Right cursors open only after left preview application, using the same arena.
pub(super) struct SelectorCompositionCartesianCursor<S, M>
Expand All @@ -64,17 +71,25 @@ where
left: CompositionNodeId,
right: CompositionNodeId,
context: MoveStreamContext,
sizes: CartesianSizes,
) -> Self {
Self {
require_hard_improvement,
left,
right,
rows: Vec::new(),
rows: Vec::with_capacity(sizes.rows),
active_row: None,
pairs: Vec::new(),
pairs: Vec::with_capacity(sizes.pairs),
context,
}
}

pub(super) fn sizes(&self) -> CartesianSizes {
CartesianSizes {
rows: self.rows.len(),
pairs: self.pairs.len(),
}
}
pub(super) fn left(&self) -> CompositionNodeId {
self.left
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Indexed mutable cursor arena over one immutable declaration arena.

use super::cartesian::SelectorCompositionCartesianCursor;
use super::cartesian::{CartesianSizes, SelectorCompositionCartesianCursor};
use super::{
ComposedFlat, CompositionNodeId, SelectorComposition, SelectorCompositionNode,
SequentialMoveCarrier, StatefulComposedFlat,
Expand All @@ -20,6 +20,10 @@ use union::SelectorCompositionUnionCursor;
/// duplicate state graph; their edges remain in the immutable declaration.
pub(super) struct SelectorCompositionStreamState<FlatState> {
pub(super) leaves: Vec<Option<FlatState>>,
/// Per cartesian node, the row and pair counts its last cursor reached,
/// so the next step's cursor allocates its row and pair lists once at
/// that size instead of regrowing them pull by pull.
pub(super) cartesian_sizes: Vec<CartesianSizes>,
}

pub(super) fn new_stream_state<S, Flat, FlatState>(
Expand All @@ -39,6 +43,7 @@ where
_ => None,
})
.collect(),
cartesian_sizes: vec![CartesianSizes::default(); selector.nodes.len()],
}
}

Expand Down Expand Up @@ -115,6 +120,11 @@ where
}
}
drop(this.flats);
for (id, node) in this.nodes.iter().enumerate() {
if let Some(CursorNode::Cartesian(cursor)) = node {
this.stream_state.cartesian_sizes[id] = cursor.sizes();
}
}
drop(this.nodes);
this.stream_state
}
Expand Down Expand Up @@ -193,7 +203,11 @@ where
self.validate_cursor(right, director);
self.open_node(left, resources, director, context);
CursorNode::Cartesian(SelectorCompositionCartesianCursor::new(
require, left, right, context,
require,
left,
right,
context,
self.stream_state.cartesian_sizes[id.index()],
))
}
};
Expand All @@ -218,6 +232,7 @@ where
}
}
CursorNode::Cartesian(cursor) => {
self.stream_state.cartesian_sizes[id.index()] = cursor.sizes();
self.close_node(cursor.left());
self.close_node(cursor.right());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ where
}
}

pub(crate) fn child_cursors_mut(&mut self) -> &mut [C] {
&mut self.cursors
}

/// Reuses `pool`'s capacity for the discovered-candidate table.
pub(crate) fn with_discovered_pool(mut self, mut pool: Vec<(usize, CandidateId)>) -> Self {
pool.clear();
self.discovered = pool;
self
}

/// Hands the discovered-candidate table back, emptied, for the next open.
pub(crate) fn take_discovered_pool(&mut self) -> Vec<(usize, CandidateId)> {
let mut pool = std::mem::take(&mut self.discovered);
pool.clear();
pool
}

fn push_discovered(&mut self, cursor_index: usize, child_index: CandidateId) -> CandidateId {
let global_id = CandidateId::new(self.discovered.len());
self.discovered.push((cursor_index, child_index));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ where
S: PlanningSolution,
E: ChangeEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
entities: Vec<usize>,
route_lens: Vec<usize>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
S: PlanningSolution,
E: MultiRuinEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
probe: P,
rng: SmallRng,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ where
S: PlanningSolution,
E: GroupChangeEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
groups: Vec<GroupSnapshot>,
entity_lens: Vec<usize>,
Expand Down Expand Up @@ -266,7 +266,7 @@ where
S: PlanningSolution,
E: GroupSwapEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
groups: Vec<GroupSnapshot>,
owners: Vec<Option<usize>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ where
S: PlanningSolution,
E: KOptEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
entity_lens: Vec<(usize, usize)>,
entity_offset: usize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ where
E: KOptEmitter<S>,
P: KOptDistanceProbe<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
distance: P,
entity_lens: Vec<(usize, usize)>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
E: ChangeEmitter<S>,
P: NearbyChangeProbe<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
probe: P,
entities: Vec<usize>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
E: SwapEmitter<S>,
P: NearbySwapProbe<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
probe: P,
entities: Vec<usize>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
S: PlanningSolution,
E: PermuteEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
entities: Vec<usize>,
route_lens: Vec<usize>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ where
S: PlanningSolution,
E: PrecedenceEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
blocks: Vec<CriticalBlock>,
route_graph: PrecedenceRouteGraph,
context: MoveStreamContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ where
S: PlanningSolution,
E: ReverseEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
entities: Vec<usize>,
route_lens: Vec<usize>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ where
S: PlanningSolution,
E: RuinEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
rng: SmallRng,
source_pool: RuinSourcePool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
S: PlanningSolution,
E: SublistChangeEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
entities: Vec<usize>,
route_lens: Vec<usize>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ where
S: PlanningSolution,
E: SublistSwapEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
entities: Vec<usize>,
route_lens: Vec<usize>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
S: PlanningSolution,
E: SwapEmitter<S>,
{
store: CandidateStore<S, E::Move>,
pub(crate) store: CandidateStore<S, E::Move>,
emitter: E,
entities: Vec<usize>,
route_lens: Vec<usize>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use solverforge_scoring::Director;
use crate::builder::context::list_access::ListAccess;
use crate::heuristic::selector::decorator::VecUnionMoveCursor;
use crate::heuristic::selector::move_selector::{
CandidateId, MoveCandidateRef, MoveCursor, MoveStreamContext,
CandidateId, CandidateStore, MoveCandidateRef, MoveCursor, MoveStreamContext,
};
use crate::heuristic::selector::nearby_list_change::CrossEntityDistanceMeter;
use crate::heuristic::selector::seed::scoped_seed;
Expand Down Expand Up @@ -101,6 +101,15 @@ where
)
})
.collect::<Vec<_>>();
let mut slot_cursors = slot_cursors;
for (cursor, positions) in slot_cursors
.iter_mut()
.zip(stream_state.slot_positions_mut(self.plan.slots.len()))
{
if let Some(store) = cursor.store_mut() {
*store = CandidateStore::with_position_pool(std::mem::take(positions));
}
}
let slot_count = slot_cursors.len();
let selection_order = match context.selection_order() {
SelectionOrder::Random | SelectionOrder::Shuffled => {
Expand All @@ -116,7 +125,8 @@ where
selection_order,
context,
vec![1; slot_count],
),
)
.with_discovered_pool(std::mem::take(&mut stream_state.discovered)),
}
}
}
Expand All @@ -126,6 +136,11 @@ where
/// interior mutability on the selector definition.
pub(crate) struct RuntimeListNeighborhoodStreamState {
ruin_rngs: Vec<SmallRng>,
/// Each slot kernel's candidate-ID table, handed back when the step's
/// cursor closes so the next step's kernel does not regrow it.
slot_positions: Vec<Vec<Option<usize>>>,
/// The slot union's discovered-candidate table, likewise.
discovered: Vec<(usize, CandidateId)>,
}

impl RuntimeListNeighborhoodStreamState {
Expand Down Expand Up @@ -155,7 +170,55 @@ impl RuntimeListNeighborhoodStreamState {
.collect(),
_ => Vec::new(),
};
Self { ruin_rngs }
Self {
ruin_rngs,
slot_positions: plan.slots.iter().map(|_| Vec::new()).collect(),
discovered: Vec::new(),
}
}

fn slot_positions_mut(&mut self, expected_slots: usize) -> &mut [Vec<Option<usize>>] {
assert_eq!(
self.slot_positions.len(),
expected_slots,
"list stream state must belong to its frozen leaf"
);
&mut self.slot_positions
}

/// Capacity of the slot tables (summed) and of the union table held for
/// the next open.
#[cfg(test)]
pub(crate) fn pool_capacities(&self) -> (usize, usize) {
(
self.slot_positions.iter().map(Vec::capacity).sum(),
self.discovered.capacity(),
)
}

/// Takes the slot kernels' candidate-ID tables back from a closing
/// cursor.
pub(crate) fn reclaim<S, V, DM, IDM>(
&mut self,
cursor: &mut RuntimeListNeighborhoodCursor<'_, S, V, DM, IDM>,
) where
S: PlanningSolution + Clone + Send + Sync + 'static,
S::Score: Score,
V: Clone + PartialEq + Send + Sync + fmt::Debug + 'static,
DM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
IDM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
{
self.discovered = cursor.inner.take_discovered_pool();
for (slot, positions) in cursor
.inner
.child_cursors_mut()
.iter_mut()
.zip(self.slot_positions.iter_mut())
{
if let Some(store) = slot.store_mut() {
*positions = store.take_position_pool();
}
}
}

fn ruin_rngs_mut(&mut self, expected_slots: usize) -> &mut [SmallRng] {
Expand Down
Loading
Loading