Class: NEAT::Evaluator

Inherits:
Operator show all
Defined in:
lib/rubyneat/evaluator.rb

Overview

Evaluator evaluates phenotype of critter for fitness, novelty, etc.

We can have a chain of these evaluators whose outputs are summed, etc.

Instance Attribute Summary

Attributes inherited from NeatOb

#controller, #name

Instance Method Summary collapse

Methods inherited from NeatOb

attr_neat, #initialize, log, #log, #to_s

Constructor Details

This class inherits a constructor from NEAT::NeatOb

Instance Method Details

#analyze_for_fitness!(critter) ⇒ Object

Analyze the evaluation and compute a fitness for the given critter. Note that if cost_func is set, we call that to integrate the cost to the fitness average fitness calculated for the fitness vector.



44
45
46
47
48
49
50
51
52
53
# File 'lib/rubyneat/evaluator.rb', line 44

def analyze_for_fitness!(critter)
  fitvec = @crit_hist[critter].map{|seq, vio| @controller.fitness_func_hook(vio[0], vio[1], seq) }
  # Average the fitness vector to get a scalar fitness.
  critter.fitness = unless @controller.cost_func_none?
                      @controller.cost_func_hook(fitvec, critter.genotype.fitness_cost)
                    else
                      fitvec.reduce {|a,r| a+r} / fitvec.size.to_f + critter.genotype.fitness_cost
                    end
  log.debug "Fitness Vector: #{fitvec}, fitness of #{critter.fitness} assigned to #{critter}"
end

#evaluate!(critter) ⇒ Object

Evaluate one step of a sequence of evaluations. For time series and realtime ongoing evaluations, everything is.

Returns [vin, vout], where vin is the input vector, and vout in the output vector from the critter. FIXME: this should not really have to deal with an error. FIXME: the error should be handled upstream from here.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubyneat/evaluator.rb', line 24

def evaluate!(critter)
  vin = @controller.query_func_hook(@controller.seq_num)
  @crit_hist[critter] = {} unless @crit_hist.member? critter
  begin
    vout = unless @controller.recurrence_func_none?
             critter.phenotype.stimulate *vin, &@controller.recurrence_func_hook_itself
           else
             critter.phenotype.stimulate *vin
           end
    log.debug "Critter #{critter.name}: vin=#{vin}. vout=#{vout}"
    @crit_hist[critter][@controller.seq_num] = [vin, vout]
  rescue Exception => e
    log.error "Exception #{e} on code:\n#{critter.phenotype.code}"
    @crit_hist[critter][@controller.seq_num] = [vin, :error]
  end
end

#ready_for_evaluation(pop) ⇒ Object

This is call prior to any sequence evaluation. Here, we clean up persistent tracking information, etc.



10
11
12
13
# File 'lib/rubyneat/evaluator.rb', line 10

def ready_for_evaluation(pop)
  @crit_hist = {}
  pop.initialize_for_recurrence!
end