use std::fmt::Debug;
use smallvec::SmallVec;
use solverforge_core::domain::PlanningSolution;
use solverforge_scoring::Director;
use super::metadata::{
append_canonical_usize_slice_pair, encode_option_debug, encode_usize, scoped_move_identity,
MoveTabuScope, ScopedEntityTabuToken, TABU_OP_PILLAR_SWAP,
};
use super::{Move, MoveTabuSignature};
pub struct PillarSwapMove<S, V> {
left_indices: Vec<usize>,
right_indices: Vec<usize>,
descriptor_index: usize,
variable_name: &'static str,
getter: fn(&S, usize, usize) -> Option<V>,
setter: fn(&mut S, usize, usize, Option<V>),
variable_index: usize,
}
impl<S, V: Clone> Clone for PillarSwapMove<S, V> {
fn clone(&self) -> Self {
Self {
left_indices: self.left_indices.clone(),
right_indices: self.right_indices.clone(),
descriptor_index: self.descriptor_index,
variable_name: self.variable_name,
getter: self.getter,
setter: self.setter,
variable_index: self.variable_index,
}
}
}
impl<S, V: Debug> Debug for PillarSwapMove<S, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PillarSwapMove")
.field("left_indices", &self.left_indices)
.field("right_indices", &self.right_indices)
.field("descriptor_index", &self.descriptor_index)
.field("variable_index", &self.variable_index)
.field("variable_name", &self.variable_name)
.finish()
}
}
impl<S, V> PillarSwapMove<S, V> {
pub fn new(
left_indices: Vec<usize>,
right_indices: Vec<usize>,
getter: fn(&S, usize, usize) -> Option<V>,
setter: fn(&mut S, usize, usize, Option<V>),
variable_index: usize,
variable_name: &'static str,
descriptor_index: usize,
) -> Self {
Self {
left_indices,
right_indices,
descriptor_index,
variable_name,
getter,
setter,
variable_index,
}
}
pub fn left_indices(&self) -> &[usize] {
&self.left_indices
}
pub fn right_indices(&self) -> &[usize] {
&self.right_indices
}
}
impl<S, V> Move<S> for PillarSwapMove<S, V>
where
S: PlanningSolution,
V: Clone + PartialEq + Send + Sync + Debug + 'static,
{
fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
if self.left_indices.is_empty() || self.right_indices.is_empty() {
return false;
}
let count = score_director.entity_count(self.descriptor_index);
let max = count.unwrap_or(0);
for &idx in self.left_indices.iter().chain(&self.right_indices) {
if idx >= max {
return false;
}
}
let left_val = self
.left_indices
.first()
.map(|&idx| (self.getter)(score_director.working_solution(), idx, self.variable_index));
let right_val = self
.right_indices
.first()
.map(|&idx| (self.getter)(score_director.working_solution(), idx, self.variable_index));
left_val != right_val
}
fn do_move<D: Director<S>>(&self, score_director: &mut D) {
let left_old: Vec<(usize, Option<V>)> = self
.left_indices
.iter()
.map(|&idx| {
(
idx,
(self.getter)(score_director.working_solution(), idx, self.variable_index),
)
})
.collect();
let right_old: Vec<(usize, Option<V>)> = self
.right_indices
.iter()
.map(|&idx| {
(
idx,
(self.getter)(score_director.working_solution(), idx, self.variable_index),
)
})
.collect();
let left_value = left_old.first().and_then(|(_, v)| v.clone());
let right_value = right_old.first().and_then(|(_, v)| v.clone());
for &idx in self.left_indices.iter().chain(&self.right_indices) {
score_director.before_variable_changed(self.descriptor_index, idx);
}
for &idx in &self.left_indices {
(self.setter)(
score_director.working_solution_mut(),
idx,
self.variable_index,
right_value.clone(),
);
}
for &idx in &self.right_indices {
(self.setter)(
score_director.working_solution_mut(),
idx,
self.variable_index,
left_value.clone(),
);
}
for &idx in self.left_indices.iter().chain(&self.right_indices) {
score_director.after_variable_changed(self.descriptor_index, idx);
}
let setter = self.setter;
let variable_index = self.variable_index;
score_director.register_undo(Box::new(move |s: &mut S| {
for (idx, old_value) in left_old {
setter(s, idx, variable_index, old_value);
}
for (idx, old_value) in right_old {
setter(s, idx, variable_index, old_value);
}
}));
}
fn descriptor_index(&self) -> usize {
self.descriptor_index
}
fn entity_indices(&self) -> &[usize] {
&self.left_indices
}
fn variable_name(&self) -> &str {
self.variable_name
}
fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
let left_value = self.left_indices.first().and_then(|&idx| {
(self.getter)(score_director.working_solution(), idx, self.variable_index)
});
let right_value = self.right_indices.first().and_then(|&idx| {
(self.getter)(score_director.working_solution(), idx, self.variable_index)
});
let left_id = encode_option_debug(left_value.as_ref());
let right_id = encode_option_debug(right_value.as_ref());
let scope = MoveTabuScope::new(self.descriptor_index, self.variable_name);
let mut entity_ids: SmallVec<[u64; 2]> = self
.left_indices
.iter()
.chain(&self.right_indices)
.map(|&idx| encode_usize(idx))
.collect();
entity_ids.sort_unstable();
entity_ids.dedup();
let entity_tokens: SmallVec<[ScopedEntityTabuToken; 2]> = entity_ids
.iter()
.copied()
.map(|entity_id| scope.entity_token(entity_id))
.collect();
let mut move_id = scoped_move_identity(scope, TABU_OP_PILLAR_SWAP, std::iter::empty());
append_canonical_usize_slice_pair(&mut move_id, &self.left_indices, &self.right_indices);
MoveTabuSignature::new(scope, move_id.clone(), move_id)
.with_entity_tokens(entity_tokens)
.with_destination_value_tokens([
scope.value_token(right_id),
scope.value_token(left_id),
])
}
}