use std::fmt::Debug;
use solverforge_core::domain::PlanningSolution;
use solverforge_scoring::Director;
use super::metadata::{
encode_option_debug, encode_usize, ordered_coordinate_pair, scoped_move_identity,
MoveTabuScope, TABU_OP_SWAP,
};
use super::{Move, MoveTabuSignature};
pub struct SwapMove<S, V> {
left_entity_index: usize,
right_entity_index: 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,
indices: [usize; 2],
}
impl<S, V> Clone for SwapMove<S, V> {
fn clone(&self) -> Self {
*self
}
}
impl<S, V> Copy for SwapMove<S, V> {}
impl<S, V: Debug> Debug for SwapMove<S, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SwapMove")
.field("left_entity_index", &self.left_entity_index)
.field("right_entity_index", &self.right_entity_index)
.field("descriptor_index", &self.descriptor_index)
.field("variable_index", &self.variable_index)
.field("variable_name", &self.variable_name)
.finish()
}
}
impl<S, V> SwapMove<S, V> {
pub fn new(
left_entity_index: usize,
right_entity_index: 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_entity_index,
right_entity_index,
getter,
setter,
variable_index,
variable_name,
descriptor_index,
indices: [left_entity_index, right_entity_index],
}
}
pub fn left_entity_index(&self) -> usize {
self.left_entity_index
}
pub fn right_entity_index(&self) -> usize {
self.right_entity_index
}
}
impl<S, V> Move<S> for SwapMove<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_entity_index == self.right_entity_index {
return false;
}
let left_val = (self.getter)(
score_director.working_solution(),
self.left_entity_index,
self.variable_index,
);
let right_val = (self.getter)(
score_director.working_solution(),
self.right_entity_index,
self.variable_index,
);
left_val != right_val
}
fn do_move<D: Director<S>>(&self, score_director: &mut D) {
let left_value = (self.getter)(
score_director.working_solution(),
self.left_entity_index,
self.variable_index,
);
let right_value = (self.getter)(
score_director.working_solution(),
self.right_entity_index,
self.variable_index,
);
score_director.before_variable_changed(self.descriptor_index, self.left_entity_index);
score_director.before_variable_changed(self.descriptor_index, self.right_entity_index);
(self.setter)(
score_director.working_solution_mut(),
self.left_entity_index,
self.variable_index,
right_value.clone(),
);
(self.setter)(
score_director.working_solution_mut(),
self.right_entity_index,
self.variable_index,
left_value.clone(),
);
score_director.after_variable_changed(self.descriptor_index, self.left_entity_index);
score_director.after_variable_changed(self.descriptor_index, self.right_entity_index);
let setter = self.setter;
let left_idx = self.left_entity_index;
let right_idx = self.right_entity_index;
let variable_index = self.variable_index;
score_director.register_undo(Box::new(move |s: &mut S| {
setter(s, left_idx, variable_index, left_value);
setter(s, right_idx, variable_index, right_value);
}));
}
fn descriptor_index(&self) -> usize {
self.descriptor_index
}
fn entity_indices(&self) -> &[usize] {
&self.indices
}
fn variable_name(&self) -> &str {
self.variable_name
}
fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
let left_val = (self.getter)(
score_director.working_solution(),
self.left_entity_index,
self.variable_index,
);
let right_val = (self.getter)(
score_director.working_solution(),
self.right_entity_index,
self.variable_index,
);
let left_id = encode_option_debug(left_val.as_ref());
let right_id = encode_option_debug(right_val.as_ref());
let left_entity_id = encode_usize(self.left_entity_index);
let right_entity_id = encode_usize(self.right_entity_index);
let scope = MoveTabuScope::new(self.descriptor_index, self.variable_name);
let entity_pair = ordered_coordinate_pair((left_entity_id, 0), (right_entity_id, 0));
let move_id = scoped_move_identity(
scope,
TABU_OP_SWAP,
entity_pair.into_iter().map(|(entity_id, _)| entity_id),
);
MoveTabuSignature::new(scope, move_id.clone(), move_id)
.with_entity_tokens([
scope.entity_token(left_entity_id),
scope.entity_token(right_entity_id),
])
.with_destination_value_tokens([
scope.value_token(right_id),
scope.value_token(left_id),
])
}
}