pub struct SwapMove<S, V> { /* private fields */ }Expand description
A move that swaps values between two entities.
Stores entity indices and typed function pointers for zero-erasure access.
Undo is handled by RecordingScoreDirector, not by this move.
§Type Parameters
S- The planning solution typeV- The variable value type
§Example
use solverforge_solver::heuristic::r#move::SwapMove;
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SimpleScore;
#[derive(Clone)]
struct Sol { values: Vec<Option<i32>>, score: Option<SimpleScore> }
impl PlanningSolution for Sol {
type Score = SimpleScore;
fn score(&self) -> Option<Self::Score> { self.score }
fn set_score(&mut self, score: Option<Self::Score>) { self.score = score; }
}
// Typed getter/setter with zero erasure
fn get_v(s: &Sol, idx: usize) -> Option<i32> { s.values.get(idx).copied().flatten() }
fn set_v(s: &mut Sol, idx: usize, v: Option<i32>) { if let Some(x) = s.values.get_mut(idx) { *x = v; } }
// Swap values between entities 0 and 1
let swap = SwapMove::<Sol, i32>::new(0, 1, get_v, set_v, "value", 0);Implementations§
Source§impl<S, V> SwapMove<S, V>
impl<S, V> SwapMove<S, V>
Sourcepub fn new(
left_entity_index: usize,
right_entity_index: usize,
getter: fn(&S, usize) -> Option<V>,
setter: fn(&mut S, usize, Option<V>),
variable_name: &'static str,
descriptor_index: usize,
) -> Self
pub fn new( left_entity_index: usize, right_entity_index: usize, getter: fn(&S, usize) -> Option<V>, setter: fn(&mut S, usize, Option<V>), variable_name: &'static str, descriptor_index: usize, ) -> Self
Creates a new swap move with typed function pointers.
§Arguments
left_entity_index- Index of the first entityright_entity_index- Index of the second entitygetter- Typed getter function pointersetter- Typed setter function pointervariable_name- Name of the variable being swappeddescriptor_index- Index in the entity descriptor
Sourcepub fn left_entity_index(&self) -> usize
pub fn left_entity_index(&self) -> usize
Returns the left entity index.
Sourcepub fn right_entity_index(&self) -> usize
pub fn right_entity_index(&self) -> usize
Returns the right entity index.
Trait Implementations§
Source§impl<S, V> Move<S> for SwapMove<S, V>
impl<S, V> Move<S> for SwapMove<S, V>
Source§fn is_doable<D: ScoreDirector<S>>(&self, score_director: &D) -> bool
fn is_doable<D: ScoreDirector<S>>(&self, score_director: &D) -> bool
Returns true if this move can be executed in the current state. Read more
Source§fn do_move<D: ScoreDirector<S>>(&self, score_director: &mut D)
fn do_move<D: ScoreDirector<S>>(&self, score_director: &mut D)
Executes this move, modifying the working solution. Read more
Source§fn descriptor_index(&self) -> usize
fn descriptor_index(&self) -> usize
Returns the descriptor index of the entity type this move affects.
Source§fn entity_indices(&self) -> &[usize]
fn entity_indices(&self) -> &[usize]
Returns the entity indices involved in this move.
Source§fn variable_name(&self) -> &str
fn variable_name(&self) -> &str
Returns the variable name this move affects.
Source§impl<S, V, LES, RES> MoveSelector<S, SwapMove<S, V>> for SwapMoveSelector<S, V, LES, RES>where
S: PlanningSolution,
V: Clone + PartialEq + Send + Sync + Debug + 'static,
LES: EntitySelector<S>,
RES: EntitySelector<S>,
impl<S, V, LES, RES> MoveSelector<S, SwapMove<S, V>> for SwapMoveSelector<S, V, LES, RES>where
S: PlanningSolution,
V: Clone + PartialEq + Send + Sync + Debug + 'static,
LES: EntitySelector<S>,
RES: EntitySelector<S>,
Source§fn iter_moves<'a, D: ScoreDirector<S>>(
&'a self,
score_director: &'a D,
) -> Box<dyn Iterator<Item = SwapMove<S, V>> + 'a>
fn iter_moves<'a, D: ScoreDirector<S>>( &'a self, score_director: &'a D, ) -> Box<dyn Iterator<Item = SwapMove<S, V>> + 'a>
Returns an iterator over typed moves.
Source§fn size<D: ScoreDirector<S>>(&self, score_director: &D) -> usize
fn size<D: ScoreDirector<S>>(&self, score_director: &D) -> usize
Returns the approximate number of moves.
Source§fn is_never_ending(&self) -> bool
fn is_never_ending(&self) -> bool
Returns true if this selector may return the same move multiple times.
impl<S, V> Copy for SwapMove<S, V>
Auto Trait Implementations§
impl<S, V> Freeze for SwapMove<S, V>
impl<S, V> RefUnwindSafe for SwapMove<S, V>
impl<S, V> Send for SwapMove<S, V>
impl<S, V> Sync for SwapMove<S, V>
impl<S, V> Unpin for SwapMove<S, V>
impl<S, V> UnwindSafe for SwapMove<S, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more