# \[ANN\] charlie 0.5.0 Released - A genetic algorithms library

**URL:** <https://rubytalk.org/t/ann-charlie-0-5-0-released-a-genetic-algorithms-library/43156>\
**Category:** ruby-talk\
**Created:** [19 December 2007 17:52 UTC](https://rubytalk.org/t/ann-charlie-0-5-0-released-a-genetic-algorithms-library/43156 "2007-12-19T17:52:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sander\_Land](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@Sander\_Land](https://rubytalk.org/u/Sander_Land)\
**Post date:** [19 December 2007 17:52 UTC](https://rubytalk.org/t/ann-charlie-0-5-0-released-a-genetic-algorithms-library/43156/1 "2007-12-19T17:52:54Z")

</div>

Hi all,

I'm pleased to announce the first public release of charlie, a genetic  
algorithms library for Ruby.

## FEATURES:  
- Quickly develop GAs by combining several parts (genotype, selection,  
crossover, mutation) provided by the library.  
- Sensible defaults are provided with any genotype, so often you only  
need to define a fitness function.  
- Easily replace any of the parts by your own code.  
- Test different strategies in GA, and generate reports comparing them.

## EXAMPLE: (also at [http://pastie.caboo.se/130559](http://pastie.caboo.se/130559) with better formatting)  
This example finds the binary representation of the number 512.

require 'rubygems'  
require 'charlie'  
class Find512 \< BitStringGenotype(10) # choose a genotype, in this  
case a list of 10 bits represents a solution  
&nbsp;&nbsp;&nbsp;# Define a fitness function. This one returns minus the offset to  
the best solution, so a higher number is better.  
&nbsp;&nbsp;&nbsp;# Usually, you won't know the best solution, and will define this  
as some value that needs to be maximized.  
&nbsp;&nbsp;&nbsp;def fitness  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Use the 'genes' function to retrieve the array of bits  
representing this solution.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-(genes.map(&:to\_s).join.to\_i(2) - 512).abs  
&nbsp;&nbsp;&nbsp;end  
end  
# Finally, create an instance of a population (with the default size  
of 20) and let it run for the default number of 100 generations.  
Population.new(Find512).evolve\_on\_console

## RUBYQUIZ #142 SOLUTION:  
I know, it's a bit late. 😉

require 'rubygems'  
require 'charlie'  
N=5  
CITIES = (0...N).map{|i| (0...N).map{|j| [i,j] } }.inject{|a,b|a+b}  
class TSP \< PermutationGenotype(CITIES.size)  
&nbsp;&nbsp;&nbsp;def fitness  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d=0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(genes + [genes[0]]).each\_cons(2){|a,b|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a,b=CITIES[a],CITIES[b]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d += Math.sqrt( (a[0]-b[0])\*\*2 + (a[1]-b[1])\*\*2 )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-d # lower distance -\> higher fitness.  
&nbsp;&nbsp;&nbsp;end  
end  
pop = Population.new(TSP,20).evolve\_on\_console(50)

Several other simple examples are included in the gem/tarball.

## INSTALLATION:  
\* sudo gem install charlie

## Links  
\* [http://rubyforge.org/projects/charlie/](http://rubyforge.org/projects/charlie/)  
\* [http://charlie.rubyforge.org](http://charlie.rubyforge.org)

## LICENSE:  
MIT license.

---

<div class="post-metadata">

**Author:** ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)\
**Post date:** [19 December 2007 20:36 UTC](https://rubytalk.org/t/ann-charlie-0-5-0-released-a-genetic-algorithms-library/43156/2 "2007-12-19T20:36:40Z")

</div>

Awesome work!

Going to check it out.. Thanks.

> **···**
>
> On Dec 19, 2007 11:22 PM, Sander Land \<sander.land@gmail.com\> wrote:
> 
> > Hi all,
> > 
> > I'm pleased to announce the first public release of charlie, a genetic  
> > algorithms library for Ruby.
> > 
> > ## FEATURES:  
> > - Quickly develop GAs by combining several parts (genotype, selection,  
> > crossover, mutation) provided by the library.  
> > - Sensible defaults are provided with any genotype, so often you only  
> > need to define a fitness function.  
> > - Easily replace any of the parts by your own code.  
> > - Test different strategies in GA, and generate reports comparing them.
> > 
> > ## EXAMPLE: (also at [http://pastie.caboo.se/130559](http://pastie.caboo.se/130559) with better formatting)  
> > This example finds the binary representation of the number 512.
> > 
> > require 'rubygems'  
> > require 'charlie'  
> > class Find512 \< BitStringGenotype(10) # choose a genotype, in this  
> > case a list of 10 bits represents a solution  
> > &nbsp;&nbsp;&nbsp;# Define a fitness function. This one returns minus the offset to  
> > the best solution, so a higher number is better.  
> > &nbsp;&nbsp;&nbsp;# Usually, you won't know the best solution, and will define this  
> > as some value that needs to be maximized.  
> > &nbsp;&nbsp;&nbsp;def fitness  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Use the 'genes' function to retrieve the array of bits  
> > representing this solution.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-(genes.map(&:to\_s).join.to\_i(2) - 512).abs  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > # Finally, create an instance of a population (with the default size  
> > of 20) and let it run for the default number of 100 generations.  
> > Population.new(Find512).evolve\_on\_console
> > 
> > ## RUBYQUIZ #142 SOLUTION:  
> > I know, it's a bit late. 😉
> > 
> > require 'rubygems'  
> > require 'charlie'  
> > N=5  
> > CITIES = (0...N).map{|i| (0...N).map{|j| [i,j] } }.inject{|a,b|a+b}  
> > class TSP \< PermutationGenotype(CITIES.size)  
> > &nbsp;&nbsp;&nbsp;def fitness  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d=0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(genes + [genes[0]]).each\_cons(2){|a,b|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a,b=CITIES[a],CITIES[b]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d += Math.sqrt( (a[0]-b[0])\*\*2 + (a[1]-b[1])\*\*2 )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-d # lower distance -\> higher fitness.  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > pop = Population.new(TSP,20).evolve\_on\_console(50)
> > 
> > Several other simple examples are included in the gem/tarball.
> > 
> > ## INSTALLATION:  
> > \* sudo gem install charlie
> > 
> > ## Links  
> > \* [http://rubyforge.org/projects/charlie/](http://rubyforge.org/projects/charlie/)  
> > \* [http://charlie.rubyforge.org](http://charlie.rubyforge.org)
> 
> --  
> Let them talk of their oriental summer climes of everlasting  
> conservatories; give me the privilege of making my own summer with my  
> own coals.
> 
> [http://gnufied.org](http://gnufied.org)

---

<div class="post-metadata">

**Author:** ![Ian\_Leitch](https://avatars.discourse-cdn.com/v4/letter/i/e79b87/32.png) [@Ian\_Leitch](https://rubytalk.org/u/Ian_Leitch)\
**Post date:** [20 December 2007 10:11 UTC](https://rubytalk.org/t/ann-charlie-0-5-0-released-a-genetic-algorithms-library/43156/3 "2007-12-20T10:11:33Z")

</div>

Excellent, I've been wanting such a library for a while now. Cheers!

> **···**
>
> On 19/12/2007, hemant \<gethemant@gmail.com\> wrote:
> 
> > On Dec 19, 2007 11:22 PM, Sander Land \<sander.land@gmail.com\> wrote:  
> > \> Hi all,  
> > \>  
> > \> I'm pleased to announce the first public release of charlie, a genetic  
> > \> algorithms library for Ruby.  
> > \>  
> > \> ## FEATURES:  
> > \> - Quickly develop GAs by combining several parts (genotype, selection,  
> > \> crossover, mutation) provided by the library.  
> > \> - Sensible defaults are provided with any genotype, so often you only  
> > \> need to define a fitness function.  
> > \> - Easily replace any of the parts by your own code.  
> > \> - Test different strategies in GA, and generate reports comparing them.  
> > \>  
> > \> ## EXAMPLE: (also at [http://pastie.caboo.se/130559](http://pastie.caboo.se/130559) with better  
> > formatting)  
> > \> This example finds the binary representation of the number 512.  
> > \>  
> > \> require 'rubygems'  
> > \> require 'charlie'  
> > \> class Find512 \< BitStringGenotype(10) # choose a genotype, in this  
> > \> case a list of 10 bits represents a solution  
> > \> # Define a fitness function. This one returns minus the offset to  
> > \> the best solution, so a higher number is better.  
> > \> # Usually, you won't know the best solution, and will define this  
> > \> as some value that needs to be maximized.  
> > \> def fitness  
> > \> # Use the 'genes' function to retrieve the array of bits  
> > \> representing this solution.  
> > \> -(genes.map(&:to\_s).join.to\_i(2) - 512).abs  
> > \> end  
> > \> end  
> > \> # Finally, create an instance of a population (with the default size  
> > \> of 20) and let it run for the default number of 100 generations.  
> > \> Population.new(Find512).evolve\_on\_console  
> > \>  
> > \> ## RUBYQUIZ #142 SOLUTION:  
> > \> I know, it's a bit late. 😉  
> > \>  
> > \> require 'rubygems'  
> > \> require 'charlie'  
> > \> N=5  
> > \> CITIES = (0...N).map{|i| (0...N).map{|j| [i,j] } }.inject{|a,b|a+b}  
> > \> class TSP \< PermutationGenotype(CITIES.size)  
> > \> def fitness  
> > \> d=0  
> > \> (genes + [genes[0]]).each\_cons(2){|a,b|  
> > \> a,b=CITIES[a],CITIES[b]  
> > \> d += Math.sqrt( (a[0]-b[0])\*\*2 + (a[1]-b[1])\*\*2 )  
> > \> }  
> > \> -d # lower distance -\> higher fitness.  
> > \> end  
> > \> end  
> > \> pop = Population.new(TSP,20).evolve\_on\_console(50)  
> > \>  
> > \> Several other simple examples are included in the gem/tarball.  
> > \>  
> > \> ## INSTALLATION:  
> > \> \* sudo gem install charlie  
> > \>  
> > \> ## Links  
> > \> \* [http://rubyforge.org/projects/charlie/](http://rubyforge.org/projects/charlie/)  
> > \> \* [http://charlie.rubyforge.org](http://charlie.rubyforge.org)  
> > \>
> > 
> > Awesome work!
> > 
> > Going to check it out.. Thanks.
> > 
> > --  
> > Let them talk of their oriental summer climes of everlasting  
> > conservatories; give me the privilege of making my own summer with my  
> > own coals.
> > 
> > [http://gnufied.org](http://gnufied.org)
