Class: EvoSynth::Output::Logger

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/evosynth/output/logger.rb

Overview

Customizable logger

logger = EvoSynth::Output::Logger.new(10, true, “gen” => ->{ evolver.generations_computed }, “best” => ->{ profile.population.best.fitness }, “worst” => ->{ profile.population.worst.fitness } ) evolver.add_observer(logger)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_step, save_data, things_to_log = {}) ⇒ Logger

Returns a new instance of Logger.



43
44
45
46
47
48
49
50
# File 'lib/evosynth/output/logger.rb', line 43

def initialize(log_step, save_data, things_to_log = {})
  @data = {}

  @log_step = log_step
  @save_data = save_data
  @things_to_log = things_to_log
  @show_errors = false
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



41
42
43
# File 'lib/evosynth/output/logger.rb', line 41

def data
  @data
end

#save_dataObject

Returns the value of attribute save_data.



40
41
42
# File 'lib/evosynth/output/logger.rb', line 40

def save_data
  @save_data
end

#show_errorsObject

Returns the value of attribute show_errors.



40
41
42
# File 'lib/evosynth/output/logger.rb', line 40

def show_errors
  @show_errors
end

#things_to_logObject

Returns the value of attribute things_to_log.



40
41
42
# File 'lib/evosynth/output/logger.rb', line 40

def things_to_log
  @things_to_log
end

Instance Method Details

#clear_dataObject



52
53
54
# File 'lib/evosynth/output/logger.rb', line 52

def clear_data
  @data = {}
end

#column_namesObject



56
57
58
59
60
# File 'lib/evosynth/output/logger.rb', line 56

def column_names
  line = []
  @things_to_log.each_pair { |key, value|  line << key }
  line
end

#update(observable, counter) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/evosynth/output/logger.rb', line 62

def update(observable, counter)
  return unless counter % @log_step == 0

  line = []
  @things_to_log.each_pair do |key, value|
    begin
      line << value.call
    rescue
      @show_errors ? line << "ERROR while retrieving #{value.inspect}" : line << "\t"
    end
  end

  @data[counter] = line if save_data

  changed
  notify_observers self, counter, line
end