Esempio n. 1
0
def fussf(pop, nkeep, Optimizer):
    """Selection function to employ basic Fixed Uniform Selection Scheme to select and 
	order individuals in a population based on fingerprint distance values.
	Picks one fuss point for the selection process
	FUSS point must reside within fusslimit
	"""
    fusslimit = Optimizer.fusslimit
    #FUSS for fingerprint
    newpop = []
    # Collect fitnesses
    fits = [ind.fitness for ind in pop]
    minindex = min(xrange(len(fits)), key=fits.__getitem__)
    fits = []
    STR = 'Distances: \n'
    for i in range(len(pop)):
        dist = fingerprint_dist(pop[minindex].fingerprint, pop[i].fingerprint)
        fits.append(dist)
        STR += repr(dist) + '\n'

    #Find min and max fitness
    minf = min(fits)
    maxf = max(fits)
    if abs(maxf - minf) > fusslimit:
        maxf = minf + fusslimit
    #Select random point on fitness line
    pt = random.uniform(minf, maxf)
    #Calculate the distance of each individual's fitness from that point
    i = 0
    distances = []
    for fit in fits:
        distances.append((abs(pt - fit), i))
        i += 1
    #Sort distances from min to max
    dlist = sorted(distances, key=lambda one: one[0])
    #Select individuals with lowest distance
    indices = []
    for i in range(nkeep):
        newpop.append(pop[dlist[i][1]])
        indices.append(dlist[i][1])
    #Always keep lowest energy individual
    if minindex not in indices:
        rm = random.choice(indices)
        newpop = [pop[inx] for inx in indices if inx != rm]
        newpop.append(pop[minindex])
    return newpop
Esempio n. 2
0
File: fussf.py Progetto: uw-cmg/MAST
def fussf(pop, nkeep, Optimizer):
    """Selection function to employ basic Fixed Uniform Selection Scheme to select and 
	order individuals in a population based on fingerprint distance values.
	Picks one fuss point for the selection process
	FUSS point must reside within fusslimit
	"""
    fusslimit = Optimizer.fusslimit
    # FUSS for fingerprint
    newpop = []
    # Collect fitnesses
    fits = [ind.fitness for ind in pop]
    minindex = min(xrange(len(fits)), key=fits.__getitem__)
    fits = []
    STR = "Distances: \n"
    for i in range(len(pop)):
        dist = fingerprint_dist(pop[minindex].fingerprint, pop[i].fingerprint)
        fits.append(dist)
        STR += repr(dist) + "\n"

        # Find min and max fitness
    minf = min(fits)
    maxf = max(fits)
    if abs(maxf - minf) > fusslimit:
        maxf = minf + fusslimit
        # Select random point on fitness line
    pt = random.uniform(minf, maxf)
    # Calculate the distance of each individual's fitness from that point
    i = 0
    distances = []
    for fit in fits:
        distances.append((abs(pt - fit), i))
        i += 1
        # Sort distances from min to max
    dlist = sorted(distances, key=lambda one: one[0])
    # Select individuals with lowest distance
    indices = []
    for i in range(nkeep):
        newpop.append(pop[dlist[i][1]])
        indices.append(dlist[i][1])
        # Always keep lowest energy individual
    if minindex not in indices:
        rm = random.choice(indices)
        newpop = [pop[inx] for inx in indices if inx != rm]
        newpop.append(pop[minindex])
    return newpop
Esempio n. 3
0
 def generation_eval(self, pop):
     global logger
     emx = max(ind.energy for ind in pop)
     emn = min(ind.energy for ind in pop)
     for ind in pop:
         ind.tenergymx = emx
         ind.tenergymin = emn
     #DEBUG: Write relaxed individual
     if 'MA' in self.debug:
         if self.generation > 0: 
             inp_out.write_xyz(self.debugfile,pop[self.nindiv][0],\
             'First Relaxed Offspring '+repr(pop[self.nindiv-1].energy))    
             #DEBUG: Write relaxed ind in solid
             if self.structure=='Defect' or self.structure=='Surface':
                 inp_out.write_xyz(self.debugfile,pop[self.nindiv].bulki,\
                 'First Relaxed bulki '+repr(pop[self.nindiv-1].energy))    
                 sols = pop[self.nindiv][0].copy()
                 sols.extend(pop[self.nindiv].bulki)
                 inp_out.write_xyz(self.debugfile,sols,'First from Invalid-ind + Bulki '+\
                 repr(pop[self.nindiv].energy))
                 sols = pop[self.nindiv][0].copy()
                 sols.extend(pop[self.nindiv].bulko)
                 inp_out.write_xyz(self.debugfile,sols,\
                 'First from Invalid-ind + Bulko '+repr(pop[self.nindiv].energy))
     if self.generation==0:
         logger.info('Initializing Bests list')
         self.BESTS = list()
     if self.best_inds_list:
         self.BESTS = tools.BestInds(pop,self.BESTS,self,writefile=True)
     # Determine survival based on fitness predator
     if 'lambda,mu' not in self.algorithm_type:
         pop = tools.get_best(pop, len(pop))
     if self.fingerprinting:
         logger.info('Writing fingerprint files')
         for one in pop:
             self.fpfile.write(repr(fingerprinting.fingerprint_dist(
                 pop[0].fingerprint,one.fingerprint))+' '+repr(one.energy)+' ')
         self.fpfile.write('\n')
         self.fpminfile.write(repr(pop[0].fingerprint)+'\n')
         self.fpminfile.write(repr(pop[0].energy)+'\n')
     nevals = len(pop)/2
     if self.generation !=0:
         logger.info('Applying predator')
         pop = predator_switch(pop,self)
     else:
         self.genrep = 0
         self.minfit = 0
     # Evaluate population
     logger.info('Checking population for convergence')
     self.check_pop(pop)
     #Update general output tracking
     if self.generation !=0:
         histlist = []
         for ind in pop:
             histlist.append(ind.history_index)
         self.Runtimes.append(time.time())
         self.Evaluations.append(nevals)
         cxsuccess = 0
         mutsuccess = []
         for one in histlist:
             if '+' in one:
                 cxsuccess +=1
             if 'm' in one:
                 mutsuccess.append(one)
         self.CXs.append((self.cxattempts,cxsuccess))
         mutslist = [[0,0] for one in self.mutation_options]
         for one in mutsuccess:
             for two, opt in self.mutattempts:
                 if one==two:
                     index = [ind for ind,value in enumerate(
                         self.mutation_options) if value==opt][0]
                     mutslist[index][1]+=1
         for one,opt in self.mutattempts:
             index = [ind for ind,value in enumerate(
             self.mutation_options) if value==opt][0]
             mutslist[index][0]+=1
         self.Muts.append(mutslist)
         self.output.write('\n----- Generation Stats -----\n')
         self.output.write('Attempted Crossovers: ' + repr(self.cxattempts)+'\n')
         self.output.write('Successful Crossovers: ' + repr(cxsuccess)+'\n')
         self.output.write('Mutations:\n')
         i=0
         for opt in self.mutation_options:
             self.output.write('    Attempted ' + opt + ' : ' + repr(mutslist[i][0]) + '\n')
             self.output.write('    Successful ' + opt + ' : ' + repr(mutslist[i][1]) + '\n')
             i+=1
     self.generation += 1
     # Set new index values
     index1 = 0
     for ind in pop:
         ind.index = index1
         index1+=1
     index1 = 0
     if not self.convergence:
         try:
             self.calc.clean()
             if self.fixed_region:
                 self.static_calc.clean()
         except:
             pass
         if self.lammps_keep_files:
             try:
                 if self.parallel and ('Island_Method' not in self.algorithm_type):
                     nproc = MPI.COMM_WORLD.Get_size()
                     lmpfilepath = os.path.dirname(self.calc.tmp_dir)
                     for proc in range(nproc):
                         pth = os.path.join(lmpfilepath,'rank-{0}'.format(proc))
                         fls = [fl for fl in os.listdir(pth) if '.' not in fl]
                         for one in fls:
                             os.remove(pth+'/'+one)
                 else:
                     fls = [fl for fl in os.listdir(self.calc.tmp_dir) if '.' not in fl]
                     for one in fls:
                         os.remove(self.calc.tmp_dir+'/'+one)
             except Exception, e:
                 logger.error('Print issue in removing files {0}.'.format(e),exc_info=True)
                 print str(e)
                 pass
Esempio n. 4
0
def fingerprint_niche(pop, Optimizer):
    """Use a k-means clustering approach to identify individuals for new population"""
    STR = ''
    #Get coordinates for each individual
    pts = []
    for one in pop:
        fpd = fingerprint_dist(pop[0].fingerprint, one.fingerprint)
        pts.append([one.fitness - pop[0].fitness, fpd])

    for one in pts:
        if numpy.isnan(one[1]):
            one[1] = 0
    #Identify spatial bounds
    mins = min(pts)
    maxs = max(pts)

    attemptcount = 10
    passflag = False
    while attemptcount > 0:
        #Select random initial centroid locations
        cents = []
        #for i in range(Optimizer.nindiv):
        #	cents.append([random.uniform(mins[0],maxs[0]),random.uniform(mins[1],maxs[1])])
        while len(cents) < Optimizer.nindiv:
            a = random.choice(pts)
            if a not in cents:
                cents.append(a)
        #Assign individual to closest centroid
        clustlist = [[] for i in range(Optimizer.nindiv)]
        for one in pts:
            ds = []
            for i in range(len(cents)):
                d = ((one[0] - cents[i][0])**2 +
                     (one[1] - cents[i][1])**2)**0.5
                ds.append([d, i])
            ds = sorted(ds, key=lambda two: two[0])
            loc = ds[0]
            clustlist[loc[1]].append(one)
        try:
            count = 0
            while True:
                count += 1
                #Calculate the centroid of the new cluster
                centsnew = []
                for one in clustlist:
                    if len(one) > 0:
                        m = [1.0] * len(one)
                        #cop = numpy.dot(m, one) / sum(m)
                        cop = [
                            sum([es for es, fps in one]) / sum(m),
                            sum([fps for es, fps in one]) / sum(m)
                        ]
                        centsnew.append(cop)
                    else:
                        centsnew.append(one)
                #Assign individuals to new centroids
                clustlistn = [[] for i in range(Optimizer.nindiv)]
                for one in pts:
                    ds = []
                    for i in range(len(centsnew)):
                        d = ((one[0] - centsnew[i][0])**2 +
                             (one[1] - centsnew[i][1])**2)**0.5
                        ds.append([d, i])
                    ds = sorted(ds, key=lambda two: two[0])
                    loc = ds[0]
                    clustlistn[loc[1]].append(one)
                if clustlistn == clustlist:
                    clustlist = clustlistn
                    centroids = centsnew
                    attemptcount = -10
                    passflag = True
                    break
                else:
                    clustlist = clustlistn
        except:
            attemptcount -= 1
            STR += 'WARNING: PREDATOR: K-means cluster difficulty\n'
    if passflag == True:
        #Select one individual from each cluster
        news = []
        for one in clustlist:
            #Random
            #npop.append(random.choice(one))
            #Minimum energy
            opts = sorted(one, key=lambda two: two[0])
            news.append(opts[0])

        indices = []
        for one in news:
            for i in range(len(pts)):
                if pts[i] == one:
                    indices.append(i)
                    break
        npop = [pop[ind] for ind in indices]
    else:
        STR += 'WARNING: PREDATOR: Population converging. Unable to find nindiv distinct clusters\n'
        npop = mutation_dups(pop, Optimizer)
        #npop = pop
        STR += 'WARNING: PREDATOR: Chose new population based on Mutation-Dups predator\n'
    pop = get_best(npop, len(npop))
    return pop, STR
Esempio n. 5
0
def fingerprint_niche(pop, Optimizer):
    """Use a k-means clustering approach to identify individuals for new population"""
    STR = ''
    #Get coordinates for each individual
    pts = []
    for one in pop:
        fpd = fingerprint_dist(pop[0].fingerprint,one.fingerprint)
        pts.append([one.fitness - pop[0].fitness,fpd])

    for one in pts:
        if numpy.isnan(one[1]):
            one[1] = 0
    #Identify spatial bounds
    mins = min(pts)
    maxs = max(pts)

    attemptcount=10
    passflag=False
    while attemptcount > 0:
        #Select random initial centroid locations
        cents = []
        #for i in range(Optimizer.nindiv):
        #	cents.append([random.uniform(mins[0],maxs[0]),random.uniform(mins[1],maxs[1])])
        while len(cents) < Optimizer.nindiv:
            a = random.choice(pts)
            if a not in cents:
                cents.append(a)
        #Assign individual to closest centroid
        clustlist = [[] for i in range(Optimizer.nindiv)]
        for one in pts:
            ds = []
            for i in range(len(cents)):
                d = ((one[0] - cents[i][0])**2 + (one[1] - cents[i][1])**2)**0.5
                ds.append([d,i])
            ds = sorted(ds, key=lambda two: two[0])
            loc = ds[0]
            clustlist[loc[1]].append(one)
        try:
            count = 0
            while True:
                count += 1
                #Calculate the centroid of the new cluster
                centsnew = []
                for one in clustlist:
                    if len(one) > 0:
                        m = [1.0]*len(one)
                        #cop = numpy.dot(m, one) / sum(m)
                        cop = [sum([es for es,fps in one]) / sum(m),sum([fps for es,fps in one]) / sum(m)]
                        centsnew.append(cop)
                    else:
                        centsnew.append(one)
                #Assign individuals to new centroids
                clustlistn=[[] for i in range(Optimizer.nindiv)]
                for one in pts:
                    ds=[]
                    for i in range(len(centsnew)):
                        d = ((one[0] - centsnew[i][0])**2 + (one[1] - centsnew[i][1])**2)**0.5
                        ds.append([d,i])
                    ds = sorted(ds, key=lambda two: two[0])
                    loc = ds[0]
                    clustlistn[loc[1]].append(one)
                if clustlistn==clustlist:
                    clustlist = clustlistn
                    centroids = centsnew
                    attemptcount=-10
                    passflag=True
                    break
                else:
                    clustlist = clustlistn
        except:
            attemptcount-=1
            STR+= 'WARNING: PREDATOR: K-means cluster difficulty\n'
    if passflag==True:
        #Select one individual from each cluster
        news = []
        for one in clustlist:
            #Random
            #npop.append(random.choice(one))
            #Minimum energy
            opts = sorted(one, key=lambda two: two[0])
            news.append(opts[0])

        indices = []
        for one in news:
            for i in range(len(pts)):
                if pts[i]==one:
                    indices.append(i)
                    break
        npop = [pop[ind] for ind in indices]
    else:
        STR+='WARNING: PREDATOR: Population converging. Unable to find nindiv distinct clusters\n'
        npop = mutation_dups(pop,Optimizer)
        #npop = pop
        STR+='WARNING: PREDATOR: Chose new population based on Mutation-Dups predator\n'
    pop = get_best(npop,len(npop))
    return pop, STR