@@ -58,54 +58,72 @@ function initial_state(method::GA, options, objfun, population)
58
58
return GAState (N, eliteSize, minfit, fitness, copy (population[fitidx]))
59
59
end
60
60
61
- function update_state! (objfun, constraints, state, population :: AbstractVector{IT} , method:: GA , options, itr) where {IT}
62
- @unpack populationSize,crossoverRate,mutationRate,ɛ,selection,crossover,mutation = method
61
+ function update_state! (objfun, constraints, state, parents :: AbstractVector{IT} , method:: GA , options, itr) where {IT}
62
+ populationSize = method. populationSize
63
63
evaltype = options. parallelization
64
64
rng = options. rng
65
- offspring = similar (population )
65
+ offspring = similar (parents )
66
66
67
- # Select offspring
68
- selected = selection (state. fitpop, populationSize, rng= rng)
67
+ # select offspring
68
+ selected = method . selection (state. fitpop, populationSize, rng= rng)
69
69
70
- # Perform mating
71
- offidx = randperm (rng, populationSize)
70
+ # perform mating
72
71
offspringSize = populationSize - state. eliteSize
73
- for i in 1 : 2 : offspringSize
74
- j = (i == offspringSize) ? i- 1 : i+ 1
75
- if rand (rng) < crossoverRate
76
- offspring[i], offspring[j] = crossover (population[selected[offidx[i]]], population[selected[offidx[j]]], rng= rng)
77
- else
78
- offspring[i], offspring[j] = population[selected[i]], population[selected[j]]
79
- end
80
- end
72
+ recombine! (offspring, parents, selected, method, offspringSize)
81
73
82
74
# Elitism (copy population individuals before they pass to the offspring & get mutated)
83
75
fitidxs = sortperm (state. fitpop)
84
76
for i in 1 : state. eliteSize
85
77
subs = offspringSize+ i
86
- offspring[subs] = copy (population [fitidxs[i]])
78
+ offspring[subs] = copy (parents [fitidxs[i]])
87
79
end
88
80
89
- # Perform mutation
90
- for i in 1 : offspringSize
91
- if rand (rng) < mutationRate
92
- mutation (offspring[i], rng= rng)
93
- end
94
- end
81
+ # perform mutation
82
+ mutate! (offspring, method, constraints, rng= rng)
95
83
96
- # Create new generation & evaluate it
97
- for i in 1 : populationSize
98
- population[i] = apply! (constraints, offspring[i])
99
- end
100
84
# calculate fitness of the population
101
- value! (objfun, state. fitpop, population)
102
- # apply penalty to fitness
103
- penalty! (state. fitpop, constraints, population)
85
+ evaluate! (objfun, state. fitpop, offspring, constraints)
104
86
105
- # find the best individual
87
+ # select the best individual
106
88
minfit, fitidx = findmin (state. fitpop)
107
- state. fittest = population [fitidx]
89
+ state. fittest = parents [fitidx]
108
90
state. fitness = state. fitpop[fitidx]
91
+
92
+ # replace population
93
+ parents .= offspring
109
94
110
95
return false
111
96
end
97
+
98
+ function recombine! (offspring, parents, selected, method, n= length (selected);
99
+ rng:: AbstractRNG = Random. GLOBAL_RNG)
100
+ mates = ((i,i == n ? i- 1 : i+ 1 ) for i in 1 : 2 : n)
101
+ for (i,j) in mates
102
+ p1, p2 = parents[selected[i]], parents[selected[j]]
103
+ if rand (rng) < method. crossoverRate
104
+ offspring[i], offspring[j] = method. crossover (p1, p2, rng= rng)
105
+ else
106
+ offspring[i], offspring[j] = p1, p2
107
+ end
108
+ end
109
+
110
+ end
111
+
112
+ function mutate! (population, method, constraints;
113
+ rng:: AbstractRNG = Random. GLOBAL_RNG)
114
+ n = length (population)
115
+ for i in 1 : n
116
+ if rand (rng) < method. mutationRate
117
+ method. mutation (population[i], rng= rng)
118
+ end
119
+ apply! (constraints, population[i])
120
+ end
121
+ end
122
+
123
+ function evaluate! (objfun, fitness, population, constraints)
124
+ # calculate fitness of the population
125
+ value! (objfun, fitness, population)
126
+ # apply penalty to fitness
127
+ penalty! (fitness, constraints, population)
128
+ end
129
+
0 commit comments