GithubHelp home page GithubHelp logo

gga4r's Introduction

gga4r - General Genetic Algorithm for Ruby

##Introduction

General Genetic Algorithm for Ruby is a Ruby Genetic Algorithm that is very simple to use:

  1. Take a class to evolve it and define fitness, recombine and mutate methods.
class StringPopulation < Array
  def fitness
    self.select { |pos| pos == 1 }.size.to_f / self.size.to_f
  end

  def recombine(c2)
    cross_point = (rand * c2.size).to_i
    c1_a, c1_b = self.separate(cross_point)
    c2_a, c2_b = c2.separate(cross_point)
    StringPopulation.new(c1_a + c2_b)
  end

  def mutate
    mutate_point = (rand * self.size).to_i
    self[mutate_point] = 1
  end
end
  1. Create a GeneticAlgorithm object with the population.
def create_population_with_fit_all_1s(s_long = 10, num = 10)
    population = []
    num.times  do
      chromosome = StringPopulation.new(Array.new(s_long).collect { (rand > 0.2) ? 0:1 })
      population << chromosome
    end
    population
end

ga = GeneticAlgorithm.new(create_population_with_fit_all_1s)
  1. Call the evolve method as many times as you want and see the best evolution.
100.times { |i|  ga.evolve }
p ga.best_fit[0]

##Install

  1. Execute:
gem install gga4r
  1. Add require in your code headers:
require "rubygems"
require "gga4r"

##Documentation

Documentation can be generated using rdoc tool under the source code with:

rdoc README lib

##Contributors

##Copying

This work is developed by Sergio Espeja ( www.upf.edu/pdi/iula/sergio.espeja, sergio.espeja at gmail.com ) mainly in Institut Universitari de Lingüística Aplicada of Universitat Pompeu Fabra ( www.iula.upf.es ), and also in bee.com.es ( bee.com.es ).

It is free software, and may be redistributed under GPL license.

gga4r's People

Contributors

benprew avatar spejman avatar pcarranzav avatar hstove avatar trans avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.