pmeth 0.0.5 → 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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pmeth.rb +20 -21
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ceada8203f5a3f30779c0659894ce9ba093ace9b
4
- data.tar.gz: c3986f76c2d706c48cc727752070be2157fb552d
3
+ metadata.gz: f2d15cc0b7b591e80a1cf83f22061ff3fb6a03b6
4
+ data.tar.gz: 7e99134af27d63fe2a3b1c20c49889dd1164ab63
5
5
  SHA512:
6
- metadata.gz: 499c46045ead1236234721902860501e27971ed7cd331b234a1a90168778db4cf753f0722f2cd6b4e7ff480ce8ce8b99000133ba8f13d073fe36fd5aa38d2fe1
7
- data.tar.gz: 0434f5177db12f1e65a48fc94c56efabf10325e6971d2825c83b92e44641ed05c3418f67bbed47627fe0c30e2c902e31103593baa47fc2a3dbef134bbc91370e
6
+ metadata.gz: e98efcde0963c58bd0e22b17c451be8ea710c3d7b8f505ddefcef424f1e5f389973b772e5b49685fb096905a3e9b1d7ec65ba4e88b5cfad58a13adfd8be77c2f
7
+ data.tar.gz: 53779c27902cc3e802af1592cafe9fa9fb33cfde6ce7dec97cec30ede90dc3285c67d20fade4b30c387b53264dde5da59bc0afd58046db3cc3ac563e87400f8a
data/lib/pmeth.rb CHANGED
@@ -1,31 +1,31 @@
1
1
  #encoding: utf-8
2
+
3
+ require 'prime'
4
+
5
+ class Integer
6
+ #returns all positive factors of an Integer
7
+ def factors
8
+ (1..self).select { |n| (self % n).zero? }
9
+ end
10
+
11
+ #returns true if self is a prime number, false otherwise
12
+ def prime?
13
+ Prime.prime?(self)
14
+ end
15
+ end
16
+
17
+
2
18
  class PMeth
3
- # Returns true if integer n is a prime number, false if not
4
- def self.prime?(n)
5
- for d in 2..(n - 1)
6
- if (n % d) == 0
7
- return false
8
- end
9
- end
10
- true
11
- end
12
19
 
13
20
  # Returns a random integer that array can be divided by to get another integer (other than the array length itself)
21
+ # by getting all the factors of the array length and ditching the array length
14
22
  def self.division(array)
15
- x = 1.5 # x assigned a non-integer value to begin with
16
- if prime?(array.length) # array with prime number of objects is not divisible by any integer other than 1 and itself
17
- x = 1
18
- else
19
- until x > 0 && array.length/x.to_f == (array.length/x.to_f).to_i && Integer === x
20
- x = rand(array.length)
21
- end
22
- end
23
- return x
23
+ array.length.factors.select {|n| n != array.length}.sample
24
24
  end
25
25
 
26
26
  # Returns a new permutation that has had a randomly sized sub-section re-ordered by shuffle
27
27
  def self.chunk_mutate(permutation)
28
- if prime?(permutation.length) # if there are a prime number of objects in the permutation
28
+ if permutation.length.prime? # if there are a prime number of objects in the permutation
29
29
  ig = rand(permutation.length)-1 # choose a random object to ignore - to add back at its original index after mutation
30
30
  ig_obj = permutation[ig] # save the object
31
31
  permutation.delete_at(ig)
@@ -64,9 +64,8 @@ class PMeth
64
64
 
65
65
  # Returns a permutation whose objects are ordered partly like parent_1 permutation, and partly like parent_2 permutation
66
66
  def self.recombine(parent_1, parent_2)
67
- child = []
68
67
  x = division(parent_1) # the randomly chosen size of chunks that permutations will be split into
69
- if prime?(parent_1.length) # to compensate for permutations with a prime number of objects:
68
+ if parent_1.length.prime? # to compensate for permutations with a prime number of objects:
70
69
  ig = rand(parent_1.length)-1 # choose a random object to ignore - to add back at its original index after mutation
71
70
  p2_ig = parent_2.index(parent_1[ig]) # choose the same object from parent_2
72
71
  parent_1_reduced, parent_2_reduced = parent_1.dup, parent_2.dup # then create duplicates of the parent arrays...
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pmeth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edward Chalstrey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-07 00:00:00.000000000 Z
11
+ date: 2014-05-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: "Reproduction methods for genetic (and other iterative improvement) algorithms,
14
14
  being used to solve permutation problems, \n where permutations are arrays of unique