This project implements a genetic algorithm to approximate a target image using a set of colored triangles. The algorithm progressively evolves a population of solutions by optimizing the arrangement and properties of triangles to closely resemble the target image over successive generations.
- Utilizes a genetic algorithm with mutation, crossover, and selection strategies.
- Includes enhancements like elitism, adaptive mutation rates, and dynamic triangle addition to improve convergence efficiency.
- Measures the similarity between a generated image and the target image using Mean Squared Error (MSE).
- Optimizes fitness by minimizing the error.
- Each individual solution (chromosome) is composed of multiple triangles.
- Triangles are represented by:
- Three points (x, y coordinates).
- RGB color with alpha transparency.
- Dynamically increases the number of triangles to improve detail as the algorithm progresses.
- Implements fine-tuning to enhance triangle properties for lower MSE.
-
Crossover:
- Combines traits from two parent solutions to create a child.
- Randomly selects triangles from each parent with a 50% chance.
-
Mutation:
- Introduces random changes to maintain genetic diversity.
- Modifies triangle colors or positions with a small probability.
-
Selection:
- Employs elitism to retain the best solutions across generations.
- Uses a combination of roulette wheel and tournament selection for creating the next generation.
- Gradually increases the number of triangles to enhance image detail as the algorithm converges.
- Calculates fitness as the negative MSE between the generated and target images.
- Smaller MSE indicates a closer match to the target image.
- Larger populations lead to better fitness improvement rates by exploring a broader solution space.
| Population Size | Initial Fitness | Fitness (Generation 50) | Fitness (Generation 100) |
|---|---|---|---|
| 50 | -0.199 | -0.068 | -0.025 |
| 100 | -0.203 | -0.064 | -0.020 |
| 150 | -0.202 | -0.081 | -0.022 |
- Increasing the number of triangles allows for finer details and better approximation of the target image.
- Parallelization: Speed up fitness evaluation by leveraging parallel computing.
- Interactive Visualization: Include a real-time visualization of the algorithm's progress.
- Support for Different Shapes: Extend the algorithm to support polygons other than triangles for image reconstruction.
Feel free to experiment with the parameters and share your results! 🎨