genetica 0.0.1.beta.1 → 0.0.1.beta.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,7 @@ module Genetica
5
5
  attr_reader :generation
6
6
 
7
7
  attr_accessor :alleles
8
+ attr_accessor :elitism
8
9
  attr_accessor :crossover_probability
9
10
  attr_accessor :mutation_probability
10
11
  attr_accessor :fitness_function
@@ -18,10 +19,18 @@ module Genetica
18
19
  @population.at @population_fitness.index(self.best_fitness)
19
20
  end
20
21
 
22
+ def best_chromosomes(quantity=1)
23
+ self.best_fitnesses(quantity).collect { |fitness| @population.at(@population_fitness.index fitness) }
24
+ end
25
+
21
26
  def best_fitness
22
27
  @population_fitness.max
23
28
  end
24
29
 
30
+ def best_fitnesses(quantity=1)
31
+ @population_fitness.sort.reverse.take(quantity)
32
+ end
33
+
25
34
  def average_fitness
26
35
  @population_fitness.inject(:+) / @population_fitness.size.to_f
27
36
  end
@@ -61,6 +70,9 @@ module Genetica
61
70
  # Generate a new chromosome population
62
71
  population = Array.new
63
72
 
73
+ # If elitism if greater than 0 then we save the same number of chromosomes to the next generation
74
+ population += self.best_chromosomes(quantity=@elitism) if @elitism > 0
75
+
64
76
  while population.size < @population.size
65
77
  # 1. Selection Step
66
78
  # Select a pair of parent chromosomes from the current population.
@@ -3,6 +3,7 @@ module Genetica
3
3
 
4
4
  # Population attributes
5
5
  attr_accessor :size
6
+ attr_accessor :elitism
6
7
  attr_accessor :crossover_probability
7
8
  attr_accessor :mutation_probability
8
9
  attr_accessor :fitness_function
@@ -13,6 +14,7 @@ module Genetica
13
14
  def initialize
14
15
  # Default Population values
15
16
  @size = 20
17
+ @elitism = 0
16
18
  @crossover_probability = 0.7
17
19
  @mutation_probability = 0.001
18
20
  @fitness_function = nil
@@ -33,6 +35,7 @@ module Genetica
33
35
  # Generating Population
34
36
  population = Population.new chromosome_population
35
37
  population.alleles = @chromosome_alleles
38
+ population.elitism = @elitism
36
39
  population.crossover_probability = @crossover_probability
37
40
  population.mutation_probability = @mutation_probability
38
41
  population.fitness_function = @fitness_function
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genetica
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta.1
4
+ version: 0.0.1.beta.2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-08 00:00:00.000000000 +02:00
12
+ date: 2011-11-23 00:00:00.000000000 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
- description: Genetica is a library to create and use Genetic Algorithms solutions
16
- using Ruby language.
15
+ description: Genetica is a library to create and use Genetics Algorithms with Ruby.
17
16
  email: josefranciscocalvo@gmail.com
18
17
  executables: []
19
18
  extensions: []