magoptlib.genetic_algorithm =========================== .. py:module:: magoptlib.genetic_algorithm Functions --------- .. autoapisummary:: magoptlib.genetic_algorithm.fitness_batch_gpu magoptlib.genetic_algorithm.best_50_selection magoptlib.genetic_algorithm.tournament_selection magoptlib.genetic_algorithm.roulette_wheel_selection magoptlib.genetic_algorithm.ACROMUSE_adaptive magoptlib.genetic_algorithm.genetic_algorithm_gpu Module Contents --------------- .. py:function:: fitness_batch_gpu(angle_vectors, positions_gpu, init_orientations_gpu, l_vals, m_vals, shX_gpu, shY_gpu, shZ_gpu, pG_gpu, objective, alpha) Compute fitness values and total magnetic fields for a population of Halbach configurations. :param angle_vectors: Candidate magnetization angles per individual. :type angle_vectors: cupy.ndarray of shape (pop_size, d) :param positions_gpu: 3D positions of the magnets. :type positions_gpu: cupy.ndarray of shape (d, 3) :param init_orientations_gpu: Base orientation matrices for each magnet. :type init_orientations_gpu: cupy.ndarray of shape (d, 3, 3) :param l_vals: Spherical harmonic degrees and orders. :type l_vals: np.ndarray :param m_vals: Spherical harmonic degrees and orders. :type m_vals: np.ndarray :param shZ_gpu: SH coefficients for each magnet. :type shZ_gpu: cupy.ndarray :param shX_gpu: SH coefficients for each magnet. :type shX_gpu: cupy.ndarray :param shY_gpu: SH coefficients for each magnet. :type shY_gpu: cupy.ndarray :param pG_gpu: Points of interest to evaluate the field. :type pG_gpu: cupy.ndarray of shape (n_points, 3) :param alpha: Penalty scaling factor (currently unused). :type alpha: float :returns: * **fitnesses** (*cupy.ndarray of shape (pop_size,)*) -- Fitness for each individual. * **B_total** (*cupy.ndarray of shape (pop_size, n_points, 3)*) -- Total magnetic field at all PoIs per individual. .. py:function:: best_50_selection(population, population_size, sorted_idx) .. py:function:: tournament_selection(population, fitnesses_cpu, T_size, population_size, max_fitness_wins=True) .. py:function:: roulette_wheel_selection(population, fitnesses_cpu, population_size, minimize=True) Roulette-wheel selection that supports both minimization and maximization. Uses a shift based on min() or max() of the fitnesses so that probabilities are well-scaled and nonnegative. .. py:function:: ACROMUSE_adaptive(population, fitnesses_cpu, T_size_max, population_size, HPD, HPD_max, minimize) .. py:function:: genetic_algorithm_gpu(positions_cpu, init_orientations_gpu, l_vals_cpu, m_vals_cpu, shX_cpu, shY_cpu, shZ_cpu, points_of_interest, possible_angles_cpu, population_size, generations, mutation_rate, objective, parent_selection, alpha, elitism_frac=0.05) Runs a GA where each generation’s fitness is evaluated in one GPU batch. Returns: best_angles (d,), best_fitness (scalar), Bx,By,Bz at best, fitness_history (generations,).