use super::super::Metaheuristics;
use rand::{thread_rng, Rng};
use time::{Duration, Instant};
pub fn solve<T>(problem: &mut dyn Metaheuristics<T>, runtime: Duration, probability: f64) -> T {
let mut best_candidate = problem.generate_candidate();
let mut current_candidate = problem.clone_candidate(&best_candidate);
let start_time = Instant::now();
while start_time.elapsed() < runtime {
if probability > thread_rng().gen_range(0.0..1.0) {
current_candidate = problem.generate_candidate();
continue;
}
let next_candidate = problem.tweak_candidate(¤t_candidate);
if problem.rank_candidate(&next_candidate) > problem.rank_candidate(¤t_candidate) {
current_candidate = problem.clone_candidate(&next_candidate);
}
if problem.rank_candidate(¤t_candidate) > problem.rank_candidate(&best_candidate) {
best_candidate = problem.clone_candidate(&next_candidate);
}
}
best_candidate
}