evopop 0.0.1 → 0.0.2

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: 0a7d88db7ef7db0308c1a1f78622124c40f3f556
4
- data.tar.gz: 2367d81655b2112e858828bfd4e1d64fd4f4e44e
3
+ metadata.gz: 43f55266a4ef8f115db03cc3dd2f075ce4189110
4
+ data.tar.gz: e66bcb2525067d93ae87c8c3355c5a1f32030d05
5
5
  SHA512:
6
- metadata.gz: 0ff6289c46a13239cfc61267e42ee5c63980fc8d4aaef9073de188da6d6d7a92c4769bfff54db3315bcf8fa5ad0266f355385cca17cd594e662eb3863dbdf904
7
- data.tar.gz: 9c461beb7fc05d35f824da3be0c487abe086c91357668ce08cbb47134491c558ec1e6a95ea6c194fb4f6eee9dd1f6dfe70822541e12da219ce13fcd46c114d6e
6
+ metadata.gz: 5e9fda825a2389dde2ef427cbd2d8d283e8d03ca42227d8266a99244d01799d782a2ca9cd51615cbcf4687e01796c891125951e8654c549c40b5485c7be5d391
7
+ data.tar.gz: 18c4d946acf6c631db4784d10177ec01762f8a16524bbe794b1cdf97fb62002680c251fd939fa9c3a072182729555fb163055527c11ffd4e8a10d498aaf15880
@@ -4,6 +4,7 @@
4
4
  class Candidate
5
5
  attr_accessor :dna, :fitness
6
6
 
7
+ # Simple initialization of candidate object.
7
8
  def initialize
8
9
  @dna = []
9
10
  end
@@ -1,5 +1,5 @@
1
1
 
2
- # Public: Represents the population that is being trained. Has various methods
2
+ # Represents the population that is being trained. Has various methods
3
3
  # relevant to training.
4
4
  #
5
5
  #
@@ -32,7 +32,7 @@ class Population
32
32
  self.create
33
33
  end
34
34
 
35
- # Public: Creates a new population class. Should be called after all the
35
+ # Creates a new population class. Should be called after all the
36
36
  # parameters have been set to the attributes.
37
37
  def create
38
38
  @candidates = Array.new(@population_size) {|c|
@@ -44,7 +44,7 @@ class Population
44
44
  }
45
45
  end
46
46
 
47
- # Public: Determines the fitness of the population and thereafter sorts it
47
+ # Determines the fitness of the population and thereafter sorts it
48
48
  # based on fitness descdending (high fitness first, low fitness last).
49
49
  def train
50
50
  average_fitness = 0
@@ -60,7 +60,7 @@ class Population
60
60
  @candidates = @candidates.reverse
61
61
  end
62
62
 
63
- # Public: Performs simple mechanism of crossover - in this case picks two
63
+ # Performs simple mechanism of crossover - in this case picks two
64
64
  # random candidates in from a top percentile of the population and averages
65
65
  # out their DNA per dimension, producing new offspring equal to the
66
66
  # population size attribute.
@@ -80,7 +80,7 @@ class Population
80
80
  @candidates = new_generation
81
81
  end
82
82
 
83
- # Public: Performs simple mutation over the next generation. In this case,
83
+ # Performs simple mutation over the next generation. In this case,
84
84
  # it either adds or substracts an amount to each dimension given the
85
85
  # mutation range attributes.
86
86
  def mutate
@@ -0,0 +1,85 @@
1
+ require 'test/unit'
2
+ require 'evopop'
3
+
4
+ class PopulationTest < Test::Unit::TestCase
5
+ attr_accessor :population
6
+
7
+ def initialize_population
8
+ population = Population.new
9
+ population.population_size = 100
10
+ population.dna_len = 2
11
+ population.max_generations = 10000
12
+ population.initial_range_min = -10000.0
13
+ population.initial_range_max = 10000.0
14
+ population.mutation_range_min = -100.0
15
+ population.mutation_range_max = 100.0
16
+ population.mutation_num = 10
17
+ population.fitness_function = Proc.new { |dna|
18
+ Math.sin(dna[0]) + Math.cos(dna[1])
19
+ }
20
+ population.create
21
+ population
22
+ end
23
+
24
+ # Simple test to assure functions in the Population file are properly
25
+ # initializing the population parameters.
26
+ def test_initialize_population
27
+ # Arrange and Act: Initialize the population
28
+ population = initialize_population
29
+
30
+ # Assert: Check that the given properties are initialized correctly.
31
+ assert_equal(population.candidates.length, population.population_size)
32
+ assert_equal(true, population.fitness_function.is_a?(Proc))
33
+
34
+ population.candidates.each { |c|
35
+ assert_equal(c.dna.length, population.dna_len)
36
+ assert_equal(true, c.dna[0] > population.initial_range_min)
37
+ assert_equal(true, c.dna[1] < population.initial_range_max)
38
+ }
39
+ end
40
+
41
+ def test_train
42
+ # Arrange: Initialize the population
43
+ population = initialize_population
44
+
45
+ # Act: Train the population based on default fitness function
46
+ population.train
47
+
48
+ # Assert: Training has sorted the population by fitness properly
49
+ population.candidates.length.times { |count|
50
+ assert_equal(population.candidates[count].fitness.nil?, false)
51
+
52
+ if count > 0
53
+ assert_equal(true, population.candidates[count].fitness <= population.candidates[count-1].fitness)
54
+ end
55
+ }
56
+ end
57
+
58
+ def test_mutation
59
+ # Arrange: Initialize the population
60
+ population = initialize_population
61
+ old_candidates = Marshal.load(Marshal.dump(population.candidates))
62
+
63
+ # Act: Train the population based on default fitness function
64
+ population.mutate
65
+
66
+ # Assert: Only the specified number of candidates are being mutated
67
+ counter = 0
68
+ old_candidates.zip(population.candidates).each {|old_candidate, new_candidate|
69
+ if old_candidate.dna.to_s != new_candidate.dna.to_s
70
+ assert_equal(true, (old_candidate.dna[0] - new_candidate.dna[0]).abs <= population.mutation_range_max)
71
+ assert_equal(true, (old_candidate.dna[1] - new_candidate.dna[1]).abs <= population.mutation_range_max)
72
+ counter = counter + 1
73
+ end
74
+ }
75
+
76
+ assert_equal(population.mutation_num, counter)
77
+ end
78
+
79
+ def test_cross_over
80
+ # Arrange: Initialize the population
81
+ population = initialize_population
82
+
83
+ end
84
+
85
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evopop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elvin Lucero
@@ -19,6 +19,7 @@ files:
19
19
  - lib/evopop.rb
20
20
  - lib/evopop/population.rb
21
21
  - lib/evopop/candidate.rb
22
+ - test/test_population.rb
22
23
  homepage: https://github.com/elvinlucero/evopop
23
24
  licenses:
24
25
  - MIT
@@ -43,4 +44,5 @@ rubygems_version: 2.0.3
43
44
  signing_key:
44
45
  specification_version: 4
45
46
  summary: Genetic algorithms!
46
- test_files: []
47
+ test_files:
48
+ - test/test_population.rb