use rand_core::Rng;
use super::INFORMED_ATTEMPTS;
use crate::{Problem, Sampler, StateSpace, unit_f64};
pub(super) fn draw_target<Sp, Sa, C, St, R>(
problem: &mut Problem<Sp, Sa, C, St>,
goal_bias: f64,
informed_bound: Option<f64>,
rng: &mut R,
) -> Sp::State
where
Sp: StateSpace,
Sa: Sampler<Sp::State>,
R: Rng + ?Sized,
{
if unit_f64(rng) < goal_bias {
return problem.goal.center().clone();
}
let Some(bound) = informed_bound else {
return problem.sampler.sample(rng);
};
let direct = problem.sampler.sample_informed(
&problem.space,
rng,
&problem.start,
problem.goal.center(),
bound,
);
if let Some(state) = direct {
return state;
}
let mut sample = problem.sampler.sample(rng);
for _ in 1..INFORMED_ATTEMPTS {
if informed_cost(problem, &sample) < bound {
break;
}
sample = draw_target(problem, goal_bias, None, rng);
}
sample
}
pub(super) fn informed_cost<Sp: StateSpace, Sa, C, St>(
problem: &Problem<Sp, Sa, C, St>,
state: &Sp::State,
) -> f64 {
problem.space.distance(&problem.start, state)
+ problem.space.distance(state, problem.goal.center())
}