Module: Evolvable::ClassMethods
- Defined in:
- lib/evolvable.rb
Instance Attribute Summary collapse
-
#cluster_config ⇒ Object
readonly
Returns the value of attribute cluster_config.
-
#gene_config ⇒ Object
readonly
Returns the value of attribute gene_config.
Instance Method Summary collapse
- #after_evolution(population) ⇒ Object
- #before_evaluation(population) ⇒ Object
- #before_evolution(population) ⇒ Object
- #cluster(cluster_name, type:, **opts) ⇒ Object
- #gene(name, type:, count: 1, cluster: nil) ⇒ Object
-
#inherited(subclass) ⇒ void
Ensures that subclasses inherit the gene and cluster configuration.
-
#initialize_evolvable ⇒ Evolvable
Override this method to customize how your evolvable instances are initialized.
-
#new_evolvable(population: nil, genome: nil, generation_index: nil) ⇒ Object
Initializes a new instance.
-
#new_gene_space ⇒ Evolvable::GeneSpace
Creates a new gene space for this evolvable class.
- #new_population(keyword_args = {}) ⇒ Object
Instance Attribute Details
#cluster_config ⇒ Object (readonly)
Returns the value of attribute cluster_config.
245 246 247 |
# File 'lib/evolvable.rb', line 245 def cluster_config @cluster_config end |
#gene_config ⇒ Object (readonly)
Returns the value of attribute gene_config.
245 246 247 |
# File 'lib/evolvable.rb', line 245 def gene_config @gene_config end |
Instance Method Details
#after_evolution(population) ⇒ Object
297 |
# File 'lib/evolvable.rb', line 297 def after_evolution(population); end |
#before_evaluation(population) ⇒ Object
273 |
# File 'lib/evolvable.rb', line 273 def before_evaluation(population); end |
#before_evolution(population) ⇒ Object
291 |
# File 'lib/evolvable.rb', line 291 def before_evolution(population); end |
#cluster(cluster_name, type:, **opts) ⇒ Object
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/evolvable.rb', line 234 def cluster(cluster_name, type:, **opts) recipe = type.is_a?(String) ? Object.const_get(type) : type unless recipe.respond_to?(:apply_cluster) raise ArgumentError, "#{recipe} cannot apply a gene cluster" end recipe.apply_cluster(self, cluster_name, **opts) define_method(cluster_name) { find_gene_cluster(cluster_name) } end |
#gene(name, type:, count: 1, cluster: nil) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/evolvable.rb', line 174 def gene(name, type:, count: 1, cluster: nil) raise Error, "Gene name '#{name}' is already defined" if @gene_config.key?(name) @gene_config[name] = { type: type, count: count } if (count.is_a?(Range) ? count.last : count) > 1 define_method(name) { find_genes(name) } else define_method(name) { find_gene(name) } end if cluster raise Error, "Cluster name '#{cluster}' conflicts with an existing gene name" if @gene_config.key?(cluster) if @cluster_config[cluster] @cluster_config[cluster] << name else @cluster_config[cluster] = [name] define_method(cluster) { find_gene_cluster(cluster) } end end end |
#inherited(subclass) ⇒ void
This method returns an undefined value.
Ensures that subclasses inherit the gene and cluster configuration.
263 264 265 266 267 |
# File 'lib/evolvable.rb', line 263 def inherited(subclass) super subclass.instance_variable_set(:@gene_config, @gene_config.dup) subclass.instance_variable_set(:@cluster_config, @cluster_config.dup) end |
#initialize_evolvable ⇒ Evolvable
Override this method to customize how your evolvable instances are initialized. By default, simply calls new with no arguments.
143 144 145 |
# File 'lib/evolvable.rb', line 143 def initialize_evolvable new end |
#new_evolvable(population: nil, genome: nil, generation_index: nil) ⇒ Object
Initializes a new instance. Accepts a population object, an array of gene objects, and the instance's population index. This method is useful for re-initializing instances and populations that have been saved.
It is not recommended that you override this method as it is used by Evolvable internals. If you need to customize how your instances are initialized you can override either of the following two "initialize_instance" methods.
130 131 132 133 134 135 |
# File 'lib/evolvable.rb', line 130 def new_evolvable(population: nil, genome: nil, generation_index: nil) evolvable = initialize_evolvable evolvable.make_evolvable(population: population, genome: genome, generation_index: generation_index) evolvable.after_initialize_evolvable evolvable end |
#new_gene_space ⇒ Evolvable::GeneSpace
Creates a new gene space for this evolvable class. Used internally when initializing populations.
253 254 255 |
# File 'lib/evolvable.rb', line 253 def new_gene_space GeneSpace.build(@gene_config, self) end |
#new_population(keyword_args = {}) ⇒ Object
115 116 117 118 |
# File 'lib/evolvable.rb', line 115 def new_population(keyword_args = {}) keyword_args[:evolvable_type] = self Population.new(**keyword_args) end |