Skip to content

Commit

Permalink
use numpy vectorized calls to speed up crossover and mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
wright committed Sep 3, 2020
1 parent 5c2a197 commit ca539ec
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions openmdao/drivers/differential_evolution_driver.py
Expand Up @@ -578,16 +578,17 @@ def execute_ga(self, x0, vlb, vub, pop_size, max_gen, random_state, F=0.5, Pc=0.
while c == ii or c == a or c == b:
c = rng.integers(0, self.npop)

# randomly select chromosome index for crossover
# randomly select chromosome index for forced crossover
r = rng.integers(0, self.lchrom)

# crossover and mutation
population[ii] = parentPop[ii] # start the same as parent
# clip mutant so that it cannot be outside the bounds
mutant = np.clip(parentPop[a] + F * (parentPop[b] - parentPop[c]), vlb, vub)
for j in range(self.lchrom):
# sometimes replace parent's feature with mutant's
if rng.random() < Pc or j == r:
population[ii][j] = mutant[j]
# sometimes replace parent's feature with mutant's
rr = rng.random(self.lchrom)
idx = np.where(rr < Pc)
population[ii][idx] = mutant[idx]
population[ii][r] = mutant[r] # always replace at least one with mutant's

return xopt, fopt, nfit

0 comments on commit ca539ec

Please sign in to comment.