An evolutionary framework for writing programs that use operations such as selection, combination, and mutation. Explore ideas generatively in any domain, discover novel solutions to complex problems, and build intuitions about intelligence, complexity, and the natural world.
Subscribe to the Evolvable Newsletter to slowly learn more, or keep reading this contextualization of the full documentation.
- Installation
- Getting Started
- Concepts
- Genes
- Populations
- Evaluation
- Evolution
- Selection
- Combination
- Mutation
- Search Space
Add gem "evolvable" to your Gemfile and run bundle install
or install it yourself with: gem install evolvable
{@readme Evolvable}
To demonstrate these steps, we'll look at the Hello World example program.
Let's build the evolvable hello world program using the above steps. It'll evolve a population of arbitrary strings to be more like a given target string. After installing this gem, run evolvable hello
at the command line to see it in action.
Below is example output from evolving a population of randomly initialized string objects to match "Hello World!", then "Hello Evolvable World".
❯ Enter a string to evolve: Hello World!
pp`W^jXG'_N`% Generation 0
H-OQXZ\a~{H* Generation 1 ...
HRv9X WorlNi Generation 50 ...
HRl6W World# Generation 100 ...
Hello World! Generation 165
❯ Enter a string to evolve: Hello Evolvable World
Helgo World!b+=1}3 Generation 165
Helgo Worlv!}:c(SoV Generation 166
Helgo WorlvsC`X(Joqs Generation 167
Helgo WorlvsC`X(So1RE Generation 168 ...
Hello Evolv#"l{ Wor*5 Generation 300 ...
Hello Evolvable World Generation 388
Let's begin by defining a HelloWorld
class and have it include the Evolvable
module.
class HelloWorld
include Evolvable
end
Now we can define the .search_space
class method with the types of genes that we want our our evolvable "hello world" instances to be able to have. We'll use CharGene
instances to represent single characters within strings. So an instance with the string value of "Hello" would be composed of five CharGene
instances.
class HelloWorld
include Evolvable
def self.search_space
["CharGene", 1..40]
end
end
The Search Space can be defined in a variety of ways. The above is shorthand that's useful for when there's only one type of gene. This method can also return an array of arrays or hash.
The 1..40
specifies the range of possible genes for a particular HelloWorld instance. Evolvable translates this range or integer value into a Evolvable::CountGene
object.
By specifying a range, an Evolvable::CountGene
instance can change the number of genes that are present in an evovlable instance. Count genes undergo evolutionary operations like any other gene. Their effects can be seen in the letter changes from Generation 165 to 168 in the above example output.
To finish step 2, we'll define the gene class that we referenced in the above .search_space
method. Gene classes should include the Evolvable::Gene
module.
class CharGene
include Evolvable::Gene
def self.chars
@chars ||= 32.upto(126).map(&:chr)
end
def to_s
@to_s ||= self.class.chars.sample
end
end
It's important that, once accessed, the data for a particular gene never change. When the #to_s
method first runs, Ruby's ||=
operator memoizes the result of randomly picking a char, enabling this method to sample a char only once per gene.
After defining the search space, we can now initialize HelloWorld
instances with random genes, but to actually evolve them, we need to define the #value
instance method. It provides the basis for comparing different evolvable instances.
In the next step, we'll set the goal value to 0, so that evolution favors evolvable HelloWorld instances with #value
methods that return numbers closer to 0. That means we want instances that more closely match their targets to return scores nearer to 0. As an example, if our target is "hello world", an instance that returns "jello world" would have a value of 1 and "hello world" would have a value of 0.
For a working implementation, see the #value
method in examples/hello_world.rb
Now it's time to initialize a population with .new_population
. By default, evolvable populations seek to maximize numeric values. In this program, we always know the best possible value, so setting the goal to a concrete number makes sense. This is done by passing the evaluation params with equalize set to 0.
We'll also specify the number of instances in a population using the population's size
parameter and change the mutation porbability from 0.03 (3%) to 0.6 (60%).
Experimentation has suggested that a large mutation probability tends to decrease the time it takes to evolve matches with short strings and has the opposite effect for long strings. This is demonstrated in the example output above by how many generations it took to go from "Hello World!" to "Hello Evolvable World". As an optimization, we could dynamically change the mutation probability using a population hook detailed below, but doing so will be left as an exercise for the reader. Pull requests are welcome.
population = HelloWorld.new_population(size: 100,
evaluation: { equalize: 0 },
mutation: { probability: 0.6 }
At this point, everything should work when we run population.evolve
, but it'll look like nothing is happening. The next section will allow us to gain instight by hooking into the evolutionary process.
The following class methods can be implemented on your Evolvable class, e.g. HelloWorld, to hook into the Population#evolve lifecycle. This is useful for logging evolutionary progress, optimizing parameters, and creating interactions with and between evolvable instances.
.before_evaluation(population)
- {@readme Evolvable::ClassMethods#before_evaluation}.before_evolution(population)
- {@readme Evolvable::ClassMethods#before_evolution}.after_evolution(population)
- {@readme Evolvable::ClassMethods#after_evolution}
Let's define .before_evolution
to print the best value for each generation. We'll also define HelloWorld#to_s
, which implicitly delegates to CharGene#to_s
during the string interpolation that happens.
class HelloWorld
include Evolvable
def self.before_evolution(population)
best_evolvable = population.best_evolvable
evolutions_count = population.evolutions_count
puts "#{best_evolvable} - Generation #{evolutions_count}"
end
# ...
def to_s
@to_s ||= genes.join
end
# ...
end
Finally we can evolve the population with the Evolvable::Population#evolve
instance method.
population.evolve
You now know the fundamental steps to building evolvable programs of endless complexity in any domain! 🐸 The exact implementation for the command line demo can be found in exe/hello and examples/hello_world.rb.
Populations are composed of evolvables which are composed of genes. Evolvables orchestrate behaviors by delegating to gene objects. Collections of genes are organized into genomes and constitute the search space. Evaluation and evolution objects are used to evolve populations. By default, evolution is composed of selection, combination, and mutation.
The following concept map depicts how genes flow through populations.
Evolvable is designed with extensibility in mind. Evolvable objects such as evaluation, evolution, selection, combination, and mutation can be extended and swapped, potentially in ways that alter the above graph.
{@readme Evolvable::Gene}
{@example Evolvable::Gene}
{@readme Evolvable::Population}
{@example Evolvable::Population}
{@readme Evolvable::Evaluation}
{@example Evolvable::Evaluation}
{@readme Evolvable::Evolution}
{@readme Evolvable::Selection}
{@example Evolvable::Selection}
{@readme Evolvable::GeneCombination}
{@readme Evolvable::Mutation}
{@example Evolvable::Mutation}
{@readme Evolvable::SearchSpace}
{@example Evolvable::SearchSpace}
Bug reports and pull requests are welcome on GitHub at https://github.com/mattruzicka/evolvable.
If you're interested in contributing, but don't know where to get started, message me on twitter at @mattruzicka.