Skip to content

ActiveRecord support for evolvable Ruby models and their populations.

Notifications You must be signed in to change notification settings

mattruzicka/evolvable-activerecord

Repository files navigation

Evolvable::ActiveRecord

A Ruby gem that provides ActiveRecord persistence for the Evolvable evolutionary computation framework. This gem allows you to create evolvable models that can participate in evolutionary processes, manage populations, and track generations of evolution with database persistence.

Installation

Add this line to your application's Gemfile:

gem 'evolvable-activerecord'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install evolvable-activerecord

Setup

After installing the gem, you need to set up the database tables. Run the following command:

$ rails generate evolvable:install

This will create the necessary migration files. Then run:

$ rails db:migrate

This will create the following tables:

  • evolvables - Stores evolvable entities with their type, fitness score, and serialized genome data
  • evolvable_communities - Manages communities of evolvables by type for grouped evolution
  • evolvable_ecosystems - Connects communities and populations with cycling configurations for managing evolution schedules
  • evolvable_populations - Tracks populations of specific evolvable types with evolution counts and serialized state
  • evolvable_generations - Records generations within populations, tracking evolution progress over time
  • evolvable_instances - Creates relationships between specific evolvables and their community, population, and generation with genome data

Usage

Evolvable::ActiveRecord extends the Evolvable framework by providing database persistence for evolvable objects, populations, and communities.

Evolvable Classes

Suppose you have a model class that should evolve over time, like a Melody class:

class Melody
  include Evolvable
  include Evolvable::ActiveRecord

  gene :notes, type: NotesGene, count: 4..16
  gene :octave, type: OctaveGene, count: 1
  gene :duration, type: DurationGene, count: 4..16

  # ...
end

By including Evolvable::ActiveRecord, you gain access to the following persistence functionality:

# Find or create a population for your evolvable class
population = Melody.find_or_create_population

# Create a new evolvable instance
melody = population.new_evolvable

# Save the evolvable after using/evaluating it
melody.save_evolvable!

# Find a specific evolvable by ID
melody = Melody.find_evolvable(id)

# Find all populations for this evolvable class
populations = Melody.find_populations

Community Classes

Suppose you want to manage multiple related evolvable populations together, like different musical elements that make up a Song:

class Song
  include Evolvable::Community
  include Evolvable::ActiveRecord

  evolvable_community melody: Melody,
                     rhythm: Rhythm,
                     bassline: Bassline,
                     chord_progression: ChordProgression


  # ...
end

By including Evolvable::ActiveRecord with Evolvable::Community, you gain database persistence for community instances, allowing you to:

# Create a new community with persisted populations
song = Song.new_community

# Find an existing community by ID
song = Song.find_community(id)

# Access specific evolvables from their populations (creates new instances if none exist)
song.melody      # => Returns a melody from the melody population
song.rhythm      # => Returns a rhythm from the rhythm population

# Add a specific population to the community
song.add_population(:vocals, VocalsPopulation.find_or_create_population)

# Find or manipulate specific evolvables
specific_bassline = song.find_evolvable(:bassline)
song.add_evolvable(:drums, drum_instance)

# Save the community and all its associated evolvables
song.save_community!

How It Works

This persistence behavior is achieved by extending the core classes and modules from the Evolvable gem to add database storage capabilities while maintaining the same interface, making it seamless to track evolutionary changes using ActiveRecord storage.

  • evolvable_extension.rb - Extends the core Evolvable module with ActiveRecord persistence methods
  • population_extension.rb - Extends the Evolvable::Population class with ActiveRecord persistence methods
  • community_extension.rb - Extends the Evolvable::Community module with ActiveRecord persistence methods

Direct Database Access

For easier direct access to the database tables in your Rails application, you can create model classes that inherit from the ActiveRecord models:

# app/models/population.rb
class Population < Evolvable::ActiveRecord::Population
end

# app/models/community.rb
class Community < Evolvable::ActiveRecord::Community
end

# app/models/instance.rb
class Instance < Evolvable::ActiveRecord::Instance
end

This is completely optional, but allows you to use standard ActiveRecord queries and operations more succinctly:

# Find a specific population by ID
population = Population.find(id)

# Find all populations for a specific evolvable type
melody_populations = Population.where(evolvable_type: 'Melody')

# Find communities created in the last week
recent_communities = Community.where('created_at > ?', 1.week.ago)

# Access associations directly
population.evolvables
population.generations

These model classes give you the full power of ActiveRecord for querying, reporting, and managing your evolutionary data.

About

ActiveRecord support for evolvable Ruby models and their populations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published