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};
use super::{Move, MoveTabuSignature};
pub struct ChangeMove<S, V> {
entity_index: usize,
to_value: Option<V>,
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> Clone for ChangeMove<S, V> {
fn clone(&self) -> Self {
Self {
entity_index: self.entity_index,
to_value: self.to_value.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: Copy> Copy for ChangeMove<S, V> {}
impl<S, V: Debug> Debug for ChangeMove<S, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ChangeMove")
.field("entity_index", &self.entity_index)
.field("descriptor_index", &self.descriptor_index)
.field("variable_index", &self.variable_index)
.field("variable_name", &self.variable_name)
.field("to_value", &self.to_value)
.finish()
}
}
impl<S, V> ChangeMove<S, V> {
pub fn new(
entity_index: usize,
to_value: Option<V>,
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_index,
to_value,
getter,
setter,
variable_index,
variable_name,
descriptor_index,
}
}
pub fn entity_index(&self) -> usize {
self.entity_index
}
pub fn to_value(&self) -> Option<&V> {
self.to_value.as_ref()
}
pub fn getter(&self) -> fn(&S, usize, usize) -> Option<V> {
self.getter
}
pub fn setter(&self) -> fn(&mut S, usize, usize, Option<V>) {
self.setter
}
pub fn variable_index(&self) -> usize {
self.variable_index
}
}
impl<S, V> Move<S> for ChangeMove<S, V>
where
S: PlanningSolution,
V: Clone + PartialEq + Send + Sync + Debug + 'static,
{
fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
let current = (self.getter)(
score_director.working_solution(),
self.entity_index,
self.variable_index,
);
match (¤t, &self.to_value) {
(None, None) => false, (Some(cur), Some(target)) => cur != target, _ => true, }
}
fn do_move<D: Director<S>>(&self, score_director: &mut D) {
let old_value = (self.getter)(
score_director.working_solution(),
self.entity_index,
self.variable_index,
);
score_director.before_variable_changed(self.descriptor_index, self.entity_index);
(self.setter)(
score_director.working_solution_mut(),
self.entity_index,
self.variable_index,
self.to_value.clone(),
);
score_director.after_variable_changed(self.descriptor_index, self.entity_index);
let setter = self.setter;
let idx = self.entity_index;
let variable_index = self.variable_index;
score_director.register_undo(Box::new(move |s: &mut S| {
setter(s, idx, variable_index, old_value);
}));
}
fn descriptor_index(&self) -> usize {
self.descriptor_index
}
fn entity_indices(&self) -> &[usize] {
std::slice::from_ref(&self.entity_index)
}
fn variable_name(&self) -> &str {
self.variable_name
}
fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
let current = (self.getter)(
score_director.working_solution(),
self.entity_index,
self.variable_index,
);
let from_id = encode_option_debug(current.as_ref());
let to_id = encode_option_debug(self.to_value.as_ref());
let entity_id = encode_usize(self.entity_index);
let variable_id = hash_str(self.variable_name);
let scope = MoveTabuScope::new(self.descriptor_index, self.variable_name);
MoveTabuSignature::new(
scope,
smallvec![
encode_usize(self.descriptor_index),
variable_id,
entity_id,
from_id,
to_id
],
smallvec![
encode_usize(self.descriptor_index),
variable_id,
entity_id,
to_id,
from_id
],
)
.with_entity_tokens([scope.entity_token(entity_id)])
.with_destination_value_tokens([scope.value_token(to_id)])
}
}