### Install dependencies Source: https://github.com/lucidrains/firefly-torch/blob/main/README.md Install the required packages using pip. ```bash $ pip install -r requirements.txt ``` -------------------------------- ### Full Configuration Example with Colony Resets Source: https://context7.com/lucidrains/firefly-torch/llms.txt A comprehensive example demonstrating the full configuration of the Firefly algorithm with colony resets, including all parameters for steps, colonies, bounds, migration, exploitation, genetic algorithm, and colony resets. ```bash # Full configuration example python firefly_with_colony_resets.py \ --steps 5000 \ --colonies 5 \ --population-size 1000 \ --dimensions 15 \ --lower-bound -4.0 \ --upper-bound 4.0 \ --migrate-every 200 \ --frac-migrate 0.1 \ --beta0 2.0 \ --gamma 1.0 \ --alpha 0.1 \ --alpha-decay 0.995 \ --use-genetic-algorithm \ --breed-every 5 \ --tournament-size 100 \ --num-children 250 \ --num-colonies-reset 1 \ --reset-colonies-every 400 \ --reset-frac 0.95 \ --reset-tournament-size 25 ``` -------------------------------- ### Run Basic Firefly Algorithm Source: https://context7.com/lucidrains/firefly-torch/llms.txt Executes the standard Firefly algorithm on the Rosenbrock function with specified parameters for steps, colonies, population size, and dimensions. ```bash # Run basic Firefly algorithm on Rosenbrock function python firefly.py --steps 5000 --colonies 4 --population-size 1000 --dimensions 12 ``` -------------------------------- ### Configure Genetic Algorithm Parameters Source: https://context7.com/lucidrains/firefly-torch/llms.txt Sets parameters for the genetic algorithm component, including breeding frequency, tournament size for selection, and the number of children generated per breeding cycle. ```bash # Configure genetic algorithm parameters python firefly.py \ --use-genetic-algorithm \ --breed-every 10 \ --tournament-size 50 \ --num-children 250 ``` -------------------------------- ### Configure Colony Reset Parameters Source: https://context7.com/lucidrains/firefly-torch/llms.txt Sets parameters for the colony reset mechanism, controlling the number of colonies to reset, their frequency, the fraction of the colony to replace, and the tournament size for diversity. ```bash # Configure colony reset parameters python firefly_with_colony_resets.py \ --use-genetic-algorithm \ --num-colonies-reset 1 \ --reset-colonies-every 400 \ --reset-frac 0.95 \ --reset-tournament-size 25 ``` -------------------------------- ### Run Firefly with Colony Resets Source: https://context7.com/lucidrains/firefly-torch/llms.txt Executes an enhanced Firefly algorithm that includes colony reset functionality to prevent premature convergence, using genetic algorithms and specifying colony and dimension counts. ```bash # Run with colony resets for improved convergence on difficult problems python firefly_with_colony_resets.py \ --use-genetic-algorithm \ --colonies 5 \ --dimensions 15 \ --steps 5000 ``` -------------------------------- ### Define Firefly Algorithm hyperparameters Source: https://context7.com/lucidrains/firefly-torch/llms.txt A dictionary-based reference for configuring population structure, search bounds, firefly behavior, and genetic algorithm settings. ```python # Parameter reference with descriptions and typical values parameters = { # Population structure "steps": 5000, # Total optimization iterations "colonies": 4, # Number of parallel populations (islands) "population_size": 1000, # Fireflies per colony "dimensions": 12, # Problem dimensionality # Search bounds "lower_bound": -4.0, # Minimum coordinate value "upper_bound": 4.0, # Maximum coordinate value # Firefly behavior "beta0": 2.0, # Base attraction (exploitation) # Higher = stronger pull toward bright fireflies "gamma": 1.0, # Light absorption coefficient # 0 = infinite visibility (vanilla PSO) # Higher = more localized attraction "alpha": 0.1, # Random walk magnitude (exploration) "alpha_decay": 0.995, # Exploration decay per step # Gradually shifts from exploration to exploitation # Colony migration "migrate_every": 50, # Migration frequency (0 = disabled) "frac_migrate": 0.1, # Fraction of colony that migrates # Genetic algorithm (optional) "use_genetic_algorithm": False, "breed_every": 10, # Breeding frequency "tournament_size": 50, # Selection pressure (higher = more elitist) "num_children": 250, # Offspring per breeding cycle # Colony resets (firefly_with_colony_resets.py only) "num_colonies_reset": 1, # Colonies to reset per cycle "reset_colonies_every": 400, # Reset frequency "reset_frac": 0.95, # Replacement fraction (preserves elite) "reset_tournament_size": 25, # Lower for diversity } ``` -------------------------------- ### Run Hybrid Firefly + Genetic Algorithm Source: https://context7.com/lucidrains/firefly-torch/llms.txt Activates the hybrid Firefly and Genetic Algorithm for improved performance, particularly on higher-dimensional problems. Requires specifying dimensions and steps. ```bash # Run hybrid Firefly + Genetic Algorithm (recommended for higher dimensions) python firefly.py --use-genetic-algorithm --dimensions 15 --steps 5000 ``` -------------------------------- ### Run Rosenbrock minimization Source: https://github.com/lucidrains/firefly-torch/blob/main/README.md Execute the firefly script with the genetic algorithm flag enabled. ```bash $ python firefly.py --use-genetic-algorithm ``` -------------------------------- ### Customize Firefly Exploitation/Exploration Source: https://context7.com/lucidrains/firefly-torch/llms.txt Adjusts the core parameters controlling the Firefly algorithm's exploitation and exploration behavior, including attraction strength, light decay, and random walk magnitude. ```bash # Customize exploitation/exploration parameters python firefly.py \ --beta0 2.0 \ --gamma 1.0 \ --alpha 0.1 \ --alpha-decay 0.995 ``` -------------------------------- ### Implement tournament-based genetic selection Source: https://context7.com/lucidrains/firefly-torch/llms.txt Uses tournament selection and uniform crossover to generate offspring from firefly populations. Requires input tensors for fireflies and their corresponding fitness values. ```python import torch import einx def children_from_tournaments( fireflies, # Tensor of shape (colonies, population, dimensions) fitness, # Tensor of shape (colonies, population) num_children, # Number of children to produce per colony tournament_size # Number of participants per tournament ): """ Generate children through tournament selection and uniform crossover. Returns: Tensor of shape (colonies, num_children, dimensions) """ device = fireflies.device shape = list(fireflies.shape) shape[-2] = num_children # Random tournament participant selection batch_randperm = torch.randn(shape, device=device).argsort(dim=-1) tournament_indices = batch_randperm[..., :tournament_size] # Select tournament winners (top 2 fitness) participant_fitnesses = einx.get_at('... [p], ... c t -> ... c t', fitness, tournament_indices) winner_tournament_ids = participant_fitnesses.topk(2, dim=-1).indices winning_firefly_indices = einx.get_at('... c [t], ... c parents -> ... c parents', tournament_indices, winner_tournament_ids) # Breed winners with uniform crossover parent1, parent2 = einx.get_at('... [p] d, ... c parents -> parents ... c d', fireflies, winning_firefly_indices) crossover_mask = torch.rand_like(parent1) < 0.5 children = torch.where(crossover_mask, parent1, parent2) return children # Example usage colonies, population, dimensions = 4, 100, 10 fireflies = torch.randn(colonies, population, dimensions) costs = rosenbrock(fireflies) fitness = 1.0 / costs children = children_from_tournaments( fireflies=fireflies, fitness=fitness, num_children=25, tournament_size=10 ) print(f"Children shape: {children.shape}") # Output: Children shape: torch.Size([4, 25, 10]) ``` -------------------------------- ### Adjust Colony Migration Source: https://context7.com/lucidrains/firefly-torch/llms.txt Configures parameters for multi-colony parallelization, including the number of colonies, migration frequency, and population size per colony. ```bash # Adjust colony migration python firefly.py \ --colonies 4 \ --migrate-every 50 \ --population-size 1000 ``` -------------------------------- ### Academic citations Source: https://github.com/lucidrains/firefly-torch/blob/main/README.md BibTeX entries for the Firefly algorithm and hybrid genetic-firefly research. ```bibtex @article{Yang2018WhyTF, title = {Why the Firefly Algorithm Works?}, author = {Xin-She Yang and Xingshi He}, journal = {ArXiv}, year = {2018}, volume = {abs/1806.01632}, url = {https://api.semanticscholar.org/CorpusID:46940737} } ``` ```bibtex @article{article, author = {El-Shorbagy, M. and Elrefaey, Adel}, year = {2022}, month = {04}, pages = {706-730}, title = {A hybrid genetic-firefly algorithm for engineering design problems}, volume = {Journal of Computational Design and Engineering, Volume 9}, journal = {Journal of Computational Design and Engineering}, doi = {10.1093/jcde/qwac013} } ``` -------------------------------- ### Rosenbrock Function Implementation Source: https://context7.com/lucidrains/firefly-torch/llms.txt Implements the Rosenbrock function, a standard benchmark for optimization algorithms. It takes a tensor of shape (..., d) and returns a tensor of function values. ```python import torch def rosenbrock(x): """ Compute the Rosenbrock function value for input tensor. Args: x: Tensor of shape (..., d) where d is the number of dimensions Returns: Tensor of shape (...) containing function values """ return (100 * (x[..., 1:] - x[..., :-1] ** 2) ** 2 + (1 - x[..., :-1]) ** 2).sum(dim=-1) # Example usage x = torch.tensor([[1.0, 1.0, 1.0], # Global minimum [0.0, 0.0, 0.0], # Non-optimal point [2.0, 4.0, 8.0]]) # Far from minimum costs = rosenbrock(x) print(f"Costs: {costs}") # Output: Costs: tensor([0., 2., 3620.]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.