Class: Algorithms::GeneticAlgorithm::BitsetSolution
- Inherits:
-
Object
- Object
- Algorithms::GeneticAlgorithm::BitsetSolution
show all
- Includes:
- Solution
- Defined in:
- lib/algorithms/genetic_algorithm/solutions/bitset.rb
Instance Attribute Summary
Attributes included from Solution
#data
Instance Method Summary
collapse
Methods included from Solution
#create_new_solution, #valid?
Constructor Details
#initialize(bitset, mutate_change_rate: 0.5) ⇒ BitsetSolution
Returns a new instance of BitsetSolution.
4
5
6
|
# File 'lib/algorithms/genetic_algorithm/solutions/bitset.rb', line 4
def initialize(bitset, mutate_change_rate: 0.5)
@data, @mutate_change_rate = bitset, mutate_change_rate
end
|
Instance Method Details
#crossover(other_solution) ⇒ Object
20
21
22
23
24
25
26
27
|
# File 'lib/algorithms/genetic_algorithm/solutions/bitset.rb', line 20
def crossover(other_solution)
new_data = if rand < 0.5
@data ^ other_solution.data
else
@data - other_solution.data
end
create_new_solution(new_data)
end
|
#mutate ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/algorithms/genetic_algorithm/solutions/bitset.rb', line 8
def mutate
new_data = @data.dup
indices_to_change = []
(0...@data.size).each do |idx|
if rand < @mutate_change_rate
indices_to_change << idx
end
end
new_data.flip(indices_to_change)
create_new_solution(new_data)
end
|