Exemplo n.º 1
0
def process(options, args):

    if options.seed:
        random.seed(int(options.seed_value))

    if options.singlethreaded:
        rank = 0
        size = 1
    else:
        rank = MPI.COMM_WORLD.rank  # The process ID (integer 0-3 for 4-process run)
        size = MPI.COMM_WORLD.size  # The number of processes in the job.

    # get the hostname
    if options.singlethreaded:
        ip = 'localhost'
    else:
        ip = socket.gethostbyname(socket.gethostname())

    logging.debug("Process %d ip address is : %s" % (rank, ip))

    f2 = open(options.id_filename, 'r')
    info = json.load(f2)
    f2.close()

    logging.debug("Loading Lookup")
    f1 = h5py.File(options.lookup_filename, 'r')
    lookup = {}
    for beam in info['beams']:
        logging.debug("Loading beam %s" % (beam['name']))
        lookup[beam['name']] = f1[beam['name']][...]
    f1.close()

    barrier(options.singlethreaded)

    logging.debug("Loading Initial Bfield")
    f1 = h5py.File(options.bfield_filename, 'r')
    real_bfield = f1['id_Bfield'][...]
    f1.close()
    logging.debug(real_bfield)

    barrier(options.singlethreaded)

    logging.debug("Loading magnets")
    mags = magnets.Magnets()
    mags.load(options.magnets_filename)

    logging.debug('mpi runenr calling fg.generate_reference_magnets()')
    ref_mags = fg.generate_reference_magnets(mags)
    logging.debug('mpi runenr calling MagLists()')
    ref_maglist = magnets.MagLists(ref_mags)
    logging.debug('after ref_maglist')
    ref_total_id_field = fg.generate_id_field(info, ref_maglist, ref_mags,
                                              lookup)
    pherr, ref_trajectories = mt.calculate_phase_error(info,
                                                       ref_total_id_field)

    barrier(options.singlethreaded)

    #epoch_path = os.path.join(args[0], 'epoch')
    #next_epoch_path = os.path.join(args[0], 'nextepoch')
    # start by creating the directory to put the initial population in

    population = []
    estar = options.e

    # Load the initial genome
    initialgenome = ID_BCell()
    initialgenome.load(options.genome_filename)

    referencegenome = ID_BCell()
    referencegenome.load(options.genome_filename)

    # make the initial population
    for i in range(options.setup):
        # create a fresh maglist
        newgenome = ID_Shim_BCell()
        newgenome.create(info, lookup, mags, initialgenome.genome,
                         ref_trajectories, options.number_of_changes,
                         real_bfield)
        population.append(newgenome)

    # gather the population
    trans = []
    for i in range(size):
        trans.append(population)

    allpop = alltoall(options.singlethreaded, trans)

    barrier(options.singlethreaded)

    newpop = []
    for pop in allpop:
        newpop += pop

    # Need to deal with replicas and old genomes
    popdict = {}
    for genome in newpop:
        fitness_key = "%1.8E" % (genome.fitness)
        if fitness_key in popdict.keys():
            if popdict[fitness_key].age < genome.age:
                popdict[fitness_key] = genome
        else:
            popdict[fitness_key] = genome

    newpop = []
    for genome in popdict.values():
        if genome.age < options.max_age:
            newpop.append(genome)

    newpop.sort(key=lambda x: x.fitness)

    newpop = newpop[options.setup * rank:options.setup * (rank + 1)]

    for genome in newpop:
        logging.debug("genome fitness: %1.8E   Age : %2i   Mutations : %4i" %
                      (genome.fitness, genome.age, genome.mutations))

    #Checkpoint best solution
    if rank == 0:
        logging.debug("Best fitness so far is %f" % (newpop[0].fitness))
        newpop[0].save(args[0])

    # now run the processing
    for i in range(options.iterations):

        barrier(options.singlethreaded)
        logging.debug("Starting itteration %i" % (i))

        nextpop = []

        for genome in newpop:

            # now we have to create the offspring
            # TODO this is for the moment
            logging.debug("Generating children for %s" % (genome.uid))
            number_of_children = options.setup
            number_of_mutations = mutations(options.c, estar, genome.fitness,
                                            options.scale)
            children = genome.generate_children(number_of_children,
                                                number_of_mutations,
                                                info,
                                                lookup,
                                                mags,
                                                ref_trajectories,
                                                real_bfield=real_bfield)

            # now save the children into the new file
            for child in children:
                nextpop.append(child)

            # and save the original
            nextpop.append(genome)

        # gather the population
        trans = []
        for i in range(size):
            trans.append(nextpop)

        allpop = alltoall(options.singlethreaded, trans)

        newpop = []
        for pop in allpop:
            newpop += pop

        popdict = {}
        for genome in newpop:
            fitness_key = "%1.8E" % (genome.fitness)
            if fitness_key in popdict.keys():
                if popdict[fitness_key].age < genome.age:
                    popdict[fitness_key] = genome
            else:
                popdict[fitness_key] = genome

        newpop = []
        for genome in popdict.values():
            if genome.age < options.max_age:
                newpop.append(genome)

        newpop.sort(key=lambda x: x.fitness)

        estar = newpop[0].fitness * 0.99
        logging.debug("new estar is %f" % (estar))

        newpop = newpop[options.setup * rank:options.setup * (rank + 1)]

        #Checkpoint best solution
        if rank == 0:
            initialgenome.genome.mutate_from_list(newpop[0].genome)
            initialgenome.fitness = newpop[0].fitness
            initialgenome.uid = "A" + newpop[0].uid
            initialgenome.save(args[0])
            saveh5(args[0], initialgenome, referencegenome, info, mags,
                   real_bfield, lookup)
            # After the save reload the original data
            initialgenome.load(options.genome_filename)
            newpop[0].save(args[0])

        for genome in newpop:
            logging.debug(
                "genome fitness: %1.8E   Age : %2i   Mutations : %4i" %
                (genome.fitness, genome.age, genome.mutations))

        barrier(options.singlethreaded)

    barrier(options.singlethreaded)

    # gather the population
    trans = []
    for i in range(size):
        trans.append(nextpop)

    allpop = alltoall(options.singlethreaded, trans)

    newpop = []
    for pop in allpop:
        newpop += pop

    newpop.sort(key=lambda x: x.fitness)

    newpop = newpop[options.setup * rank:options.setup * (rank + 1)]

    #Checkpoint best solution
    if rank == 0:
        initialgenome.genome.mutate_from_list(newpop[0].genome)
        initialgenome.age_bcell()
        initialgenome.save(args[0])
        newpop[0].save(args[0])
Exemplo n.º 2
0
            outfile = os.path.join(args[0], os.path.split(filename)[1] + '.h5')
            fg.output_fields(outfile, options.id_filename,
                             options.lookup_filename, options.magnets_filename,
                             genome.genome)
        sys.exit(0)

    if options.setup > 0:
        print("Running setup")
        for i in range(options.setup):
            # create a fresh maglist
            maglist = magnets.MagLists(mags)
            maglist.shuffle_all()
            genome = ID_BCell(options.id_filename, options.lookup_filename,
                              options.magnets_filename)
            genome.create(maglist)
            genome.save(args[0])

    for filename in args[1::]:
        print("Processing file %s" % (filename))

        # load the genome
        genome = ID_BCell(options.id_filename, options.lookup_filename,
                          options.magnets_filename)
        genome.load(filename)

        # now we have to create the offspring
        # TODO this is for the moment
        number_of_children = options.processing
        number_of_mutations = mutations(options.c, options.e, genome.fitness,
                                        options.scale)
        children = genome.generate_children(number_of_children,
        if genome.age < options.max_age:
            newpop.append(genome)
    
    newpop.sort(key=lambda x: x.fitness)

    estar = newpop[0].fitness * 0.99
    logging.debug("new estar is %f" % (estar) )
    
    newpop = newpop[options.setup*rank:options.setup*(rank+1)]
    
    #Checkpoint best solution
    if rank == 0:
        initialgenome.genome.mutate_from_list(newpop[0].genome)
        initialgenome.fitness = newpop[0].fitness
        initialgenome.uid = "A"+newpop[0].uid
        initialgenome.save(args[0])
        saveh5(args[0], initialgenome, referencegenome, info, mags, real_bfield)
        # After the save reload the original data
        initialgenome.load(options.genome_filename)
        newpop[0].save(args[0])
    
    for genome in newpop:
        logging.debug("genome fitness: %1.8E   Age : %2i   Mutations : %4i" % (genome.fitness, genome.age, genome.mutations))
    
    MPI.COMM_WORLD.Barrier()

MPI.COMM_WORLD.Barrier()

# gather the population
trans = []
for i in range(size):
Exemplo n.º 4
0
            genome.load(filename)
            
            outfile = os.path.join(args[0], os.path.split(filename)[1]+'.h5')
            fg.output_fields(outfile, options.id_filename, options.lookup_filename, 
                              options.magnets_filename, genome.genome);
        sys.exit(0)

    if options.setup > 0:
        print("Running setup")
        for i in range(options.setup):
            # create a fresh maglist
            maglist = magnets.MagLists(mags)
            maglist.shuffle_all()
            genome = ID_BCell(options.id_filename, options.lookup_filename, options.magnets_filename)
            genome.create(maglist)
            genome.save(args[0])
    
    for filename in args[1::]:
        print("Processing file %s" % (filename))
        
        # load the genome
        genome = ID_BCell(options.id_filename, options.lookup_filename, options.magnets_filename)
        genome.load(filename)
        
        # now we have to create the offspring
        # TODO this is for the moment
        number_of_children = options.processing
        number_of_mutations = mutations(options.c, options.e, genome.fitness, options.scale)
        children = genome.generate_children(number_of_children, number_of_mutations)
        
        # now save the children into the new file