envelope_alloc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9dfff2cc8bd14dc5e3fae539763b92bdee0d5af9
4
+ data.tar.gz: 26275ab1ffe2baf6804f3d4a8abf1a487314fbd1
5
+ SHA512:
6
+ metadata.gz: 69e32613c5afa944ff94cf09e9c18f32ab8aff08cc225b30e99812771f044a9687cc3efb7f33670241a7e73aed8ed685ab942fe7abc8ac160c3d8be089f67c39
7
+ data.tar.gz: 8dfcd1004aa5297feac7dcd688ce8041876c8814583d97a46422e4630ade9e186baec63c5f3ef97627423cee52ae21b462997238a5209cc273bde9c3edd7b5c7
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea
10
+ *.gem
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.16.0.pre.2
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://gems.ruby-china.org"
2
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ envelope_alloc (0.1.0)
5
+
6
+ GEM
7
+ remote: https://gems.ruby-china.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.4.2)
11
+ rspec (3.6.0)
12
+ rspec-core (~> 3.6.0)
13
+ rspec-expectations (~> 3.6.0)
14
+ rspec-mocks (~> 3.6.0)
15
+ rspec-core (3.6.0)
16
+ rspec-support (~> 3.6.0)
17
+ rspec-expectations (3.6.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.6.0)
20
+ rspec-mocks (3.6.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.6.0)
23
+ rspec-support (3.6.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler
30
+ envelope_alloc!
31
+ rake
32
+ rspec
33
+
34
+ BUNDLED WITH
35
+ 1.16.0.pre.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 LiBo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ # EnvelopeAlloc
2
+
3
+ You can split big number to arrays like:
4
+
5
+ ``` ruby
6
+ unit = 100
7
+ provider = EnvelopeAlloc::Provider.new 20*unit, 13, unit: unit # 20*unit => total_num|big_integer, 13 => alloced num witch u want, unit => mabey u should set 100 or other integer
8
+ provider.alloc_items #=> [147, 156, 181, 176, 169, 102, 198, 190, 115, 126, 164, 151, 125]
9
+
10
+ ```
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'envelope_alloc'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install envelope_alloc
27
+
28
+ Enjoy!
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "envelope_alloc"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'envelope_alloc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'envelope_alloc'
8
+ spec.version = EnvelopeAlloc::VERSION
9
+ spec.authors = ['Mixbo']
10
+ spec.email = ['lb563@foxmail.com']
11
+
12
+ spec.summary = %q{gaussian allocation a number}
13
+ spec.description = %q{This gem can use gaussian algorithm allocation a number }
14
+ spec.homepage = 'https://github.com/lihlio/envelope_alloc'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
23
+ 'public gem pushes.'
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ spec.add_development_dependency 'bundler'
34
+ spec.add_development_dependency 'rake'
35
+ spec.add_development_dependency 'rspec'
36
+ end
@@ -0,0 +1,57 @@
1
+ require "envelope_alloc/version"
2
+ require "envelope_alloc/gaussian"
3
+
4
+ module EnvelopeAlloc
5
+ class Provider
6
+ attr_reader :total_num, :alloc_num, :unit, :item_max
7
+
8
+ def initialize total_num, alloc_num, options={}
9
+ @total_num = total_num
10
+ @alloc_num = alloc_num
11
+ @unit = options[:unit] || 100
12
+ @item_max = options[:item_max] || (200 * @unit)
13
+ end
14
+
15
+ def alloc_items
16
+ return [total_num] if alloc_num == 1
17
+
18
+ money_left = (total_num - alloc_num * unit)
19
+
20
+ value_vec = []
21
+ 0.upto(alloc_num-2) do |i|
22
+ mu = money_left / (alloc_num - i)
23
+ sigma = mu / 2
24
+ rg = RandomGaussian.new(mu, sigma)
25
+ noise_value = rg.rand.to_i
26
+ noise_value = 0 if noise_value < 0
27
+ noise_value = money_left if noise_value > money_left
28
+ threshold_value = item_max - unit
29
+ noise_value = threshold_value if noise_value > threshold_value
30
+ value_vec << (noise_value + unit)
31
+ money_left -= noise_value
32
+ end
33
+
34
+ money_left = (total_num - value_vec.reduce(&:+))
35
+ last_money = money_left > item_max ? item_max : money_left
36
+ value_vec << last_money
37
+ money_left = money_left - last_money
38
+ split_left_items(money_left, value_vec)
39
+ value_vec
40
+ end
41
+
42
+ def split_left_items(money_left, value_vec)
43
+ return unless money_left > 0
44
+ value_vec.map! do |val|
45
+ gap = item_max - val
46
+ if money_left > gap
47
+ money_left -= gap
48
+ item_max
49
+ else
50
+ value = val + money_left
51
+ money_left = 0
52
+ value
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,33 @@
1
+ module EnvelopeAlloc
2
+ class RandomGaussian
3
+
4
+ def self.gaussian(mean, stddev, rand)
5
+ rho = Math.sqrt(-2 * Math.log(1 - rand.call))
6
+ scale = stddev * rho
7
+ theta = 2 * Math::PI * rand.call
8
+ x = mean + scale * Math.cos(theta)
9
+ y = mean + scale * Math.sin(theta)
10
+ [x, y]
11
+ end
12
+
13
+ def initialize(mean, stddev, rand_helper = lambda {Kernel.rand})
14
+ @rand_helper = rand_helper
15
+ @mean = mean
16
+ @stddev = stddev
17
+ @valid = false
18
+ @next = 0
19
+ end
20
+
21
+ def rand
22
+ if @valid
23
+ @valid = false
24
+ @next
25
+ else
26
+ @valid = true
27
+ x, y = self.class.gaussian(@mean, @stddev, @rand_helper)
28
+ @next = y
29
+ x
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module EnvelopeAlloc
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: envelope_alloc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mixbo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: 'This gem can use gaussian algorithm allocation a number '
56
+ email:
57
+ - lb563@foxmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - envelope_alloc.gemspec
73
+ - lib/envelope_alloc.rb
74
+ - lib/envelope_alloc/gaussian.rb
75
+ - lib/envelope_alloc/version.rb
76
+ homepage: https://github.com/lihlio/envelope_alloc
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ allowed_push_host: https://rubygems.org
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.5.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: gaussian allocation a number
101
+ test_files: []
102
+ has_rdoc: