def main(): from MAST.structopt import Optimizer from MAST.structopt import tools #Test for cluster structures parameters = { 'structure': 'Cluster', 'optimizer_type': 'GA', 'atomlist': [('Au', 10, 0, 0), ('Cu', 10, 0, 0)] } A = Optimizer(parameters) A.algorithm_initialize() A.calc = tools.setup_calculator(A) offspring = A.generation_set([]) indiv = offspring[0].duplicate() ind1 = offspring[1].duplicate() ind2 = offspring[2].duplicate() test_mutations(indiv) test_cx(ind1, ind2) A.close_output() #Test for defect structures parameters = { 'structure': 'Defect', 'optimizer_type': 'GA', 'atomlist': [('Au', 3, 0, 0), ('Cu', 3, 0, 0)], 'supercell': (3, 3, 3), 'SolidFile': 'BFe.xyz', 'SolidCell': [8.61, 8.61, 8.61] } A = Optimizer(parameters) A.algorithm_initialize() A.calc = tools.setup_calculator(A) offspring = A.generation_set([]) indiv = offspring[0].duplicate() ind1 = offspring[1].duplicate() ind2 = offspring[2].duplicate() test_mutations(indiv) test_cx(ind1, ind2) A.close_output() #Test for crystal structures parameters = { 'structure': 'Crystal', 'optimizer_type': 'GA', 'atomlist': [('Au', 10, 0, 0), ('Cu', 10, 0, 0)] } A = Optimizer(parameters) A.algorithm_initialize() A.calc = tools.setup_calculator(A) offspring = A.generation_set([]) indiv = offspring[0].duplicate() ind1 = offspring[1].duplicate() ind2 = offspring[2].duplicate() test_mutations(indiv) test_cx(ind1, ind2) A.close_output()
def quench(indiv, Optimizer): """Move function to perform a molecular dynamics NVT quenching of structure Inputs: indiv = Individual class object to be altered Optimizer = Optimizer class object with needed parameters Outputs: indiv = Altered Individual class object """ if "MU" in Optimizer.debug: debug = True else: debug = False olammps_min = Optimizer.lammps_min ocalc = Optimizer.calc mincomd = ( "1.0e-8 1.0e-8 1000 100000\nunfix fix_nve\nfix fix_nvt all nvt temp " + repr(Optimizer.quench_max_temp) + " " + repr(Optimizer.quench_min_temp) + " " + repr(Optimizer.quench_step_size) ) mincomd += ( "\nrun " + repr(Optimizer.quench_n_steps_1) + "\nunfix fix_nvt\nfix fix_nve all nve\nminimize 1.0e-8 1.0e-8 1000 100000" ) mincomd += ( "\nunfix fix_nve\nfix fix_nvt all nvt temp " + repr(Optimizer.quench_max_temp) + " " + repr(Optimizer.quench_min_temp) + " " + repr(Optimizer.quench_step_size) ) mincomd += ( "\nrun " + repr(Optimizer.quench_n_steps_2) + "\nunfix fix_nvt\nfix fix_nve all nve\nminimize 1.0e-8 1.0e-8 1000 100000" ) Optimizer.lammps_min = mincomd Optimizer.calc = setup_calculator(Optimizer) outs = fitness_switch([Optimizer, indiv]) indiv = outs[0] Optimizer.output.write("Quench NVT Mutation performed on individual\n") Optimizer.output.write("Index = " + repr(indiv.index) + "\n") Optimizer.output.write(outs[1]) Optimizer.output.write("New fitness : " + repr(indiv.fitness) + "\n") muttype = "Q" Optimizer.lammps_min = olammps_min Optimizer.calc = setup_calculator(Optimizer) if indiv.energy == 0: indiv.history_index = indiv.history_index + "m" + muttype else: indiv.history_index = repr(indiv.index) + "m" + muttype return indiv
def quench(indiv, Optimizer): """Move function to perform a molecular dynamics NVT quenching of structure Inputs: indiv = Individual class object to be altered Optimizer = Optimizer class object with needed parameters Outputs: indiv = Altered Individual class object """ if 'MU' in Optimizer.debug: debug = True else: debug = False olammps_min = Optimizer.lammps_min ocalc = Optimizer.calc mincomd = '1.0e-8 1.0e-8 1000 100000\nunfix fix_nve\nfix fix_nvt all nvt temp ' + repr( Optimizer.quench_max_temp) + ' ' + repr( Optimizer.quench_min_temp) + ' ' + repr(Optimizer.quench_step_size) mincomd += '\nrun ' + repr( Optimizer.quench_n_steps_1 ) + '\nunfix fix_nvt\nfix fix_nve all nve\nminimize 1.0e-8 1.0e-8 1000 100000' mincomd += '\nunfix fix_nve\nfix fix_nvt all nvt temp ' + repr( Optimizer.quench_max_temp) + ' ' + repr( Optimizer.quench_min_temp) + ' ' + repr(Optimizer.quench_step_size) mincomd += '\nrun ' + repr( Optimizer.quench_n_steps_2 ) + '\nunfix fix_nvt\nfix fix_nve all nve\nminimize 1.0e-8 1.0e-8 1000 100000' Optimizer.lammps_min = mincomd Optimizer.calc = setup_calculator(Optimizer) outs = fitness_switch([Optimizer, indiv]) indiv = outs[0] Optimizer.output.write('Quench NVT Mutation performed on individual\n') Optimizer.output.write('Index = ' + repr(indiv.index) + '\n') Optimizer.output.write(outs[1]) Optimizer.output.write('New fitness : ' + repr(indiv.fitness) + '\n') muttype = 'Q' Optimizer.lammps_min = olammps_min Optimizer.calc = setup_calculator(Optimizer) if indiv.energy == 0: indiv.history_index = indiv.history_index + 'm' + muttype else: indiv.history_index = repr(indiv.index) + 'm' + muttype return indiv
def main(): from MAST.structopt import Optimizer from MAST.structopt import tools #Test for cluster structures parameters = {'structure':'Cluster','optimizer_type': 'GA','atomlist':[('Au', 10, 0, 0), ('Cu', 10, 0, 0)]} A = Optimizer(parameters) A.algorithm_initialize() A.calc = tools.setup_calculator(A) offspring = A.generation_set([]) indiv = offspring[0].duplicate() ind1 = offspring[1].duplicate() ind2 = offspring[2].duplicate() test_mutations(indiv) test_cx(ind1,ind2) A.close_output() #Test for defect structures parameters = {'structure':'Defect','optimizer_type': 'GA','atomlist':[('Au', 3, 0, 0), ('Cu', 3, 0, 0)],'supercell':(3,3,3),'SolidFile':'BFe.xyz','SolidCell':[8.61, 8.61, 8.61]} A = Optimizer(parameters) A.algorithm_initialize() A.calc = tools.setup_calculator(A) offspring = A.generation_set([]) indiv = offspring[0].duplicate() ind1 = offspring[1].duplicate() ind2 = offspring[2].duplicate() test_mutations(indiv) test_cx(ind1,ind2) A.close_output() #Test for crystal structures parameters = {'structure':'Crystal','optimizer_type': 'GA','atomlist':[('Au', 10, 0, 0), ('Cu', 10, 0, 0)]} A = Optimizer(parameters) A.algorithm_initialize() A.calc = tools.setup_calculator(A) offspring = A.generation_set([]) indiv = offspring[0].duplicate() ind1 = offspring[1].duplicate() ind2 = offspring[2].duplicate() test_mutations(indiv) test_cx(ind1,ind2) A.close_output()
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