Class: EvoSynth::BinaryGenome

Inherits:
Array
  • Object
show all
Defined in:
lib/evosynth/core/binary_genome.rb

Overview

Binary genome which keeps track of changes (changed attribute) to reduce the need to recalculate the fitness function (see Evaluator)

This genome is a simple bitstring and each gene is a boolean.

FIXME: implment in C for better performance - right now its pretty useless TODO: complete documentation

Instance Method Summary collapse

Constructor Details

#initialize(intial_value = 0) ⇒ BinaryGenome

Creates a BinaryGenome with a given initial (Integer) value. Default constructs a new BinaryGenome with the initial value of 0.



40
41
42
43
# File 'lib/evosynth/core/binary_genome.rb', line 40

def initialize(intial_value = 0)
	@data = intial_value
	@changed = true
end

Instance Method Details

#[](*args) ⇒ Object

Array like index accessor

index
index, length
range


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/evosynth/core/binary_genome.rb', line 71

def [](*args)
	if args.size == 1

		case args[0]
			when Numeric
				@data[args[0]]
			when Range
				get_sub_range args[0]
			else
				raise ArgumentError, "argument should be either index or range"
			end

	elsif args.size == 2
		get_sub_range Range.new(args[0], args[0] + args[1] - 1)
	else
		raise ArgumentError, "wrong number of arguments"
	end
end

#[]=(*args) ⇒ Object

Array like index accessor



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/evosynth/core/binary_genome.rb', line 92

def []=(*args)
	if args.size == 2

		case args[0]
			when Numeric
				set_gene(args[0], args[1])
			when Range
				case args[1]
					when Numeric
						args[0].each { |index| set_gene(index, args[1]) }
					when Array
						offset = args[0].begin
						args[0].each { |index| set_gene(index, args[1][index - offset]) }
					else
						raise ArgumentError, "argument (1) should be either index or range"
				end

			else
				raise ArgumentError, "argument (0) should be either index or range"
			end

	elsif args.size == 3
		args[1].times { |offset| set_gene(args[0] + offset, args[2]) }
	else
		raise ArgumentError, "wrong number of arguments"
	end
end

#changed=(value) ⇒ Object

Set the changed flag (boolean) of the genome



47
48
49
# File 'lib/evosynth/core/binary_genome.rb', line 47

def changed=(value)
	@changed = value
end

#changed?Boolean

True if the genome has changed, false otherwise. Has to be set to false manually.

Returns:

  • (Boolean)


53
54
55
# File 'lib/evosynth/core/binary_genome.rb', line 53

def changed?
	@changed
end

#cloneObject

Returns a clone of this genome



59
60
61
62
63
# File 'lib/evosynth/core/binary_genome.rb', line 59

def clone
	my_clone = BinaryGenome.new(@data)
	my_clone.changed = false
	my_clone
end

#flip!(index) ⇒ Object

Flips (inverts) the gene at the given index



122
123
124
# File 'lib/evosynth/core/binary_genome.rb', line 122

def flip!(index)
	@data = @data ^ (1 << index)
end

#sizeObject

Returns the size (in bits) of this genome.



128
129
130
131
# File 'lib/evosynth/core/binary_genome.rb', line 128

def size
	@size = @data.to_s(2).size unless defined? @size
	@size
end

#to_sObject

Return a printable version of the genome



135
136
137
# File 'lib/evosynth/core/binary_genome.rb', line 135

def to_s
	@data.to_s(2)
end