pmeth 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pmeth.rb +124 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9ac013c2d83864852ab50ad3a4d994f633465bed
4
+ data.tar.gz: 33b41165eba56e6d2b9e2e5dc6a80d4f2d547fc2
5
+ SHA512:
6
+ metadata.gz: 5fa177b83c11c023932a13b8b12a9c940059e29b3dee0606d54f21a9e6bc95131877e268ff006bf503f16cb5f631ab1ad545e6312b0d7cdea3d6608e262d569e
7
+ data.tar.gz: e6f269c1cb85f2143b51d353de4495f1eb40c1386ee06b987797978194accf57c988ff753b889dfb891b5f98b7f34ba5e6c4ac63a18b166174826d7c531cd1ba
data/lib/pmeth.rb ADDED
@@ -0,0 +1,124 @@
1
+ #encoding: utf-8
2
+ class PMeth
3
+ # Input: An integer you wish to know whether is prime
4
+ # Output: true/false
5
+ def self.prime?(n)
6
+ for d in 2..(n - 1)
7
+ if (n % d) == 0
8
+ return false
9
+ end
10
+ end
11
+ true
12
+ end
13
+
14
+ # Input: A permutation array of unique objects
15
+ # Output: A random integer that the length of the Input 0 array can be divided by to get another integer (the randomly chosen size of chunks that permutations will be split into, in the recombine/mutate methods)
16
+ def self.division(objects) #number of objects must be => 10
17
+ x = 1.5
18
+ until objects.length/x == (objects.length/x).to_i && x <= objects.length
19
+ x = (objects.length/10).to_f + rand(objects.length).to_f
20
+ end
21
+ return x
22
+ end
23
+
24
+ # Input 0: A parent permutation array of unique objects
25
+ # Input 1: A second parent permutation array of the same unique objects as input 0
26
+ # Output: A child permutation array, whose order is a recombination of the parent permutations (the same unique objects ordered differently to either input)
27
+ def self.recombine(a_parent, b_parent)
28
+ kid = []
29
+ 1.times do # so we can use redo
30
+ x = division(a_parent)
31
+ if x == a_parent.length && prime?(x) == false # If a permutation with a non-prime number of objects comes up with x == array length, redo
32
+ redo
33
+ elsif x == a_parent.length # to compensate for permutations with a prime number of objects:
34
+ ig = rand(a_parent.length)-1 # choose a random element of the array to ignore - we add the object at this element back at its original position after recombination
35
+ a_parent_reduced = a_parent.dup
36
+ b_parent_reduced = b_parent.dup
37
+ a_parent_reduced.delete_at(ig)
38
+ b_parent_reduced.delete_at(ig)
39
+ x = division(a_parent_reduced)
40
+ a_parent_sliced = a_parent_reduced.each_slice(x).to_a
41
+ b_parent_sliced = b_parent_reduced.each_slice(x).to_a
42
+ else
43
+ a_parent_sliced = a_parent.each_slice(x).to_a
44
+ b_parent_sliced = b_parent.each_slice(x).to_a
45
+ end
46
+ chosen = rand(b_parent_sliced.length)-1 # choose one of the chunks (sub array from a permutation) to keep from b_parent
47
+ child = a_parent_sliced.flatten.dup
48
+ y = 0
49
+ pos_array = []
50
+ a_parent_sliced[chosen].each do |object| # place each object in the equivalent a_parent chunk into the position it's corresponding object (from b_parent) occupies in a_parent
51
+ chunk = b_parent_sliced[chosen][y] # the equivalent object in the chosen b_parent chunk
52
+ pos = a_parent_sliced.flatten.index(chunk) # the position of the b_parent chunk in a_parent
53
+ c_pos = a_parent_sliced.flatten.index(object) # the position of the object in a_parent
54
+ pos_array << pos
55
+ y+=1
56
+ end
57
+ if pos_array.include?(nil)
58
+ redo
59
+ else
60
+ y = 0
61
+ pos_array.each do |pos|
62
+ unless b_parent_sliced[chosen].include?(a_parent_sliced[chosen][y])
63
+ child[pos] = a_parent_sliced[chosen][y]
64
+ child[a_parent_sliced.flatten.index(a_parent_sliced[chosen][y])] = b_parent_sliced[chosen][y] # swapping the positions of objects in chunks from parents, to give their positions in child
65
+ end
66
+ y+=1
67
+ end
68
+ end
69
+ if ig != nil
70
+ if b_parent_sliced[chosen].include?(b_parent[ig]) # add the ignored object from b_parent if it's in the chosen chunk...
71
+ child.insert(ig, b_parent[ig])
72
+ else
73
+ child.insert(ig, a_parent[ig]) # ...otherwise add the ignored object from a_parent
74
+ end
75
+ end
76
+ if child != child.uniq
77
+ redo
78
+ end
79
+ kid << child # so we can access this outside the loop
80
+ end
81
+ return kid[0]
82
+ end
83
+
84
+ # Input: A permutation array of unique objects
85
+ # Output: A slightly different permutation array of the same unique objects
86
+ def self.mutate(fasta)
87
+ mutant = []
88
+ 1.times do
89
+ x = 0
90
+ until x > 2
91
+ x = division(fasta)
92
+ end
93
+ sliced = fasta.each_slice(x).to_a
94
+ e = rand(sliced.length-1).to_i
95
+ sliced[e] = sliced[e].shuffle
96
+ if sliced.flatten == fasta
97
+ redo
98
+ end
99
+ mutant << sliced.flatten
100
+ end
101
+ return mutant[0]
102
+ end
103
+
104
+ # Input: A permutation array of unique objects
105
+ # Output: A slightly different permutation array of the same unique objects
106
+ def self.mini_mutate(fasta)
107
+ a = b = 0
108
+ until a != b
109
+ a = fasta[rand(fasta.length-1)]
110
+ b = fasta[rand(fasta.length-1)]
111
+ end
112
+ mutant = []
113
+ fasta.each do |i|
114
+ if i == a
115
+ mutant << b
116
+ elsif i == b
117
+ mutant << a
118
+ else
119
+ mutant << i
120
+ end
121
+ end
122
+ return mutant
123
+ end
124
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pmeth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Edward Chalstrey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: "Reproduction methods for genetic (and other iterative improvement) algorithms,
14
+ being used to solve permutation problems, \n where permutations are arrays of unique
15
+ objects. github: https://github.com/edwardchalstrey1/pmeth"
16
+ email: edwardchalstrey@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/pmeth.rb
22
+ homepage: http://rubygems.org/gems/pmeth
23
+ licenses:
24
+ - none
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.0.3
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Methods for permutation reproduction
46
+ test_files: []