gale_shapley 0.2.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 51db3f9f6fbc3398c570abb818fb67617c96e788db078f381f1a336d0d3b63e0
4
+ data.tar.gz: e0bf9a0f8a988b81db52914beb9a73545f928a636d23d03b4291d696bca52f5a
5
+ SHA512:
6
+ metadata.gz: fa30cd55c7fc9e57d5c5c22e73a0b84966e54b6d81039824433c5b2fafb7600f93a91b23bcf2cbb08fbe8cd903d8a0eef93a28c4f7cb11f0c7ab8afde81cf06f
7
+ data.tar.gz: b8ae178fe4cc8ca40bc514782def578394ca2365e5fee7a806cbbce9c7c71af1048f97beb31e71a64574bc094d50e4dae6ef2afba696ac5c1d9f000caddecb6e
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at wetsand.wfs@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # GaleShapley
2
+
3
+ This gem provide a basic algorithm to solve 'stable marriage problem', finding a stable matching between two equally sized sets of elements given an ordering of preferences for each element.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gale_shapley'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gale_shapley
20
+
21
+ ## Usage
22
+
23
+ I recommend NOT to use
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mrbigass/gale_shapley. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/mrbigass/gale_shapley/blob/master/CODE_OF_CONDUCT.md).
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
39
+
40
+ ## Code of Conduct
41
+
42
+ Everyone interacting in the GaleShapley project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mrbigass/gale_shapley/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,32 @@
1
+ require_relative 'lib/gale_shapley/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "gale_shapley"
5
+ spec.version = GaleShapley::VERSION
6
+ spec.authors = ["Akito Hikasa"]
7
+ spec.email = ["wetsand.wfs@gmail.com"]
8
+
9
+ spec.summary = %q{matching algorithm}
10
+ spec.description = %q{}
11
+ spec.homepage = "https://github.com/mrbigass/gale_shapley"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata = {
16
+ "bug_tracker_uri" => "https://github.com/kakubin/gale_shapley/issues",
17
+ "allowed_push_host" => "https://rubygems.org",
18
+ "homepage_uri" => spec.homepage,
19
+ "source_code_uri" => spec.homepage
20
+ }
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = %w(gale_shapley.gemspec) + Dir["*.md", "lib/**/*.rb"]
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler"
30
+ spec.add_development_dependency "rake"
31
+ spec.add_development_dependency "test-unit"
32
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Acceptor # :nodoc:
4
+ attr_accessor :name, :proposer
5
+
6
+ def initialize(name, preferences)
7
+ @name = name
8
+ @preferences = preferences
9
+ @proposer = nil
10
+ end
11
+
12
+ def engaged?
13
+ !!@proposer
14
+ end
15
+
16
+ def engage(proposer)
17
+ @proposer&.free
18
+ @proposer = proposer
19
+ @proposer.engage
20
+ end
21
+
22
+ def more_attractive?(proposer)
23
+ @preferences.index(proposer.name) < @preferences.index(@proposer.name)
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Proposer # :nodoc:
4
+ attr_reader :name
5
+
6
+ def initialize(name, preferences)
7
+ @name = name
8
+ @preferences = preferences
9
+ @index = 0
10
+ @engaged = false
11
+ end
12
+
13
+ def unengaged?
14
+ !@engaged
15
+ end
16
+
17
+ def engage
18
+ @engaged = true
19
+ end
20
+
21
+ def current_acceptor_name
22
+ @preferences[@index]
23
+ end
24
+
25
+ def propose(acceptor)
26
+ if acceptor.engaged?
27
+ acceptor.more_attractive?(self) ? acceptor.engage(self) : free
28
+ else
29
+ acceptor.engage(self)
30
+ end
31
+ end
32
+
33
+ def free
34
+ @index += 1
35
+ @engaged = false
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GaleShapley
4
+ VERSION = '0.2.0'
5
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'gale_shapley/proposer'
4
+ require 'gale_shapley/acceptor'
5
+
6
+ class GaleShapley # :nodoc:
7
+ class UnproperSizeError < StandardError; end
8
+
9
+ class << self
10
+ def resolve(proposers_preferences, acceptors_preferences)
11
+ init(proposers_preferences, acceptors_preferences)
12
+ loop_til_stable
13
+
14
+ @proposers.map { |proposer| [proposer.name, proposer.current_acceptor_name] }.to_h
15
+ end
16
+
17
+ private
18
+
19
+ def init(proposers_preferences, acceptors_preferences)
20
+ check_resolvablity(proposers_preferences, acceptors_preferences)
21
+
22
+ proposers_preferences = proposers_preferences.transform_keys(&:to_s)
23
+ acceptors_preferences = acceptors_preferences.transform_keys(&:to_s)
24
+
25
+ @proposers = proposers_preferences.map do |name, preferences|
26
+ Proposer.new(name, preferences)
27
+ end
28
+ @acceptors = acceptors_preferences.map do |name, preferences|
29
+ Acceptor.new(name, preferences)
30
+ end
31
+ end
32
+
33
+ def check_resolvablity(proposers_preferences, acceptors_preferences)
34
+ size = proposers_preferences.keys.size
35
+
36
+ unless acceptors_preferences.keys.size == size &&
37
+ acceptors_preferences.all? { |_, preference| preference.size == size } &&
38
+ proposers_preferences.all? { |_, preference| preference.size == size }
39
+ raise UnproperSizeError, "can't resolve due to preference size"
40
+ end
41
+ end
42
+
43
+ def loop_til_stable
44
+ loop do
45
+ unengageds = @proposers.select(&:unengaged?)
46
+ break if unengageds.empty?
47
+
48
+ unengageds.each do |proposer|
49
+ name = proposer.current_acceptor_name
50
+ target = @acceptors.find { |acceptor| acceptor.name == name }
51
+ proposer.propose(target)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gale_shapley
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Akito Hikasa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-14 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: test-unit
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: ''
56
+ email:
57
+ - wetsand.wfs@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CODE_OF_CONDUCT.md
63
+ - README.md
64
+ - gale_shapley.gemspec
65
+ - lib/gale_shapley.rb
66
+ - lib/gale_shapley/acceptor.rb
67
+ - lib/gale_shapley/proposer.rb
68
+ - lib/gale_shapley/version.rb
69
+ homepage: https://github.com/mrbigass/gale_shapley
70
+ licenses:
71
+ - MIT
72
+ metadata:
73
+ bug_tracker_uri: https://github.com/kakubin/gale_shapley/issues
74
+ allowed_push_host: https://rubygems.org
75
+ homepage_uri: https://github.com/mrbigass/gale_shapley
76
+ source_code_uri: https://github.com/mrbigass/gale_shapley
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 2.3.0
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.2.3
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: matching algorithm
96
+ test_files: []