use std::fmt::Debug;
use solverforge_core::domain::PlanningSolution;
use solverforge_scoring::Director;
use super::list_kernel::{
swap_candidate_trace_identity, swap_do_move, swap_is_doable, swap_tabu_signature,
StaticListSwapAccess, SwapCoordinates,
};
use super::{Move, MoveTabuSignature};
pub struct ListSwapMove<S, V> {
first_entity_index: usize,
first_position: usize,
second_entity_index: usize,
second_position: usize,
list_len: fn(&S, usize) -> usize,
list_get: fn(&S, usize, usize) -> Option<V>,
list_set: fn(&mut S, usize, usize, V),
variable_name: &'static str,
descriptor_index: usize,
indices: [usize; 2],
}
impl<S, V> Clone for ListSwapMove<S, V> {
fn clone(&self) -> Self {
*self
}
}
impl<S, V> Copy for ListSwapMove<S, V> {}
impl<S, V: Debug> Debug for ListSwapMove<S, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ListSwapMove")
.field("first_entity", &self.first_entity_index)
.field("first_position", &self.first_position)
.field("second_entity", &self.second_entity_index)
.field("second_position", &self.second_position)
.field("variable_name", &self.variable_name)
.finish()
}
}
impl<S, V> ListSwapMove<S, V> {
#[allow(clippy::too_many_arguments)]
pub fn new(
first_entity_index: usize,
first_position: usize,
second_entity_index: usize,
second_position: usize,
list_len: fn(&S, usize) -> usize,
list_get: fn(&S, usize, usize) -> Option<V>,
list_set: fn(&mut S, usize, usize, V),
variable_name: &'static str,
descriptor_index: usize,
) -> Self {
Self {
first_entity_index,
first_position,
second_entity_index,
second_position,
list_len,
list_get,
list_set,
variable_name,
descriptor_index,
indices: [first_entity_index, second_entity_index],
}
}
pub fn first_entity_index(&self) -> usize {
self.first_entity_index
}
pub fn first_position(&self) -> usize {
self.first_position
}
pub fn second_entity_index(&self) -> usize {
self.second_entity_index
}
pub fn second_position(&self) -> usize {
self.second_position
}
pub fn is_intra_list(&self) -> bool {
self.first_entity_index == self.second_entity_index
}
fn access(&self) -> StaticListSwapAccess<S, V> {
StaticListSwapAccess {
list_len: self.list_len,
list_get: self.list_get,
list_set: self.list_set,
variable_name: self.variable_name,
descriptor_index: self.descriptor_index,
}
}
fn coordinates(&self) -> SwapCoordinates {
SwapCoordinates {
first_entity: self.first_entity_index,
first_position: self.first_position,
second_entity: self.second_entity_index,
second_position: self.second_position,
}
}
}
impl<S, V> Move<S> for ListSwapMove<S, V>
where
S: PlanningSolution,
V: Clone + PartialEq + Send + Sync + Debug + 'static,
{
type Undo = ();
fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
swap_is_doable(&self.access(), self.coordinates(), score_director)
}
fn do_move<D: Director<S>>(&self, score_director: &mut D) -> Self::Undo {
swap_do_move(&self.access(), self.coordinates(), score_director);
}
fn undo_move<D: Director<S>>(&self, score_director: &mut D, (): Self::Undo) {
swap_do_move(&self.access(), self.coordinates(), score_director);
}
fn descriptor_index(&self) -> usize {
self.descriptor_index
}
fn entity_indices(&self) -> &[usize] {
if self.is_intra_list() {
&self.indices[0..1]
} else {
&self.indices
}
}
fn variable_name(&self) -> &str {
self.variable_name
}
fn telemetry_label(&self) -> &'static str {
"list_swap"
}
fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
swap_tabu_signature(&self.access(), self.coordinates(), score_director)
}
fn candidate_trace_identity(&self) -> Option<crate::stats::CandidateTraceIdentity> {
Some(swap_candidate_trace_identity(
&self.access(),
self.coordinates(),
))
}
}