use std::fmt::Debug;
use smallvec::SmallVec;
use solverforge_core::domain::PlanningSolution;
use solverforge_scoring::Director;
use super::metadata::{
encode_option_debug, encode_usize, hash_str, MoveTabuScope, ScopedEntityTabuToken,
};
use super::{Move, MoveTabuSignature};
pub struct RuinMove<S, V> {
entity_indices: SmallVec<[usize; 8]>,
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,
}
impl<S, V> Clone for RuinMove<S, V> {
fn clone(&self) -> Self {
Self {
entity_indices: self.entity_indices.clone(),
getter: self.getter,
setter: self.setter,
variable_index: self.variable_index,
variable_name: self.variable_name,
descriptor_index: self.descriptor_index,
}
}
}
impl<S, V: Debug> Debug for RuinMove<S, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RuinMove")
.field("entities", &self.entity_indices.as_slice())
.field("variable_index", &self.variable_index)
.field("variable_name", &self.variable_name)
.finish()
}
}
impl<S, V> RuinMove<S, V> {
pub fn new(
entity_indices: &[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 {
entity_indices: SmallVec::from_slice(entity_indices),
getter,
setter,
variable_index,
variable_name,
descriptor_index,
}
}
pub fn entity_indices_slice(&self) -> &[usize] {
&self.entity_indices
}
pub fn ruin_count(&self) -> usize {
self.entity_indices.len()
}
}
impl<S, V> Move<S> for RuinMove<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();
self.entity_indices
.iter()
.any(|&idx| (self.getter)(solution, idx, self.variable_index).is_some())
}
fn do_move<D: Director<S>>(&self, score_director: &mut D) {
let getter = self.getter;
let setter = self.setter;
let descriptor = self.descriptor_index;
let variable_index = self.variable_index;
let old_values: SmallVec<[(usize, Option<V>); 8]> = self
.entity_indices
.iter()
.map(|&idx| {
let old = getter(score_director.working_solution(), idx, variable_index);
(idx, old)
})
.collect();
for &idx in &self.entity_indices {
score_director.before_variable_changed(descriptor, idx);
setter(
score_director.working_solution_mut(),
idx,
variable_index,
None,
);
score_director.after_variable_changed(descriptor, idx);
}
score_director.register_undo(Box::new(move |s: &mut S| {
for (idx, old_value) in old_values {
setter(s, idx, variable_index, old_value);
}
}));
}
fn descriptor_index(&self) -> usize {
self.descriptor_index
}
fn entity_indices(&self) -> &[usize] {
&self.entity_indices
}
fn variable_name(&self) -> &str {
self.variable_name
}
fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
let scope = MoveTabuScope::new(self.descriptor_index, self.variable_name);
let entity_ids: SmallVec<[u64; 2]> = self
.entity_indices
.iter()
.map(|&idx| encode_usize(idx))
.collect();
let entity_tokens: SmallVec<[ScopedEntityTabuToken; 2]> = entity_ids
.iter()
.copied()
.map(|entity_id| scope.entity_token(entity_id))
.collect();
let mut value_ids: SmallVec<[u64; 2]> = SmallVec::new();
for &idx in &self.entity_indices {
let value = (self.getter)(score_director.working_solution(), idx, self.variable_index);
value_ids.push(encode_option_debug(value.as_ref()));
}
let variable_id = hash_str(self.variable_name);
let mut move_id = SmallVec::<[u64; 8]>::from_slice(&[
encode_usize(self.descriptor_index),
variable_id,
encode_usize(self.entity_indices.len()),
]);
move_id.extend(entity_ids.iter().copied());
move_id.extend(value_ids.iter().copied());
MoveTabuSignature::new(scope, move_id.clone(), move_id).with_entity_tokens(entity_tokens)
}
}