abstract class Talgene::Genome(T)

Overview

A convenience abstract class to avoid some boilerplate over the implementation of a #fitness method.

require "talgene"

class Model < Talgene::Genome(Int32)
  def fitness : Float64
    genes.each.slice(2).sum 0.0 do |(x, y)|
      y - x
    end
  end
end

model = Model.new [0, 1, 2, 3, 4, 5]
model.fitness # => 3.0

Included Modules

Defined in:

models/genome.cr

Constructors

Instance Method Summary

Instance methods inherited from module Talgene::Fittable

<=>(other : Fittable) <=>, fitness : Float64 fitness

Constructor Detail

def self.new(genes : Array(T)) #

Creates a new Talgene::Genome instance with supplied array of genes.


[View source]

Instance Method Detail

def genes : Array(T) #

Returns the underlying array of genes of this genetic representation.


[View source]
def size #
Description copied from module Indexable(T)

Returns the number of elements in this container.


[View source]
def unsafe_fetch(index : Int) #
Description copied from module Indexable(T)

Returns the element at the given index, without doing any bounds check.

Indexable makes sure to invoke this method with index in 0...size, so converting negative indices to positive ones is not needed here.

Clients never invoke this method directly. Instead, they access elements with #[](index) and #[]?(index).

This method should only be directly invoked if you are absolutely sure the index is in bounds, to avoid a bounds check for a small boost of performance.


[View source]