darwinning icon indicating copy to clipboard operation
darwinning copied to clipboard

Organism with arguments in initialize

Open davetapley opened this issue 7 years ago • 2 comments

Is there any way to specify arguments to be passed in to the organism's initialize?

E.g: Consider the example in the README, how could I modify this so I can pass in the target number, instead of having it hard coded to 100?

davetapley avatar Mar 31 '17 23:03 davetapley

Sure, you can define your fitness method any way you want. For example you could write something like this:

require 'darwinning'

class Triple
  include Darwinning

  ...

  def initialize(target_sum)
    @target_sum = target_sum
  end

  def fitness
    # Try to get the sum of the 3 digits to add up to 100
    (first_number + second_number + third_number - @target_sum).abs
  end
end

dorkrawk avatar Apr 05 '17 20:04 dorkrawk

So, I wrote something similar to that also, but when I used the snippet showing how to call evolve!, I get the following error:

in `initialize': wrong number of arguments (given 0, expected 1) (ArgumentError)
        from /usr/local/bundle/gems/darwinning-0.1.1/lib/darwinning/population.rb:101:in `new'
        from /usr/local/bundle/gems/darwinning-0.1.1/lib/darwinning/population.rb:101:in `build_member'
        from /usr/local/bundle/gems/darwinning-0.1.1/lib/darwinning/population.rb:31:in `block in build_population'
        from /usr/local/bundle/gems/darwinning-0.1.1/lib/darwinning/population.rb:30:in `times'
        from /usr/local/bundle/gems/darwinning-0.1.1/lib/darwinning/population.rb:30:in `build_population'
        from /usr/local/bundle/gems/darwinning-0.1.1/lib/darwinning/population.rb:25:in `initialize'
        from /usr/local/bundle/gems/darwinning-0.1.1/lib/darwinning.rb:18:in `new'
        from /usr/local/bundle/gems/darwinning-0.1.1/lib/darwinning.rb:18:in `build_population'
        from test.rb:25:in `<main>'

:arrow_up: that is from this.

davetapley avatar Apr 05 '17 21:04 davetapley