This project is a Python implementation of a genetic algorithm that determines the combination of colors needed to create a specific color. The input is the RGB values of the desired color and the palette of colors from where to get the proportions, and the output is the combination of colors from the palette needed to create it, and the similarity between this combination and the original color.
- Python3
- pip3
- pipenv
Run
pipenv installto install the necesary dependencies
pipenv run python main.py [config_file]To configure the execution of the program a .json configuration file with a specific format is needed. The specific
format is as follows
plot: configure if program should plot or no -- Options(never_ask, ask)stop_condition: configuration about the stop condition method to usecondition: name of the method to use -- Options(time, generation, acceptable_solution)parameter: parameter the method uses as limit (time, number of generations, similitude to solution)
selection_method: configure the selection method to usemethod: name of the method to use -- Options(elite, roulette, universal, ranking, boltzmann, probabilistic_tournament, deterministic_tournament)parameter: parameter the method uses (m for deterministic tournament, TODO for boltzmann)
crossover_method: method to use for crossover -- Options(single_point, two_point, annular, uniform)mutation_method: configure the mutation methodmethod: method to choose when to mutatename: name of the method to use -- Options(random, gradient)parameter: parameter the method uses (deviation to use for gradient)
probability: mutation probabilitymutation: mutation method to apply -- Options(single_genome, limited_multi-genome, uniform_multi-genome, complete)
generation_selection: method for how to choose new generation -- Options(use_all, new_over_actual)n: number of individuals in a generationk: number of children to create for the next generationgoal: string describing the goal color with formatr,g,bpalette: string describing the initial palette of colors separated with;for example " 0,0,0;0,0,255;0,255,0;0,255,255;255,0,0;255,0,255;255,255,0;255,255,255"
A configuration file is provided as an example
The program generates a population of potential solutions that are combinations of colors in varying proportions. The fitness of each solution is determined by how close its resulting color is to the desired color. The genetic algorithm involves the following steps:
- Initialize a population of potential solutions.
- Evaluate the fitness of each solution.
- Select the fittest solutions to reproduce.
- Generate offspring through crossover and mutation.
- Evaluate the fitness of the offspring.
- Select the fittest offspring to form the next generation.
- Repeat steps 3-6 until a satisfactory solution is found or a maximum number of generations is reached.
The program uses NumPy to generate random numbers and Matplotlib to visualize the fitness of each generation.
This implementation has several limitations:
- It assumes that the colors are a combination of red, green, and blue channels.
- It only considers combinations of colors, whereas other factors such as brightness and saturation can also affect the resulting color.
- It may converge to a suboptimal solution if the population size or number of generations is too small, or even if the color palette is not adequate.