@@ -58,54 +58,72 @@ function initial_state(method::GA, options, objfun, population)
5858 return GAState (N, eliteSize, minfit, fitness, copy (population[fitidx]))
5959end
6060
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
6363 evaltype = options. parallelization
6464 rng = options. rng
65- offspring = similar (population )
65+ offspring = similar (parents )
6666
67- # Select offspring
68- selected = selection (state. fitpop, populationSize, rng= rng)
67+ # select offspring
68+ selected = method . selection (state. fitpop, populationSize, rng= rng)
6969
70- # Perform mating
71- offidx = randperm (rng, populationSize)
70+ # perform mating
7271 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)
8173
8274 # Elitism (copy population individuals before they pass to the offspring & get mutated)
8375 fitidxs = sortperm (state. fitpop)
8476 for i in 1 : state. eliteSize
8577 subs = offspringSize+ i
86- offspring[subs] = copy (population [fitidxs[i]])
78+ offspring[subs] = copy (parents [fitidxs[i]])
8779 end
8880
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)
9583
96- # Create new generation & evaluate it
97- for i in 1 : populationSize
98- population[i] = apply! (constraints, offspring[i])
99- end
10084 # 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)
10486
105- # find the best individual
87+ # select the best individual
10688 minfit, fitidx = findmin (state. fitpop)
107- state. fittest = population [fitidx]
89+ state. fittest = parents [fitidx]
10890 state. fitness = state. fitpop[fitidx]
91+
92+ # replace population
93+ parents .= offspring
10994
11095 return false
11196end
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