def fitpred_new(pop, Optimizer): """Predator function to identify similar structures based on energy and replace one with new structure. """ 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: if Optimizer.structure == 'Defect' or Optimizer.structure == 'Cluster': ind = gen_pop_box(Optimizer.atomlist, Optimizer.size) elif Optimizer.structure == 'Crystal': outts = gen_pop_box(Optimizer.atomlist, Optimizer.size, Optimizer.cell_shape_options) ind = outts[0] elif Optimizer.structure == 'Surface': mutopto = Optimizer.mutation_options Optimizer.mutation_options = ['Lattice_Alteration_rdrd'] topind = random.choice(pop)[0].copy() ind, scheme = moves_switch(topind, Optimizer) Optimizer.mutation_options = mutopto individ = Individual(ind) #CHECK THIS LATER!! MAY NEED TO ADD MORE PROPERTIES!! individ.energy = 1000 individ.fitness = 1000 newpop.append(individ) STR += 'Predator: Adding mutated duplicates to new pop history=' + individ.history_index + '\n' nindices.append(individ.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)) return pop, STR
def fitpred_bests(pop,Optimizer): """Predator function to identify similar structures based on energy and replace one with structure from BESTS List. """ 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]) count = 0 while len(newpop) < Optimizer.nindiv: try: Optimizer.BESTS except: Optimizer.BESTS=[] if len(Optimizer.BESTS) > 0: idx = random.choice(range(len(Optimizer.BESTS))) newpop.append(Optimizer.BESTS[idx]) STR+='Predator: Adding in structure from Best List from position = {0} with fitness = {1}\n'.format(idx,Optimizer.BESTS[idx].fitness) newindices.append(len(pop)+count) count+=1 else: 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)) return pop, STR
def fitpred_new(pop,Optimizer): """Predator function to identify similar structures based on energy and replace one with new structure. """ 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: if Optimizer.structure=='Defect' or Optimizer.structure=='Cluster': ind=gen_pop_box(Optimizer.atomlist,Optimizer.size) elif Optimizer.structure=='Crystal': outts=gen_pop_box(Optimizer.atomlist,Optimizer.size,Optimizer.cell_shape_options) ind=outts[0] elif Optimizer.structure=='Surface': mutopto=Optimizer.mutation_options Optimizer.mutation_options=['Lattice_Alteration_rdrd'] topind=random.choice(pop)[0].copy() ind, scheme = moves_switch(topind,Optimizer) Optimizer.mutation_options=mutopto individ=Individual(ind) #CHECK THIS LATER!! MAY NEED TO ADD MORE PROPERTIES!! individ.energy=1000 individ.fitness=1000 newpop.append(individ) STR+='Predator: Adding mutated duplicates to new pop history='+individ.history_index+'\n' nindices.append(individ.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)) return pop, STR
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 initialize_structures(self): global logger self.output.write('\n----Initialize Structures----\n') #self.logger.info('Initializing Structures') # Initialize Population - Generate a list of ncluster individuals # Set bulk and index atributes if self.restart: logger.info('Loading previous population') pop = generate.get_restart_population(self) else: logger.info('Generating new population') pop = generate.get_population(self) if 'MA' in self.debug: inp_out.write_xyz(self.debugfile,pop[0][0],'First Generated Individual') #Use if concentration of interstitials is unknown if self.swaplist: mutlist=self.mutation_options self.mutation_options=['IntSwapLocal'] for i in range(len(pop)): one = pop[i] one.swaplist=copy.deepcopy(swaplist) if random.random() < 0.25: pop[i], opt = switches.moves_switch(one,self) self.mutation_options=mutlist # Write Initial structures to files self.output.write('\n---Starting Structures---\n') inp_out.write_pop(self, pop) #Print number of atoms to Summary file if self.structure=='Defect' or self.structure=='Surface': natstart=len(pop[0][0])+len(pop[0].bulki) else: natstart=len(pop[0][0]) if not self.restart: if self.structure=='Defect': self.summary.write('Defect Run Pure Bulk Energy per Atom : '+repr(self.purebulkenpa)+'\n') else: self.summary.write(self.structure+' Run : '+repr(0)+'\n') self.summary.write('Natoms '+repr(natstart)+ '\n') #Print data headers to summary file self.summary.write('Generation Fitmin Fitavg Fitmedium Fitmax std time \n') offspring=pop #pop=[] #BESTS=[] return offspring
def mutation_dups_zp(pop, Optimizer): """Predator function that selects individuals that are too similar based fitness and replaces them with a zero point rotation of the structure """ 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() mutopts = Optimizer.mutation_options Optimizer.mutation_options = ['ZP_Rotation'] indiv = moves_switch(indiv, Optimizer) Optimizer.mutation_options = mutopts newpop.append(indiv) nindices.append(indiv.index) STR+='Predator: Adding mutated duplicates to new pop history='+indiv.history_index+'\n' 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)) return pop, STR
def mutation_dups_energy(pop, Optimizer): """Predator function that removes duplicates based on energy and replaces with mutations """ fitlist = [one.energy 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)) return pop, STR
def get_surface_indiv(Optimizer): """ Function to generate an structopt Individual class object containing as surface structure. Inputs: Optimizer = structopt Optimizer class Outputs: individ = structopt Individual class object containing surface structure data """ #Load surface structure surfs = read_xyz(Optimizer.surfacefile) cells = Optimizer.surfacecell surfs.set_cell(cells) surf.set_pbc([True, True, False]) #Find top layer top, bulks = find_top_layer(surfs, Optimizer.surftopthick) mutopto = Optimizer.mutation_options Optimizer.mutation_options = ['lattice_alteration_rdrd'] topind = top.copy() ind = moves_switch(topind, Optimizer) Optimizer.mutation_options = mutopto individ = Individual(ind) individ.bulki = bulks.copy() individ.bulko = bulks.copy() return individ
def get_surface_indiv(Optimizer): """ Function to generate an structopt Individual class object containing as surface structure. Inputs: Optimizer = structopt Optimizer class Outputs: individ = structopt Individual class object containing surface structure data """ #Load surface structure surfs = read_xyz(Optimizer.surfacefile) cells = Optimizer.surfacecell surfs.set_cell(cells) surf.set_pbc([True,True,False]) #Find top layer top,bulks=find_top_layer(surfs,Optimizer.surftopthick) mutopto = Optimizer.mutation_options Optimizer.mutation_options = ['lattice_alteration_rdrd'] topind = top.copy() ind = moves_switch(topind,Optimizer) Optimizer.mutation_options = mutopto individ = Individual(ind) individ.bulki = bulks.copy() individ.bulko = bulks.copy() return individ
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 mutation_dups_quench(pop, Optimizer): """Predator function that removes individuals based on fitness and mutates replacements Also quenches top individuals """ 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)) if Optimizer.genrep >10: from MAST.structopt.moves.quench import quench import os olammpsvar = os.environ['LAMMPS_COMMAND'] try: from mpi4py import MPI if '-n' in olammpsvar: lcommand = olammpsvar.split('-n') lcommand[1]=lcommand[1].split() nproc = MPI.COMM_WORLD.Get_size() os.environ['LAMMPS_COMMAND'] = '{0}-n {1} {2}'.format(lcommand[0],nproc,lcommand[1][1]) except: pass oqns2 = Optimizer.quench_n_steps_2 Optimizer.quench_n_steps_2 = 100000 opar = Optimizer.parallel Optimizer.parallel = False for i in range(3): pop[i] = quench(pop[i],Optimizer) Optimizer.quench_n_steps_2 = oqns2 os.environ['LAMMPS_COMMAND'] = olammpsvar Optimizer.parallel = opar return pop, STR
def mutation_dups_quench(pop, Optimizer): """Predator function that removes individuals based on fitness and mutates replacements Also quenches top individuals """ 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)) if Optimizer.genrep > 10: from MAST.structopt.moves.quench import quench import os olammpsvar = os.environ['LAMMPS_COMMAND'] try: from mpi4py import MPI if '-n' in olammpsvar: lcommand = olammpsvar.split('-n') lcommand[1] = lcommand[1].split() nproc = MPI.COMM_WORLD.Get_size() os.environ['LAMMPS_COMMAND'] = '{0}-n {1} {2}'.format( lcommand[0], nproc, lcommand[1][1]) except: pass oqns2 = Optimizer.quench_n_steps_2 Optimizer.quench_n_steps_2 = 100000 opar = Optimizer.parallel Optimizer.parallel = False for i in range(3): pop[i] = quench(pop[i], Optimizer) Optimizer.quench_n_steps_2 = oqns2 os.environ['LAMMPS_COMMAND'] = olammpsvar Optimizer.parallel = opar return pop, STR