apportion 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0164723475a1fb9b94e2f42e8cdea11a7329618d
4
+ data.tar.gz: bc55cb9674ec51022132a40d1192c574e95302e0
5
+ SHA512:
6
+ metadata.gz: 34533ff02d06f3b15f1ff0bcb8a878eeea4ea983e13cb160df4daecf745d777ecd80dae96bd59803c4e488a4da14097e8e01017e71a8dfc4cd38a82f7439967b
7
+ data.tar.gz: 001cc71d1a2063588b764d86b825d7b2e1fd3f3120a9683138d30603ab0a8cc1664ed99062dfaf8721f542712724260d7afda8a62f90329728b6b5a7a7ee50e1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at gfleshman@newforge-tech.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in apportion.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Gary Fleshman
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.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Apportion [![travis][ci_img]][travis] [![yard docs][yd_img]][yard_docs] [![code climate][cc_img]][code_climate]
2
+
3
+ A Ruby implementation for apportionment
4
+
5
+ ### Description
6
+ Apportionment is most often associated with the [US Congressional apportionment][congress_ep],
7
+ using the **Equal Proportions** (or Huntington-Hill) method.
8
+
9
+ In general, apportionment involves the division of a scarce resource among recipients, according
10
+ to a pre-determined set of recipient weights. Further, a series of apportionments might occur over
11
+ time, with each new apportionment overlaying the previous.
12
+
13
+ Different domains might make use of apportionment. One example is the allocation (or distribution)
14
+ of sellable inventory among a number of retail stores, according to the expected sales rate for
15
+ each store. For this usage, recipients would likely begin an apportionment with varying quantities
16
+ of existing inventory.
17
+
18
+ Other apportionment methods exist, notably the [**Quota**][ams_balinski] method,
19
+ that address issues with **Equal Proportions**.
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'apportion'
27
+ ```
28
+
29
+ And then execute
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as
34
+
35
+ $ gem install apportion
36
+
37
+ ## Usage
38
+
39
+ ### Apportion.division(weights, size, options = {})
40
+
41
+ Returns a hash (a key for each recipient) with the size quantity distributed among recipients
42
+ (as values)
43
+
44
+ #### Parameters
45
+
46
+ `weights` (required) a [Hash] of relative integer proportions for each recipient
47
+
48
+ `size` (required) an [Integer] quantity to apportion
49
+
50
+ `options` (optional) [Hash] keys
51
+
52
+ * **:prior_portions** a [Hash] prior portions for each recipient from a previous apportionment
53
+ * **:required_minimum** an [Integer] smallest portion for each recipient
54
+
55
+ ## Development
56
+
57
+ To install dependencies
58
+
59
+ $ bundle
60
+
61
+ To run the tests
62
+
63
+ $ rake
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License][license].
68
+
69
+ [congress_ep]: https://en.wikipedia.org/wiki/United_States_congressional_apportionment#The_method_of_equal_proportions
70
+ [ams_balinski]: https://www.maa.org/sites/default/files/pdf/upload_library/22/Ford/BalinskiYoung.pdf
71
+ [license]: http://opensource.org/licenses/MIT
72
+
73
+ [travis]: https://travis-ci.org/garyf/apportion
74
+ [ci_img]: https://travis-ci.org/garyf/apportion.svg?branch=master
75
+ [yard_docs]: http://www.rubydoc.info/github/garyf/apportion
76
+ [yd_img]: http://img.shields.io/badge/yard-docs-blue.svg
77
+ [code_climate]: https://codeclimate.com/github/garyf/apportion
78
+ [cc_img]: https://codeclimate.com/github/garyf/apportion/badges/gpa.svg
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
data/apportion.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'apportion/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "apportion"
8
+ spec.version = Apportion::VERSION
9
+ spec.authors = ["Gary Fleshman"]
10
+ spec.email = ["gfleshman@newforge-tech.com"]
11
+
12
+ spec.summary = 'Apportion for Ruby'
13
+ spec.description = 'Ruby implementation for apportionment'
14
+ spec.homepage = 'https://github.com/garyf/apportion'
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency 'json', '~> 1.8'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.11'
25
+ spec.add_development_dependency 'minitest', '~> 5.8'
26
+ spec.add_development_dependency 'minitest-reporters', '~> 1.1'
27
+ spec.add_development_dependency 'rake', '~> 10.5'
28
+ spec.add_development_dependency 'simplecov', '~> 0.11'
29
+ spec.add_development_dependency 'yard', '~> 0.8'
30
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "apportion"
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
data/bin/setup ADDED
@@ -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,39 @@
1
+ module Apportion
2
+ module Algorithm
3
+ # Selects the next recipient
4
+ module EqualProportions
5
+
6
+ BIG_FIXNUM = 2 ** 30
7
+
8
+ module_function
9
+
10
+ # Selects the next recipient by sorting the equal proportions rank-index of the recipients
11
+ #
12
+ # see Balinski, M. and H. Young, The Quota Method of Apportionment, Amer. Math. Monthly 82 (1975) 701-730.
13
+ #
14
+ # @param weights [Hash] relative integer proportions
15
+ # @param portions [Hash]
16
+ # @return [Symbol] recipient having the highest equal proportions rank-index
17
+ # @example
18
+ # next_recipient({a: 41, b: 32, c: 27}, {a: 4, b: 3, c: 2})
19
+ # # => :c
20
+
21
+ def next_recipient(weights, portions)
22
+ weights.max_by { |k, v| recipient_rank(v, portions[k]) }[0]
23
+ end
24
+
25
+ def recipient_rank(weight, portion)
26
+ return zero_rank(weight) if portion == 0
27
+ weight / Math.sqrt(portion * (portion + 1)).to_f
28
+ end
29
+
30
+ # protect from division by zero
31
+ def zero_rank(weight)
32
+ weight * BIG_FIXNUM
33
+ end
34
+
35
+ private_class_method :recipient_rank,
36
+ :zero_rank
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ require 'apportion/util'
2
+
3
+ module Apportion
4
+ # Configure per options
5
+ module Setup
6
+ module_function
7
+
8
+ # Determines initial portions for recipients according to optional constraints
9
+ #
10
+ # @param keys [Hash] correspond to recipients
11
+ # @param options [Hash]
12
+ # @param options [Hash] :prior_portions from a previous apportionment
13
+ # @option options [Integer] :required_minimum smallest portion for each recipient
14
+ # @return [Hash] portions
15
+ # @example
16
+ # initial_portions({a: 41, b: 32, c: 27}, required_minimum: 2)
17
+ # # => {a: 2, b: 2, c: 2}
18
+
19
+ def initial_portions(keys, options)
20
+ portions = Hash.new
21
+ int = options[:required_minimum] || 0
22
+ keys.each { |k| portions[k] = int }
23
+ hash = options[:prior_portions]
24
+ return portions unless hash
25
+ prior_portions_included(portions, hash)
26
+ end
27
+
28
+ def prior_portions_included(portions, prior_portions)
29
+ portions.merge(prior_portions) { |_k, o, n| [o, n].max }
30
+ end
31
+
32
+ # @raise [RuntimeError] if size too small
33
+ def validate(size, sum_portions)
34
+ fail('Size less than minimum sum_portions') if size < sum_portions
35
+ end
36
+
37
+ private_class_method :prior_portions_included
38
+ end
39
+ end
@@ -0,0 +1,54 @@
1
+ module Apportion
2
+ # Utility methods
3
+ module Util
4
+ module_function
5
+
6
+ # Reads a JSON file and returns a hash with symbols as keys
7
+ #
8
+ # @param path_to_directory [String]
9
+ # @param json_file_name [String]
10
+ # @return [Hash] with symbolized keys
11
+ # @example
12
+ # file_read_json_to_hash(path_to_directory, json_file_name)
13
+ # # => {a: 5, b: 2, c: 3}
14
+
15
+ def file_read_json_to_hash(path_to_directory, json_file_name)
16
+ hash = JSON.parse(File.read(File.join(path_to_directory, json_file_name)))
17
+ symbolize_keys(hash)
18
+ end
19
+
20
+ # Sums all of the values in a hash
21
+ #
22
+ # @param hash [Hash] with numeric values
23
+ # @return [Numeric] the sum of all values
24
+ # @example
25
+ # hash_values_sum({a: 8, b: 2, c: 3})
26
+ # # => 13
27
+
28
+ def hash_values_sum(hash)
29
+ hash.values.reduce(0) { |acc, v| acc + v }
30
+ end
31
+
32
+ # Returns a new hash with symbols as keys
33
+ #
34
+ # @param hash [Hash]
35
+ # @return [Hash] a new hash with all string keys converted to symbols,
36
+ # provided that the keys respond to #to_sym
37
+ # @see cf. rails activesupport/lib/active_support/core_ext/hash/keys.rb
38
+ # @example
39
+ # symbolize_keys({'a' => 0, 'b' => '2', c: '3'})
40
+ # # => {a: 0, b: '2', c: '3'}
41
+
42
+ def symbolize_keys(hash)
43
+ transform_keys(hash) { |key| key.to_sym rescue key }
44
+ end
45
+
46
+ def transform_keys(hash)
47
+ result_hash = Hash.new
48
+ hash.keys.each { |k| result_hash[yield(k)] = hash[k] }
49
+ result_hash
50
+ end
51
+
52
+ private_class_method :transform_keys
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module Apportion
2
+ VERSION = '0.0.1'
3
+ end
data/lib/apportion.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'apportion/algorithm/equal_proportions'
2
+ require 'apportion/setup'
3
+ require 'apportion/util'
4
+
5
+ # Top level interface, or API, to distribute a quantity among recipients
6
+ module Apportion
7
+ include Apportion::Setup
8
+ include Apportion::Util
9
+ module_function
10
+
11
+ # Distributes a quantity among recipients by dividing proportionally according to relative
12
+ # weights using the 'equal proportions' algorithm
13
+ #
14
+ # @param weights [Hash] relative integer proportions
15
+ # @param size [Integer] quantity to apportion
16
+ # @param options [Hash]
17
+ # @option options [Hash] :prior_portions from a previous apportionment
18
+ # @option options [Integer] :required_minimum smallest portion for each recipient
19
+ # @return [Hash] portions
20
+ # @example
21
+ # division({a: 41, b: 32, c: 27}, 7)
22
+ # # => {a: 3, b: 2, c: 2}
23
+
24
+ def division(weights, size, options = {})
25
+ portions = initial_portions(weights.keys, options)
26
+ sum_portions = hash_values_sum(portions)
27
+ validate(size, sum_portions)
28
+ next_step(weights, portions, sum_portions, size)
29
+ end
30
+
31
+ # recursively
32
+ def next_step(weights, portions, sum_portions, size)
33
+ return portions if sum_portions == size
34
+ key = Algorithm::EqualProportions.next_recipient(weights, portions)
35
+ next_portions = portions.merge(key => portions[key] + 1)
36
+ next_step(weights, next_portions, sum_portions + 1, size)
37
+ end
38
+
39
+ private_class_method :next_step
40
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apportion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gary Fleshman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.11'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.11'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
+ description: Ruby implementation for apportionment
112
+ email:
113
+ - gfleshman@newforge-tech.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - CODE_OF_CONDUCT.md
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - apportion.gemspec
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/apportion.rb
129
+ - lib/apportion/algorithm/equal_proportions.rb
130
+ - lib/apportion/setup.rb
131
+ - lib/apportion/util.rb
132
+ - lib/apportion/version.rb
133
+ homepage: https://github.com/garyf/apportion
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.5.1
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Apportion for Ruby
157
+ test_files: []
158
+ has_rdoc: