class NEAT::Evaluator

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

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

Public Instance Methods

analyze_for_fitness!(critter) click to toggle source

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.

# File lib/rubyneat/evaluator.rb, line 40
def analyze_for_fitness!(critter)
  fitvec = @crit_hist[critter].map{|seq, vio| @controller.fitness_func.(vio[0], vio[1], seq) }
  # Average the fitness vector to get a scalar fitness.
  critter.fitness = unless @controller.cost_func.nil?
                      @controller.cost_func.(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) click to toggle source

Evaluate one step of a sequence of evaluations. For time series and realtime ongoing evaluations, @controller.seq_num governs where in the sequence 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.

# File lib/rubyneat/evaluator.rb, line 24
def evaluate!(critter)
  vin = @controller.query_func.(@controller.seq_num)
  @crit_hist[critter] = {} unless @crit_hist.member? critter
  begin
    vout = critter.phenotype.stimulate *vin, &@controller.recurrence_func
    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) click to toggle source

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

# File lib/rubyneat/evaluator.rb, line 10
def ready_for_evaluation(pop)
  @crit_hist = {}
  pop.initialize_for_recurrence!
end