This repository presents a genetic algorithm designed to mimic a reference image using random circles of different colors, sizes, and opacities. The algorithm evolves a population of candidate solutions over several generations to closely match the reference image by optimizing a fitness function.
- Introduction
- Genetic Algorithm Components
- Configuration
- Main Functions
- Algorithm Steps
- Results
- Installation
- Usage
- Contributors
- License
A genetic algorithm (GA) is a search heuristic inspired by the process of natural selection. It is commonly used to find approximate solutions to optimization and search problems. This project utilizes a GA to approximate a reference image using randomly generated circles. The main steps in a genetic algorithm are:
- Initialization: Start with a randomly generated population of individuals.
- Evaluation: Assess the fitness of each individual in the population.
- Selection: Select individuals based on their fitness to become parents for the next generation.
- Crossover: Combine pairs of parents to produce offspring, introducing genetic diversity.
- Mutation: Apply random changes to offspring to maintain genetic diversity.
- Replacement: Form a new population by replacing some or all of the old population with the new offspring.
- Termination: Repeat steps 2-6 until a stopping criterion is met (e.g., a maximum number of generations or a satisfactory fitness level).
- Chromosome: A single solution represented by a list of circles. Each circle is defined by its position (x, y), radius, color (R, G, B), and opacity (alpha).
- Gene: A single circle within a chromosome.
- Population: A collection of chromosomes (solutions).
- Fitness Function: Measures how closely a chromosome matches the reference image, with lower values indicating a better match.
- Selection: Uses tournament selection to choose the best chromosomes for reproduction.
- Crossover: Swaps genes between pairs of chromosomes to create new offspring.
- Mutation: Randomly alters genes within chromosomes to introduce new variations.
The algorithm is configured through several parameters:
img_width
andimg_height
: Dimensions of the images to be processed.max_circle_radius
: Maximum radius of the circles.num_genes
: Number of circles in each chromosome.tm_size
: Tournament size for selection.mutation_type
: Mutation strategy (guided or unguided).mutation_prob
: Probability of mutation.num_generation
: Maximum number of generations.num_inds
: Number of individuals in the population.frac_elites
andfrac_parents
: Fractions of elites and parents in the population.early_stop
: Whether to stop early if a certain fitness level is reached.show_img
: Whether to display intermediate images.save_image_per_generation
: Determines how often to save images during evolution.
init_population(num_inds, num_genes)
: Initializes the population with random chromosomes. Each chromosome is a list of circles with random attributes (position, radius, color, opacity).
evaluate_fitness(pop, num_inds, num_genes, background, generation)
: Evaluates the fitness of the population by drawing the circles on a canvas and comparing the result to the reference image using a sum of squared differences.
crossover(parents)
: Combines pairs of parent chromosomes to create offspring, introducing genetic diversity.mutation(parents_after_crossover)
: Applies mutations to the parents. If the mutation is guided, the changes are made relative to the current gene values.
tournament_selection(population)
: Selects parents using tournament selection, preserving a certain number of elites.
-
Initialize population:
- The population is initialized with random chromosomes.
-
Evaluate initial fitness:
- The initial fitness of the population is evaluated.
-
Genetic Algorithm Loop:
- If
early_stop
is enabled, the loop continues until a satisfactory fitness level is reached. - Otherwise, the loop runs for a specified number of generations.
- In each generation:
- Selection: Selects parents and elites.
- Crossover: Creates offspring from parents.
- Mutation: Applies mutations to the offspring.
- Create new population: Combines elites and mutated offspring.
- Evaluate fitness: Evaluates the fitness of the new population.
- Output fitness: Outputs the current fitness and generation number.
- If
-
Plot Generated Image:
- After each N iterations, the generated image is plotted and saved. The fitness values across generations are printed as well.
Below are the reference image and an example generated image:
- Python 3.8 or higher
- pip (Python package installer)
python -m venv env
source env/bin/activate # On Windows use `env\Scripts\activate`
git clone https://github.com/ardaxz99/Genetic-Algorithm-for-Drawing.git
cd Genetic-Algorithm-Drawing
pip install -r requirements.txt
Execute the Jupyter notebook to reproduce the results:
jupyter nbconvert --to notebook --execute main.ipynb
- Arda Baris Basaran
This project is licensed under the MIT License - see the LICENSE.md file for details.