sefar 0.2.0

sefar is library for evolutionary optimization algorithms.
Documentation
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/// -------------------------------------------------------------------------------------
/// Plant competition optimization (PCO)
/// Implemented in Rust programming language by Saad Dahmani (s.dahmani@univ-bouira.dz)
/// Original Matlab code :
/// https://github.com/iman-aliabdi/PCO-Plant-Competition-Optimization
/// Plant competition optimization: A novel metaheuristic algorithm
/// Piper's link : https://onlinelibrary.wiley.com/doi/abs/10.1111/exsy.12956
/// -------------------------------------------------------------------------------------
/// 

extern crate rand;

use rand::distributions::{Distribution, Uniform};
use rand::Rng;

use crate::core::eoa::EOA;
use crate::core::genome::Genome;
use crate::core::parameters::Parameters;
use crate::core::problem::Problem;
use crate::core::optimization_result::OptimizationResult;
use crate::common::*;

///
/// Plant competition optimization (PCO) (Sequential)
/// Reference:
/// Plant competition optimization: A novel metaheuristic algorithm
/// Piper's link : https://onlinelibrary.wiley.com/doi/abs/10.1111/exsy.12956
/// Original Matlab code:
/// https://github.com/iman-aliabdi/PCO-Plant-Competition-Optimization
/// 
#[deprecated(note = "This version of PCO is note checked. Please use EO, PSO or GO instead.")]
#[derive(Debug)]
#[allow(dead_code)]
pub struct PCO<'a, T : Problem> {
     pub problem : &'a mut T,
     pub params : &'a PCOparams<'a>,
     pub optimization_result : OptimizationResult,
}

#[allow(dead_code)]
impl<'a, T : Problem> PCO<'a, T>{

    pub fn new(settings :&'a PCOparams, problem : &'a mut T )->Self{       
        let result = OptimizationResult{
            best_genome : None,
            best_fitness :None,
            convergence_trend : None,
            computation_time : None,
            err_report : None, 
        };
       
        PCO { 
             problem,
             params: settings,
             optimization_result: result,            
        }
    }
}

#[allow(dead_code)]
impl<'a, T: Problem> EOA for PCO<'a, T> {
   
    fn run(&mut self)-> OptimizationResult{

        let n : usize = self.params.get_population_size();
        let dim : usize = self.params.get_dimensions();
        let ub  = self.params.get_upper_bounds();
        let lb = self.params.get_lower_bounds();

        let noi : usize = self.params.get_max_iterations();
        let max_plant_number : usize = self.params.max_plant_number;
        let vmax : f64 = self.params.vmax as f64;
        let alpha : f64 = self.params.alpha;
        let k :f64  = self.params.k;
        let miu : f64 = self.params.miu;

        //----------------------------------------
        //let rmax_vec : Vec<f64> = vec![0.0f64; dim];
        let mut rmax : f64 = 0.0;
        let mut r : Vec<f64> = Vec::new();

        let mut v : Vec<f64> = vec![0.0f64; n];
        let mut dv : Vec<f64> = Vec::new();// vec![0.0f64; n];
       
        let mut best : Vec<f64> = Vec::new();

        let mut nos : Vec<usize> = Vec::new();
                
        let mut f : Vec<f64> = Vec::new(); //let mut f : Vec<f64> = vec![0.0f64; n];
        let mut fn_vec : Vec<f64> = Vec::new();

        let mut fitness : Vec<f64> = Vec::new(); //vec![0.0f64; n];
        let mut fc : Vec<f64> = Vec::new(); //vec![0.0f64; n];
        
        let mut migrant_seeds_no : usize = 0;
        let mut migrant_plant :Vec<usize> = Vec::new();

        //--------------------------------------------
        let between = Uniform::from(0.0..=1.0);
        let mut rng = rand::thread_rng();
        //--------------------------------------------

        let maxteta : f64 = f64::exp(-1.0);
        let teta : f64 = maxteta.clone();
        let mut plant_number= n.clone();
        let mut iteration : usize = 1;

        let mut max_plant : usize = n.clone();
        let mut plant_number_old : usize =0;


        //-----------------------------------------
        
        /*  let mut i : usize = 0;
        for (a,b) in ub.iter().zip(lb.iter()){
            rmax[i] = a-b;
            i+=1;
        } */

        rmax = ub[0]-lb[0];

        println!("rmax : {:?}", rmax);

        //-------------------------------------------


        let mut plants = self.initialize(self.params);
        let x= plants.clone();

        randomize(&mut v);

        while plant_number <= max_plant_number && iteration <= noi {

            #[cfg(feature="report")]println!("---- Iter : [{}]", iteration);
            //for i=1:plantNumber
                //f(i)=fobj(plants(i,:));
            //end

            // Evaluation of candidate solutions:
            f.clear();

            for i in 0..plants.len() {
                f.push(self.problem.objectivefunction(&plants[i].genes));
            }

            // Calculate Fitness Coefficient=fc

           let min_f = match f.iter().min_by(|a, b| a.total_cmp(b)){
                Some(min_value) => *min_value,
                None => f64::MAX,
           };

            #[cfg(feature="report")] println!("fitness before sorting, f : {:?}", f);
            #[cfg(feature= "report")] println!("minf = min(f) = {}", min_f);

            best.push(min_f);

            let normf = f.iter().map(|&x| x * x).sum::<f64>().sqrt();

           fn_vec.clear();
            for i in 0..f.len() {
                fn_vec.push(f[i]/normf);                
            }

            fitness.clear();

            for i in 0..fn_vec.len() {
                fitness.push(1.0/(1.0 + fn_vec[i]));
            }

            let mx : f64 = fitness.iter().fold(f64::MIN, |mx, y| mx.max(*y));
            let mn : f64 = fitness.iter().fold(f64::MAX, |mn, y| mn.min(*y));

            //println!("mx : {}; mn : {}", mx,mn);
            
            fc.clear();
            
            if mx == mn {
                for i in 0..fitness.len() {
                    fc.push(fitness[i]/mx);
                }    
            }
            else {
                // fc=(fitness-mn)./(mx-mn);
                let dif_mx_mn = mx-mn;
                for i in 0..fitness.len() {
                    fc.push((fitness[i]-mn)/ dif_mx_mn);
                }
            }
            
            fitness.sort_by(|a,b| b.partial_cmp(a).unwrap());

            #[cfg(feature="report")] println!("fitness after sorting : {:?}", fitness);

            max_plant = plant_number;

            let mut survive : Vec<bool> = Vec::new();
            for &value in &fc {
                survive.push(value >= fc[max_plant-1]);
            }

            #[cfg(feature="report")] println!("survive : {:?}", survive );
          

            let mut  new_plant : Vec<Genome>= Vec::new();
            
            for i in 0..survive.len() {
                if survive[i] == true {
                    new_plant.push(plants[i].clone());
                }
            }

            //plants=newPlant;
            plants = new_plant;

            //sz=size(newPlant);
            //let sz : usize = plants.len();
            //let mut x1 : Vec<f64> = Vec::new();
            //let mut y : Vec<f64> = Vec::new();

            //for j in 0..plants.len() {
                //x1.push(plants[j].genes[0]);
                //y.push(plants[j].genes[1]);
            //}

            plant_number = plants.len(); //x1.len();

            #[cfg(feature ="report")] println!("Iter {};  plant_number :{}", iteration, plant_number);

            // st=zeros(plantNumber,1);   
            let mut st : Vec<f64> = vec![0.0f64; plant_number];

            r.clear();
            dv.clear();

            for i in 0..plant_number {
                //Compute Neighborhood Radius
                //r(i)=teta*rmax*exp(1-(5*v(i))/vmax);

                r.push(teta * rmax*f64::exp(1.0-(alpha*v[i])/vmax));
                
                // non : number of neighbours
                let mut non : f64 =0.0;

                for j in 0..plant_number {
                    if euclidian_dist(&plants[i], &plants[j]) <= r[i] { // are neighbours in this case:
                        // st(i)=st(i)+v(j);
                        //non=non+1;
                        st[i] += v[j];
                        non +=1.0;                  
                    }
                }

                // dv(i)=fc(i)*k*(log(non*vmax)-log(st(i)));
                let tmpdvi = fc[i]*k*(f64::ln(non*vmax)- f64::ln(st[i]));
                dv.push(tmpdvi);

                // if v(i)+dv(i)<vmax
                //      v(i)=v(i)+dv(i);
                // else
                //      v(i)=vmax;
                // end

                if (v[i]+dv[i]) < vmax {
                    v[i] = v[i] + dv[i];
                } 
                else {
                    v[i] = vmax;
                }
            }

             // ----- SEED PRODUCTION ----------------
             // sumNos=0;
             let mut sum_nos : usize =0;
             nos.clear();

             for i in 0..plant_number {
                // NOS(i)=floor(v(i)+1);
                nos.push((v[i]+1.0).floor() as usize);
                
                //sumNos=sumNos+NOS(i);
                sum_nos += nos[i];

                for j in 0..nos[i]{
                    //RND=randi(dim);
                    let rnd = rng.gen_range(0..dim);
                    
                    //Temp=(plants(i,RND)-r(i))+2*r(i)*rand;
                    let rand01 = between.sample(&mut rng);
                    let  temp = (plants[i].genes[rnd]-r[i])+(2.0*r[i]* rand01);
                    
                    // seed=plants(i,:);
                    // seed(RND)=Temp;
                    // plants=[plants;seed];
                    // v=[v;rand];
                    let mut seed = plants[i].clone();
                    seed.genes[rnd]=temp;
                    plants.push(seed);

                    let rand010 = between.sample(&mut rng);
                    v.push(rand010);

                }
             }

             // -- SEED MIGRATION -------------------
             
             // migrantSeedsNoOld = migrantSeedsNo;
             let migrant_seeds_no_old = migrant_seeds_no.clone();
             
             //migrantSeedsNo=floor(miu*sumNos);
             migrant_seeds_no = (miu*sum_nos as f64).floor() as usize;

             //migrantPlantOld=migrantPlant;
             //let migrant_plant_old = migrant_plant.clone();
             if (plant_number+1) <= (plant_number + sum_nos) {

            
                //migrantPlant=randi([plantNumber+1,plantNumber+sumNos],1,migrantSeedsNo);
                migrant_plant = rand_vec(plant_number+1,plant_number + sum_nos, migrant_seeds_no);

                /* for i=1:migrantSeedsNo
                    temp=A+(B-A).*rand(1,dim);
                    plants(migrantPlant(i),:)=temp;
                end */
                
                for i in 0..migrant_seeds_no {
                    for j in 0..dim {
                        plants[migrant_plant[i]].genes[j] = lb[j] + (ub[j]-lb[j])*between.sample(&mut rng); 
                    }
                } 

            }

            //plantNumberOld=plantNumber;
            plant_number_old = plant_number;
            iteration +=1; 
        }

        let result = OptimizationResult{
            best_genome : None, //Some(Genome::new(0, self.params.dimensions)),
            best_fitness : None, //Some(-1111.1111),
            convergence_trend : Some(best), //Some(convergence_curve),
            computation_time : None, //Some(duration),
            err_report : None,
        };
        return result;   

    }    

}






#[derive(Debug, Clone)]
pub struct PCOparams<'a> {
    /// Number of initial plants.
    pub population_size : usize,

    /// Size of the problem (number of decision variables).
    pub dimensions : usize,

    /// Maximum number of iterations.
    pub max_iterations: usize,

    pub lower_bounds : &'a [f64],
    pub upper_bounds : &'a [f64],
    
    /// vmax : Maximum size of plants.
    pub vmax : usize,

    /// Maximum of Plants Number.
    pub max_plant_number : usize,

    /// Growth rate.
    /// It shoud be greather than the maximum value, Theta_max = e^-1=0.36788.
    pub theta : f64,


    /// Parameter (can be considered as constant too).
    /// Defautl value : alpha = 5.0. 
    pub alpha : f64,
    
    /// Growth parameter. 
    pub k : f64,

    ///Seed migration rate.
    pub miu : f64,    
}

#[allow(dead_code)]
impl<'a> PCOparams<'a>{
    pub fn new(p_size: usize, dim : usize, max_iter : usize, lb : &'a [f64], 
    ub : &'a [f64], vmax : usize, max_plant_number : usize, theta : f64, alpha : f64, k : f64, miu : f64)-> Result<PCOparams<'a>, String> {
                      
        let params = PCOparams{
            population_size : p_size,
            dimensions : dim,
            max_iterations : max_iter,
            lower_bounds : lb,
            upper_bounds : ub,
            vmax,
            max_plant_number,
            theta : f64::min(theta, f64::exp(-1.0)),
            alpha,
            k,
            miu,
        };

       match params.check() {
           Err(error)=> Err(error),
           Ok(())=> Ok(params),
       }
    }
}

impl<'a> Parameters for PCOparams<'a> {

    fn get_population_size(&self)->usize{
         self.population_size
     }
 
    fn get_dimensions(&self)-> usize {
         self.dimensions
     }
 
     fn get_max_iterations(&self)->usize{
         self.max_iterations
     }
 
     fn get_lower_bounds(&self)-> Vec<f64>{
         self.lower_bounds.to_vec()
     }
 
     fn get_upper_bounds(&self)-> Vec<f64>{
         self.upper_bounds.to_vec()
     }        
 }
 

impl<'a> Default for PCOparams<'a>{

    ///
    /// Return default values of parameters, as following :
    /// 
    /// ~~~
    /// 
    ///  use sefar::sequential_algos::pco::*;
    /// 
    ///  PCOparams{
    ///     population_size : 10,
    ///     dimensions : 3,
    ///     max_iterations : 100,
    ///     lower_bounds : &[-100.0f64, -100.0, -100.0],
    ///     upper_bounds : &[100.0f64, 100.0, 100.0],
    ///     vmax : 20,
    ///     max_plant_number : 500,
    ///     theta : 0.005,
    ///     alpha : 5.0,
    ///     k : 0.1,
    ///     miu : 0.05,
    /// };
    /// ~~~
    /// 
    fn default()->Self{
        PCOparams{
            population_size : 10,
            dimensions : 3,
            max_iterations : 100,
            lower_bounds : &[-100.0f64, -100.0, -100.0],
            upper_bounds : &[100.0f64, 100.0, 100.0],
            vmax : 20,
            max_plant_number : 500,
            theta : 0.005,
            alpha : 5.0,
            k : 0.1,
            miu : 0.05,
        }
    }
}