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 +4 -4
- data/lib/simple_ga/genetic_algorithm.rb +53 -28
- data/lib/simple_ga/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f114fda4c0f03b0625aea25a72382111bd082d93
|
4
|
+
data.tar.gz: b5ee1a4ab195ab8f1a09cef09e028926afd696a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
38
|
+
selected_to_breed = selection #Evaluates current population
|
39
|
+
|
40
40
|
selected_to_breed.each do |chromosome|
|
41
|
-
|
41
|
+
elite_chromosomes << chromosome
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
|
+
offsprings = reproduction selected_to_breed #Generate the population for this new generation
|
44
45
|
replace_worst_ranked offsprings
|
45
46
|
end
|
46
|
-
|
47
|
-
|
48
|
-
return best_chromosome
|
47
|
+
unique_chromosomes = uniquify elite_chromosomes
|
48
|
+
return unique_chromosomes
|
49
49
|
end
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
-
|
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
|
data/lib/simple_ga/version.rb
CHANGED