Example #1
0
    def generate_children(self, number_of_children, number_of_mutations, info, lookup, magnets, ref_trajectories, real_bfield=None):
        # first age, as we are now creating children
        self.age_bcell()
        children = []
        
        # Generate the IDfiled for the parent, as we need to calculate it fully here.
        original_bfield = None
        if real_bfield == None:
            original_bfield, calculated_fitness = fg.calculate_cached_trajectory_fitness(info, lookup, magnets, self.genome, ref_trajectories)
            fitness_error = abs(self.fitness - calculated_fitness)
            logging.debug("Estimated fitness to real fitness error %2.10e"%(fitness_error))
            self.fitness = calculated_fitness
        else :
            original_bfield = real_bfield
            logging.debug("Using real bfield")
            
        original_magnets = fg.generate_per_magnet_array(info, self.genome, magnets)

        for i in range(number_of_children):
            maglist = copy.deepcopy(self.genome)
            maglist.mutate(number_of_mutations)
            new_magnets = fg.generate_per_magnet_array(info, maglist, magnets)
            update = fg.compare_magnet_arrays(original_magnets, new_magnets, lookup)
            child = ID_BCell()
            child.mutations = number_of_mutations
            child.genome = maglist
            updated_bfield = np.array(original_bfield)
            for beam in update.keys() :
                if update[beam].size != 0:
                    updated_bfield = updated_bfield - update[beam]
            child.fitness = fg.calculate_trajectory_fitness_from_array(updated_bfield, info, ref_trajectories)
            #child.create(info, lookup, magnets, maglist, ref_total_id_field)
            children.append(child)
            logging.debug("Child created with fitness : %f" % (child.fitness))
        return children
Example #2
0
    def generate_children(self,
                          number_of_children,
                          number_of_mutations,
                          info,
                          lookup,
                          magnets,
                          ref_trajectories,
                          real_bfield=None):
        # first age, as we are now creating children
        self.age_bcell()
        children = []

        # Generate the IDfiled for the parent, as we need to calculate it fully here.
        original_bfield = None
        if real_bfield == None:
            original_bfield, calculated_fitness = fg.calculate_cached_trajectory_fitness(
                info, lookup, magnets, self.genome, ref_trajectories)
            fitness_error = abs(self.fitness - calculated_fitness)
            logging.debug("Estimated fitness to real fitness error %2.10e" %
                          (fitness_error))
            self.fitness = calculated_fitness
        else:
            original_bfield = real_bfield
            logging.debug("Using real bfield")

        original_magnets = fg.generate_per_magnet_array(
            info, self.genome, magnets)

        for i in range(number_of_children):
            maglist = copy.deepcopy(self.genome)
            available_magnets = {
                key: range(len(magnets.magnet_sets[key]))
                for key in magnets.magnet_sets.keys()
            }
            maglist.mutate(number_of_mutations, available_magnets)
            new_magnets = fg.generate_per_magnet_array(info, maglist, magnets)
            update = fg.compare_magnet_arrays(original_magnets, new_magnets,
                                              lookup)
            child = ID_BCell()
            child.mutations = number_of_mutations
            child.genome = maglist
            updated_bfield = np.array(original_bfield)
            for beam in update.keys():
                if update[beam].size != 0:
                    updated_bfield = updated_bfield - update[beam]
            child.fitness = fg.calculate_trajectory_fitness_from_array(
                updated_bfield, info, ref_trajectories)
            #child.create(info, lookup, magnets, maglist, ref_total_id_field)
            children.append(child)
            logging.debug("Child created with fitness : %f" % (child.fitness))
        return children
Example #3
0
 def create(self, info, lookup, magnets, maglist, ref_trajectories, number_of_changes, original_bfield):
     self.maglist = maglist
     self.changes = number_of_changes
     self.create_genome(number_of_changes)
     
     maglist = copy.deepcopy(self.maglist)
     maglist.mutate_from_list(self.genome)
     new_magnets = fg.generate_per_magnet_array(info, maglist, magnets)
     original_magnets = fg.generate_per_magnet_array(info, self.maglist, magnets)
     update = fg.compare_magnet_arrays(original_magnets, new_magnets, lookup)
     updated_bfield = np.array(original_bfield)
     
     for beam in update.keys() :
         if update[beam].size != 0:
             updated_bfield = updated_bfield - update[beam]
     self.fitness = fg.calculate_trajectory_fitness_from_array(updated_bfield, info, ref_trajectories)
Example #4
0
    def create(self, info, lookup, magnets, maglist, ref_trajectories,
               number_of_changes, original_bfield):
        self.maglist = maglist
        self.changes = number_of_changes
        self.create_genome(number_of_changes)

        maglist = copy.deepcopy(self.maglist)
        maglist.mutate_from_list(self.genome)
        new_magnets = fg.generate_per_magnet_array(info, maglist, magnets)
        original_magnets = fg.generate_per_magnet_array(
            info, self.maglist, magnets)
        update = fg.compare_magnet_arrays(original_magnets, new_magnets,
                                          lookup)
        updated_bfield = np.array(original_bfield)

        for beam in update.keys():
            if update[beam].size != 0:
                updated_bfield = updated_bfield - update[beam]
        self.fitness = fg.calculate_trajectory_fitness_from_array(
            updated_bfield, info, ref_trajectories)
def saveh5(path, best, genome, info, mags, real_bfield):
    new_magnets = fg.generate_per_magnet_array(info, best.genome, mags)
    original_magnets = fg.generate_per_magnet_array(info, genome.genome, mags)
    
    update = fg.compare_magnet_arrays(original_magnets, new_magnets, lookup)
    
    updated_bfield = np.array(real_bfield)
    for beam in update.keys() :
        if update[beam].size != 0:
            updated_bfield = updated_bfield - update[beam]
    
    outfile = os.path.join(path, genome.uid+'-'+best.uid+'.h5')
    logging.debug("filename is %s" % (outfile))
    f = h5py.File(outfile, 'w')
    
    total_id_field = real_bfield
    f.create_dataset('id_Bfield_original', data=total_id_field)
    trajectory_information=mt.calculate_phase_error(info, total_id_field)
    f.create_dataset('id_phase_error_original', data = trajectory_information[0])
    f.create_dataset('id_trajectory_original', data = trajectory_information[1])
    
    total_id_field = updated_bfield
    f.create_dataset('id_Bfield_shimmed', data=total_id_field)
    trajectory_information=mt.calculate_phase_error(info, total_id_field)
    f.create_dataset('id_phase_error_shimmed', data = trajectory_information[0])
    f.create_dataset('id_trajectory_shimmed', data = trajectory_information[1])
    
    
    ref_mags=generate_reference_magnets(mags)
    total_id_field = generate_id_field(info, best.genome, ref_mags, lookup)
    
    f.create_dataset('id_Bfield_perfect', data=total_id_field)
    trajectory_information=mt.calculate_phase_error(info, total_id_field)
    f.create_dataset('id_phase_error_perfect', data = trajectory_information[0])
    f.create_dataset('id_trajectory_perfect', data = trajectory_information[1])
    
    f.close()
Example #6
0
def saveh5(path, best, genome, info, mags, real_bfield, lookup):
    new_magnets = fg.generate_per_magnet_array(info, best.genome, mags)
    original_magnets = fg.generate_per_magnet_array(info, genome.genome, mags)

    update = fg.compare_magnet_arrays(original_magnets, new_magnets, lookup)

    updated_bfield = np.array(real_bfield)
    for beam in update.keys():
        if update[beam].size != 0:
            updated_bfield = updated_bfield - update[beam]

    outfile = os.path.join(path, genome.uid + '-' + best.uid + '.h5')
    logging.debug("filename is %s" % (outfile))
    f = h5py.File(outfile, 'w')

    total_id_field = real_bfield
    f.create_dataset('id_Bfield_original', data=total_id_field)
    trajectory_information = mt.calculate_phase_error(info, total_id_field)
    f.create_dataset('id_phase_error_original', data=trajectory_information[0])
    f.create_dataset('id_trajectory_original', data=trajectory_information[1])

    total_id_field = updated_bfield
    f.create_dataset('id_Bfield_shimmed', data=total_id_field)
    trajectory_information = mt.calculate_phase_error(info, total_id_field)
    f.create_dataset('id_phase_error_shimmed', data=trajectory_information[0])
    f.create_dataset('id_trajectory_shimmed', data=trajectory_information[1])

    ref_mags = generate_reference_magnets(mags)
    total_id_field = generate_id_field(info, best.genome, ref_mags, lookup)

    f.create_dataset('id_Bfield_perfect', data=total_id_field)
    trajectory_information = mt.calculate_phase_error(info, total_id_field)
    f.create_dataset('id_phase_error_perfect', data=trajectory_information[0])
    f.create_dataset('id_trajectory_perfect', data=trajectory_information[1])

    f.close()
Example #7
0
# send the best member of the population for sorting
trans = []
for i in range(size):
    trans.append(children[0])

allpop = MPI.COMM_WORLD.alltoall(trans) 

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

best = allpop[0]

if rank == 0:
    best.save(args[0])


    new_magnets = fg.generate_per_magnet_array(info, best.genome, mags)
    original_magnets = fg.generate_per_magnet_array(info, genome.genome, mags)
    
    update = fg.compare_magnet_arrays(original_magnets, new_magnets, lookup)
    
    updated_bfield = np.array(real_bfield)
    for beam in update.keys() :
        if update[beam].size != 0:
            updated_bfield = updated_bfield - update[beam]
    
    outfile = os.path.join(args[0], genome.uid+'-'+best.uid+'.h5')
    logging.debug("filename is %s" % (outfile))
    f = h5py.File(outfile, 'w')
    
    total_id_field = real_bfield
    f.create_dataset('id_Bfield_original', data=total_id_field)
Example #8
0
# send the best member of the population for sorting
trans = []
for i in range(size):
    trans.append(children[0])

allpop = MPI.COMM_WORLD.alltoall(trans)

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

best = allpop[0]

if rank == 0:
    best.save(args[0])

    new_magnets = fg.generate_per_magnet_array(info, best.genome, mags)
    original_magnets = fg.generate_per_magnet_array(info, genome.genome, mags)

    update = fg.compare_magnet_arrays(original_magnets, new_magnets, lookup)

    updated_bfield = np.array(real_bfield)
    for beam in update.keys():
        if update[beam].size != 0:
            updated_bfield = updated_bfield - update[beam]

    outfile = os.path.join(args[0], genome.uid + '-' + best.uid + '.h5')
    logging.debug("filename is %s" % (outfile))
    f = h5py.File(outfile, 'w')

    total_id_field = real_bfield
    f.create_dataset('id_Bfield_original', data=total_id_field)
Example #9
0
    def generate_children(self,
                          number_of_children,
                          number_of_mutations,
                          info,
                          lookup,
                          magnets,
                          ref_trajectories,
                          real_bfield=None):
        # first age, as we are now creating children
        self.age_bcell()
        children = []

        # Generate the IDfiled for the parent, as we need to calculate it fully here.
        original_bfield = None
        original_fitness = None
        if real_bfield is None:
            original_bfield, original_fitness = fg.calculate_cached_trajectory_fitness(
                info, lookup, magnets, self.genome, ref_trajectories)
            fitness_error = abs(self.fitness - original_fitness)
            logging.debug("Estimated fitness to real fitness error %2.10e" %
                          (fitness_error))

        else:
            original_bfield = real_bfield
            original_fitness = fg.calculate_trajectory_fitness_from_array(
                original_bfield, info, ref_trajectories)
            fitness_error = abs(self.fitness - original_fitness)
            logging.debug("Using real bfield")

        original_magnets = fg.generate_per_magnet_array(
            info, self.maglist, magnets)
        available = magnets.availability()
        maglist = copy.deepcopy(self.maglist)
        mutation_list = self.create_mutant(number_of_mutations, available)
        maglist.mutate_from_list(mutation_list)
        new_magnets = fg.generate_per_magnet_array(info, maglist, magnets)
        update = fg.compare_magnet_arrays(original_magnets, new_magnets,
                                          lookup)
        updated_bfield = np.array(original_bfield)
        for beam in update.keys():
            if update[beam].size != 0:
                updated_bfield = updated_bfield - update[beam]
        self.fitness = fg.calculate_trajectory_fitness_from_array(
            updated_bfield, info, ref_trajectories)

        for i in range(number_of_children):
            maglist = copy.deepcopy(self.maglist)
            mutation_list = self.create_mutant(number_of_mutations, available)
            maglist.mutate_from_list(mutation_list)
            new_magnets = fg.generate_per_magnet_array(info, maglist, magnets)
            update = fg.compare_magnet_arrays(original_magnets, new_magnets,
                                              lookup)
            child = ID_Shim_BCell()
            child.mutations = number_of_mutations
            child.genome = mutation_list
            child.maglist = self.maglist
            updated_bfield = np.array(original_bfield)
            for beam in update.keys():
                if update[beam].size != 0:
                    updated_bfield = updated_bfield - update[beam]
            child.fitness = fg.calculate_trajectory_fitness_from_array(
                updated_bfield, info, ref_trajectories)
            children.append(child)
            logging.debug("Child created with fitness : %2.10e" %
                          (child.fitness))
        return children