pub fn adaptive_wsp(set: &mut PointSet, obj_nb: usize, verbose: bool) {
    let mut d_min = set.d_min;
    let mut d_max = set.d_max;
    let mut d_search = (d_min + d_max) / 2.0;
    let mut iter = 0;
    let mut best_distance = 0.0;
    let mut best_difference_active = set.nb_active - obj_nb;
    loop {
        iter += 1;
        wsp(set, d_search);

        // Binary search the best d_min
        if verbose {
            println!(
                "Iter #{}: distance={}, nb_active={}",
                iter, d_search, set.nb_active
            );
        }
        match set.nb_active.cmp(&obj_nb) {
            Ordering::Greater => d_min = d_search,
            Ordering::Less => d_max = d_search,
            Ordering::Equal => return,
        };

        // The search space is not continuous.
        // We must also track the best result to recover it afterwards
        if (set.nb_active as i32 - obj_nb as i32).abs() < best_difference_active as i32 {
            best_difference_active = (set.nb_active as i32 - obj_nb as i32).abs() as usize;
            best_distance = d_search;
        }

        // Stop condition if we cannot exactly reach the target number
        let last_d_search = d_search;
        d_search = (d_min + d_max) / 2.0;
        if (last_d_search - d_search).abs() <= f64::EPSILON {
            break;
        }

        // Reset parameters for the next iteration
        set.reset_reseach_params();
    }

    // Recompute a last time if the best distance is not the last computed distance
    if (best_distance - d_search).abs() > f64::EPSILON {
        d_search = best_distance;
        set.reset_reseach_params();
        wsp(set, d_search);
    }
    if verbose {
        println!(
            "Last iter: best approximation is distance={}, nb_active={}",
            d_search, set.nb_active
        );
    }
}
/// This is an adaptive version of the WSP algorithm.
/// The traditional algorithm requires a d_min and
/// based on that we obtain a set of a given number of points.
/// Here we adaptively change d_min to get (an approximation of)
/// the desired number of points active after the algorithm.
///
/// # Arguments
///
/// * `set` - The PointSet instance. `set` is mutably borrowed.
/// * `obj_nb` - The desired number of points remaining active in the set after the algorithm.
/// * `verbose` - Print running information about the iterations of the algorithm.
///
/// # Example
///
/// ```
/// let mut points = wsp::PointSet::init_from_random(1000, 20, 51);
/// let objective_nb: usize = 100;
/// wsp::adaptive_wsp(&mut points, objective_nb, false);
/// ```
This is an adaptive version of the WSP algorithm.
The traditional algorithm requires a d_min and
based on that we obtain a set of a given number of points.
Here we adaptively change d_min to get (an approximation of)
the desired number of points active after the algorithm.
This is an adaptive version of the WSP algorithm . The traditional algorithm requires a d_min and based on that we obtain a set of a given number of points . Here we adaptively change d_min to get ( an approximation of ) the desired number of points active after the algorithm .
pub fn adaptive_wsp(set: &mut PointSet, obj_nb: usize, verbose: bool) {
    let mut d_min = set.d_min;
    let mut d_max = set.d_max;
    let mut d_search = (d_min + d_max) / 2.0;
    let mut iter = 0;
    let mut best_distance = 0.0;
    let mut best_difference_active = set.nb_active - obj_nb;
    loop {
        iter += 1;
        wsp(set, d_search);
        // Binary search the best d_min
        if verbose {
            println!(
                "Iter #{}: distance={}, nb_active={}",
                iter, d_search, set.nb_active
            );
        }
        match set.nb_active.cmp(&obj_nb) {
            Ordering::Greater => d_min = d_search,
            Ordering::Less => d_max = d_search,
            Ordering::Equal => return,
        };
        // The search space is not continuous.
        // We must also track the best result to recover it afterwards
        if (set.nb_active as i32 - obj_nb as i32).abs() < best_difference_active as i32 {
            best_difference_active = (set.nb_active as i32 - obj_nb as i32).abs() as usize;
            best_distance = d_search;
        }
        // Stop condition if we cannot exactly reach the target number
        let last_d_search = d_search;
        d_search = (d_min + d_max) / 2.0;
        if (last_d_search - d_search).abs() <= f64::EPSILON {
            break;
        }
        // Reset parameters for the next iteration
        set.reset_reseach_params();
    }
    // Recompute a last time if the best distance is not the last computed distance
    if (best_distance - d_search).abs() > f64::EPSILON {
        d_search = best_distance;
        set.reset_reseach_params();
        wsp(set, d_search);
    }
    if verbose {
        println!(
            "Last iter: best approximation is distance={}, nb_active={}",
            d_search, set.nb_active
        );
    }
}
pub fn adaptive_wsp ( set : & mut PointSet , obj_nb : usize , verbose : bool ) { let mut d_min = set . d_min ; let mut d_max = set . d_max ; let mut d_search = ( d_min + d_max ) / 2.0 ; let mut iter = 0 ; let mut best_distance = 0.0 ; let mut best_difference_active = set . nb_active - obj_nb ; loop { iter += 1 ; wsp ( set , d_search ) ; // Binary search the best d_min if verbose { println ! ( " " iter d_search set nb_active ) ; } match set . nb_active . cmp ( & obj_nb ) { Ordering :: Greater => d_min = d_search , Ordering :: Less => d_max = d_search , Ordering :: Equal => return , } ; // The search space is not continuous. // We must also track the best result to recover it afterwards if ( set . nb_active as i32 - obj_nb as i32 ) . abs ( ) < best_difference_active as i32 { best_difference_active = ( set . nb_active as i32 - obj_nb as i32 ) . abs ( ) as usize ; best_distance = d_search ; } // Stop condition if we cannot exactly reach the target number let last_d_search = d_search ; d_search = ( d_min + d_max ) / 2.0 ; if ( last_d_search - d_search ) . abs ( ) <= f64 :: EPSILON { break ; } // Reset parameters for the next iteration set . reset_reseach_params ( ) ; } // Recompute a last time if the best distance is not the last computed distance if ( best_distance - d_search ) . abs ( ) > f64 :: EPSILON { d_search = best_distance ; set . reset_reseach_params ( ) ; wsp ( set , d_search ) ; } if verbose { println ! ( " " d_search set nb_active ) ; } }
This is an adaptive version of the WSP algorithm.
This is an adaptive version of the WSP algorithm .
set
&mut PointSet
obj_nb
usize
verbose
bool
set
&mut PointSet
obj_nb
usize
verbose
bool
example
```
let mut points = wsp::PointSet::init_from_random(1000, 20, 51);
let objective_nb: usize = 100;
wsp::adaptive_wsp(&mut points, objective_nb, false);
```
` ` ` let mut points = wsp :: PointSet :: init_from_random ( 1000 20 51 ) ; let objective_nb : usize = 100 ; wsp :: adaptive_wsp ( &mut points objective_nb false ) ; ` ` `