Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Thursday, October 05, 2006

Genetic Algorithms with Ruby

I was very excited when Shark Hunter presented his first session on AI. He started with Genetic Algorithms. The idea that a solution "magically" evolves is fascinating. All you need to do is present your candidate solutions as a sequence "chromosome" of some objects "genes", then assign a fitness to each chromosome, then mimic nature by mating and mutating the population. Eventually "life finds a way", and solution is found.

One of the nice sites with demos is here.

So I thought this will be a very cool exercise to practice my newly acquired Ruby skills. It took me a few hours to come up with a simple "framework" to represent a general GA.

I will keep updating my GA framework and add more tools and functionality to it. Ultimately I want to be able to modify my sub Chromosome class only, and just add a fitness method, then fire my simulation and let the search begin.


Basically the framework consists of a Chromosome class, and a Population Class.


The Chromosome Class

This class represents a Base Chromosome. It is basically a sequence of genes that have a fitness, and possibly represent a certain value. The Base class knows how to marry, or crossover. The Base class crossover is a single point, and no mutation is done. A subclass will be created that can have two crossover points. The framework is still in very early stages, and will be re-factored. But basically, that is all there is to it.


The Population Class

This class represents a Population of Chromosomes. It holds the entire population and is responsible for Selection and Generating offsprings. The Base class does a simple Roulette Wheel Selection based on a fitness value. To make the initial code simple, the fitness value must be higher for fitter solutions. So, a chromosome of fitness = 10 will be 10 times likely to be selected that a chromosome with fitness = 1.


Genes?

I did not create any classes for genes. I wanted to make things simple, so the sequence itself will represent any kind of genes. It is tested as a Strings in this initial version. The main thing is finding a way to represent the fitness of the chromosome. I will see if representing binary numbers, integers as sequences, or real numbers will require any changes in the sequence attribute or require the creation of a Gene class. But since Ruby is so dynamic, I doubt this will be needed.


The Code

Anxious to see the code and try it out? Drop my a line, and I will send it to you or publish the current version somewhere. It is still in very early stages, but works for simple problems.


Saturday, September 09, 2006

Java + Ruby = JRuby

JRuby is an implementation of the Ruby programming language in Java. The common Ruby implementation is in C. So what is so cool another implementation of Ruby in Java?
Well, I did several small applications in Java, and it was my language of choice for some time now. Java is Free, and lots of IDE's are free, like eclipse, and netbeans. Java has millions of free libraries for almost anything. Java can be used on the server with jsp, j2ee and on the client with Swing. So it's an all-in-one language.
So, having a Ruby implementation in Java, you can leverage your Java code, and use the elegance and beauty, and fun of coding in Ruby. You can extend your Java classes using Ruby, and you can automate some of the high level work for your applications using Ruby.
Another GREAT news is that JRuby is now endorsed, and officially supported by Sun. The core JRuby developers are paid by Sun. Way to go Sun!

There are some nice tutorials in the JRuby site. I'll try that out myself and see how far I can go with JRuby.

Thursday, September 07, 2006

Ruby Oneliners

Ruby can be very useful for performing some rapid and powerful operations on files. The Ruby syntax is much more user friendly than awk or sed, especially if you are familiar with Ruby.
The one liners can be found here.
If you are running on Windows, then substitute type for cat.
As an example, Here is a handy oneliner

# delete leading whitespace (spaces/tabs/etc) from beginning of each line
$ cat | ruby -pe 'gsub(/^\s+/, "")'

Note that the command
ruby -pe
will execute the Ruby commands that follow it on each input line, and sends the output to STDOUT. To capture the output you can use the Linux/Unix or DOS > char.
Another useful oneliner is the following:

# print line number 52
$ cat | ruby -pe 'next unless $. == 52'

The above oneliners depend on the $ Ruby global variables. Here are some of the most commonly used:

$! error message
$@ position of an error occurrence
$_ latest read string by `gets'
$. latest read number of line by interpreter
$& latest matched string by the regexep.
$1, $2... latest matched string by nth parentheses of regexp.
$~ data for latest match for regexp
$= whether or not case-sensitive in string matching
$/ input record separator
$\ output record separator
$0 the name of the ruby script file
$* command line arguments for the ruby script
$$ PID for ruby interpreter
$? status of the latest executed child process

Have fun.

Sunday, August 27, 2006

Ruby Scripts - Simple SQL dumps

We have an old version of SQL GUI tool that can return the results of an SQL statement as tab delimited data. This works fine for small result sets. However I created a Reconciliation system that needed few 10's of thousands of records. The GUI just could not handle that. So, a nice exercise to use Ruby to the rescue and learn more about it's DBI.

Here is the sample Ruby code:
require 'DBI'

# make an ODBC connection
conn = DBI.connect('DBI:ODBC:','[USERNAME-HERE]','[PASSWORD-HERE]')

sth = conn.execute("
[SELECT STATEMENT HERE]
")
# If you need column names, then you can use the below statement to get them
# cols=sth.column_names
sth.each do |row|
puts = row.join("\t")
end
sth.finish

Nothing except the Ruby Installer was needed to run this.

Just Google it!