An experimental set of classes that attempts to put together an application that will evolve a webpage. This is intended to be a test application that allows me to learn the techniques involved in genetic algorithms as well as how to to put together a modern PHP application with composer. As a result things might change significantly without warning.
I'm interested in finding out if anyone has looked at this code or has any suggestions for improvement. Please get in touch via the issue queue and let me know.
Requirements
- PHP (preferably 7.2+).
- Composer.
Structure
The application is structured into different types of objects in order to ensure separate concerns.
Types
The types classes take care of the data types at the core of the evolution process. At it's core a Type class only cares about the data that it contains. Type classes do not have functionliaty to perform mutations or calculate fitness metrics, this is the domain of Individual classes.
Individuals
Individual classes provide mutation and fitness functionality for Type classes. Instead of extending the types they wrap them as a parameter. Individual classes can be regarded as decorators of the Type classes. Each Type class maps to an Individual class.
Populations
The Population class provides a collection of Individuals.
Evolution
An Evolution class is used to control the process of evolution. This ties together the Population, Individual and Type classes and controls each generation of Population objects. An EvolutionStorage class has been created to allow larger number of Individuals to be evolved at once using a database as a storage engine.
Types Detail
The following details each of the types and any interactions they have with other types.
Number
Defines a class that contains a number. The NumberType class can add or subtract from this number. It was created as a very simple initial type in order to test the functionality of the evolution process.
Text
Defines a class that contains a string. The class has functions to get or set the string.
Color
Defines a class that contains information about a color. This is stored internally by the RGB value of the color.
Image
This defines a 2 dimensional array that renders out into an image. The array consists of either “on” or “off” values, which are then used to create a grid of boxes in an image.
Element
This represents a HTML element. The core property is the element type (div, p, strong etc) and one or more attributes. The attributes can be used to give the elements a class or id, although any attribute is allowed. An Element object can also contain one or more child Element objects, which creates a hierarchical structure of Elements.
Unit
The Unit is an object that holds information about a measurement in CSS. This is represented by a number and a unit of measurement. So values like 10px, 10em, 10% can exist. A special value of ‘auto’ can also be represented.
Style
This defines an object that contains information about a CSS rule set against a single element. The class contains a selector property and an attributes property that combine together to form a CSS block.
Page
A Page brings together the Style and Element classes into a page structure. The use of the Page class allows the basic HTML structure of a page to be static whilst the elements and styles within it can change.
The Evolution Process
The following describes how the process of evolution works for each generation.
Culling
To start the evolution process a certain amount of the population is removed. The chances of an individual being removed are dependent of the fitness of the individual. Individuals with a high fitness are less likely to be removed.
Replicate
The existing individuals in the population are then replicated to bring the population number back to a designated minimum. This means that the current population will contain the fittest individuals.
Mutate
The population is then randomly mutated. How the population is mutated is dependent on the type of object being used. After the population has been through the mutation cycle the population can then go through a crossover cycle. This is where individuals are mixed together and their attributes are swapped.
Generate Statistics
Any statistics for the population are then calculated.
Storage
This action clones the population into an array to that it can be retrieved later. This is important when looking through the history of the population.
Next steps
Some things that need to happen.
- Have a look at performance. Lots of objects being created can use up a lot of memory maybe look at only storing the top (or bottom) individuals in a population instead of everything.
- The way in which the TextIndividual works out it's fitness is based on an external factor. As such it would be better to abstract that functionality out so that it's not just this class that has this functionality.
Future plans
- Add a dependency injection container to streamline the use of the application in other applications.
- Generate a family tree of individuals.
- Performance testing and improvements.