Skip to content

Commit

Permalink
allow callbacks to be optionally nil
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomascountz committed Jul 29, 2023
1 parent d1ec152 commit 8957a57
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ The `Configuration` class in Petri Dish allows you to customize various aspects
| `crossover_function` | A function used to perform crossover between two parents | `Proc[Array[Member], Member]` |
| `mutation_function` | A function used to mutate the genes of an individual | `Proc[Member, Member]` |
| `fitness_function` | A function used to calculate the fitness of an individual | `Proc[Member, Numeric]` |
| `highest_fitness_callback` | A callback function invoked when a new highest fitness is found | `Proc[Member, void]` |
| `max_generation_reached_callback` | A callback function invoked when the maximum number of generations is reached | `Proc[void, void]` |
| `end_condition_function` | A function that determines whether the evolution process should stop premature of `max_generations` | `Proc[Member, bool]` |
| `generation_start_callback` | A callback function invoked at the start of each new generation | `Proc[void, void]` |
| `end_condition_reached_callback` | A callback function invoked when the end condition is met. It is called with the `Member` which triggered the `end_condition_function` | `Proc[Member, void]` |
| `highest_fitness_callback` | A callback function invoked when a new highest fitness is found | `nil | Proc[Member, void]` |
| `max_generation_reached_callback` | A callback function invoked when the maximum number of generations is reached | `nil | Proc[void, void]` |
| `end_condition_function` | A function that determines whether the evolution process should stop premature of `max_generations` | `nil | Proc[Member, bool]` |
| `generation_start_callback` | A callback function invoked at the start of each new generation | `nil | Proc[Integer, void]` |
| `end_condition_reached_callback` | A callback function invoked when the end condition is met. It is called with the `Member` which triggered the `end_condition_function` | `nil | Proc[Member, void]` |

You can create a new `Configuration` object by calling `Configuration.configure` and providing a block:

Expand Down
16 changes: 8 additions & 8 deletions lib/petri_dish/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ def validate!
raise ArgumentError, "crossover_function must respond to :call" unless crossover_function.respond_to?(:call)
raise ArgumentError, "mutation_function must respond to :call" unless mutation_function.respond_to?(:call)
raise ArgumentError, "end_condition_function must respond to :call" unless end_condition_function.respond_to?(:call)
raise ArgumentError, "highest_fitness_callback must respond to :call" unless highest_fitness_callback.respond_to?(:call)
raise ArgumentError, "max_generation_reached_callback must respond to :call" unless max_generation_reached_callback.respond_to?(:call)
raise ArgumentError, "generation_start_callback must respond to :call" unless generation_start_callback.respond_to?(:call)
raise ArgumentError, "end_condition_reached_callback must respond to :call" unless end_condition_reached_callback.respond_to?(:call)
raise ArgumentError, "highest_fitness_callback must respond to :call" unless highest_fitness_callback.nil? || highest_fitness_callback.respond_to?(:call)
raise ArgumentError, "max_generation_reached_callback must respond to :call" unless max_generation_reached_callback.nil? || max_generation_reached_callback.respond_to?(:call)
raise ArgumentError, "generation_start_callback must respond to :call" unless generation_start_callback.nil? || generation_start_callback.respond_to?(:call)
raise ArgumentError, "end_condition_reached_callback must respond to :call" unless end_condition_reached_callback.nil? || end_condition_reached_callback.respond_to?(:call)
end

def reset!
Expand Down Expand Up @@ -110,16 +110,16 @@ def default_mutation_function = ->(_member) { raise ArgumentError, "mutation_fun

def default_end_condition_function = ->(_member) { false }

def default_highest_fitness_callback = ->(_member) { :noop }
def default_highest_fitness_callback = nil

# TODO: We might want to consider whether we really want to use `exit` as a
# default callback. This will stop the entire Ruby process, which could be
# surprising behavior if the user of the library doesn't override these
# callbacks.
def default_max_generation_reached_callback = -> { exit }
def default_max_generation_reached_callback = nil

def default_generation_start_callback = ->(_generation) { :noop }
def default_generation_start_callback = nil

def default_end_condition_reached_callback = ->(_member) { exit }
def default_end_condition_reached_callback = nil
end
end
8 changes: 4 additions & 4 deletions lib/petri_dish/world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def run(
configuration: Configuration.new,
metadata: Metadata.new
)
configuration.generation_start_callback.call(metadata.generation_count)
configuration.generation_start_callback&.call(metadata.generation_count)

end_condition_reached = false
max_generation_reached = false
Expand All @@ -33,7 +33,7 @@ def run(
configuration.logger.info(metadata.to_json)

if metadata.generation_count >= configuration.max_generations
configuration.max_generation_reached_callback.call
configuration.max_generation_reached_callback&.call
max_generation_reached = true
end

Expand All @@ -46,13 +46,13 @@ def run(
configuration.mutation_function.call(child_member).tap do |mutated_child|
if metadata.highest_fitness < mutated_child.fitness
metadata.set_highest_fitness(mutated_child.fitness)
configuration.highest_fitness_callback.call(mutated_child)
configuration.highest_fitness_callback&.call(mutated_child)

configuration.logger.info(metadata.to_json)
end

if configuration.end_condition_function.call(mutated_child)
configuration.end_condition_reached_callback.call(mutated_child)
configuration.end_condition_reached_callback&.call(mutated_child)
end_condition_reached = true
end
end
Expand Down

0 comments on commit 8957a57

Please sign in to comment.