Class: SimpleGa::GeneticAlgorithm::Chromosome
- Inherits:
-
Object
- Object
- SimpleGa::GeneticAlgorithm::Chromosome
- Defined in:
- lib/simple_ga/genetic_algorithm.rb
Overview
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.
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#normalized_fitness ⇒ Object
Returns the value of attribute normalized_fitness.
Class Method Summary collapse
-
.mutate(chromosome) ⇒ Object
Mutation method is used to maintain genetic diversity from one generation of a population of chromosomes to the next.
-
.reproduce(a, b) ⇒ Object
Chromosome a and b might be the same.
-
.seed ⇒ Object
Initializes an individual solution (chromosome) for the initial population.
Instance Method Summary collapse
-
#fitness ⇒ Object
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.
-
#initialize(data) ⇒ Chromosome
constructor
Chromosome.new.
Constructor Details
#initialize(data) ⇒ Chromosome
Chromosome.new
169 170 171 |
# File 'lib/simple_ga/genetic_algorithm.rb', line 169 def initialize(data) # Chromosome.new @data = data end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
166 167 168 |
# File 'lib/simple_ga/genetic_algorithm.rb', line 166 def data @data end |
#normalized_fitness ⇒ Object
Returns the value of attribute normalized_fitness.
167 168 169 |
# File 'lib/simple_ga/genetic_algorithm.rb', line 167 def normalized_fitness @normalized_fitness end |
Class Method Details
.mutate(chromosome) ⇒ Object
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.
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/simple_ga/genetic_algorithm.rb', line 257 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 |
.reproduce(a, b) ⇒ Object
Chromosome a and b might be the same.
- NOTE
-
Why is chromosome a the same as chromosome b?
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/simple_ga/genetic_algorithm.rb', line 298 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 |
.seed ⇒ Object
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.
337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/simple_ga/genetic_algorithm.rb', line 337 def self.seed # Current state inputs to be retrieved from the database. ncourse = 11 seed = [] 1.step(ncourse*2, 2) do |j| seed << rand(2) seed << (1 + rand(6)) end return Chromosome.new(seed) end |
Instance Method Details
#fitness ⇒ Object
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.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/simple_ga/genetic_algorithm.rb', line 180 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 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 && cgpa >= target_cgpa @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 |