def main(num_to_find=5): from genetic_lattice import decode_lattice, decode_string config = decode_lattice(decode_string('RBBBRFFFUBUFRDDBUBLLLDDFUUFRB')) pool = Pool() global_checker = GlobalChecker(pool, num_to_find, config) print "1" for i in range(cpu_count()*30): global_checker.check_local() print "3" #i = 0 while True: #print "4 %d" % i #i += 1 #if i>10: break global_checker.restart_locals() #pool.join() from time import sleep sleep(5)
def calc_genetic_energy(config,sequence): return energy_function(decode_lattice(config),sequence)
def global_minimum(): if len(sys.argv)==1 or 'first' in sys.argv: sequence = convert_sequence('HHPHHPPHPPPHPPHPHHHHPPPPHHPPHPHHHHPP') else: sequence = convert_sequence('HHHPPHHPHHHHPHHPPHPHHPHPPHPPPHHPPPPP') sequence_len = len(sequence) if len(sys.argv)>1 and not sys.argv[1].isdigit(): sys.argv.pop(1) # remove first/second lenargv = len(sys.argv) # GA data: # number of individuals per generation ind_per_gen = int(sys.argv[1]) if lenargv>1 else 100 # number of generations num_generations = int(sys.argv[2]) if lenargv>2 else 100 # number of GA/SA swaps num_tries = int(sys.argv[3]) if lenargv>3 else 10 # SA data: alpha = 0.995 ntemp = int(sys.argv[4]) if lenargv>4 else 1000 ncycles = int(sys.argv[5]) if lenargv>5 else 100 orig_T_star = float(sys.argv[6]) if lenargv>6 else 7.0 # T_star should be <0.2 after ntemp, so # T_star*alpha**ntemp<0.2 # (0.2/T_star)**(1.0/ntemp)=alpha alpha = (0.2/orig_T_star)**(1.0/(ntemp+1)) print alpha # mutation rates rate_cross = 0.8 rate_mutate = 0.8 population = birth(sequence_len,ind_per_gen) lowest_V = lowest_V_sofar = 0 highest_V = 1 equal_generations = 0 lower_generations = [] for numtry in xrange(num_tries): choose_fit(population,ind_per_gen,sequence) # just so it sorts for i in xrange(num_generations): print_at('generation %d' % i,0) # mate best half? with TimingContext(1): #mating_population = choose_fit(population,ind_per_gen/2,sequence,resort=False) mating_population = population[:ind_per_gen/2] population = population[ind_per_gen/2:] # population is pre-sorted by choose_fit with TimingContext(2): if lowest_V < -1.0 and lowest_V == highest_V: # rebirth soon necessary equal_generations += 1 else: equal_generations = 0 if equal_generations > 4: # we lost genetic diversity... mating_population = birth(sequence_len,ind_per_gen) population = sample(population,2) with TimingContext(3): do_cross_over(mating_population,rate_cross,sequence_len) with TimingContext(4): do_mutations(mating_population,rate_mutate,sequence_len) # keep best ones only with TimingContext(5): population.extend(mating_population) with TimingContext(6): population = choose_fit(population,ind_per_gen,sequence) # select lowest energy with TimingContext(7): #if True: lowest_conf = population[0] lowest_V = calc_genetic_energy(lowest_conf,sequence) highest_V = calc_genetic_energy(population[-1],sequence) if lowest_V<lowest_V_sofar: lowest_V_sofar = lowest_V print_at("lowest V: %f for config %s" % (lowest_V,str(lowest_conf).replace(' ','')),8) lower_generations.append((i,lowest_V,lowest_conf)) print_at("current_lowest %f" % lowest_V,9) print_at("current_highest %f" % highest_V,10) print_at("Generations worth their CPU: %s" % str(lower_generations).replace(' ',''), 11) # have lowest conf, try annealing now... lowest_conf = decode_lattice(lowest_conf) T_star = orig_T_star for k in xrange(ntemp): print_at("SA temperature %f (%d)" % (T_star,k),25) current_config = lowest_conf current_V = lowest_V print_at("lowest_V: %f" % lowest_V,26) for j in xrange(ncycles): print_at("Cycle %d" % j,27) next_conf,next_V = MC_step(current_config, sequence, T_star) if next_V<lowest_V: current_V = next_V current_config = next_conf print_at("current V: %f" % next_V,28) # take best config from cycles and use that if current_V < lowest_V: lowest_V = current_V lowest_conf = current_config print_at("lowest V: %f for config %s" % (lowest_V,str(encode_lattice(lowest_conf)).replace(' ','')),29) T_star *= alpha # aaaaand start over population = [encode_lattice(lowest_conf)]+birth(sequence_len,ind_per_gen) mating_population = [] print_at('',30)
from genetic_lattice import decode_string, decode_lattice, print_at from energy_function import energy_function def sequence_from_i(i,sequence_len): return tuple((i>>j)&1 for j in range(sequence_len)) def num_H(sequence): return sum(sequence) if __name__=='__main__': configuration = decode_lattice(decode_string('RBBBRFFFUBUFRDDBUBLLLDDFUUFRB')) sequence_len = len(configuration) sequence_len2 = len(configuration)/2 max_configs = int(2**sequence_len) #start = 10000 start = 32767 # first half-H config #found = [] lowest_seq = None lowest_V = 0 j = 0 for i in xrange(start,max_configs): sequence = sequence_from_i(i,sequence_len) if num_H(sequence)!=sequence_len2: continue j += 1 #found.append(sequence) V = energy_function(configuration, sequence) if j%10000==0: print_at(str(sequence), 2) print_at("current V: %f, lowest_V: %f" % (V,lowest_V), 3)