matrix.rb

Path: lib/charlie/list/matrix.rb
Last Update: Fri Feb 08 23:53:03 +0100 2008

Generic ancestor for matrix genotypes

Methods

Constants

MatrixMutationStrategies = { :probability => MMSPRB = Proc.new{|genes,pointmut,p| p ||= 0.05   The mutation strategies for MatrixMutator
  • :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
  • :expected_n_per_row[n=3], :expected_n_per_column[n=3] : Likewise

Public Class methods

[Source]

    # File lib/charlie/list/matrix.rb, line 23
23:   def initialize
24:     self.genes = Array.new(rows){ Array.new(columns){  rand * (@@range.end - @@range.begin) + @@range.begin } }
25:   end

[Source]

    # File lib/charlie/list/matrix.rb, line 36
36:   def initialize
37:     self.genes = Array.new(rows){ Array.new(columns){  rand(2) } }
38:   end

Public Instance methods

Genotype for a 2D array of bits, for example: connection matrices for graphs

[Source]

    # File lib/charlie/list/matrix.rb, line 34
34: def BitMatrixGenotype(rows,columns=rows)
35:  Class.new(MatrixGenotype(rows,columns)) {
36:   def initialize
37:     self.genes = Array.new(rows){ Array.new(columns){  rand(2) } }
38:   end
39:   def to_s
40:     @genes.map{|r| r.map(&:to_s).join }.join("\n")
41:   end
42:   use MatrixMutator(:expected_n,:flip)
43:  }
44: end

Genotype for a 2D array of floats.

[Source]

    # File lib/charlie/list/matrix.rb, line 20
20: def FloatMatrixGenotype(rows,columns=rows,range=0..1)
21:  Class.new(MatrixGenotype(rows,columns)) {
22:   @@range = range
23:   def initialize
24:     self.genes = Array.new(rows){ Array.new(columns){  rand * (@@range.end - @@range.begin) + @@range.begin } }
25:   end
26:   def to_s
27:     @genes.inspect
28:   end
29:   use MatrixMutator()
30:  }
31: end

Generic ancestor for matrix genotypes

[Source]

    # File lib/charlie/list/matrix.rb, line 5
 5: def MatrixGenotype(rows,columns=rows)
 6:  Class.new(Genotype) {
 7:   [self,metaclass].each{|c| c.class_eval{
 8:    define_method(:rows)    { rows }
 9:    define_method(:columns) { columns }
10:    define_method(:size)    { rows * columns }
11:   }}
12:   def genes=(g)
13:     @genes = g.map(&:dup)
14:   end
15:   use MatrixUniformCrossover.dup
16:  }
17: end

Generates a module which can be used as a mutator for matrix-based genotypes like FloatMatrixGenotype and BitMatrixGenotype

  • strategy should be one of the MatrixMutationStrategies, or a proc
  • point_mutator should be one of the PointMutators (like in ListMutator), or a proc
  • nil is equivalent to proc{} for the point mutator

[Source]

     # File lib/charlie/list/matrix.rb, line 96
 96: def MatrixMutator(strategy=:expected_n ,point_mutator=:uniform)
 97:   strat, *strat_args = *strategy
 98:   pm   , *pm_args    = *point_mutator
 99: 
100:   pm ||= proc{}
101: 
102:   strat = MatrixMutationStrategies[strat.intern] unless strat.is_a? Proc
103:   pm    = PointMutators[pm.intern]         unless pm.is_a? Proc
104:   
105:   raise ArgumentError,"Invalid mutation strategy" if strat.nil?
106:   raise ArgumentError,"Invalid point mutator"     if point_mutator.nil?
107: 
108:   if pm_args.empty?
109:     point_mutator_with_args = pm    
110:   else
111:     point_mutator_with_args = proc{|*args| pm.call(*(args+pm_args) ) }
112:   end
113: 
114:   Module.new{
115:     define_method(:mutate!) {
116:       strat.call(@genes,point_mutator_with_args,*strat_args)
117:       self
118:     }
119:     self.name= "MatrixMutator(#{strategy.inspect},#{point_mutator.inspect})"
120:   }
121: end

[Source]

    # File lib/charlie/list/matrix.rb, line 12
12:   def genes=(g)
13:     @genes = g.map(&:dup)
14:   end

[Source]

    # File lib/charlie/list/matrix.rb, line 39
39:   def to_s
40:     @genes.map{|r| r.map(&:to_s).join }.join("\n")
41:   end

[Source]

    # File lib/charlie/list/matrix.rb, line 26
26:   def to_s
27:     @genes.inspect
28:   end

[Validate]