Class: FeldtRuby::Optimize::Archive::GlobalTopList

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

Overview

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

Direct Known Subclasses

GoalTopList, WeirdoTopList

Instance Method Summary collapse

Constructor Details

#initialize(maxSize, objective) ⇒ GlobalTopList

Returns a new instance of GlobalTopList.



150
151
152
153
154
# File 'lib/feldtruby/optimize/archive.rb', line 150

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

Instance Method Details

#[](index) ⇒ Object



161
162
163
# File 'lib/feldtruby/optimize/archive.rb', line 161

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

#add(candidate) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/feldtruby/optimize/archive.rb', line 165

def add(candidate)
  last = @top_list.last
  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
end

#data_to_json_hashObject



197
198
199
200
201
202
203
204
# File 'lib/feldtruby/optimize/archive.rb', line 197

def data_to_json_hash
  {
    'max_size' => @max_size,
    'top_list' => @top_list,
    'quality_values' => @top_list.map {|c| @objective.quality_of(c)},
    'objective' => @objective
  }
end

#delete_candidates(candidates) ⇒ Object



178
179
180
# File 'lib/feldtruby/optimize/archive.rb', line 178

def delete_candidates(candidates)
  @top_list = @top_list - candidates
end

#eachObject



157
158
159
# File 'lib/feldtruby/optimize/archive.rb', line 157

def each
  @top_list.each {|e| yield(e)}
end

#inspectObject



186
187
188
# File 'lib/feldtruby/optimize/archive.rb', line 186

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

#is_better_than?(candidate1, candidate2) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/feldtruby/optimize/archive.rb', line 174

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

#lengthObject



155
# File 'lib/feldtruby/optimize/archive.rb', line 155

def length; @top_list.length; end

#sort_top_listObject



182
183
184
# File 'lib/feldtruby/optimize/archive.rb', line 182

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

#to_json(*a) ⇒ Object



190
191
192
193
194
195
# File 'lib/feldtruby/optimize/archive.rb', line 190

def to_json(*a)
  {
    'json_class'   => self.class.name,
    'data'         => data_to_json_hash,
  }.to_json(*a)
end