pikk 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +39 -0
- data/README.md +36 -1
- data/examples/object_example.rb +33 -0
- data/lib/pikk/main.rb +18 -10
- data/lib/pikk/version.rb +1 -1
- data/pikk.gemspec +2 -0
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a54b718fdcbaeaab7308714745c25eac5307470
|
4
|
+
data.tar.gz: f2ac55e78df3d66096665e20d640a7ef3835f1a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6173337493b12f7739043475480138ed2e8764fc7866d4e8aaedb87985c0e9303106ce904db7968e9a57533d2484efb083534f7600cfec3c2e9a7afb9bcbd1c9
|
7
|
+
data.tar.gz: f6b796f2fe5e5225d0fda61e24fa51a15d089de0e76d6a724c52804f4266645e24fbb15a1c0d41be0a5327596dedff90d5f7ce824d1019d7ea313c51b139d199
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pikk (0.1.0)
|
5
|
+
terminal-table
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.3)
|
11
|
+
rake (10.5.0)
|
12
|
+
rspec (3.7.0)
|
13
|
+
rspec-core (~> 3.7.0)
|
14
|
+
rspec-expectations (~> 3.7.0)
|
15
|
+
rspec-mocks (~> 3.7.0)
|
16
|
+
rspec-core (3.7.1)
|
17
|
+
rspec-support (~> 3.7.0)
|
18
|
+
rspec-expectations (3.7.0)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.7.0)
|
21
|
+
rspec-mocks (3.7.0)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.7.0)
|
24
|
+
rspec-support (3.7.1)
|
25
|
+
terminal-table (1.8.0)
|
26
|
+
unicode-display_width (~> 1.1, >= 1.1.1)
|
27
|
+
unicode-display_width (1.3.0)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
bundler (~> 1.16)
|
34
|
+
pikk!
|
35
|
+
rake (~> 10.0)
|
36
|
+
rspec (~> 3.0)
|
37
|
+
|
38
|
+
BUNDLED WITH
|
39
|
+
1.16.1
|
data/README.md
CHANGED
@@ -20,7 +20,42 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
> **WARNING!**
|
24
|
+
> **Your hash should have the keys `:name` and `:weight`!**
|
25
|
+
|
26
|
+
There are two methods. One is `single_select` and the other is `pool`. Former, select one hash object from the array by its weight, and return it. Latter, do an iteration of selection, 50 by default, and return the array.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'pikk'
|
30
|
+
|
31
|
+
ary = [{name: 'a', weight: 5.5}, {name: 'b', weight: 3}, {name: 'c', weight: 7}, {name: 'd', weight: 1.5}]
|
32
|
+
|
33
|
+
Pikk.single_select(ary)
|
34
|
+
# => {:name=>"c", :weight=>7, :count=>0, :probability=>0.4117647058823529}
|
35
|
+
|
36
|
+
Pikk.pool(ary)
|
37
|
+
# => [{:name=>"c", :weight=>7, :count=>22, :probability=>0.4117647058823529}, {:name=>"a", :weight=>5.5, :count=>15, :probability=>0.3235294117647059}, {:name=>"b", :weight=>3, :count=>8, :probability=>0.17647058823529413}, {:name=>"d", :weight=>1.5, :count=>5, :probability=>0.08823529411764706}]
|
38
|
+
|
39
|
+
Pikk.pool(ary, iteration: 100)
|
40
|
+
# => [{:name=>"c", :weight=>7, :count=>37, :probability=>0.4117647058823529}, {:name=>"a", :weight=>5.5, :count=>34, :probability=>0.3235294117647059}, {:name=>"b", :weight=>3, :count=>18, :probability=>0.17647058823529413}, {:name=>"d", :weight=>1.5, :count=>11, :probability=>0.08823529411764706}]
|
41
|
+
|
42
|
+
Pikk.pool(ary, pp: true)
|
43
|
+
# =>
|
44
|
+
# +-----------------+--------+-------+---------------------+--------------------+
|
45
|
+
# | Name | Weight | Count | Probability | (C/(P*I)) |
|
46
|
+
# +-----------------+--------+-------+---------------------+--------------------+
|
47
|
+
# | a | 5.5 | 19 | 0.3235294117647059 | 1.1745454545454546 |
|
48
|
+
# | b | 3 | 8 | 0.17647058823529413 | 0.9066666666666666 |
|
49
|
+
# | c | 7 | 19 | 0.4117647058823529 | 0.9228571428571429 |
|
50
|
+
# | d | 1.5 | 4 | 0.08823529411764706 | 0.9066666666666666 |
|
51
|
+
# +-----------------+--------+-------+---------------------+--------------------+
|
52
|
+
# | Iteration Count | 50 |
|
53
|
+
# +-----------------+--------+-------+---------------------+--------------------+
|
54
|
+
# | Sum Ratio | 0.02231601731601729 |
|
55
|
+
# +-----------------+--------+-------+---------------------+--------------------+
|
56
|
+
```
|
57
|
+
|
58
|
+
|
24
59
|
|
25
60
|
## Development
|
26
61
|
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'pikk'
|
2
|
+
require 'active_support/core_ext/hash/keys'
|
3
|
+
require 'active_support/core_ext/object/json'
|
4
|
+
|
5
|
+
class Fruit
|
6
|
+
attr_reader :name, :weight
|
7
|
+
|
8
|
+
def initialize(name, weight)
|
9
|
+
@name = name
|
10
|
+
@weight = weight
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
fruits = []
|
15
|
+
|
16
|
+
fruits << mango = Fruit.new("mango", 5).as_json.symbolize_keys
|
17
|
+
fruits << apple = Fruit.new("apple", 3).as_json.symbolize_keys
|
18
|
+
fruits << cherry = Fruit.new("cherry", 2).as_json.symbolize_keys
|
19
|
+
fruits << banana = Fruit.new("banana", 1).as_json.symbolize_keys
|
20
|
+
fruits << melon = Fruit.new("melon", 8).as_json.symbolize_keys
|
21
|
+
fruits << orange = Fruit.new("orange", 6).as_json.symbolize_keys
|
22
|
+
|
23
|
+
puts
|
24
|
+
puts 'single select'
|
25
|
+
puts Pikk.single_select(fruits)
|
26
|
+
|
27
|
+
puts
|
28
|
+
puts 'pool select'
|
29
|
+
puts Pikk.pool(fruits, iteration: 10_000)
|
30
|
+
|
31
|
+
puts
|
32
|
+
puts 'pool select, table print'
|
33
|
+
puts Pikk.pool(fruits, iteration: 10_000, pp: true)
|
data/lib/pikk/main.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'terminal-table/import'
|
2
|
+
|
1
3
|
module Pikk
|
2
4
|
class << self
|
3
5
|
def single_select(list = [{}])
|
@@ -11,11 +13,11 @@ module Pikk
|
|
11
13
|
|
12
14
|
def pool(list = [{}], iteration: 50, pp: false)
|
13
15
|
raise ArgumentError, 'Input is empty!' if list == [{}]
|
14
|
-
|
16
|
+
$iteration = iteration
|
15
17
|
|
16
18
|
arr = calculate_prob(list)
|
17
19
|
|
18
|
-
|
20
|
+
$iteration.times do
|
19
21
|
selected_object = pick_from(arr)
|
20
22
|
selected_object[:count] += 1
|
21
23
|
end
|
@@ -51,18 +53,24 @@ module Pikk
|
|
51
53
|
def to_table(list = [{}])
|
52
54
|
list.sort_by { |h| h[:weight] }.reverse!
|
53
55
|
|
54
|
-
|
55
|
-
|
56
|
+
lsum = 0
|
57
|
+
|
58
|
+
ltable = table do
|
59
|
+
self.headings = "Name", "Weight", "Count", "Probability", "(C/(P*I))"
|
56
60
|
|
57
|
-
|
61
|
+
list.each do |f|
|
62
|
+
add_row [f[:name], f[:weight], f[:count], f[:probability], (f[:count].to_f/(f[:probability] * $iteration))]
|
58
63
|
|
59
|
-
|
60
|
-
|
61
|
-
|
64
|
+
lsum += f[:count].to_f/(f[:probability] * $iteration)
|
65
|
+
end
|
66
|
+
add_separator
|
67
|
+
add_row ['Iteration Count', { value: $iteration, colspan: 4, alignment: :right }]
|
68
|
+
add_separator
|
69
|
+
add_row ['Sum Ratio', { value: (1 - lsum / list.count).abs, colspan: 4, alignment: :right }]
|
70
|
+
align_column 1, :center
|
62
71
|
end
|
63
72
|
|
64
|
-
puts
|
65
|
-
puts (1 - fsum / list.count).abs
|
73
|
+
puts ltable
|
66
74
|
end
|
67
75
|
end
|
68
76
|
end
|
data/lib/pikk/version.rb
CHANGED
data/pikk.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pikk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oguzhan Ozdemir
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: terminal-table
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: Generic Algorithm Pool Selection.
|
56
70
|
email:
|
57
71
|
- aoozdemir@live.com
|
@@ -64,11 +78,13 @@ files:
|
|
64
78
|
- ".travis.yml"
|
65
79
|
- CODE_OF_CONDUCT.md
|
66
80
|
- Gemfile
|
81
|
+
- Gemfile.lock
|
67
82
|
- LICENSE.txt
|
68
83
|
- README.md
|
69
84
|
- Rakefile
|
70
85
|
- bin/console
|
71
86
|
- bin/setup
|
87
|
+
- examples/object_example.rb
|
72
88
|
- lib/pikk.rb
|
73
89
|
- lib/pikk/main.rb
|
74
90
|
- lib/pikk/version.rb
|