Module: Evolvable::Community::ClassMethods

Defined in:
lib/evolvable/community.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#community_configObject

Returns the value of attribute community_config.



77
78
79
# File 'lib/evolvable/community.rb', line 77

def community_config
  @community_config
end

Instance Method Details

#add_population(name, population) ⇒ Object

Adds a population to the community

Parameters:

  • name (String, Symbol)

    The name of the population

  • population (Object)

    The population instance

Returns:

  • (Object)

    The added population



105
106
107
# File 'lib/evolvable/community.rb', line 105

def add_population(name, population)
  populations_by_name[name.to_sym] = population
end

#evolvable_community(community_config) ⇒ void

This method returns an undefined value.

Configures a class as an evolvable community with specified populations

Parameters:

  • community_config (Hash)

    A hash mapping names to population classes



82
83
84
85
86
87
88
89
90
# File 'lib/evolvable/community.rb', line 82

def evolvable_community(community_config)
  self.community_config = community_config
  community_config.each_key do |name|
    method_name = name.to_sym
    define_method(method_name) do
      instance_variable_get("@#{method_name}") || instance_variable_set("@#{method_name}", find_or_new_evolvable(method_name))
    end
  end
end

#initialize_communityObject

Initializes a new community instance This method exists as a hook for classes that include this module to override and customize the community instantiation process.

Returns:

  • (Object)

    A new community instance



69
70
71
# File 'lib/evolvable/community.rb', line 69

def initialize_community
  new
end

#load_community(data) ⇒ Object



73
74
75
# File 'lib/evolvable/community.rb', line 73

def load_community(data)
  Serializer.load(data)
end

#new_communityObject

Creates a new instance of the community

Returns:

  • (Object)

    A new community instance



61
62
63
# File 'lib/evolvable/community.rb', line 61

def new_community
  initialize_community
end

#populations_by_nameHash

Returns a hash of population instances by name

Returns:

  • (Hash)

    A hash mapping names to population instances



94
95
96
97
98
99
# File 'lib/evolvable/community.rb', line 94

def populations_by_name
  @populations_by_name ||= community_config.inject({}) do |hash, (name, population_klass)|
    hash[name.to_sym] = population_klass.new_population(size: 0)
    hash
  end
end