-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_GeneticAlg.int.R
286 lines (229 loc) · 9.35 KB
/
my_GeneticAlg.int.R
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
source("GeneticAlg_helpers.R")
my_GeneticAlg.int <- function(genomeLen, codonMin, codonMax,
genomeMin=rep.int(codonMin, genomeLen), genomeMax=rep.int(codonMax, genomeLen),
suggestions=NULL,
popSize=50,
iterations=100, terminationCost=NA,
mutationChance= 1/(genomeLen+1),
elitism=floor(popSize/10),
geneCrossoverPoints=NULL,
monitorFunc=NULL,
evalFunc,
allowrepeat = TRUE,
showSettings=FALSE, verbose=FALSE,
plapply = lapply) {
# Optimizes an Integer chromosome using a genetic algorithm.
#
# popSize = the population size
# iterations = number of generations
# terminationCost = The cost (error) that if reached, the GA should termiante
# mutationChance = chance that a var in the string gets mutated
# geneCrossoverPoints = An array determining the genes to be swapped in crossover
#
# Partially based on "R Based Genetic Algorithm (genalg package)""
# http://cran.r-project.org/web/packages/genalg/
is.verbose = verbose
verbose = function(...) { if (is.verbose) cat(...)}
if (is.null(evalFunc)) {
stop("A evaluation function must be provided. See the evalFunc parameter.");
}
stopifnot(genomeLen > 1)
# do a variaty of sanity checks first
verbose("Testing the sanity of parameters...\n");
if (length(genomeMin) != length(genomeMax)) {
stop("The vectors genomeMin and genomeMax must be of equal length.");
}
if (popSize < 5) {
stop("The population size must be at least 5.");
}
if (iterations < 1) {
stop("The number of iterations must be at least 1.");
}
if (!(elitism < popSize)) {
stop("The population size must be greater than the elitism.");
}
if (elitism < 0) {
stop("elitism must be at least 0.");
}
if ((mutationChance < 0) | (mutationChance > 1)) {
stop("mutationChance must be between 0 and 1.");
}
if (!is.null(geneCrossoverPoints)) {
if (!is.numeric(geneCrossoverPoints) | length(geneCrossoverPoints) != genomeLen) {
stop("Invalid geneCrossoverPoints.");
}
}
if (showSettings) {
verbose("The start conditions:\n");
result = list(genomeMin=genomeMin, genomeMax=genomeMax, suggestions=suggestions,
popSize=popSize, iterations=iterations,
elitism=elitism, mutationChance=mutationChance);
class(result) = "rbga";
cat(summary(result));
} else {
verbose("Not showing GA settings...\n");
}
##########
# Creation
population = matrix(nrow=popSize, ncol=genomeLen);
if (!is.null(suggestions)) {
verbose("Adding suggestions to first population...\n");
suggestionCount = nrow(suggestions)
population[1:suggestionCount,] = population[i,]
verbose("Filling others with random values in the given domains...\n");
} else {
verbose("Starting with random values in the given domains...\n");
suggestionCount = 0
}
for (i in (suggestionCount+1):popSize) {
population[i,] = ga.new.chromosome(genomeLen, genomeMin, genomeMax, allowrepeat)
}
############################################################################
# do iterations
bestEvals = rep(NA, iterations);
meanEvals = rep(NA, iterations);
evalVals = rep(NA, popSize);
###added on 12/18
stagnantCount<-0
stagnantList<-rep(NA, ((iterations)/20)*4);
sLen<-length(stagnantList)
for (iter in 1:iterations) {
verbose(paste("Starting iteration", iter, "\n"));
cat("Starting iteration", iter, "\n")
##########
# Evaluation
verbose("Calucating evaluation values... ");
to.eval.Ids = which(is.na(evalVals))
evalVals[to.eval.Ids] = unlist(plapply(to.eval.Ids,
function(i, population, evalFunc) evalFunc(population[i, ]),
population, evalFunc))
# check for invalid items
if ((!all(is.numeric(evalVals))) |
any(is.na(evalVals)) |
any(is.nan(evalVals))) {
stop("Invalid cost function return value (NA or NaN).")
}
# extract statistics about generation
bestEvals[iter] = min(evalVals);
meanEvals[iter] = mean(evalVals);
bestInd = which.min(evalVals)
verbose(" done.\n");
collect.results <- function() {
settings = list(genomeMin=genomeMin, genomeMax=genomeMax,
popSize=popSize, elitism=elitism, geneCrossoverPoints = geneCrossoverPoints,
iterations=iterations, suggestions=suggestions,
mutationChance=mutationChance)
pop.info = list(population=population, evaluations=evalVals, best=bestEvals, mean=meanEvals, currentIteration=iter)
best = list(genome=population[bestInd,], cost = evalVals[bestInd]);
ret = list(settings = settings, population = pop.info, best = best)
##modifier by Effat, 11/8
# class(ret) = "GeneticAlg.int";
return (ret)
}
if (!is.null(monitorFunc)) {
verbose("Sending current state to rgba.monitor()...\n");
# report on GA results
monitorFunc(collect.results());
}
##########
# check termination conditions
if (iter == iterations) {
verbose("End of generations iteration reached.\n");
break
}
if (!is.na(terminationCost)) {
if (bestEvals[iter] <= terminationCost) {
verbose("Cost better than termination cost reached.\n");
break
}
}
##########
# Selection
verbose("Creating next generation...\n");
newPopulation = matrix(nrow=popSize, ncol=genomeLen);
newEvalVals = rep(NA, popSize);
verbose(" sorting results...\n");
sortedEvaluations = sort(evalVals, index=TRUE);
sortedPopulation = matrix(population[sortedEvaluations$ix,], ncol=genomeLen);
# save the best
if (elitism > 0) {
verbose(" applying elitism...\n");
newPopulation[1:elitism,] = sortedPopulation[1:elitism,];
newEvalVals[1:elitism] = sortedEvaluations$x[1:elitism]
} # ok, save nothing
##########
# Crossover
# fill the rest by doing crossover
verbose(" applying crossover...\n");
for (child in (elitism+1):popSize) {
# ok, pick two random parents using roulette wheel probability
parentProb = dnorm(1:popSize, mean=0, sd=(popSize/3))
parentIDs = sample(1:popSize, 2, prob=parentProb)
parents = sortedPopulation[parentIDs,]
# default cross-over swaps genomes from a random point
if (is.null(geneCrossoverPoints)) {
crossOverPoint = sample(0:genomeLen,1)
if (crossOverPoint == 0) {
newPopulation[child, ] = parents[2,]
newEvalVals[child] = sortedEvaluations$x[parentIDs[2]]
} else if (crossOverPoint == genomeLen) {
newPopulation[child, ] = parents[1,]
newEvalVals[child] = sortedEvaluations$x[parentIDs[1]]
} else {
newPopulation[child, ] =
c(parents[1, 1:crossOverPoint],
parents[2, (crossOverPoint+1):genomeLen])
}
} else {
# swap genes based on the location of gene crossoverPoints
p2.indices = which(geneCrossoverPoints %% 2 != 0)
newPopulation[child, ] = parents[1,]
newPopulation[child, p2.indices] = parents[2, p2.indices]
}
}
if (!allowrepeat) {
for (i in (elitism+1):popSize) {
population[i,] = ga.unique.maker(population[i,], genomeMin, genomeMax)
}
}
population = newPopulation;
evalVals = newEvalVals;
##########
# Mutation
if (mutationChance > 0) {
verbose(" applying mutations... ");
mutationCount = 0;
for (object in (elitism+1):popSize) { # don't mutate the best
dempeningFactor = (iterations-iter)/iterations
mutResult <- my_mutation(population[object,], mutationChance, genomeLen,
codonMin, codonMax, allowrepeat,
dempeningFactor)
population[object, ] = mutResult;
evalVals[object] = NA;
mutationCount = mutationCount + 1;
}
verbose(paste(mutationCount, "mutations applied\n"));
}
########################### stagnation Count
stagnantCount<-stagnantCount+1
if(stagnantCount>sLen )
stagnantCount<-1
stagnantList[stagnantCount]<-evalVals[1]
isStagnant<-FALSE
sCount<-2
while((sCount<=sLen) && (iter> sLen)){
# print(paste0("mute2 ", sCount," ", sCount-1," ", stagnantList[sCount], " ",stagnantList[sCount-1]))
if(stagnantList[sCount]!=stagnantList[1]){
break;
}
sCount<-sCount+1
}
if(sCount>sLen)
isStagnant<-TRUE
if(isStagnant==TRUE)
break;
}###end for iteration
# report on GA results
result = collect.results()
return(result);
}