Esempio n. 1
0
def lambdacommamu(pop, Optimizer):
    """Selection function to employ a lambda,mu GA scheme
    """
    fits = [ind.fitness for ind in pop]
    minindex = [i for i in range(len(pop)) if pop[i].fitness==min(fits)]
    try:
        Optimizer.mark
    except:
        Optimizer.mark = len(pop)/2
    parents = pop[0:Optimizer.mark]
    offspring = pop[Optimizer.mark::]
    if minindex < Optimizer.mark:
        offspring.append(pop[minindex])
    if len(offspring) < Optimizer.nindiv:
        diff = Optimizer.nindiv-len(offspring)
        if Optimizer.natural_selection_scheme=='elitism':
            addins = get_best(parents,diff)
            STR = 'Adding in '+repr(diff)+' lowest fitness parents\n'
        else:
            addins = selection_switch(parents, diff, Optimizer.natural_selection_scheme, Optimizer)
            STR = 'Adding in '+repr(diff)+' parents based on natural selection\n'
        for one in addins:
            offspring.append(one)
    elif len(offspring) > Optimizer.nindiv:
        diff = len(offspring)-Optimizer.nindiv
        if Optimizer.natural_selection_scheme=='elitism':
            offspring = get_best(offspring,Optimizer.nindiv)
            STR = 'Removing lowest '+repr(diff)+' fitness offspring\n'
        else:
            offspring = selection_switch(offspring, Optimizer.nindiv, Optimizer.natural_selection_scheme, Optimizer)
            STR = 'Removing '+repr(diff)+' offspring by natural selection\n'
    else:
        STR = 'Number of offspring = {0}\n'.format(len(offspring))
    return offspring, STR
Esempio n. 2
0
def lambdacommamu(pop, Optimizer):
    """Selection function to employ a lambda,mu GA scheme
    """
    fits = [ind.fitness for ind in pop]
    minindex = [i for i in range(len(pop)) if pop[i].fitness == min(fits)]
    try:
        Optimizer.mark
    except:
        Optimizer.mark = len(pop) / 2
    parents = pop[0:Optimizer.mark]
    offspring = pop[Optimizer.mark::]
    if minindex < Optimizer.mark:
        offspring.append(pop[minindex])
    if len(offspring) < Optimizer.nindiv:
        diff = Optimizer.nindiv - len(offspring)
        if Optimizer.natural_selection_scheme == 'elitism':
            addins = get_best(parents, diff)
            STR = 'Adding in ' + repr(diff) + ' lowest fitness parents\n'
        else:
            addins = selection_switch(parents, diff,
                                      Optimizer.natural_selection_scheme,
                                      Optimizer)
            STR = 'Adding in ' + repr(
                diff) + ' parents based on natural selection\n'
        for one in addins:
            offspring.append(one)
    elif len(offspring) > Optimizer.nindiv:
        diff = len(offspring) - Optimizer.nindiv
        if Optimizer.natural_selection_scheme == 'elitism':
            offspring = get_best(offspring, Optimizer.nindiv)
            STR = 'Removing lowest ' + repr(diff) + ' fitness offspring\n'
        else:
            offspring = selection_switch(offspring, Optimizer.nindiv,
                                         Optimizer.natural_selection_scheme,
                                         Optimizer)
            STR = 'Removing ' + repr(
                diff) + ' offspring by natural selection\n'
    else:
        STR = 'Number of offspring = {0}\n'.format(len(offspring))
    return offspring, STR
Esempio n. 3
0
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
Esempio n. 4
0
def adapting(pop, Optimizer):
    """Function to provide an adapting fitness function for GA evaluation
    Input:
        pop = population consisting of list of Individual Class objects to be evaluated
        Optimizer = Optimizer class object with fitness parameters
    Output:
        pop = new population updated based on fitness evaluation
    *** needs work ***
    """
    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:
        Optimizer.output.write('Predator: Adding duplicates back')
        indiv = random.choice(otherlist)
        newpop.append(indiv)
        STR += 'Predator: Adding mutated duplicates to new pop history=' + indiv.history_index + '\n'
    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 genrep >= Optimizer.reqrep * Optimizer.adaptbegin:
        ofusslim = Optimizer.fusslimit
        nfusslim = ofusslim * math.exp(-Optimizer.adaptmultiplier * float(
            Optimizer.genrep) / float(Optimizer.reqrep))
        Optimizer.fusslimit = nfusslim
    else:
        ofusslim = Optimizer.fusslimit
    pop = selection_switch(newpop, Optimizer.nindiv,
                           Optimizer.natural_selection_scheme, Optimizer)
    pop = get_best(pop, len(pop))
    Optimizer.fusslimit = ofusslim
    return pop, STR
Esempio n. 5
0
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
Esempio n. 6
0
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
Esempio n. 7
0
def adapting(pop, Optimizer):
    """Function to provide an adapting fitness function for GA evaluation
    Input:
        pop = population consisting of list of Individual Class objects to be evaluated
        Optimizer = Optimizer class object with fitness parameters
    Output:
        pop = new population updated based on fitness evaluation
    *** needs work ***
    """
    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:
        Optimizer.output.write('Predator: Adding duplicates back')
        indiv = random.choice(otherlist)
        newpop.append(indiv)
        STR+='Predator: Adding mutated duplicates to new pop history='+indiv.history_index+'\n'
    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 genrep >= Optimizer.reqrep*Optimizer.adaptbegin:
        ofusslim = Optimizer.fusslimit
        nfusslim = ofusslim*math.exp(-Optimizer.adaptmultiplier*float(Optimizer.genrep)/float(Optimizer.reqrep))
        Optimizer.fusslimit = nfusslim
    else:
        ofusslim = Optimizer.fusslimit
    pop = selection_switch(newpop, Optimizer.nindiv,Optimizer.natural_selection_scheme,Optimizer)
    pop = get_best(pop,len(pop))
    Optimizer.fusslimit=ofusslim
    return pop, STR
Esempio n. 8
0
 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
Esempio n. 9
0
            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:
            STR+='Predator: Adding duplicates back\n'
            choice = random.choice(otherlist)
            if choice.index not in nindices:
                newpop.append(choice)
                nindices.append(choice.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,str = lambdacommamu.lambdacommamu(newpop, Optimizer)
            STR+=str
        else:
            pop = selection_switch(newpop, Optimizer.nindiv, Optimizer.natural_selection_scheme, Optimizer)
        pop = get_best(pop,len(pop))
    Optimizer.output.write(STR)
    return pop

Esempio n. 10
0
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
Esempio n. 11
0
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