use std::fmt::Debug;
use std::marker::PhantomData;
use smallvec::{smallvec, SmallVec};
use solverforge_core::domain::PlanningSolution;
use solverforge_scoring::Director;
use super::metadata::{
encode_option_debug, encode_usize, hash_str, MoveTabuScope, ScopedEntityTabuToken,
ScopedValueTabuToken,
};
use super::segment_layout::derive_segment_relocation_layout;
use super::{Move, MoveTabuSignature};
pub struct SublistChangeMove<S, V> {
source_entity_index: usize,
source_start: usize,
source_end: usize,
dest_entity_index: usize,
dest_position: usize,
list_len: fn(&S, usize) -> usize,
list_get: fn(&S, usize, usize) -> Option<V>,
sublist_remove: fn(&mut S, usize, usize, usize) -> Vec<V>,
sublist_insert: fn(&mut S, usize, usize, Vec<V>),
variable_name: &'static str,
descriptor_index: usize,
indices: [usize; 2],
_phantom: PhantomData<fn() -> V>,
}
impl<S, V> Clone for SublistChangeMove<S, V> {
fn clone(&self) -> Self {
*self
}
}
impl<S, V> Copy for SublistChangeMove<S, V> {}
impl<S, V: Debug> Debug for SublistChangeMove<S, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SublistChangeMove")
.field("source_entity", &self.source_entity_index)
.field("source_range", &(self.source_start..self.source_end))
.field("dest_entity", &self.dest_entity_index)
.field("dest_position", &self.dest_position)
.field("variable_name", &self.variable_name)
.finish()
}
}
impl<S, V> SublistChangeMove<S, V> {
#[allow(clippy::too_many_arguments)]
pub fn new(
source_entity_index: usize,
source_start: usize,
source_end: usize,
dest_entity_index: usize,
dest_position: usize,
list_len: fn(&S, usize) -> usize,
list_get: fn(&S, usize, usize) -> Option<V>,
sublist_remove: fn(&mut S, usize, usize, usize) -> Vec<V>,
sublist_insert: fn(&mut S, usize, usize, Vec<V>),
variable_name: &'static str,
descriptor_index: usize,
) -> Self {
Self {
source_entity_index,
source_start,
source_end,
dest_entity_index,
dest_position,
list_len,
list_get,
sublist_remove,
sublist_insert,
variable_name,
descriptor_index,
indices: [source_entity_index, dest_entity_index],
_phantom: PhantomData,
}
}
pub fn source_entity_index(&self) -> usize {
self.source_entity_index
}
pub fn source_start(&self) -> usize {
self.source_start
}
pub fn source_end(&self) -> usize {
self.source_end
}
pub fn sublist_len(&self) -> usize {
self.source_end.saturating_sub(self.source_start)
}
pub fn dest_entity_index(&self) -> usize {
self.dest_entity_index
}
pub fn dest_position(&self) -> usize {
self.dest_position
}
pub fn is_intra_list(&self) -> bool {
self.source_entity_index == self.dest_entity_index
}
}
impl<S, V> Move<S> for SublistChangeMove<S, V>
where
S: PlanningSolution,
V: Clone + Send + Sync + Debug + 'static,
{
fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
let solution = score_director.working_solution();
if self.source_start >= self.source_end {
return false;
}
let source_len = (self.list_len)(solution, self.source_entity_index);
if self.source_end > source_len {
return false;
}
let dest_len = (self.list_len)(solution, self.dest_entity_index);
let sublist_len = self.sublist_len();
let max_dest = if self.is_intra_list() {
source_len - sublist_len
} else {
dest_len
};
if self.dest_position > max_dest {
return false;
}
if self.is_intra_list() {
if self.dest_position == self.source_start {
return false;
}
}
true
}
fn do_move<D: Director<S>>(&self, score_director: &mut D) {
let layout = derive_segment_relocation_layout(
self.source_entity_index,
self.source_start,
self.source_end,
self.dest_entity_index,
self.dest_position,
);
score_director.before_variable_changed(self.descriptor_index, self.source_entity_index);
if !self.is_intra_list() {
score_director.before_variable_changed(self.descriptor_index, self.dest_entity_index);
}
let elements = (self.sublist_remove)(
score_director.working_solution_mut(),
self.source_entity_index,
self.source_start,
self.source_end,
);
let dest_pos = layout.exact.dest_position;
(self.sublist_insert)(
score_director.working_solution_mut(),
self.dest_entity_index,
dest_pos,
elements,
);
score_director.after_variable_changed(self.descriptor_index, self.source_entity_index);
if !self.is_intra_list() {
score_director.after_variable_changed(self.descriptor_index, self.dest_entity_index);
}
let sublist_remove = self.sublist_remove;
let sublist_insert = self.sublist_insert;
let inverse = layout.inverse;
score_director.register_undo(Box::new(move |s: &mut S| {
let removed = sublist_remove(
s,
inverse.source_entity_index,
inverse.source_range.start,
inverse.source_range.end,
);
sublist_insert(s, inverse.dest_entity_index, inverse.dest_position, removed);
}));
}
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 tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
let layout = derive_segment_relocation_layout(
self.source_entity_index,
self.source_start,
self.source_end,
self.dest_entity_index,
self.dest_position,
);
let mut moved_ids: SmallVec<[u64; 2]> = SmallVec::new();
for pos in self.source_start..self.source_end {
let value = (self.list_get)(
score_director.working_solution(),
self.source_entity_index,
pos,
);
moved_ids.push(encode_option_debug(value.as_ref()));
}
let source_entity_id = encode_usize(self.source_entity_index);
let dest_entity_id = encode_usize(self.dest_entity_index);
let variable_id = hash_str(self.variable_name);
let scope = MoveTabuScope::new(self.descriptor_index, self.variable_name);
let mut entity_tokens: SmallVec<[ScopedEntityTabuToken; 2]> =
smallvec![scope.entity_token(source_entity_id)];
if !self.is_intra_list() {
entity_tokens.push(scope.entity_token(dest_entity_id));
}
let destination_value_tokens: SmallVec<[ScopedValueTabuToken; 2]> = moved_ids
.iter()
.copied()
.map(|value_id| scope.value_token(value_id))
.collect();
let mut move_id = smallvec![
encode_usize(self.descriptor_index),
variable_id,
source_entity_id,
encode_usize(layout.exact.source_range.start),
encode_usize(layout.exact.source_range.end),
dest_entity_id,
encode_usize(layout.exact.dest_position)
];
move_id.extend(moved_ids.iter().copied());
let mut undo_move_id = smallvec![
encode_usize(self.descriptor_index),
variable_id,
encode_usize(layout.inverse.source_entity_index),
encode_usize(layout.inverse.source_range.start),
encode_usize(layout.inverse.source_range.end),
encode_usize(layout.inverse.dest_entity_index),
encode_usize(layout.inverse.dest_position)
];
undo_move_id.extend(moved_ids.iter().copied());
MoveTabuSignature::new(scope, move_id, undo_move_id)
.with_entity_tokens(entity_tokens)
.with_destination_value_tokens(destination_value_tokens)
}
}