def MC_step(present_configuration, sequence, T_star): # run over all beads in configuration present_V = energy_function(present_configuration,sequence) for m in range(len(sequence)): moves = find_move(m, present_configuration, len(sequence)) if not moves: continue next_configuration = apply_move(moves, present_configuration) next_V = energy_function(next_configuration, sequence) if calc_acceptance(present_V, next_V, sequence, T_star): present_configuration = next_configuration present_V = next_V return present_configuration, present_V
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 MC_step(present_configuration, sequence, T_star,sequence_traversals, nbr_moves): # run over all beads in configuration present_V = energy_function(present_configuration,sequence) for m in range(len(sequence)): moves = find_move(m, present_configuration, len(sequence)) if not moves: continue next_configuration = apply_move(moves, present_configuration) next_V = energy_function(next_configuration, sequence) if calc_acceptance(present_V, next_V, sequence, T_star): present_configuration = next_configuration present_V = next_V nbr_moves += 1 #~ print 'Moved! Number of moves = %f and presentV = %f .' % (nbr_moves, present_V) sequence_traversals += 1 return (present_configuration, sequence_traversals, nbr_moves)
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 run_multiple_MC(configurations, temperatures, sequence, number_of_cycles): number_of_temperature_configurations = len(temperatures) # Initialize array_of_V's and record initial energies at each temperature # ex. [[V1, V2], [V3, V4]] array_of_Vs = [] for i in range(number_of_temperature_configurations): array_of_Vs.append([energy_function(configurations[i], sequence)]) for i in range(number_of_cycles): print "running cycle " + str(i) # For each temperature configuration for w in range(number_of_temperature_configurations): # Perform MC step on that configuration configurations[w],energy = MC_step(configurations[w], sequence, temperatures[w]) # Save energy for that configuration at that time step array_of_Vs[w].append(energy) # when done, return the array_of_Vs #~ print "Got V of %s" % array_of_Vs return array_of_Vs
def calc_genetic_energy(config,sequence): return energy_function(decode_lattice(config),sequence)
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) if V<lowest_V: lowest_V = V lowest_seq = sequence lowest_seq_str = ''.join(['H' if s==0 else 'P' for s in lowest_seq]) print_at("lowest V: %f for sequence %s" % (lowest_V,lowest_seq_str),4) lowest_seq_str = ''.join(['H' if s==0 else 'P' for s in lowest_seq]) print_at("V: %f for sequence %s" % (lowest_V,lowest_seq_str),5) #print_at(len(found),5) #print_at(found,6)