Description Details Author(s) Examples
R version of genetic toolbox for implementing a wide range of genetic algorithm methods.
To create populations:
crtbase
Create a base vector
crtbp
Create arbitrary discrete random populations
crtrp
Create real-valued initial population
Fitness assignment:
ranking
Generalised rank-based fitness assignment
scaling
Proportional fitness scailing
Selection functions:
reins
Uniform random and fitness-based reinsertion
rws
Roulette wheel selection
select
High-level selection routine
sus
Stochastic universal sampling
Mutation operators:
mut
Discrete mutation
mutate
High-level mutation function
mutbga
Real-value mutation
Crossover operators:
recdis
Discrete recombination
recint
Intermediate recombination
reclin
Line recombination
recmut
Line recombination with mutation features
recombin
High-level recombination operator
xovdp
Double-point crossover
xovdprs
Double-point reduced surrogate crossover
xovmp
General multi-point crossover
xovsh
Shuffle crossover
xovshrs
Shuffle reduced surrogate crossover
xovsp
Single-point crossover
xovsprs
Single-point reduced surrogate crossover
Subpopulation support:
migrate
exchange individuals between subpopulations
Utility functions:
bs2rv
binary string to real-value conversion
David Zhao wethenwethen@gmail.com
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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.