#![feature(core)]

extern crate rand;

use std::iter::FromIterator;
use rand::distributions::{IndependentSample, Range};

const N: usize = 50;
const P: usize = 128;

fn schwefel(solution: &Vec<f64>) -> f64 {
    418.9829_f64 * solution.len() as f64
        + solution.iter().fold(0_f64, |sum, x| sum + x * x.abs().sqrt().sin())
}

fn main() {
    let mut rng = rand::thread_rng();
    let range = Range::new(-512.03_f64, 511.97);

    // initialize population
    let population = Vec::from_iter((0..P).map(|_| {
        Vec::from_iter((0..N).map(|_| {
            range.ind_sample(&mut rng)
        }))
    }));

    println!("{:?}", population); // debug print of solutions

    let solution = population.iter().min_by(|x| schwefel(x) as i64).unwrap();
    println!("The best solution was {:?}", solution);
    println!("Its fitness was {}", schwefel(solution));
}

extern crate rand;

use std::iter::FromIterator;
use rand::distributions::{IndependentSample, Range};

const N: usize = 50;
const P: usize = 128;

fn schwefel(solution: &Vec<f64>) -> f64 {
    418.9829_f64 * solution.len() as f64
        + solution.iter().fold(0_f64, |sum, x| sum + x * x.abs().sqrt().sin())
}

fn main() {
    let mut rng = rand::thread_rng();
    let range = Range::new(-512.03_f64, 511.97);

    // initialize population
    let population = Vec::from_iter((0..P).map(|_| {
        Vec::from_iter((0..N).map(|_| {
            range.ind_sample(&mut rng)
        }))
    }));

    println!("{:?}", population); // debug print of solutions

    println!("The best solution's fitness was {}", schwefel(&population[0]));
}

--- 
+++ 
@@ -1,3 +1,5 @@
+#![feature(core)]
+
 extern crate rand;
 
 use std::iter::FromIterator;
@@ -24,5 +26,7 @@
 
     println!("{:?}", population); // debug print of solutions
 
-    println!("The best solution's fitness was {}", schwefel(&population[0]));
+    let solution = population.iter().min_by(|x| schwefel(x) as i64).unwrap();
+    println!("The best solution was {:?}", solution);
+    println!("Its fitness was {}", schwefel(solution));
 }
Use min_by to find most fit solution

The problem is that f64's do not implement Ordering, only Partial
Ordering, and min_by is an unstable feature. The plan is to replace with
better abstractions.
