1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use rand;
use rand::{Rng};
use rand::distributions::{Normal, IndependentSample, Range};

use context::*;
use ea::*;
use problem::*;
use result::*;
use settings::*;


/// Baseline structure for [Genetic Algorithm](https://en.wikipedia.org/wiki/Genetic_algorithm)
///
/// # Example: Running GA to solve minimization problem:
/// ```
/// extern crate rand;
/// extern crate revonet;
///
/// use revonet::ea::*;
/// use revonet::ga::*;
/// use revonet::problem::*;
/// use revonet::settings::*;
///
/// fn main() {
///     let pop_size = 20u32;
///     let problem_dim = 10u32;
///     let problem = SphereProblem{};
///
///     let gen_count = 10u32;
///     let settings = EASettings::new(pop_size, gen_count, problem_dim);
///     let mut ga: GA<SphereProblem> = GA::new(&problem);
///     let res = ga.run(settings).expect("Error during GA run");
///     println!("\n\nGA results: {:?}", res);
/// }
/// ```
pub struct GA<'a, P: Problem + 'a> {
    /// Context structure containing information about GA run, its progress and results.
    ctx: Option<EAContext<RealCodedIndividual>>,
    /// Reference to the objective function object implementing `Problem` trait.
    problem: &'a P,
}

impl<'a, P: Problem> GA<'a, P> {
    /// Create a new GA instance for the given `problem`.
    pub fn new(problem: &'a P) -> GA<P> {
        GA{problem: problem,
           ctx: None,
        }
    }
}

impl<'a, P: Problem> EA<'a, P> for GA<'a, P> {
    type IndType = RealCodedIndividual;

    fn breed(&self, ctx: &mut EAContext<Self::IndType>, sel_inds: &Vec<usize>, children: &mut Vec<Self::IndType>) {
        cross(&ctx.population, sel_inds, children, ctx.settings.use_elite, ctx.settings.x_type, ctx.settings.x_prob, ctx.settings.x_alpha, &mut ctx.rng);
        mutate(children, ctx.settings.mut_type, ctx.settings.mut_prob, ctx.settings.mut_sigma, &mut ctx.rng);
    }

    fn run(&mut self, settings: EASettings) -> Result<&EAResult<Self::IndType>, ()> {
        let gen_count = settings.gen_count;
        let mut ctx = EAContext::new(settings, self.problem);
        self.run_with_context(&mut ctx, self.problem, gen_count);
        self.ctx = Some(ctx);
        Ok(&(&self.ctx.as_ref().expect("Empty EAContext")).result)
    }
}

/// Function for crossing individuals to produce children.
///
/// # Arguments:
/// * `popul` - reference to parent population.
/// * `sel_inds` - reference to vector of individuals selected for crossing.
/// * `children` - container for children individuals.
/// * `use_elite` - flag to specify whether elite individual should be copied to the children
///                 population.
/// * `x_type` - crossover operator type defined by `CrossoverOperator` enum.
/// * `x_prob` - crossing probability. If random U(0, 1) number is above this probability then
///              no crossing is performed and children are simply copy of selected parents.
/// * `x_alpha` - parameter for a crossover operator.
/// * `rng` - reference to pre-initialized RNG.
pub fn cross<R: Rng+Sized, T: Individual+Clone>(popul: &Vec<T>, sel_inds: &Vec<usize>, children: &mut Vec<T>, use_elite: bool, x_type: CrossoverOperator, x_prob: f32, x_alpha: f32, mut rng: &mut R) {
    let range = Range::new(0, popul.len());
    if use_elite {
        children.push(get_best_individual(popul));
    }
    let xfunc = match x_type {
        CrossoverOperator::Arithmetic => cross_arithmetic,
        CrossoverOperator::BlxAlpha => cross_blx_alpha,
    };
    loop {
        // select parent individuals
        let p1: usize = range.ind_sample(rng);
        let mut p2: usize = range.ind_sample(rng);
        while p2 == p1 {
            p2 = range.ind_sample(rng);
        }

        let mut c1 = popul[sel_inds[p1]].clone();   // T::new();
        let mut c2 = popul[sel_inds[p2]].clone();   // T::new();
        if rand::random::<f32>() < x_prob {
            xfunc(&popul[sel_inds[p1]], &popul[sel_inds[p2]], &mut c1, &mut c2, x_alpha, rng);
        }
        children.push(c1);
        children.push(c2);

        if children.len() >= popul.len() {break;}
    }
}

/// Implementation of the arithmetic crossover.
///
/// # Arguments:
/// * `p1` - reference to the 1st parent.
/// * `p2` - reference to the 2nd parent.
/// * `c1` - reference to the 1st child.
/// * `c2` - reference to the 2nd child.
/// * `alpha` - parameter for crossover, controlling range of the child gene values.
/// * `rng` - reference to pre-initialized RNG.
#[allow(unused_variables)]
fn cross_arithmetic<T: Individual, R: Rng+Sized>(p1: &T, p2: &T, c1: &mut T, c2: &mut T, alpha: f32, mut rng: &mut R) {
    let p1_genes = p1.to_vec().expect("Can not extract vector of genes");
    let p2_genes = p2.to_vec().expect("Can not extract vector of genes");
    // println!("{} : {}", p1_genes.len(), p2_genes.len());
    assert!(p1_genes.len() == p2_genes.len());

    let gene_count = p1_genes.len();
    let c1_genes = c1.to_vec_mut().expect("Can not extract mutable vector of genes");
    let c2_genes = c2.to_vec_mut().expect("Can not extract mutable vector of genes");

    for k in 0..gene_count {
        let a = rng.gen::<f32>();
        c1_genes[k] = p1_genes[k]*a + p2_genes[k]*(1f32-a);
        c2_genes[k] = p2_genes[k]*a + p1_genes[k]*(1f32-a);
    };
}

/// Implementation of the BLX-alpha crossover.
///
/// # Arguments:
/// * `p1` - reference to the 1st parent.
/// * `p2` - reference to the 2nd parent.
/// * `c1` - reference to the 1st child.
/// * `c2` - reference to the 2nd child.
/// * `alpha` - parameter for crossover, controlling range of the child gene values.
/// * `rng` - reference to pre-initialized RNG.
fn cross_blx_alpha<T: Individual, R: Rng+Sized>(p1: &T, p2: &T, c1: &mut T, c2: &mut T, alpha: f32, mut rng: &mut R) {
    let p1_genes = p1.to_vec().expect("Can not extract vector of genes");
    let p2_genes = p2.to_vec().expect("Can not extract vector of genes");
    // println!("{} : {}", p1_genes.len(), p2_genes.len());
    assert!(p1_genes.len() == p2_genes.len());

    let gene_count = p1_genes.len();
    let c1_genes = c1.to_vec_mut().expect("Can not extract mutable vector of genes");
    let c2_genes = c2.to_vec_mut().expect("Can not extract mutable vector of genes");

    for k in 0..gene_count {
        let (min_gene, max_gene) = if p1_genes[k] > p2_genes[k] {(p2_genes[k], p1_genes[k])}
                                   else                         {(p1_genes[k], p2_genes[k])};
        if min_gene < max_gene {
            let delta = max_gene - min_gene;
            let gene_range = Range::new(min_gene - delta*alpha, max_gene + delta*alpha);
            c1_genes[k] = gene_range.ind_sample(&mut rng);
            c2_genes[k] = gene_range.ind_sample(&mut rng);
        } else {
            c1_genes[k] = min_gene;
            c2_genes[k] = max_gene;
        }
    };
    // println!("children: {} : {}", c1_genes.len(), c2_genes.len());
}

/// Function for mutation of the given population.
///
/// # Arguments:
/// * `children` - population to undergo mutation.
/// * `mut_prob` - probability of mutation of single gene.
/// * `mut_sigma` - mutation parameter.
/// * `rng` - reference to pre-initialized RNG.
pub fn mutate<T: Individual, R: Rng>(children: &mut Vec<T>, mut_type: MutationOperator,  mut_prob: f32, mut_sigma: f32, rng: &mut R) {
    let mut_func = match mut_type {
        MutationOperator::Gaussian => mutate_gauss,
        MutationOperator::Uniform => mutate_uniform,
    };
    for k in 1..children.len() {
        mut_func(&mut children[k], mut_prob, mut_sigma, rng);
    }
}

/// Implementation of the Gaussian mutation.
///
/// # Arguments:
/// * `ind` - individual to be mutated.
/// * `prob` - probability of mutation of single gene.
/// * `sigma` - standard deviation of mutation.
/// * `rng` - reference to pre-initialized RNG.
fn mutate_gauss<T: Individual, R: Rng>(ind: &mut T, prob: f32, sigma: f32, rng: &mut R) {
    let normal_rng = Normal::new(0.0, sigma as f64);
    let genes = ind.to_vec_mut().expect("Can not extract mutable vector of genes");
    for k in 0..genes.len() {
        if rand::random::<f32>() < prob {
            genes[k] += normal_rng.ind_sample(rng) as f32;
        }
    }
}

/// Implementation of the uniform mutation. Each gene is updated with probability `prob`
/// by the value taken from `U(gene-sigma, gene+sigma)`.
///
/// # Arguments:
/// * `ind` - individual to be mutated.
/// * `prob` - probability of mutation of single gene.
/// * `sigma` - standard deviation of mutation.
/// * `rng` - reference to pre-initialized RNG.
#[allow(dead_code, unused_variables)]
fn mutate_uniform<T: Individual, R: Rng>(ind: &mut T, prob: f32, sigma: f32, rng: &mut R) {
    let genes = ind.to_vec_mut().expect("Can not extract mutable vector of genes");
    let sigma2 = sigma * 2f32;
    for k in 0..genes.len() {
        if rand::random::<f32>() < prob {
            genes[k] += rng.gen::<f32>() * sigma2 - sigma;
        }
    }
}

//============================================

#[cfg(test)]
#[allow(unused_imports, unused_mut, unused_variables)]
mod test {
    use rand;
    use rand::{StdRng, SeedableRng};

    use ea::*;
    use ga::*;
    use math::*;
    use settings::*;

    #[test]
    fn test_arithmetic_xover() {
        let mut rng = StdRng::from_seed(&[0 as usize]);

        const SIZE: usize = 20;
        let mut p1 = RealCodedIndividual::new();
        p1.init(SIZE, &mut rng);
        let mut p2 = RealCodedIndividual::new();
        p2.init(SIZE, &mut rng);

        let mut c1 = RealCodedIndividual::new();
        c1.init(SIZE, &mut rng);
        let mut c2 = RealCodedIndividual::new();
        c2.init(SIZE, &mut rng);
        cross_arithmetic(&p1, &p2, &mut c1, &mut c2, 0.1f32, &mut rng);

        let p1_genes = p1.to_vec().unwrap();
        let p2_genes = p2.to_vec().unwrap();
        let c1_genes = c1.to_vec().unwrap();
        let c2_genes = c2.to_vec().unwrap();
        for k in 0..SIZE {
            let c_sum = c1_genes[k] + c2_genes[k];
            let p_sum = p1_genes[k] + p2_genes[k];
//            println!("{} : {}", , p1_genes[k] + p2_genes[k]);
            assert!((c_sum - p_sum).abs() < 1e-6f32);
        }
    }

    #[test]
    fn test_blx_xover() {
        let mut rng = StdRng::from_seed(&[0 as usize]);

        const SIZE: usize = 20;
        let mut p1 = RealCodedIndividual::new();
        p1.init(SIZE, &mut rng);
        let mut p2 = RealCodedIndividual::new();
        p2.init(SIZE, &mut rng);

        let mut c1 = RealCodedIndividual::new();
        c1.init(SIZE, &mut rng);
        let mut c2 = RealCodedIndividual::new();
        c2.init(SIZE, &mut rng);
        cross_blx_alpha(&p1, &p2, &mut c1, &mut c2, 0.1f32, &mut rng);

        let p1_genes = p1.to_vec().unwrap();
        let p2_genes = p2.to_vec().unwrap();
        let c1_genes = c1.to_vec().unwrap();
        let c2_genes = c2.to_vec().unwrap();
        for k in 0..SIZE {
            assert!(c1_genes[k] != p1_genes[k]);
            assert!(c1_genes[k] != p2_genes[k]);
            assert!(c2_genes[k] != p1_genes[k]);
            assert!(c2_genes[k] != p2_genes[k]);
        }
    }

    #[test]
    fn test_gauss_mutation() {
        const SIZE: usize = 10;
        const TRIALS: usize = 1000;

        let sqrt_trials = (TRIALS as f32).sqrt();
//        let mut rng = rand::thread_rng();
        let mut rng = StdRng::from_seed(&[0 as usize]);
        let mut sigma = 0f32;
        while sigma <= 0.5f32 {
            let mut p1 = RealCodedIndividual::new();
            p1.genes = vec![0f32; SIZE];
            for _ in 0..TRIALS {
                mutate_gauss(&mut p1, 1f32, sigma, &mut rng);
            }
            // the resulting distribution should be gaussian with SD = sqrt(1000) * sigma
            let max_dist = sqrt_trials * sigma;
            let (max_val, min_val) = (max(&p1.genes), min(&p1.genes));
            assert!(max_val <= 4f32 *max_dist);
            assert!(min_val >= -4f32*max_dist);
            assert!(max_val >= 4f32*sigma);
            assert!(min_val <= -4f32*sigma);

            sigma += 0.1f32;
        }
    }

    #[test]
    fn test_uniform_mutation() {
        const SIZE: usize = 10;
        const TRIALS: usize = 100;

        let sqrt_trials = (TRIALS as f32).sqrt();
        //        let mut rng = rand::thread_rng();
        let mut rng = StdRng::from_seed(&[0 as usize]);
        let mut sigma = 0f32;
        for _ in 0..TRIALS {
            while sigma <= 0.5f32 {
                let mut p1 = RealCodedIndividual::new();
                p1.genes = vec![0f32; SIZE];
                mutate_uniform(&mut p1, 1f32, sigma, &mut rng);

                let norm = dot(&p1.genes, &p1.genes);
                assert!(norm <= sigma*sigma*(SIZE as f32));

//                // TODO: improve the test by utilizing CLT.
//                let max_dist = sqrt_trials * sigma;
//                let (max_val, min_val) = (max(&p1.genes), min(&p1.genes));
//                assert!(max_val <= 4f32 *max_dist);
//                assert!(min_val >= -4f32*max_dist);
//                assert!(max_val >= 4f32*sigma);
//                assert!(min_val <= -4f32*sigma);
                sigma += 0.1f32;
            }
        }
    }

    #[test]
    fn test_optimization_sphere() {
        let pop_size = 20u32;
        let problem_dim = 10u32;
        let problem = SphereProblem{};

        let gen_count = 10u32;
        let mut settings = EASettings::new(pop_size, gen_count, problem_dim);
//        settings.x_type = CrossoverOperator::Arithmetic;
        settings.mut_type = MutationOperator::Uniform;
        let mut ga: GA<SphereProblem> = GA::new(&problem);
        let res = ga.run(settings).expect("Error during GA run");
        for k in 1..res.avg_fitness.len() {
            assert!(res.avg_fitness[k-1] >= res.avg_fitness[k]);
        }
    }

    #[test]
    fn test_multi_optimization_sphere() {
        let runs_num = 5u32;
        let pop_size = 20u32;
        let problem_dim = 10u32;
        let problem = SphereProblem{};

        let gen_count = 10u32;
        let mut settings = EASettings::new(pop_size, gen_count, problem_dim);
        settings.mut_type = MutationOperator::Uniform;
        let mut ga: GA<SphereProblem> = GA::new(&problem);
        let res = ga.run_multiple(settings, runs_num).expect("No results from multiple runs");
        // println!("{:?}", res);

        assert!(res.run_count == runs_num);
    }
}