Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
    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)