charlie 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ $:.unshift File.dirname(__FILE__)
4
4
 
5
5
  # This is just a dummy module to avoid making the VERSION constant a global.
6
6
  module Charlie
7
- VERSION = '0.7.0'
7
+ VERSION = '0.7.1'
8
8
  end
9
9
 
10
10
  require 'charlie/etc/monkey'
@@ -13,7 +13,7 @@ end
13
13
  # Usage: use SingleChild(UniformCrossover)
14
14
  def SingleChild(crossover_module)
15
15
  Module.new{
16
- include crossover_module
16
+ include crossover_module.dup
17
17
  def cross(parent1,parent2)
18
18
  super(parent1,parent2).at_rand
19
19
  end
@@ -40,8 +40,10 @@ def StringGenotype(n,elements)
40
40
  elements = elements.chars if elements.is_a? String # string to array of chars
41
41
  elements = elements.to_a
42
42
  Class.new(Genotype) {
43
- define_method(:size){ n }
44
- define_method(:elements){ elements }
43
+ [self,metaclass].each{|c| c.class_eval{ # include both in class and metaclass
44
+ define_method(:size){ n }
45
+ define_method(:elements){ elements }
46
+ }}
45
47
  def initialize
46
48
  @genes = Array.new(size){ elements.at_rand }
47
49
  end
@@ -48,6 +48,7 @@ module RouletteSelection
48
48
 
49
49
  new_pop = []
50
50
  while new_pop.size < population.size
51
+ i1 = i2 = nil
51
52
  i1,i2 = [0,0].map{
52
53
  r = rand * sum
53
54
  partial_sum.index partial_sum.find{|x| x > r }
@@ -81,6 +82,7 @@ def ScaledRouletteSelection(&block)
81
82
  population = population.sort_by(&:fitness)
82
83
  new_pop = []
83
84
  while new_pop.size < population.size
85
+ i1 = i2 = nil
84
86
  i1,i2 = @@index.at_rand, @@index.at_rand until i1!=i2 # no replacement
85
87
  new_pop += yield(population[i1],population[i2])
86
88
  end
@@ -99,7 +101,7 @@ ScaledRouletteSelection = ScaledRouletteSelection()
99
101
  # Elitism is saving the best +elite_n+ individuals each generation, to ensure the best solutions are never lost.
100
102
  def Elitism(sel_module,elite_n=1)
101
103
  Module.new{
102
- include sel_module
104
+ include sel_module.dup
103
105
  @@elite_n = elite_n
104
106
  def next_generation(population)
105
107
  population = population.sort_by(&:fitness)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: charlie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sander Land
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-01-11 00:00:00 +01:00
12
+ date: 2008-01-12 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency