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 choose_fit(population,num,sequence,resort=True): if resort: population.sort(key=lambda c:calc_genetic_energy(c,sequence)) if len(population)>num: return population[:num] return population