def check_local_minimum(seq, moves, orig_config): #print "(local) checking... %s" % seq # 1k MC steps from minimization import apply_move, find_move from energy_function import energy_function from genetic_lattice import print_at init_energy = energy_function(orig_config, seq) config = orig_config for move in moves: nconfig = apply_move(move, config) if energy_function(nconfig, seq)<init_energy: #print_at("found lower energy with MC... %s" % (energy_function(nconfig, seq)),4) return (False, seq) #print_at("quick MC was a'ight %s" % seq,1) from minimization import calc_acceptance, apply_some_move sequence_len = len(seq) sequence_range = xrange(sequence_len) cE = energy_function(config, seq) #print_at("MC step?",2) for i in xrange(10**3): for res in sequence_range: moves = find_move(res, config, sequence_len) if not moves: continue nconfig = apply_some_move(moves, config) nE = energy_function(nconfig, seq) if calc_acceptance(cE,nE,seq,0.5): cE = nE config = nconfig if cE<init_energy: #print_at("MC steps beat it...i %d res %d" % (i,res),2) return (False, seq) print_at("1k MC steps still failed",3) # a little GA from minimization import birth, do_mutations, do_cross_over, calc_genetic_energy, choose_fit from genetic_lattice import encode_lattice indivs = 100 population = birth(sequence_len, indivs) + [encode_lattice(orig_config)] for gen in xrange(50): do_cross_over(population,1.0,sequence_len) do_mutations(population,1.0,sequence_len) population = choose_fit(population, indivs, seq) lowest_V = calc_genetic_energy(population[0],seq) if lowest_V<init_energy: print_at("initial energy %s" % init_energy,4) print_at("found lower energy with GA... %s" % lowest_V,5) return (False, seq) print_at("local minimum? %s" % seq,6) print "\n\n\n" return (True, seq)
def check_global_minimum(seq, orig_config): from genetic_lattice import print_at print_at("(global) checking... %s" % seq,7) f = open("/tmp/global.out",'a') f.write(str(seq)) f.close() # 1mil MC steps from minimization import apply_some_move, find_move, calc_acceptance from energy_function import energy_function config = orig_config init_energy = energy_function(config, seq) from minimization import calc_acceptance sequence_len = len(seq) sequence_range = xrange(sequence_len) cE = energy_function(config, seq) for i in xrange(10**6): for res in sequence_range: moves = find_move(res, config, sequence_len) if not moves: continue nconfig = apply_some_move(moves, config) nE = energy_function(nconfig, seq) if calc_acceptance(cE,nE,seq,0.5): cE = nE config = nconfig if cE<init_energy: print_at("MC steps beat it...i %d res %d" % (i,res),2) return (False, seq) print "10k MC steps still failed" # a little GA from minimization import birth, do_mutations, do_cross_over, calc_genetic_energy, choose_fit from genetic_lattice import encode_lattice indivs = 100 population = birth(sequence_len, indivs) + [encode_lattice(orig_config)] for gen in xrange(50): do_cross_over(population,1.0,sequence_len) do_mutations(population,1.0,sequence_len) population = choose_fit(population, indivs, seq) lowest_V = calc_genetic_energy(population[0],seq) if lowest_V<init_energy: print "initial energy %s" % init_energy print "found lower energy with GA... %s" % lowest_V return (False, seq) print "global minimum!!!" return (True, seq)
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)
print_at('Starting V = %f' % V,1) print_at('Trajectory %d' % i,2) j = 0 lowest_V = 0 while V-0.0001 > Vmin: # floating point errors... j += 1 print_at('Move %d' % j,3) print_at('With V = %f' % V,4) #~ configurations[w] = MC_step(configurations[w], sequence, temperatures[w]) #~ Result = (configuration, times down sequence, number of moves) configuration, sequence_traversals, nbr_moves, V = MC_step( configuration, sequence, temperature, sequence_traversals, nbr_moves) if V < lowest_V: lowest_V = V lowest_conf = configuration print_at('best config w/ E=%f: %s' % (lowest_V,str(encode_lattice(lowest_conf)).replace(' ','')),5) #~ print Result #~ print nbr_of_steps print_at("reached energy after %f steps with config %s" % ( nbr_moves,str(encode_lattice(configuration)).replace(' ','') ),7) MFPT.append(nbr_moves) all_sequence_traversals.append(sequence_traversals) print MFPT #~ print 'sequceT' +str(all_sequence_traversals) averageMFPT = ( sum(MFPT) / trajectories ) print averageMFPT print "Configuration #%d; Temp = %f; Energy = %f; Average number of MC Steps = %d" % ( 1, temperature, V,