Skip to content

LPRowe/genetic-algo-recreate-image

Repository files navigation

Image Reconstruction with Genetic Algorithm

ga_image_3_color.py

  1. Takes image/python-logo-50-50.png and maps it's pixels to three integers {blue pixels: 0, yellow pixels: 1, white pixels: 2}.
  2. Flattens the categorized image from 50 x 50 to a 1D array of length 2500. This is the target array.
  3. N agents are spawned where each agent has an array of n genes where n is the number of pixels in the target image.
    The first generation initializes all N agents with random values from {0, 1, 2}.
  4. The fitness of each agent is then evaluated based on what percent of it's genes match the target image.
  5. The top survival_fraction fittest agents live on to the next generation and breed to replace the less fit agents.
    Breeding consists of randomly selecting 2 fit agents and creating 2 children by splicing together segments of the parent's genes.
  6. Then the children's mutation_rate of the children's genes are mutated (randomly set to 0, 1, or 2).
    This way if all of the parents have a certain gene that is bad, there is still a chance that the child could get a good value for that gene.
  7. The process of evaluating fitness, breeding, and mutating is then repeated resulting in a steady increase in the fitness of the average agent.

settings

generations: how many generations to run for, simulation automatically stops when a perfect agent is born
target: the target image after converting blue -> 0, yellow -> 1, white -> 2 and flattening to 1D
population: how many agents per population
save_file: where to save the best agent from each generation
color_scheme: mapping for the output colors color_scheme[int] -> List[red, green, blue] where red is a float between 0 and 1
survival_fraction: the top survival_fraction · population agents survive across generations and breed
mutation_rate: starts at 100% then scales inversely with the fittest agent asymptotically approaching 0%

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages