simple_ga 0.2.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc3011bc562f5b4490d7ce0871474d96e56c3483
4
- data.tar.gz: 04cd59e64dee4bc02be52e766215d1b9251e5387
3
+ metadata.gz: f114fda4c0f03b0625aea25a72382111bd082d93
4
+ data.tar.gz: b5ee1a4ab195ab8f1a09cef09e028926afd696a6
5
5
  SHA512:
6
- metadata.gz: fb77a8312542c68c1d4347a503ea49cd7c1a2125f8602d6b87c7f6a8dfc86d50fb3059cbebea27d0a30de5a6200eaac97b722d98391436b1d049c57126f2d63d
7
- data.tar.gz: 732180260c88457c4b84e759d7fa12acdc421e76a4fb7eacc2efbd4baf8fcd087b6c967989cb8a59b70e48355857e556bfae3c7bbb5d3319e56fe65019aa89ce
6
+ metadata.gz: c11fec1cb28085ce662c2010227a935baec3aea4d5b5604488a68349ba1a7df064853b05b8a6ba9b312698917f31f5f54da4eca6e53c5b23f7d6aca7e6403e47
7
+ data.tar.gz: 367bab79430ae218bfbe0c71d51f56ecaf5ef118e921f300aca06b2798acc753a7939789ea0fd34a55aedeb5484aa6c9e84b68becd532d8b5d52f7afb779f66f
@@ -24,7 +24,6 @@ module SimpleGa
24
24
  class GeneticSearch
25
25
 
26
26
  attr_accessor :population
27
- attr_accessor :unique_solutions
28
27
 
29
28
  def initialize(initial_population_size, generations)
30
29
  @population_size = initial_population_size
@@ -33,33 +32,40 @@ module SimpleGa
33
32
  end
34
33
 
35
34
  def run
36
- generate_initial_population
37
- search_space = [] # All possible solutions to the problem.
35
+ generate_initial_population #Generate initial population
36
+ elite_chromosomes = [] #All possible solutions to the problem
38
37
  @max_generation.times do
39
- selected_to_breed = selection # Evaluates current population.
38
+ selected_to_breed = selection #Evaluates current population
39
+
40
40
  selected_to_breed.each do |chromosome|
41
- search_space << chromosome.data.dup
41
+ elite_chromosomes << chromosome
42
42
  end
43
- offsprings = reproduction selected_to_breed
43
+
44
+ offsprings = reproduction selected_to_breed #Generate the population for this new generation
44
45
  replace_worst_ranked offsprings
45
46
  end
46
- @unique_solutions = uniquify search_space
47
-
48
- return best_chromosome
47
+ unique_chromosomes = uniquify elite_chromosomes
48
+ return unique_chromosomes
49
49
  end
50
50
 
51
- def uniquify(search_space)
52
- unique_search_space = search_space.uniq
53
- # Turns every unselected courses data into nil
54
- unique_search_space.each do |s|
55
- 0.step(s.length-1, 2) do |index| # Odd index
56
- if s[index] == 0
57
- s[index] = nil
58
- s[index+1] = nil
59
- end
51
+ # elite_chromosomes is an array of chromosomes.
52
+ def uniquify(elite_chromosomes)
53
+ search_space = []
54
+ unique_solutions = []
55
+ elite_chromosomes.each do |chromosome|
56
+ search_space << chromosome.data
57
+ end
58
+ # Turns every unselected courses data into 0
59
+ search_space.each do |solution|
60
+ 0.step(solution.length-1, 2) do |index| #Odd index
61
+ solution[index+1] = 0 if solution[index] == 0
60
62
  end
61
63
  end
62
- unique_solutions = unique_search_space.uniq
64
+ unique_search_space = search_space.uniq
65
+
66
+ unique_search_space.each do |solution|
67
+ unique_solutions = Chromosome.new(solution)
68
+ end
63
69
 
64
70
  return unique_solutions
65
71
  end
@@ -191,9 +197,9 @@ module SimpleGa
191
197
  return @fitness if @fitness
192
198
 
193
199
  # Current state inputs to be retrieved from the database.
194
- credits = [2,3,3,3,5,2,4,4,4,4,3]
195
- old_gpa = 58
196
- old_total_credits = 16
200
+ @@credits = [2,3,3,3,5,2,4,4,4,4,3]
201
+ @@old_gpa = 58
202
+ @@old_total_credits = 16
197
203
  min_credits = 16
198
204
  max_credits = 20
199
205
  target_cgpa ||= 3.7
@@ -205,8 +211,8 @@ module SimpleGa
205
211
  grades << @data[j+1]
206
212
  end
207
213
 
208
- total_credits = ([courses, credits].transpose.map {|x| x.inject(:*)}).inject{|sum,x| sum + x }
209
- new_total_credits = old_total_credits + total_credits
214
+ total_credits = ([courses, @@credits].transpose.map {|x| x.inject(:*)}).inject{|sum,x| sum + x }
215
+ new_total_credits = @@old_total_credits + total_credits
210
216
 
211
217
  grades.each do |grade|
212
218
  case grade
@@ -227,8 +233,8 @@ module SimpleGa
227
233
  end
228
234
  end
229
235
 
230
- gpa = (([credits, courses, points].transpose.map {|x| x.inject(:*)}).inject{|sum,x| sum + x }).round(2)
231
- new_gpa = old_gpa + gpa
236
+ gpa = (([@@credits, courses, points].transpose.map {|x| x.inject(:*)}).inject{|sum,x| sum + x }).round(2)
237
+ new_gpa = @@old_gpa + gpa
232
238
  cgpa = (new_gpa/new_total_credits).round(2)
233
239
 
234
240
  # Core constraints.
@@ -251,6 +257,11 @@ module SimpleGa
251
257
  end
252
258
  return @fitness
253
259
  end
260
+
261
+ # The evolution value
262
+ def improved_fitness
263
+ return @fitness - @@old_cgpa
264
+ end
254
265
 
255
266
  # Mutation method is used to maintain genetic diversity from one
256
267
  # generation of a population of chromosomes to the next. It is analogous
@@ -346,15 +357,29 @@ module SimpleGa
346
357
 
347
358
  def self.seed
348
359
  # Current state inputs to be retrieved from the database.
349
- ncourse = 11
360
+ # ncourse = 11
350
361
  seed = []
351
362
 
352
- 1.step(ncourse*2, 2) do |j|
363
+ 1.step(@@ncourse*2, 2) do |j|
353
364
  seed << rand(2)
354
365
  seed << (1 + rand(6))
355
366
  end
356
367
  return Chromosome.new(seed)
357
368
  end
369
+
370
+ # available_courses is an array of arrays containing a list of available courses
371
+ # with the course information
372
+ # e.g. [[<course_name>, <course_ch], [<course_name>, <course_ch]]
373
+ def self.set_params(available_courses, current_gpa, acum_ch)
374
+ @@credits = []
375
+ @@old_gpa = current_gpa
376
+ @@old_total_credits = acum_ch
377
+ @@old_cgpa = @@old_gpa/@@old_total_credits
378
+ @@ncourse = available_courses.length
379
+ available_courses.each do |course_info|
380
+ @@credits << course_info[1]
381
+ end
382
+ end
358
383
  end # end/Chromosome
359
384
  end # end/GeneticAlgorithm
360
385
  end # end/SimpleGa
@@ -1,3 +1,3 @@
1
1
  module SimpleGa
2
- VERSION = "0.2.0"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_ga
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johnson Yeap