dinosaurs eat everybody

for all your Dave Schwantes related needs

Darwinning, My Ruby Gem for Genetic Algorithms

Well, this writeup is a bit late. I wanted to write about Darwinning right after I launched it but here we are.

So, what is Darwinning?

Darwinning is a Ruby Gem built to help people write genetic algorithms. I got into genetic algorithms while taking a machine learning course during my masters and I’ve had a casual interest in them ever since. I even wrote a comic about them! I have also been enjoying Ruby lately, so I wanted to learn how to build a Gem. This project was started to satisfy both of those interests.

I also played with the Ruby unit testing framework, RSpec, for the first time with this project. The test coverage for Darwinning isn’t great, but at least it’s a start.

So, how do I use Darwinning?

Darwinning is totally available via RubyGems so you can install it the same way you would install almost any Gem:

gem install darwinning

Once it’s installed you can make use of the Population, Organism, and Gene classes to build your own genetic algorithm based programs.

Here’s an dumb example of how you might use Darwinning to solve a pointless problem:

Let’s say for some reason you need a set of 3 number that add up to 15. This is a strange problem to have, but let’s solve it anyway.

class Triple < Darwinning::Organism

  @name = "Triple"
  @genes = [
      Darwinning::Gene.new("first digit", (0..9)),
      Darwinning::Gene.new("second digit", (0..9)),
      Darwinning::Gene.new("third digit", (0..9))
    ]

  def fitness
    # Try to get the sum of the 3 digits to add up to 15
    (genotypes.inject{ |sum, x| sum + x } - 15).abs
  end
end 

p = Darwinning::Population.new(Triple, 10, 0, 0.1, 100)
p.evolve!

p.best_member.nice_print # prints the member representing the solution

This code declares an organism class that inherits from Darwinning’s Organism parent class to represent solutions. Then we create a population of these solution organisms and evolve the population until a solution meets the fitness threshold or the generation limit is met.

So, what else can I do with it?

If any of this sounds remotely interesting to you, please check out the project on Github. I’m always happy to take good pull requests if other people want to contribute. I haven’t been very active lately because I’ve been spending a lot of time on this website and my wedding website, but I do keep an eye on the project.

Sinatra + Jekyll + Prose

Welcome to the new Dinosaurs Eat Everybody blog! The old site/blog was an interesting mess of custom PHP and some custom build hooks into a Word Press blog and though it worked pretty well but I was tired of maintaining stuff in PHP so when my old hosting was about to expire I decided to move to a VPS and build something new. The result is what you see before you today.

This new site is built mainly on Sinatra with Jekyll mixed in for some blogging functionality. I didn’t really need any sort of content management system for most of the site so there is no database behind it and I’m using flat files (mostly just handwritten JSON) for anything that seems dynamic.

The interesting part was adding a Jekyll blog to an existing Sinatra site. Jekyll is super easy to set up on its own but I didn’t want it to run the whole site. I wanted to be able to serve dynamic pages if I wanted. With some help from this blog post it was actually pretty easy to get things up and running. That post is a bit out of date but still very helpful.

To add Jekyll to Sinatra I first just created a new Jekyll site in a seperate directory and copied it over into a jekyll_blog/ folder in the root of my Sinatra project. I then removed the .gitignore, the default.html layout and the whole css/ directory (after moving the Pygments syntax.css into my main stylesheet directory) from the new Jekyll directory. I also adjusted the _post.html layout to not have a layout of its own by just removing all the YAML Front-matter from the file. This is because I want Sinatra to handle the layouts for everything.

The real meat of this is a custom function in the /blog route:

get '/blog/?*' do
	jekyll_blog(request.path)
end

def jekyll_blog(path)
  @page_title = "blog"

  file_path = File.join(File.dirname(__FILE__), 'jekyll_blog/_site',  path.gsub('/blog',''))
  file_path = File.join(file_path, 'index.html') unless file_path =~ /\.[a-z]+$/i 

  if File.exist?(file_path)
    file = File.open(file_path, "rb")
    contents = file.read
    file.close

    erb contents
  end
end

This just calls the jekyll_blog(path) function whenever you go to a page that starts with /blog/ and that just pulls the html content of either the post or the Jekyll index.html and puts into the <%= yield %> in my Sinatra erb layout.

This is a pretty simplistic solution and I want to add better 404 checking and a way to pull the post title out for use in the page title, but it gets the job done by pulling out the content of the post html file generated by Jekyll and putting it into the content container of Sinatra. Now all my layouts are handled by the same file and all my CSS comes from the same source.

While I like the idea of being able to write my blog posts in Markdown and from whatever editor I want I also wanted to have some sort of web based editor for my posts so I’m trying Prose. Prose is basically a web based editor for Github files (which is where all the content for this site resides anyways. It lets me go in and create new posts through a pretty nice looking Markdown editor with preview. It also helps with syntax and media uploading (though I haven’t tried that yet). I’ve included a _prose.rb config file that sends me right to the _posts/ directory of this project when I’m working on it:

prose:
  rooturl: 'jekyll_blog/_posts'
  media: 'public/images/blog_media'

I think that the transiton from a really polished, very user friendly system like Word Press to something more technical like Jekyll will be interesting. I feel like I still need to work some kinks out of the system and I’m still not totally won over by Prose but it has been really fun to set this up. I’d also like to add comments through Disqus soon. Hopefully now I’ll make this work worthwhile and actually write a few blog posts every now and then.