gatbxr-package: Gentic Algorithm Toolbox Implemented by R

Description Details Author(s) Examples

Description

R version of genetic toolbox for implementing a wide range of genetic algorithm methods.

Details

To create populations:

Fitness assignment:

Selection functions:

Mutation operators:

Crossover operators:

Subpopulation support:

Utility functions:

Author(s)

David Zhao wethenwethen@gmail.com

Examples

 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
## Simple Genetic Algorithm
## 
## Test objective function is De Jong's first test funciton.
## Binary representation for the individuals is used.
##
## Author:     Hartmut Pohlheim
## History:    23.03.94     file created
##             15.01.03     tested under MATLAB v6 by Alex Shenfield
##             06.06.16     Re-modified for R by David Zhao

require("gatbxr")

NIND = 40;           ## Number of individuals per subpopulations
MAXGEN = 300;        ## max Number of generations
GGAP = 0.9;          ## Generation gap, how many new individuals are created
SEL_F = 'sus';       ## Name of selection function
XOV_F = 'xovsp';     ## Name of recombination function for individuals
MUT_F = 'mut';       ## Name of mutation function for individuals
OBJ_F = 'objfun1';   ## Name of function for objective values

objfun1 <- function(Chrom){
  res <- apply(Chrom * Chrom,1,sum)
  return(res)
}

## set boundaries of objective function
   lb=-512;
   ub=512;

## Number of variables of objective function, in OBJ_F defined
   NVAR = 20;   

## Build fielddescription matrix
   PRECI = 20;    ## Precisicion of binary representation
   FieldDD = list(prec=PRECI,lb=lb,ub=ub,code="gray",scale="arith",lbin=TRUE,ubin=TRUE);

## Create population
   Chrom = crtbp(NIND, NVAR*PRECI)$Chrom;
   ObjV = objfun1(bs2rv(Chrom,FieldDD));

## reset count variables
   gen = 0;
   Best = rep(NA,MAXGEN)

## Iterate population
   while (gen < MAXGEN){
     ## Calculate objective function for population
     Best[gen+1] = min(ObjV);
     
     ## Fitness assignement to whole population
     FitnV = ranking(ObjV);
     
     ## Select individuals from population
     SelCh = select(SEL_F, Chrom, FitnV, GGAP);
     
     ## Recombine selected individuals (crossover)
     SelCh=recombin(XOV_F, SelCh);
     
     ## Mutate offspring
     SelCh=mutate(MUT_F, SelCh);
     
     ##Evaluate offspring, call objective function
     ObjVSel = objfun1(bs2rv(SelCh,FieldDD));
     
     ## Insert offspring in population replacing parents
     rs = reins(Chrom, SelCh,1,c(1,1),ObjV,ObjVSel);
     Chrom = rs$Chrom;
     ObjV  = rs$ObjVCh

     gen = gen + 1;
   }
## End of script

drizztxx/gatbxr documentation built on Dec. 27, 2021, 2:26 a.m.