def algorithm_par_mp(self): """Subprogram for running parallel version of GA Requires MPI4PY""" global logger comm = MPI.COMM_WORLD rank = MPI.COMM_WORLD.Get_rank() if rank==0: if 'MA' in self.debug: debug = True else: debug = False self.algorithm_initialize() self.convergence = False convergence = False while not convergence: if rank==0: pop = self.population offspring = self.generation_set(self,pop) # Identify the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if ind.energy==0] #Evaluate the individuals with invalid fitness self.output.write('\n--Evaluate Structures--\n') else: invalid_ind=[] for i in range(len(invalid_ind)): if self.fitness_scheme=='STEM_Cost': if self.stem_coeff==None: ind = invalid_ind.pop() from MAST.structopt.tools.StemCalc import find_stem_coeff outs = find_stem_coeff(self,ind) ind = outs[1] self.stem_coeff = outs[0] self.output.write('stem_coeff Calculated to be: '+repr(self.stem_coeff)+'\n') pop.append(ind) ind=invalid_ind[i] if 'MA' in self.debug: write_xyz(self.debugfile,ind[0],'Individual to fitness_switch') outs = switches.fitness_switch([self,ind]) self.output.write(outs[1]) invalid_ind[i]=outs[0] self.output.flush() if rank==0: pop.extend(invalid_ind) pop = self.generation_eval(pop) self.write() convergence = comm.bcast(self.convergence, root=0) if rank==0: end_signal = self.algorithm_stats(self.population) else: end_signal = None end_signal = comm.bcast(end_signal, root=0) return end_signal
def mutation_dups_adapt_stem(pop, Optimizer): """Predator function that removes individuals based on fitness and mutates replacements """ fitlist = [one.fitness for one in pop] nfitlist, nindices = remove_duplicates(fitlist, Optimizer.demin) STR = '' newpop = [] if len(nfitlist) != len(fitlist): STR+='Predator: Removed total of '+repr(len(fitlist)-len(nfitlist))+' from population\n' otherlist = [] for i in range(len(pop)): if i not in nindices: STR+='Predator: Removed '+repr(pop[i].history_index)+'\n' otherlist.append(pop[i]) else: newpop.append(pop[i]) while len(newpop) < Optimizer.nindiv: indiv = random.choice(otherlist).duplicate() indiv, scheme = moves_switch(indiv,Optimizer) indiv.energy = 1000 indiv.fitness = 1000 newpop.append(indiv) STR+='Predator: Adding mutated duplicates to new pop history='+indiv.history_index+'\n' nindices.append(indiv.index) nindices.sort() if Optimizer.natural_selection_scheme=='fussf': for ind in newpop: if ind.fingerprint == 0: ind.fingerprint = get_fingerprint(Optimizer,ind,Optimizer.fpbin,Optimizer.fpcutoff) if 'lambda,mu' in Optimizer.algorithm_type: try: mark = [ index for index,n in enumerate(nindices) if n > Optimizer.nindiv-1][0] except: mark = Optimizer.nindiv Optimizer.mark = mark pop, str1 = lambdacommamu.lambdacommamu(newpop, Optimizer) STR+=str1 else: pop = selection_switch(newpop, Optimizer.nindiv, Optimizer.natural_selection_scheme, Optimizer) pop = get_best(pop,len(pop)) indiv = pop[0] if (indiv.fitness/indiv.energy <2.0): from MAST.structopt.tools.StemCalc import find_stem_coeff outs = find_stem_coeff(Optimizer,indiv) ind = outs[1] Optimizer.stem_coeff = outs[0] STR+='Readjusting STEM Coeff = {0}'.format(Optimizer.stem_coeff)) return pop, STR
def generation_set(self,pop): global logger self.calc = tools.setup_calculator(self) #Set up calculator for atomic structures #Set up calculator for fixed region calculations if self.fixed_region: self.static_calc = self.calc #May need to copy this self.calc = tools.setup_fixed_region_calculator(self) self.output.write('\n-------- Generation '+repr(self.generation)+' --------\n') self.files[self.nindiv].write('Generation '+str(self.generation)+'\n') if len(pop) == 0: logger.info('Initializing structures') offspring = self.initialize_structures() self.population = offspring else: for i in range(len(pop)): # Reset History index pop[i].history_index=repr(pop[i].index) # Select the next generation individuals offspring = switches.selection_switch(pop, self.nindiv, self.selection_scheme, self) # Clone the selected individuals offspring=[off1.duplicate() for off1 in offspring] # Apply crossover to the offspring self.output.write('\n--Applying Crossover--\n') cxattempts = 0 for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < self.cxpb: child1,child2 = switches.crossover_switch(child1, child2, self) cxattempts+=2 self.cxattempts=cxattempts #DEBUG: Write first child if 'MA' in self.debug: inp_out.write_xyz(self.debugfile,offspring[0][0],'First Child '+ repr(offspring[0].history_index)) # Apply mutation to the offspring self.output.write('\n--Applying Mutation--\n') mutattempts = [] muts = [] for mutant in offspring: if random.random() < self.mutpb: if self.mutant_add: mutant = mutant.duplicate() mutant, optsel = switches.moves_switch(mutant,self) mutattempts.append([mutant.history_index,optsel]) if self.mutant_add: muts.append(mutant) if self.mutant_add: offspring.extend(muts) self.mutattempts=mutattempts #DEBUG: Write first offspring if 'MA' in self.debug: inp_out.write_xyz(self.debugfile,muts[0][0],'First Mutant '+\ repr(muts[0].history_index)) if 'stem' in self.fitness_scheme: if self.stem_coeff==None: logger.info('Setting STEM coeff (alpha)') ind = offspring.pop() from MAST.structopt.tools.StemCalc import find_stem_coeff outs = find_stem_coeff(self,ind) ind = outs[1] ind.fitness = 0 ind.energy = 0 self.stem_coeff = outs[0] logger.info('STEM Coeff = {0}'.format(self.stem_coeff)) self.output.write('stem_coeff Calculated to be: '+repr(self.stem_coeff)+'\n') offspring.append(ind) return offspring
def algorithm_par_mp1(self): """Subprogram for running parallel version of GA Requires MPI4PY""" comm = MPI.COMM_WORLD rank = MPI.COMM_WORLD.Get_rank() if rank==0: if 'MA' in self.debug: debug = True else: debug = False self.algorithm_initialize() self.convergence = False convergence = False while not convergence: if rank==0: self.calc = tools.setup_calculator(self) #Set up calculator for atomic structures #Set up calculator for fixed region calculations if self.fixed_region: self.static_calc = self.calc #May need to copy this self.calc = tools.setup_fixed_region_calculator(self) pop = self.population offspring = self.generation_set(self,pop) # Identify the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if ind.energy==0] #Evaluate the individuals with invalid fitness self.output.write('\n--Evaluate Structures--\n') proc_dist = int(comm.Get_size()/self.n_proc_eval) ntimes=int(math.ceil(float(len(invalid_ind))/float(proc_dist))) nadd=int(ntimes*proc_dist-len(invalid_ind)) maplist=[[] for n in range(ntimes)] strt=0 for i in range(len(maplist)): maplist[i]=[[self,indi] for indi in invalid_ind[strt:proc_dist+strt]] strt+=proc_dist for i in range(nadd): maplist[len(maplist)-1].append([None,None]) masterlist = [i*self.n_proc_eval for i in range(proc_dist)] else: ntimes=None masterlist = None ntimes = comm.bcast(ntimes,root=0) outs=[] for i in range(ntimes): if rank==0: one=maplist[i] for j in range(len(one)): comm.send(ind, dest=1, tag=11) elif rank == 1: ind = comm.recv(source=0, tag=11) else: one=None ind =comm.scatter(one,root=0) out = switches.fitness_switch(ind) else: invalid_ind=[] poorlist = [] for i in range(len(invalid_ind)): if self.fitness_scheme=='STEM_Cost': if self.stem_coeff==None: ind = invalid_ind.pop() from MAST.structopt.tools.StemCalc import find_stem_coeff outs = find_stem_coeff(self,ind) ind = outs[1] self.stem_coeff = outs[0] self.output.write('stem_coeff Calculated to be: '+repr(self.stem_coeff)+'\n') pop.append(ind) ind=invalid_ind[i] if 'MA' in self.debug: write_xyz(self.debugfile,ind[0],'Individual to fitness_switch') outs = switches.fitness_switch([self,ind]) self.output.write(outs[1]) invalid_ind[i]=outs[0] if invalid_ind[i].energy == float('inf'): poorlist.append(i) self.output.write('Removing infinite energy individual '+repr(ind.history_index)+'\n') elif invalid_ind[i].energy == float('nan'): poorlist.append(i) self.output.write('Removing nan energy individual '+repr(ind.history_index)+'\n') self.output.flush() if len(poorlist) != 0: poorlist.sort(reverse=True) for one in poorlist: del invalid_ind[one] if rank==0: pop.extend(invalid_ind) pop = self.generation_eval(pop) convergence = comm.bcast(self.convergence, root=0) if rank==0: end_signal = self.algorithm_stats(self.population) else: end_signal = None end_signal = comm.bcast(end_signal, root=0) return end_signal