@@ -5,30 +5,28 @@ module Evolvable
5
5
# @readme
6
6
# Populations orchestrate the evolutionary process through four key components:
7
7
#
8
- # 1. **Evaluation**: Sorts instances by fitness
8
+ # 1. **Evaluation**: Sorts evolvable instances by fitness
9
9
# 2. **Selection**: Chooses parents for combination
10
- # 3. **Combination**: Creates new instances from parents
11
- # 4. **Mutation**: Adds genetic diversity
10
+ # 3. **Combination**: Creates new evolvables from selected parents
11
+ # 4. **Mutation**: Introduces variation to maintain genetic diversity
12
12
#
13
13
# **Features**:
14
14
#
15
- # Initialize your population with parameters
15
+ # Initialize a population with default or custom parameters:
16
16
#
17
17
# ```ruby
18
18
# population = YourEvolvable.new_population(
19
- # size: 50, # Population size (default is 0)
20
- # evaluation: :minimize, # Set goal type (default is :maximize)
21
- # evaluation: { equalize: 0 }, # Supports setting an explicit goal (for fitness to equal 0)
22
- # selection: { size: 10 }, # Parent selection count (default is 2)
23
- # mutation: { probability: 0.2, # Odds of instance undergoing mutation (default: 0.03)
24
- # rate: 0.02 } # Odds of gene being mutated (1 gene is mutated by default)
19
+ # size: 50,
20
+ # evaluation: { equalize: 0 },
21
+ # selection: { size: 10 },
22
+ # mutation: { probability: 0.2, rate: 0.02 }
25
23
# )
26
24
# ```
27
25
#
28
- # Supports setting custom objects for the following components:
26
+ # Or inject fully customized strategy objects:
27
+ #
29
28
# ```ruby
30
29
# population = YourEvolvable.new_population(
31
- # gene_space: Your::GeneSpace.new,
32
30
# evaluation: Your::Evaluation.new,
33
31
# evolution: Your::Evolution.new,
34
32
# selection: Your::Selection.new,
@@ -37,48 +35,36 @@ module Evolvable
37
35
# )
38
36
# ```
39
37
#
40
- # Evolve your population with a certain number of generations and/or until a goal is met
41
- #
42
- # ```ruby
43
- # population.evolve(count: 20, goal_value: 100)
44
- # ```
45
- #
46
- # Initialize new evolvables with the evolution strategy defined by the population
47
- #
48
- # ```ruby
49
- # new_evolvable = population.new_evolvable
50
- # ten_evolvables = population.new_evolvables(count: 10)
51
- # ```
52
- #
53
- # Initialize an evolvable with a custom genome
38
+ # Evolve your population:
54
39
#
55
40
# ```ruby
56
- # outsider_genome = other_population.best_evolvable.genome
57
- # outsider_evolvable = population.new_evolvable(genome: outsider_genome)
41
+ # population.evolve(count: 20) # Run for 20 generations
42
+ # population.evolve_to_goal # Run until the current goal is met
43
+ # population.evolve_to_goal(0.0) # Run until a specific goal is met
44
+ # population.evolve_forever # Run indefinitely, ignoring any goal
45
+ # population.evolve_selected([...]) # Use a custom subset of evolvables
58
46
# ```
59
47
#
60
- # Hook into class methods that are called during evolution
48
+ # Create new evolvables:
61
49
#
62
50
# ```ruby
63
- # class YourEvolvable
64
- # include Evolvable
65
- #
66
- # def self.before_evaluation(population); end
67
- # def self.before_evolution(population); end
68
- # def self.after_evolution(population); end
69
- # end
51
+ # new = population.new_evolvable
52
+ # many = population.new_evolvables(count: 10)
53
+ # with_genome = population.new_evolvable(genome: another.genome)
70
54
# ```
71
55
#
72
- # Return the bsest evolvable
56
+ # Customize the evolution lifecycle by implementing hooks:
73
57
#
74
58
# ```ruby
75
- # best_evolvable = population.best_evolvable if population.met_goal?
59
+ # def self.before_evaluation(pop); end
60
+ # def self.before_evolution(pop); end
61
+ # def self.after_evolution(pop); end
76
62
# ```
77
63
#
78
- # Check if the population's goal has been met
64
+ # Evaluate progress:
79
65
#
80
66
# ```ruby
81
- # population.met_goal?
67
+ # best = population.best_evolvable if population.met_goal?
82
68
# ```
83
69
#
84
70
class Population
@@ -215,13 +201,13 @@ def evaluation=(val)
215
201
:goal=
216
202
217
203
#
218
- # Evolves the population for a specified number of generations or until a goal is met .
204
+ # Evolves the population for a specified number of generations or until the goal is achieved .
219
205
#
220
- # @param count [Integer, Float] The number of evolutions to run (default: Float::INFINITY)
221
- # @param goal_value [Numeric, nil] Optional goal value to evolve toward
222
- # @return [Evolvable::Population] The evolved population
206
+ # @param count [Integer, Float] Number of generations to evolve. Use ` Float::INFINITY` for indefinite evolution. Defaults to `1`.
207
+ # @param goal_value [Numeric, nil] Optional target value for the goal. If provided, evolution halts when this value is met.
208
+ # @return [Evolvable::Population] The evolved population.
223
209
#
224
- def evolve ( count : Float :: INFINITY , goal_value : nil )
210
+ def evolve ( count : 1 , goal_value : nil )
225
211
goal . value = goal_value if goal_value
226
212
1 . upto ( count ) do
227
213
before_evaluation ( self )
@@ -235,6 +221,31 @@ def evolve(count: Float::INFINITY, goal_value: nil)
235
221
end
236
222
end
237
223
224
+ #
225
+ # Evolves the population until the goal is met.
226
+ #
227
+ # If no goal value is provided, it uses the currently defined `goal.value`.
228
+ #
229
+ # @param goal_value [Numeric, nil] Optional target value. Overrides the current goal if provided.
230
+ # @return [Evolvable::Population] The evolved population.
231
+ #
232
+ def evolve_to_goal ( goal_value = nil )
233
+ goal_value ||= goal . value
234
+ evolve ( count : Float ::INFINITY , goal_value : goal_value )
235
+ end
236
+
237
+ #
238
+ # Evolves the population indefinitely, ignoring any goal.
239
+ #
240
+ # Clears any previously set `goal.value` to ensure evolution continues indefinitely.
241
+ #
242
+ # @return [Evolvable::Population] The evolved population.
243
+ #
244
+ def evolve_forever
245
+ goal . value = nil
246
+ evolve ( count : Float ::INFINITY )
247
+ end
248
+
238
249
#
239
250
# Evolves the population using a pre-selected set of evolvables.
240
251
# This allows for custom selection beyond the built-in selection strategies.
0 commit comments