A Ruby DSL for Genetic Algorithms using JGAP.
The JGAP binary JAR file is packaged with this gem.
JGAP is licensed under the GNU Lesser Public license; if you would like to use this gem in commercial applications, you can choose to use the Mozilla Public License and donate 50 euros to JGAP. more details
gem install ruby-jgap
or
jruby -S gem install ruby-jgap
require 'java'
require 'ruby-jgap'
# subclass JGAP::Problem
class MakeChangeProblem < JGAP::Problem
population_size 500
# define our solution chromosome
chromosome do
integer :quarters, min: 0, max: 3
integer :dimes, min: 0, max: 2
integer :nickels, min: 0, max: 1
integer :pennies, min: 0, max: 4
end
# define our fitness function
fitness_function do |subject|
target = 47 # goal: 47 cents
q = read subject, :quarters
d = read subject, :dimes
n = read subject, :nickels
p = read subject, :pennies
coins = q + d + n + p
value = 25*q + 10*d + 5*n + p
delta = (target - value).abs # how far are we from our goal?
minimize(coins + 2*delta) # minimize our cost
end
end
problem = MakeChangeProblem.new
problem.run(100) # 100 generations
problem.print_best
This outputs (probably):
quarters: 1
dimes: 2
nickels: 0
pennies: 2
Adapted from the Getting Started page on JGAP's site.