class SimpleGa::GeneticAlgorithm::Chromosome
A Chromosome
is a representation of an individual solution for a specific problem. You will have to redifine the Chromosome
representation for each particular problem, along with its fitness, mutate, reproduce, and seed methods.
Attributes
Public Class Methods
Mutation method is used to maintain genetic diversity from one generation of a population of chromosomes to the next. It is analogous to biological mutation.
The purpose of mutation in GAs is to allow the algorithm to avoid local minima by preventing the population of chromosomes from becoming too similar to each other, thus slowing or even stopping evolution.
Callling the mutate function will “probably” slightly change a chromosome randomly.
# File lib/simple_ga/genetic_algorithm.rb, line 281 def self.mutate(chromosome) if chromosome.normalized_fitness && rand < ((1 - chromosome.normalized_fitness) * 0.4) courses, grades = [], [] data = chromosome.data # Split the chromosome into 2. 0.step(data.length-1, 2) do |j| courses << data[j] grades << data[j+1] end p1 = (rand * courses.length-1).ceil courses[p1] = rand(2) p2 = (rand * grades.length-1).ceil grades[p2] = (1 + rand(6)) # Recombine the chromosome. # [0,1,2,3,4,5,6,7,8,9,10] 0.upto(courses.length-1) do |j| even_index = j * 2 odd_index = even_index + 1 data[even_index] = courses[j] data[odd_index] = grades[j] end chromosome.data = data @fitness = nil end end
# File lib/simple_ga/genetic_algorithm.rb, line 176 def initialize(data) # Chromosome.new @data = data end
Chromosome
a and b might be the same.
- NOTE
-
Why is chromosome a the same as chromosome b?
# File lib/simple_ga/genetic_algorithm.rb, line 322 def self.reproduce(a, b) # We know that (a.data.length == b.data.length) must be always true. chromosomeLength = a.data.length aCourses, bCourses, aGrades, bGrades = [], [], [], [] # Split the chromosome into 2. 0.step(chromosomeLength-1, 2) do |index| next_index = index + 1 aCourses << a.data[index] aGrades << a.data[next_index] bCourses << b.data[index] bGrades << b.data[next_index] end # One-point crossover # We know that (aCourses.length == aGrades.length) must be always true. # However, we want to cut and cross at different points for the # corresponding course and grade array. # Avoid slice(0,0) or slice (11,11). It brings no changes. course_Xpoint = rand(aCourses.length-1) + 1 grade_Xpoint = rand(aGrades.length-1) + 1 new_Courses = aCourses.slice(0, course_Xpoint) + bCourses.slice(course_Xpoint, aCourses.length) new_Grades = aGrades.slice(0, grade_Xpoint) + bGrades.slice(grade_Xpoint, aGrades.length) spawn = [] # We know that (new_Courses.length == new_Grades.length) must be always true. 0.upto(new_Courses.length-1) do |i| spawn << new_Courses[i] spawn << new_Grades[i] end return Chromosome.new(spawn) end
Initializes an individual solution (chromosome) for the initial population. Usually the chromosome is generated randomly, but you can use some problem domain knowledge, to generate a (probably) better initial solution.
# File lib/simple_ga/genetic_algorithm.rb, line 361 def self.seed # Current state inputs to be retrieved from the database. # ncourse = 11 seed = [] max_credits = 20 if @@total_credits > max_credits 1.step(@@ncourse*2, 2) do |j| seed << rand(2) seed << (1 + rand(6)) end else 1.step(@@ncourse*2, 2) do |j| seed << 1 seed << (1 + rand(6)) end end return Chromosome.new(seed) end
available_courses is an array of arrays containing a list of available courses with the course information e.g. [[<course_name>, <course_ch], [<course_name>, <course_ch]]
# File lib/simple_ga/genetic_algorithm.rb, line 385 def self.set_params(available_courses, current_gpa, acum_ch) @@credits = [] @@old_gpa = current_gpa @@old_total_credits = acum_ch @@old_cgpa = @@old_gpa/@@old_total_credits @@ncourse = available_courses.length available_courses.each do |course_info| @@credits << course_info[1] end @@total_credits = @@credits.inject(0, :+) end
Public Instance Methods
The fitness method quantifies the optimality of a solution (that is, a chromosome) in a genetic algorithm so that that particular chromosome may be ranked against all the other chromosomes.
Optimal chromosomes, or at least chromosomes which are more optimal, are allowed to breed and mix their datasets by any of several techniques, producing a new generation that will (hopefully) be even better.
# File lib/simple_ga/genetic_algorithm.rb, line 197 def fitness return @fitness if @fitness # Current state inputs to be retrieved from the database. # @@credits = [2,3,3,3,5,2,4,4,4,4,3] # @@old_gpa = 58 # @@old_total_credits = 16 min_credits = 16 max_credits = 20 target_cgpa ||= 3.7 courses, grades, points = [], [], [] # Split the chromosome into 2. 0.step(@data.length-1, 2) do |j| courses << @data[j] grades << @data[j+1] end total_credits = ([courses, @@credits].transpose.map {|x| x.inject(:*)}).inject{|sum,x| sum + x } new_total_credits = @@old_total_credits + total_credits grades.each do |grade| case grade when 1 points << 4.00 when 2 points << 3.70 when 3 points << 3.30 when 4 points << 3.00 when 5 points << 2.70 when 6 points << 2.30 when 7 points << 2.00 else points << 0.00 end end gpa = (([@@credits, courses, points].transpose.map {|x| x.inject(:*)}).inject{|sum,x| sum + x }).round(2) new_gpa = @@old_gpa + gpa cgpa = (new_gpa/new_total_credits).round(2) # Core constraints. # Elites own a high fitness value. # The higher the fitness value, the higher the chance of being selected. if total_credits <= max_credits && total_credits >= min_credits @fitness = cgpa # This chromosome doesn't satisfy the important constraints # e.g. within the range of min_credits and max_credits # should be discarded. # Perhaps, we should add cases that statisfy one of the constraints # 1. credit hours + cgpa # 2. credit hours else # Negative cgpa value will allow the invalid chromosome to be considered. # However, surprisingly the solutions suggested are more consistent and # diverse. @fitness = 0 end return @fitness end
The evolution value
# File lib/simple_ga/genetic_algorithm.rb, line 265 def improved_fitness return @fitness - @@old_cgpa end
# File lib/simple_ga/genetic_algorithm.rb, line 180 def num_of_selected_genes count = 0 data.each_with_index do |gene, index| if index % 2 == 0 && gene == 1 count += 1 end end return count end