Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seq WASM compatibility #33

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ license = "MIT/Apache-2.0"
documentation = "https://docs.rs/rsgenetic/"

[dependencies]
rand = "0.3"
rayon = "1.0.0"
rand = "^0.5.4"
rayon = "1.0.2"
3 changes: 2 additions & 1 deletion examples/enum_phenotype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use rsgenetic::sim::*;
use rsgenetic::sim::seq::Simulator;
use rsgenetic::sim::select::*;
use rsgenetic::pheno::*;
use rsgenetic::stats::NoStats;

#[derive(Clone, Copy, Debug)]
struct MyPhenotype {
Expand Down Expand Up @@ -76,7 +77,7 @@ fn main() {
population.push(MyPhenotype { variant: MyVariant::Variant2, value: i })
}
#[allow(deprecated)]
let mut s = Simulator::builder(&mut population)
let mut s: Simulator<_, _, NoStats> = Simulator::builder(&mut population)
.set_selector(Box::new(MaximizeSelector::new(10)))
.set_max_iters(100)
.build();
Expand Down
6 changes: 5 additions & 1 deletion examples/max_parabole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use rsgenetic::sim::select::*;
use rsgenetic::pheno::*;
use rand::distributions::{IndependentSample, Range};
use std::cmp::Ordering;
use rsgenetic::stats::{StatsCollector, NoStats};


struct MyFitness {
f: f64,
Expand Down Expand Up @@ -61,6 +63,8 @@ impl Fitness for MyFitness {
}
}
}
impl StatsCollector<MyFitness> for NoStats {}


struct MyData {
x: f64,
Expand Down Expand Up @@ -104,7 +108,7 @@ impl Clone for MyData {

fn main() {
let mut population = (-300..300).map(|i| MyData { x: f64::from(i) }).collect();
let mut s = Simulator::builder(&mut population)
let mut s: Simulator<_,_, NoStats> = Simulator::builder(&mut population)
.set_selector(Box::new(StochasticSelector::new(10)))
.set_max_iters(50)
.build();
Expand Down
5 changes: 4 additions & 1 deletion examples/max_parabole_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern crate rsgenetic;
use rsgenetic::sim::*;
use rsgenetic::sim::seq::Simulator;
use rsgenetic::sim::select::*;
use rsgenetic::stats::{NoStats, StatsCollector};
use rsgenetic::pheno::*;
use rand::distributions::{IndependentSample, Range};
use std::cmp::Ordering;
Expand Down Expand Up @@ -63,6 +64,8 @@ impl Fitness for MyFitness {
}
}
}
impl StatsCollector<MyFitness> for NoStats {}


struct MyData {
x: f64,
Expand Down Expand Up @@ -106,7 +109,7 @@ impl Clone for MyData {

fn main() {
let mut population = (-300..300).map(|i| MyData { x: f64::from(i) }).collect();
let mut s = Simulator::builder(&mut population)
let mut s: Simulator<_, _, NoStats> = Simulator::builder(&mut population)
.set_selector(Box::new(StochasticSelector::new(10)))
.set_max_iters(50)
.build();
Expand Down
3 changes: 2 additions & 1 deletion examples/truck_loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern crate rand;
extern crate rsgenetic;

use rsgenetic::sim::*;
use rsgenetic::stats::NoStats;
use rsgenetic::sim::seq::Simulator;
use rsgenetic::sim::select::*;
use rsgenetic::pheno::*;
Expand Down Expand Up @@ -126,7 +127,7 @@ fn main() {
population.push(LoadingScheme { scheme: pheno });
}
#[allow(deprecated)]
let mut s = Simulator::builder(&mut population)
let mut s: Simulator<_, _, NoStats> = Simulator::builder(&mut population)
.set_selector(Box::new(MaximizeSelector::new(10)))
.set_max_iters(100)
.build();
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ extern crate rayon;

/// Contains the definition of a Phenotype.
pub mod pheno;

/// Contains a way to collect information on steps via a trait.
pub mod stats;

/// Contains implementations of Simulators, which can run genetic algorithms.
pub mod sim;
/// Contains code used by unit tests.
Expand Down
15 changes: 14 additions & 1 deletion src/sim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
// limitations under the License.

use pheno::{Fitness, Phenotype};
use stats::StatsCollector;
use std::cell::RefCell;
use std::rc::Rc;

pub mod seq;
pub mod select;
Expand Down Expand Up @@ -58,14 +61,24 @@ pub enum RunResult {
}

/// A `Simulation` is an execution of a genetic algorithm.
pub trait Simulation<'a, T, F>
pub trait Simulation<'a, T, F, S>
where
T: Phenotype<F>,
F: Fitness,
S: StatsCollector<F>,
{
/// A `Builder` is used to create instances of a `Simulation`.
type B: Builder<Self>;


/// Create a `Builder` to create an instance.
///
/// `population` is a required parameter of any `Simulation`, which
/// is why it is a parameter of this function.
fn builder_with_stats(population: &'a mut Vec<T>, sc: Option<Rc<RefCell<S>>>) -> Self::B
where
Self: Sized;

/// Create a `Builder` to create an instance.
///
/// `population` is a required parameter of any `Simulation`, which
Expand Down
Loading