Image adapted from Plasmodesmata and the control of symplastic transport, Roberts 2003
def generate_random_pore_configs():
return [np.array(points_on_cube_surface(pore_N, np.cbrt(v))) for _ in range(generation_size)]
This function creates an initial population of random pore configurations, essential for starting the genetic algorithm.
def mutate(pores):
mutations = np.random.random(len(pores))
locs = np.where(mutations < mutation_rate)
pores[locs] = np.array(points_on_cube_surface(len(locs), np.cbrt(v)))
Mutations introduce variations in the population, crucial for the evolution process in the genetic algorithm.
def evaluate_individual(pores):
setups = [pores for _ in range(reps)]
with multiprocessing.Pool(processes=os.cpu_count()) as pool:
res = list(pool.imap(calc_escape_time, setups))
return np.mean(res)
Evaluating the fitness of each individual in the population to determine their effectiveness in solving the problem.
def choose_parents(times):
return times.argsort()[:2]
Selecting the fittest individuals from the population to breed and produce the next generation.
def breed(A, B):
individuals = []
for _ in range(generation_size):
inherit = np.random.random(len(A))
C = np.copy(A)
locs = np.where(inherit < parent_inherit_divide)
C[locs] = B[locs]
individuals.append(C)
return individuals
This function combines the genetic information of two parents to create new offspring, introducing new variations.
for gen in tqdm(range(n_generations)):
fitness = evaluate_generation(cur_gen)
idA, idB = choose_parents(fitness)
for id, p in zip(['A','B'],[cur_gen[idA], cur_gen[idB]]):
t = np.mean(fitness[idA]) if id == 'A' else np.mean(fitness[idB])
if id == 'A':
print(f"Best time from generation {gen} is: {t}")
np.savetxt(f"./parents/pore_config_{gen}_{id}_{t}.csv", p)
new_gen = breed(cur_gen[idA], cur_gen[idB])
mutate_pop(new_gen)
# add randomness to pool to shake things up!
new_gen.append(generate_random_pore_configs()[0])
cur_gen = new_gen
The main loop of the algorithm, where each generation is evaluated, parents are selected, offspring are created, and mutations are applied.