I'm implementing a differential evolution algorithm and when it comes to evolving a population, the page I am referencing is vague on how the new population is generated.
https://en.wikipedia.org/wiki/Differential_evolution#Algorithm
The algorithm looks like the population is mutated during evolution.
# x, a, b, c are agents in the population
# subscriptable to find position in specific dimension
# pop is filled with Agent objects
for i, x in enumerate(pop):
[a, b, c] = random.sample(pop[:i]+pop[i+1:], 3)
ri = random.randint(0, len(x))
new_position = []
for j in range(len(x)):
if random.uniform(0, 1) < CR or j == ri:
new_pos.append(a[j] + (F * (b[j] - c[j])))
else:
new_pos.append(x[j])
# Agent() class constructor for agent, takes position as arg
new_agent = Agent(new_pos)
if fitness(new_agent) <= fitness(x):
pop[i] = new_agent # replace x with new_agent
But I wonder if instead it means a new population is made and then populated iteratively:
new_pop = []
for i, x in enumerate(pop):
[a, b, c] = random.sample(pop[:i]+pop[i+1:], 3)
ri = random.randint(0, len(x))
new_position = []
for j in range(len(x)):
if random.uniform(0, 1) < CR or j == ri:
new_pos.append(a[j] + (F * (b[j] - c[j])))
else:
new_pos.append(x[j])
new_agent = Agent(new_pos)
if fitness(new_agent) <= fitness(x):
new_pop.append(new_agent)
else:
new_pop.append(x)
pop = new_pop
Note new_pop
is made, and filled with agents as the for
loop continues.
The first allows previously evolved agents to be used again in the same generation; in other words, the population is changed during the evolution. The second doesn't allow updated agents to be re-used, and only at the end is the original population changed.
Which is it?