Class Utility
In: app/models/utility.rb
Parent: Object

Methods

Public Class methods

perform a chance mutation on integer

[Source]

    # File app/models/utility.rb, line 16
16:   def self.chance_mutation(integer, mutation_rate, mutation_strength)
17:     if rand(100) < mutation_rate
18:       Utility.mutate_integer(integer, mutation_strength)
19:     else
20:       integer
21:     end
22:   end

spawn a pool of n Creatures with x points each

[Source]

    # File app/models/utility.rb, line 25
25:   def self.create_life(simulation)
26:         population_size = simulation.population_size
27:         organism_complexity = simulation.organism_complexity
28:         
29:     for n in (0..population_size-1)
30:       creature_hash = Coder.blank_hash
31:       creature = Creature.create
32: 
33:       # start point for path
34:       creature_hash[:start][:x] = rand(301)
35:       creature_hash[:start][:y] = rand(301)
36: 
37:       # points on path
38:       for x in (0..organism_complexity-1)
39:         point = Hash.new
40:         point[:x] = rand(301)
41:         point[:y] = rand(301)
42:         point[:x1] = rand(301)
43:         point[:y1] = rand(301)
44:         creature_hash[:points] << point
45:       end
46:       
47:       creature.simulation = simulation
48:       creature.genome = Coder.encode(creature_hash)
49:       creature.save
50:       creature.render
51:       creature.determine_fitness
52:     end
53:   end

mutate an integer, within the bounds of the canvas and mutation strength

[Source]

    # File app/models/utility.rb, line 4
 4:   def self.mutate_integer(integer, mutation_strength)
 5:     # perform mutation
 6:     integer = integer + rand(mutation_strength * 2) - mutation_strength
 7:     
 8:     # perform correction if we mutated outside the bounds of the canvas
 9:     integer = 0 if integer < 0
10:     integer = 300 if integer > 300
11:     
12:     integer.to_i
13:   end

[Validate]