smart_sample 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjFmMDA4NjQzMmUzNGU5NmFiYmFlZmZhMTFjYzk3NDNlMGZkMDg1Yw==
5
+ data.tar.gz: !binary |-
6
+ MTZjZTA0NjFiOWQ0YjkwNGQ2MDdjODc0NDIyZDg4ZjU0ZDU1ODU0Zg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NTFlNTljN2RmZjdkZDc5MzJkYzAwODRiNzM3MDUxYjIxYTdkZmI1ZDM5NmEx
10
+ MWE0ZDc0ZmM0Yzc0MWFlOTJkYjA3OWY5YWExNTcwMTJjMmI5OWEyZjIyMDVl
11
+ NDRjNmUwNjViNGM5OTJmYjFlOWVhNDc1NjIyZWQzNDRlNWVhYmY=
12
+ data.tar.gz: !binary |-
13
+ NjE4NzNhOWU0NzVlMGRmMGU3ZTNlOTk1ODUwZjM4NTIyZWY2MTQ2NWJmNjJj
14
+ NjVmZDIzZWY1M2Q3NjFmOTAwYzA0NDg1NzU2NTU1NDNjNTljNWE4YWFjZWEw
15
+ NDJjMzljODBiMDM1NDNiY2YxNWQ5MDFjNzFlMDM4NzE3ODM1MzQ=
@@ -0,0 +1 @@
1
+ require_relative 'smart_sample/fitness_proportionate_selector'
@@ -0,0 +1,26 @@
1
+ module SmartSample
2
+ class FitnessProportionateSelector
3
+
4
+ def initialize(weights)
5
+ @weights = adjust_weights(weights)
6
+ @size = weights.size
7
+ end
8
+
9
+ def select(num=1)
10
+ 1.upto(num).map do
11
+ begin
12
+ index = (rand * @size).to_i
13
+ end while(rand >= @weights[index])
14
+ index
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def adjust_weights(weights)
21
+ max_weight = weights.max.to_f
22
+ weights.map { |weight| weight/max_weight }
23
+ end
24
+ end
25
+ FPS = RouletteWheelSelector = FitnessProportionateSelector
26
+ end
@@ -0,0 +1,3 @@
1
+ module SmartSample
2
+ VERSION = "0.0.0"
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe SmartSample::FitnessProportionateSelector do
4
+ describe "#select" do
5
+ subject {described_class.new([0.1, 0.3, 0.6])}
6
+
7
+ it "returns the number of element requested" do
8
+ expect(subject.select(1).size).to eq(1)
9
+ expect(subject.select(5).size).to eq(5)
10
+ expect(subject.select(10).size).to eq(10)
11
+ end
12
+
13
+ context "probability distribution" do
14
+ it "returns the indexes following the weighted proportion" do
15
+ indexes = subject.select(100000)
16
+ expect(indexes.count(0)).to be_within(1000.0).of(10000.0)
17
+ expect(indexes.count(1)).to be_within(1000.0).of(30000.0)
18
+ expect(indexes.count(2)).to be_within(1000.0).of(60000.0)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ require 'simplecov'
2
+ require 'codeclimate-test-reporter'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ CodeClimate::TestReporter::Formatter
7
+ ]
8
+
9
+ SimpleCov.start do
10
+ add_filter '/spec/'
11
+ end
12
+
13
+ require 'debugger'
14
+ require 'smart_sample'
15
+
16
+ RSpec.configure do |config|
17
+ config.run_all_when_everything_filtered = true
18
+ config.color = true
19
+ config.formatter = :documentation
20
+ # Run specs in random order to surface order dependencies. If you find an
21
+ # order dependency and want to debug it, you can fix the order by providing
22
+ # the seed, which is printed after each run.
23
+ # --seed 1234
24
+ config.order = 'random'
25
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_sample
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Marin Cabillas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: codeclimate-test-reporter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ description: Implementation of various selection strategies, usually used in genetic
42
+ algorithms
43
+ email: danmarcab@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/smart_sample.rb
49
+ - lib/smart_sample/fitness_proportionate_selector.rb
50
+ - lib/smart_sample/version.rb
51
+ - spec/smart_sample/fitness_proportionate_selector_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: https://github.com/danmarcab/smart_sample
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.4.2
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Implementation of various selection strategies
77
+ test_files:
78
+ - spec/smart_sample/fitness_proportionate_selector_spec.rb
79
+ - spec/spec_helper.rb
80
+ has_rdoc: