Class: SubPopulation

Inherits:
Object
  • Object
show all
Defined in:
../algorithm/age_hierarchy_tree.rb

Constant Summary

@@left_right =
0

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (SubPopulation) initialize(parent = nil)

A new instance of SubPopulation



16
17
18
19
20
21
# File '../algorithm/age_hierarchy_tree.rb', line 16

def initialize( parent=nil )
  @parent = parent
  @name = parent.nil? ? 'R' : "#{parent.name}#{@@left_right=1-@@left_right}"       
  restart
  @@algo.report << "#{@name}.initialize"
end

Instance Attribute Details

- (Object) children

Returns the value of attribute children



7
8
9
# File '../algorithm/age_hierarchy_tree.rb', line 7

def children
  @children
end

- (Object) current

Returns the value of attribute current



7
8
9
# File '../algorithm/age_hierarchy_tree.rb', line 7

def current
  @current
end

- (Object) level

Returns the value of attribute level



7
8
9
# File '../algorithm/age_hierarchy_tree.rb', line 7

def level
  @level
end

- (Object) name (readonly)

Returns the value of attribute name



8
9
10
# File '../algorithm/age_hierarchy_tree.rb', line 8

def name
  @name
end

- (Object) parent (readonly)

Returns the value of attribute parent



8
9
10
# File '../algorithm/age_hierarchy_tree.rb', line 8

def parent
  @parent
end

Class Method Details

+ (Object) set_algo(algo)



10
11
12
# File '../algorithm/age_hierarchy_tree.rb', line 10

def SubPopulation.set_algo algo
  @@algo = algo
end

Instance Method Details

- (Object) breed



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File '../algorithm/age_hierarchy_tree.rb', line 40

def breed
  parents = @current.clone
  parents.concat @parent.current unless @parent.nil?
  
  parent_inc = 0
  @@algo.breeding( parents ).each do |offspring|
    if offspring.layer == @level or @parent.nil?
      @children << offspring
    else
      @parent.children << offspring
      parent_inc += 1
    end
  end
  @@algo.report << "#{@name}.breed parents=#{parents.size} -> self.children=#{@children.size} +@parent.children=#{parent_inc}"      
end

- (Object) environmental



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File '../algorithm/age_hierarchy_tree.rb', line 56

def environmental

  uniq = Set.new
  elite = [] 

  # elitism   
  copy_size = @@algo.deme_size - @children.size # copy the best of @current if there is not enough children
  copy_size = @@algo.elite_size if copy_size < @@algo.elite_size # but at least elite_size of them
  @current.slice( 0 ... copy_size ).each do |individual| # assuming @current is sorted
    copy = individual.clone
    copy.parents individual   # increment age
    elite << copy
    uniq << copy.phenotype unless uniq.include? copy.phenotype 
  end

  # phenotype duplicate elimination
  offspring = []
  @children.each do |i| 
    next if uniq.include? i.phenotype 
    offspring << i
    uniq << i.phenotype
  end

  # concat, sort, truncate
  @current = @@algo.sort_spea2( elite + offspring ).slice( 0 ... @@algo.deme_size )

  @@algo.report << "#{@name}.environmental pde: #{@children.size} -> #{offspring.size} + copied: #{elite.size} => #{elite.size + offspring.size} truncated: #{@current.size}"        

  @children = []   

end

- (Object) restart



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File '../algorithm/age_hierarchy_tree.rb', line 23

def restart
  unless !defined?(@current) or @current.empty? or @parent.nil?
    @current.slice( 0 ... @@algo.elite_size ).each do |individual|
      copy = individual.clone
      copy.parents individual   # increment age
      @parent.children << copy
    end

    @@algo.report << "#{@name}.restart saved elite to #{@parent.name}.children"
  end

  @current = @@algo.sort_spea2( @@algo.init_deme )
  @children = []
  @level = 0
  @@algo.report << "#{@name}.restart @current.size=#{@current.size}"   
end