list_mutate.rb

Path: lib/charlie/list/list_mutate.rb
Last Update: Wed Dec 19 17:38:34 +0100 2007

List mutators: generated by the ListMutator function

Methods

Constants

MutationStrategies = { # TODO: change to default parameters in 1.9 :probability => Proc.new{|genes,pointmut,p| p ||= 0.05   The mutation strategies for ListMutator
  • :single_point : Mutates a single element
  • :n_point[n=3] : Mutates a single element, n times. Example: :n_point, :n_point[2]
  • :probability[ p=0.05 ] : Mutates each element with probability p
  • :expected_n[n=3] : Mutates each element with probability n/genes.size, i.e. such that the expected # of mutations is n
PointMutators = { :replace => proc{|x,*pos| pos.at_rand }, :flip => proc{|x| 1-x }, :uniform => Proc.new{|x,max_size| max_size ||= 0.25;   The point mutators for ListMutator
  • :flip : flips bit (x->1-x), use in BitStringGenotype
  • :replace[ c1,c2,…,cn ] : replaces the element with one of the arguments. use in StringGenotype. Example: :replace[ *’a’..’z’ ]
  • :uniform[ max_size=0.25 ]: adds a random number in the range [-max_size,+max_size], uniformly distributed.
  • :gaussian[ sigma=0.2 ] : adds a random number, gaussian distributed with standard deviation sigma

Public Instance methods

Generates a module which can be used as a mutator for array-based genotypes like FloatListGenotype, BitStringGenotype and StringGenotype

  • strategy should be one of the MutationStrategies, or a proc
  • point_mutator should be one of the PointMutators, or a proc
  • nil is equivalent to proc{} for the point mutator

[Source]

    # File lib/charlie/list/list_mutate.rb, line 48
48: def ListMutator(strategy=:expected_n ,point_mutator=:uniform)
49:   strat, *strat_args = *strategy
50:   pm   , *pm_args    = *point_mutator
51: 
52:   pm ||= proc{}
53: 
54:   strat = MutationStrategies[strat.intern] unless strat.is_a? Proc
55:   pm    = PointMutators[pm.intern]         unless pm.is_a? Proc
56:   
57:   raise ArgumentError,"Invalid mutation strategy" if strat.nil?
58:   raise ArgumentError,"Invalid point mutator"     if point_mutator.nil?
59: 
60:   if pm_args.empty?
61:     point_mutator_with_args = pm    
62:   else
63:     point_mutator_with_args = proc{|*args| pm.call(*(args+pm_args) ) }
64:   end
65: 
66:   Module.new{
67:     define_method(:mutate!) {
68:       strat.call(@genes,point_mutator_with_args,*strat_args)
69:       self
70:     }
71:     self.name= "ListMutator(#{strategy.inspect},#{point_mutator.inspect})"
72:   }
73: end

[Validate]