def run(self,steps=200): self.init_pop() self.perc = [] self.schemas.append(sx.complete(self.pop, func =self.func)) self.get_bb() for i in range(steps): print "gen: ",i self.make_next_gen() self.get_bb() #print self.bb self.schemas.append(sx.complete(self.pop, func=self.func)) old_s,new_s = self.schemas[-2],self.schemas[-1] old_b, new_b = self.bb[-2], self.bb[-1] bs = blends(old_b) c = 0 for s in new_b: if s in bs: c +=1 if len(new_b) == 0: self.perc.append(0) else: self.perc.append((c*1.)/len(new_b))
def make_next_gen(self): self.eval_pop() self.schemata = sx.complete(self.pop, func=self.func) new = [] if self.e: new.append(self.bests[-1]) while len(new) <= self.n: mum = self.select() dad = self.select() # o1 = '' # for i in xrange(self.p): # if mum[i] == '*': # o1 += random.choice(['1','0']) # else: # o1 += mum[i] # new += [self.mutate(o1)] son = '' for i in xrange(self.p): if mum[i] == dad[i] and mum[i] != '*': son += mum[i] elif mum[i] != dad[i] and dad[i] == '*': son += mum[i] elif mum[i] != dad[i] and mum[i] == '*': son += dad[i] else: son += random.choice(['1', '0']) new += [self.mutate(son)] self.pop = new
def make_next_gen(self): self.eval_pop() self.schemata = sx.complete(self.pop,func = self.func) new = [] if self.e: new.append(self.bests[-1]) while len(new) <= self.n: mum = self.select() dad = self.select() # o1 = '' # for i in xrange(self.p): # if mum[i] == '*': # o1 += random.choice(['1','0']) # else: # o1 += mum[i] # new += [self.mutate(o1)] son = '' for i in xrange(self.p): if mum[i] == dad[i] and mum[i] != '*': son += mum[i] elif mum[i] != dad[i] and dad[i] == '*': son += mum[i] elif mum[i] != dad[i] and mum[i] == '*': son += dad[i] else: son += random.choice(['1','0']) new += [self.mutate(son)] self.pop = new
def compute_schemas(source, output): """Compute schemas.""" print('Working with:', source) data = [l.strip('\n') for l in open(source, 'r')] start_time = time.time() schemas = schematax.complete(data) end_time = round(time.time() - start_time, 1) print('\tElapsed time:', end_time, 'seconds') print('\t{} schemas obtained'.format(len(schemas))) print('\tWriting result on:', output) out = open(output, 'w') out.write('schema,order,defining_length\n') for s in schemas: if str(s) != 'e': line = '{},{},{}\n'.format(str(s), s.get_order(), s.get_defining_length()) out.write(line) print('Success!')
def top_schemas(self, schemas): print("\t\tCalculando Bloques Constructores...") lattice = sx.complete(schemas) print("\t\tBloques Constructores listos.") order_sum = 0.0 length_sum = 0.0 for sch in lattice: str_sch = str(sch) if '*' in str_sch: if str_sch in self.BLOCKS: self.BLOCKS[str_sch] += 1 else: self.BLOCKS.update({str_sch: 1}) order_sum += sch.get_order() length_sum += sch.get_defining_length() schemas = [] for k in self.BLOCKS.keys(): v = self.BLOCKS[k] schemas.append((v, k)) # print("> Top 10 bloques constructores:") best_schemas = [] top = 10 while top > 0 and len(schemas) > 0: max_i = 0 for i in range(len(schemas)): if schemas[max_i][0] < schemas[i][0]: max_i = i elem = schemas[max_i] best_schemas.append(elem) # print("\t> %s con %d frecuencias" % (elem[1], elem[0])) del schemas[max_i] top -= 1 return best_schemas, order_sum, length_sum
if s1.is_empty_schema() or s2.is_empty_schema(): continue for i in xrange(len(s1)): if s1[i] == s2[i]: blend += s1[i] elif s1[i] == '*' and s2[i] != '*': blend += s2[i] elif s2[i] == '*' and s1[i] != '*': blend += s1[i] if len(blend) == len(s1):# and blend not in schemata: bs.append(blend) return list(set(bs)) if __name__ == "__main__": x = ['110101', '100100','011000', '110100', '011001', '111111'] ss = sx.complete(x) print ss bls = blends(ss) sx.draw(bls + [sx.schema()], 'blends') sx.draw(ss, 'complete') # plt.plot(g.av_f) # plt.show() # fs = {}