Class: FeldtRuby::Optimize::EliteArchive::GlobalTopList

Inherits:
Object
  • Object
show all
Defined in:
lib/feldtruby/optimize/elite_archive.rb

Overview

A top list is an array of a fixed size that saves the top candidates based on their quality values.

Direct Known Subclasses

GoalTopList

Instance Method Summary collapse

Constructor Details

#initialize(maxSize, objective) ⇒ GlobalTopList

Returns a new instance of GlobalTopList.



26
27
28
29
30
# File 'lib/feldtruby/optimize/elite_archive.rb', line 26

def initialize(maxSize, objective)
  @max_size =maxSize
  @top_list = Array.new
  @objective = objective
end

Instance Method Details

#[](index) ⇒ Object



33
34
35
# File 'lib/feldtruby/optimize/elite_archive.rb', line 33

def [](index)
  @top_list[index]
end

#add(candidate) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/feldtruby/optimize/elite_archive.rb', line 37

def add(candidate)
  last = @top_list.last
  #puts "In #{self},\nlast = #{last}, candidate = #{candidate}, top_list = #{@top_list}"
  if @top_list.length < @max_size || last.nil? || is_better_than?(candidate, last)
    @top_list.pop if @top_list.length >= @max_size
    @top_list << candidate
    @top_list = sort_top_list
  end
  #puts "top_list = #{@top_list}"
end

#inspectObject



56
57
58
# File 'lib/feldtruby/optimize/elite_archive.rb', line 56

def inspect
  self.class.inspect + @top_list.inspect
end

#is_better_than?(candidate1, candidate2) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/feldtruby/optimize/elite_archive.rb', line 48

def is_better_than?(candidate1, candidate2)
  @objective.is_better_than?(candidate1, candidate2)
end

#lengthObject



31
# File 'lib/feldtruby/optimize/elite_archive.rb', line 31

def length; @top_list.length; end

#sort_top_listObject



52
53
54
# File 'lib/feldtruby/optimize/elite_archive.rb', line 52

def sort_top_list
  @top_list.sort_by {|c| @objective.quality_of(c).value}
end