-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGenetic Algorithm (GA)
200 lines (148 loc) · 6.83 KB
/
Genetic Algorithm (GA)
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
#### define fitness function for GA ####
# trt is the ID of polygon/pixels selected for treatment, inf is the infected raster file
pops= function(trt,inf){
trt=hzd3[trt,]
trt=gUnionCascaded(trt)
toRa=rasterize(trt,inf,field=1,background=0)
trt=list(as.matrix(toRa))
df=data.frame(matrix(0,nrow=20,ncol=1))
for (j in 1:20){
random_seed= j*1000+50
data <- pops_model(random_seed = random_seed,
use_lethal_temperature = use_lethal_temperature,
lethal_temperature = lethal_temperature,
lethal_temperature_month = lethal_temperature_month,
infected = infected_speci[[1]],
susceptible = susceptible_speci[[1]],
total_plants = total_pl[[1]],
mortality_on = mortality_on,
mortality_tracker = infected_speci[[1]]*0,
mortality = infected_speci[[1]]*0,
treatment_maps = trt,
treatment_dates = c("2019-03-01"),
pesticide_duration=c(0),
resistant = infected_speci[[1]]*0,
weather = weather,
temperature = temperature,
weather_coefficient = wc,
ew_res = ew_res, ns_res = ns_res, num_rows = num_rows, num_cols = num_cols,
time_step = time_step, reproductive_rate = reproductive_rate[[2]],
mortality_rate = mortality_rate, mortality_time_lag = mortality_time_lag,
season_month_start = season_month_start, season_month_end = season_month_end,
start_date = start_time, end_date = end_time,
treatment_method = "all infected",
natural_kernel_type = natural_kernel_type[[1]], anthropogenic_kernel_type = anthropogenic_kernel_type[[1]],
use_anthropogenic_kernel = use_anthropogenic_kernel, percent_natural_dispersal = percent_natural_dispersal[[1]],
natural_distance_scale = natural_distance_scale[[2]], anthropogenic_distance_scale = anthropogenic_distance_scale[[1]],
natural_dir = natural_dir[[1]], natural_kappa = natural_kappa[[1]],
anthropogenic_dir = anthropogenic_dir[[1]], anthropogenic_kappa = anthropogenic_kappa[[1]],output_frequency = "year")
area=data$area_infected
area=area/10000
df[j,1]=area
}
mean=mean(df[1:20,1])
return(mean)
}
#### As GA is based on population, which means for each run, GA need to get fitness value for all solutions,
#### here define a parallel function to run fitness function
popsPara= function(pop,inf,n=dim(pop)[1],ncores){
ft=data.frame(matrix(0,nrow=n,ncol=1))
colnames(ft)="Fitness"
cl=makeCluster(ncores)
registerDoParallel(cl)
result=foreach(k=1:n,.combine = rbind,.export = c("pops","pop","hzd3","inf","use_lethal_temperature","lethal_temperature","lethal_temperature_month",
"infected_speci","susceptible_speci","total_pl","mortality_on","weather","temperature","wc",
"ew_res","ns_res","num_rows","num_cols","time_step","reproductive_rate","mortality_rate","mortality_time_lag",
"season_month_start", "season_month_end","start_time","end_time","natural_kernel_type","anthropogenic_kernel_type",
"use_anthropogenic_kernel", "percent_natural_dispersal","natural_distance_scale", "anthropogenic_distance_scale",
"natural_dir", "natural_kappa","anthropogenic_dir", "anthropogenic_kappa"),.packages = c("rgdal","raster","PoPS","rgeos"))%dopar%
{
vars=unlist(as.list(pop[k,]))
return(pops(vars,inf))
}
stopCluster(cl)
ft=data.frame(result)
colnames(ft)="Fitness"
return(ft)
}
#### define function to generate initial population, important for GA to some extent ####
inipop=function(nvar=150,sele=109,psize=2000){
id=1:nvar
pop=as.data.frame(matrix(0,nrow=psize,ncol=sele))
n0=psize/2
wt=c(nvar:1)
for (i in 1:n0){
pop[i,1:sele]=as.numeric(sample(id,109,prob=wt))
}
n1=floor(sele*0.6)
n2=floor(sele*0.2)
n3=floor(sele*0.1)
n4=sele-n1-n2-n3
n5=nvar-sele-15
wt=c(rep(6,n1),rep(5,n2),rep(4,n3),rep(1,n4),rep(1,n5))
for (i in 1:n0){
pop[i+n0,1:109]=as.numeric(sample(id,109,prob=wt))
}
pop=as.matrix(pop)
pop[1,1:sele]=1:sele
return(pop)
}
#### define function for mutation and crossover ####
crossOverMute=function(N=150,inputVars,ft,nbest=50,psize=200,sele=109,cross_number=50){
ft2=ft[order(ft$Fitness,decreasing = F),]
selecVars=inputVars[order(ft$Fitness,decreasing = F),]
np=psize-nbest
varNew=data.frame(matrix(0,nrow=np,ncol=sele))
## select best cross_number solutions for crossover
selecVars2=selecVars[1:cross_number,]
psize2=cross_number
for (k in 1:np){
ids=sample(1:psize2,2,prob=c(psize2:1))
var2=selecVars2[ids,]
ids=c(var2[1,],var2[2,])
ids2=unlist(unique(ids))
l=length(ids2)
ids2=ids2[order(ids2,decreasing = F)]
nvar=ids2[sample(1:l,sele)]
varNew[k,1:sele]=nvar[1:sele]
# mutation
mu=sample(c(1,2),1,prob=c(8,2))
if (mu==2){
df=setdiff(c(1:N),nvar)
df=df[order(df,decreasing = F)]
muN=sample(df,1,prob=c(length(df):1))
nvar2=nvar[order(nvar,decreasing = F)]
muVar=sample(1:length(nvar2),1,prob=c(1:length(nvar2)))
nvar2[muVar]=muN
varNew[k,1:sele]=nvar2[1:sele]
}
}
colnames(selecVars)=colnames(varNew)
newVar=rbind(selecVars[1:nbest,],varNew)
new_ft=data.frame(matrix(0,nrow=psize,ncol=1))
new_ft[1:nbest,1]=ft2[1:nbest]
results=list(newVar,new_ft)
return(results)
}
############ GA function that wrap all funtions above ############
GA= function(number_generation=100,nvar=150,sele=109,psize=200,nbest = 50,cross_number = 50){
pop=inipop(nvar=nvar,sele=sele,psize=psize)
ftPop=popsPara(pop,inf,n=dim(pop)[1])
for (g in 1:number_generation){
pop_bestft=crossOverMute(N=nvar,inputVars=pop,ft=ftPop,nbest = nbest,psize=psize,sele=sele,cross_number = cross_number)
pop=pop_bestft[[1]]
bestft=pop_bestft[[2]]
n=dim(pop)[1]
pop2=pop[(nbest+1):n,]
ftPop2=popsPara(pop2,inf)
ftPop=c(bestft[1:nbest,1],unlist(ftPop2[,1]))
ftPop=as.data.frame(ftPop)
colnames(ftPop)="Fitness"
print(min(ftPop))
}
bestFT=min(ftPop)
bestPP=as.vector(pop[ftPop$Fitness==bestFT,])
results=list(bestPP,bestFT)
return(results)
}
GA(50,150,109,200,50,100)