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)
while r_line != '': r = tuple(int(p) for p in r_line.strip().replace(',', ' ').split()) #r = Point(coord[0],coord[1],coord[2]) configuration.append(r) #~ print configuration r_line = r_file.readline() r_file.close() start_configuration = configuration # set up temperature, target folding energy temperature = 0.65 Vmin = -34.5 # set up sequence and number_of_cycles #sequence = convert_sequence('HHPHHPPHPPPHPPHPHHHHPPPPHHPPHPHHHHPP') sequence = convert_sequence('HHHPPHHPHHHHPHHPPHPHHPHPPHPPPHHPPPPP') Max_number_of_cycles = 100 # CAN INCREASE THIS LATER, not currently used # counters for MPFT sequence_traversals = nbr_moves = 0 trajectories = 3 running_total = 0 MFPT = [] all_sequence_traversals = [] V = energy_function(configuration, sequence) print 'Starting V = ' + str(V) for i in xrange(trajectories): count = 0 while V > Vmin: