ropt 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +25 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +20 -0
- data/Gemfile.lock +143 -0
- data/LICENSE.txt +21 -0
- data/README.md +86 -0
- data/Rakefile +12 -0
- data/lib/ropt/antagonistic_games/matrix_game.rb +69 -0
- data/lib/ropt/cooperative_games/coalitions_coefficients.rb +41 -0
- data/lib/ropt/cooperative_games/shapley_value.rb +79 -0
- data/lib/ropt/cooperative_games/validator.rb +63 -0
- data/lib/ropt/vector_matrix_math/operation_vectors.rb +22 -0
- data/lib/ropt/vector_matrix_math/shell_matrix.rb +14 -0
- data/lib/ropt/vector_matrix_math/shell_vector.rb +14 -0
- data/lib/ropt/version.rb +5 -0
- data/lib/ropt.rb +14 -0
- data/ropt.gemspec +41 -0
- data/sig/ropt.rbs +4 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3fe90a0a77c2d1d45f71343e380dbad72896a24f15994af400fa8fb17207c685
|
4
|
+
data.tar.gz: 4a13f0f5cf11126c2ea109eabc73436d7e90a37185a6cca3362d998df89532fb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3c43e7b7a17b474f646927a3e550d70d6faa782e390929369680382d33bcb98af27d980000f3648eaaecb9ecd9f19290c131cee0a0f745b1c94cd6a5a13154b1
|
7
|
+
data.tar.gz: 915bba2cf9f10556bf04b6a0447fd914e30fcd111715168b03e2d31084b5c6da62fc6a50d5043df5f5b1cd3f9d5372cf14fc93863019c4c1b009a36eff5fe203
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rake
|
3
|
+
- rubocop-rspec
|
4
|
+
- rubocop-performance
|
5
|
+
|
6
|
+
AllCops:
|
7
|
+
TargetRubyVersion: 3.3
|
8
|
+
NewCops: enable
|
9
|
+
|
10
|
+
Style/StringLiterals:
|
11
|
+
Enabled: true
|
12
|
+
EnforcedStyle: double_quotes
|
13
|
+
|
14
|
+
Style/StringLiteralsInInterpolation:
|
15
|
+
Enabled: true
|
16
|
+
EnforcedStyle: double_quotes
|
17
|
+
|
18
|
+
Layout/LineLength:
|
19
|
+
Max: 120
|
20
|
+
|
21
|
+
RSpec/NestedGroups:
|
22
|
+
Max: 5
|
23
|
+
|
24
|
+
Gemspec/RequiredRubyVersion:
|
25
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at lev.pruglo@mail.ru. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
gem "rake", "~> 13.0"
|
8
|
+
|
9
|
+
gem "matrix"
|
10
|
+
|
11
|
+
group :development, :test do
|
12
|
+
gem "byebug"
|
13
|
+
gem "rspec"
|
14
|
+
gem "rubocop", require: false
|
15
|
+
gem "rubocop-performance", require: false
|
16
|
+
gem "rubocop-rake", require: false
|
17
|
+
gem "rubocop-rspec", require: false
|
18
|
+
gem "rubycritic", require: false
|
19
|
+
gem "simplecov", require: false
|
20
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ropt (1.0.0)
|
5
|
+
matrix
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.8.6)
|
11
|
+
public_suffix (>= 2.0.2, < 6.0)
|
12
|
+
ast (2.4.2)
|
13
|
+
axiom-types (0.1.1)
|
14
|
+
descendants_tracker (~> 0.0.4)
|
15
|
+
ice_nine (~> 0.11.0)
|
16
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
17
|
+
byebug (11.1.3)
|
18
|
+
coercible (1.0.0)
|
19
|
+
descendants_tracker (~> 0.0.1)
|
20
|
+
descendants_tracker (0.0.4)
|
21
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
22
|
+
diff-lcs (1.5.0)
|
23
|
+
docile (1.4.0)
|
24
|
+
erubi (1.12.0)
|
25
|
+
flay (2.13.1)
|
26
|
+
erubi (~> 1.10)
|
27
|
+
path_expander (~> 1.0)
|
28
|
+
ruby_parser (~> 3.0)
|
29
|
+
sexp_processor (~> 4.0)
|
30
|
+
flog (4.8.0)
|
31
|
+
path_expander (~> 1.0)
|
32
|
+
ruby_parser (~> 3.1, > 3.1.0)
|
33
|
+
sexp_processor (~> 4.8)
|
34
|
+
ice_nine (0.11.2)
|
35
|
+
json (2.7.1)
|
36
|
+
kwalify (0.7.2)
|
37
|
+
language_server-protocol (3.17.0.3)
|
38
|
+
launchy (2.5.2)
|
39
|
+
addressable (~> 2.8)
|
40
|
+
matrix (0.4.2)
|
41
|
+
parallel (1.23.0)
|
42
|
+
parser (3.2.2.4)
|
43
|
+
ast (~> 2.4.1)
|
44
|
+
racc
|
45
|
+
path_expander (1.1.1)
|
46
|
+
public_suffix (5.0.4)
|
47
|
+
racc (1.7.3)
|
48
|
+
rainbow (3.1.1)
|
49
|
+
rake (13.1.0)
|
50
|
+
reek (6.1.4)
|
51
|
+
kwalify (~> 0.7.0)
|
52
|
+
parser (~> 3.2.0)
|
53
|
+
rainbow (>= 2.0, < 4.0)
|
54
|
+
regexp_parser (2.8.3)
|
55
|
+
rexml (3.2.6)
|
56
|
+
rspec (3.12.0)
|
57
|
+
rspec-core (~> 3.12.0)
|
58
|
+
rspec-expectations (~> 3.12.0)
|
59
|
+
rspec-mocks (~> 3.12.0)
|
60
|
+
rspec-core (3.12.2)
|
61
|
+
rspec-support (~> 3.12.0)
|
62
|
+
rspec-expectations (3.12.3)
|
63
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
64
|
+
rspec-support (~> 3.12.0)
|
65
|
+
rspec-mocks (3.12.6)
|
66
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
67
|
+
rspec-support (~> 3.12.0)
|
68
|
+
rspec-support (3.12.1)
|
69
|
+
rubocop (1.59.0)
|
70
|
+
json (~> 2.3)
|
71
|
+
language_server-protocol (>= 3.17.0)
|
72
|
+
parallel (~> 1.10)
|
73
|
+
parser (>= 3.2.2.4)
|
74
|
+
rainbow (>= 2.2.2, < 4.0)
|
75
|
+
regexp_parser (>= 1.8, < 3.0)
|
76
|
+
rexml (>= 3.2.5, < 4.0)
|
77
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
78
|
+
ruby-progressbar (~> 1.7)
|
79
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
80
|
+
rubocop-ast (1.30.0)
|
81
|
+
parser (>= 3.2.1.0)
|
82
|
+
rubocop-capybara (2.19.0)
|
83
|
+
rubocop (~> 1.41)
|
84
|
+
rubocop-factory_bot (2.24.0)
|
85
|
+
rubocop (~> 1.33)
|
86
|
+
rubocop-performance (1.19.1)
|
87
|
+
rubocop (>= 1.7.0, < 2.0)
|
88
|
+
rubocop-ast (>= 0.4.0)
|
89
|
+
rubocop-rake (0.6.0)
|
90
|
+
rubocop (~> 1.0)
|
91
|
+
rubocop-rspec (2.25.0)
|
92
|
+
rubocop (~> 1.40)
|
93
|
+
rubocop-capybara (~> 2.17)
|
94
|
+
rubocop-factory_bot (~> 2.22)
|
95
|
+
ruby-progressbar (1.13.0)
|
96
|
+
ruby_parser (3.20.3)
|
97
|
+
sexp_processor (~> 4.16)
|
98
|
+
rubycritic (4.9.0)
|
99
|
+
flay (~> 2.13)
|
100
|
+
flog (~> 4.7)
|
101
|
+
launchy (>= 2.5.2)
|
102
|
+
parser (>= 3.2.2.1)
|
103
|
+
rainbow (~> 3.1.1)
|
104
|
+
reek (~> 6.0, < 7.0)
|
105
|
+
rexml
|
106
|
+
ruby_parser (~> 3.20)
|
107
|
+
simplecov (>= 0.22.0)
|
108
|
+
tty-which (~> 0.5.0)
|
109
|
+
virtus (~> 2.0)
|
110
|
+
sexp_processor (4.17.0)
|
111
|
+
simplecov (0.22.0)
|
112
|
+
docile (~> 1.1)
|
113
|
+
simplecov-html (~> 0.11)
|
114
|
+
simplecov_json_formatter (~> 0.1)
|
115
|
+
simplecov-html (0.12.3)
|
116
|
+
simplecov_json_formatter (0.1.4)
|
117
|
+
thread_safe (0.3.6)
|
118
|
+
tty-which (0.5.0)
|
119
|
+
unicode-display_width (2.5.0)
|
120
|
+
virtus (2.0.0)
|
121
|
+
axiom-types (~> 0.1)
|
122
|
+
coercible (~> 1.0)
|
123
|
+
descendants_tracker (~> 0.0, >= 0.0.3)
|
124
|
+
|
125
|
+
PLATFORMS
|
126
|
+
x86_64-darwin-21
|
127
|
+
x86_64-linux
|
128
|
+
|
129
|
+
DEPENDENCIES
|
130
|
+
byebug
|
131
|
+
matrix
|
132
|
+
rake (~> 13.0)
|
133
|
+
ropt!
|
134
|
+
rspec
|
135
|
+
rubocop
|
136
|
+
rubocop-performance
|
137
|
+
rubocop-rake
|
138
|
+
rubocop-rspec
|
139
|
+
rubycritic
|
140
|
+
simplecov
|
141
|
+
|
142
|
+
BUNDLED WITH
|
143
|
+
2.5.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Lev Pruglo
|
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,86 @@
|
|
1
|
+
# Ropt
|
2
|
+
|
3
|
+
The library for mathematical optimization in Ruby is designed to help solve many problems in the computational, financial, social, and energy fields and, in theory, should include components of game theory, combinatorics, probability theory, linear and nonlinear optimization, cluster, regression, and other analysis.
|
4
|
+
|
5
|
+
At the moment, it is a simple library for working with some types of [Antagonistic](https://en.wikipedia.org/wiki/Zero-sum_game) and [Cooperative](https://en.wikipedia.org/wiki/Cooperative_game_theory) Games described in game theory.
|
6
|
+
Solvers have been written to calculate the lower and upper values of the [Matrix Game](https://en.wikipedia.org/wiki/Normal-form_game), indicating whether there is a Nash equilibrium.
|
7
|
+
A solver for calculating the [Shapley Vector](https://en.wikipedia.org/wiki/Shapley_value) has been implemented for the cooperative part of game theory.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Adding to a gem:
|
12
|
+
|
13
|
+
`gem install ropt`
|
14
|
+
|
15
|
+
Or adding to your project:
|
16
|
+
|
17
|
+
`gem "ropt", "~> 1.0"`
|
18
|
+
|
19
|
+
Run bundle install:
|
20
|
+
|
21
|
+
`bundle install`
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
To call the matrix game solver, you need to do:
|
26
|
+
|
27
|
+
```
|
28
|
+
strategies = [[2, 1], [2, 0]]
|
29
|
+
Ropt::AntagonisticGames::MatrixGame.new(strategies).solve
|
30
|
+
```
|
31
|
+
|
32
|
+
In this example, the result will be:
|
33
|
+
```
|
34
|
+
{
|
35
|
+
:first_gamer_optimal_strategy=>[2, 1],
|
36
|
+
:second_gamer_optimal_strategy=>[1, 0],
|
37
|
+
:lower_value=>1,
|
38
|
+
:higher_value=>1,
|
39
|
+
:equilibrium=>true
|
40
|
+
}
|
41
|
+
```
|
42
|
+
|
43
|
+
For large arrays, there may be a memory limit, which leads to a slowdown in the operation of the method.
|
44
|
+
|
45
|
+
Consider an example using a solver to calculate the Shapley Vector.
|
46
|
+
|
47
|
+
The call looks like this:
|
48
|
+
```
|
49
|
+
coalitions = {
|
50
|
+
B: 0,
|
51
|
+
A: 0,
|
52
|
+
AB: 1,
|
53
|
+
C: 0,
|
54
|
+
ABC: 1,
|
55
|
+
AC: 1
|
56
|
+
}
|
57
|
+
|
58
|
+
number_gamers = 3
|
59
|
+
|
60
|
+
Ropt::CooperativeGames::ShapleyValue.new(coalitions, number_gamers).solve
|
61
|
+
```
|
62
|
+
|
63
|
+
In this example, the result of Shapley Value will be:
|
64
|
+
|
65
|
+
```
|
66
|
+
{
|
67
|
+
:B=>0.167,
|
68
|
+
:A=>0.667,
|
69
|
+
:C=>0.167
|
70
|
+
}
|
71
|
+
```
|
72
|
+
It is worth noting that coalitions and the number of gamers must be sent, otherwise a validation error will occur.
|
73
|
+
If it is determined that the value of the grand coalition characteristic function is not equal to the sum of the components of the Shapley Vector, the solver will raise an exception.
|
74
|
+
|
75
|
+
The constant defines an upper limit for the number of gamers, which is 171 gamers. Exceeding this number will result in an exception being raised.
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Leozack-red/ropt. 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/[USERNAME]/ropt/blob/main/CODE_OF_CONDUCT.md).
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
83
|
+
|
84
|
+
## Code of Conduct
|
85
|
+
|
86
|
+
Everyone interacting in the Ropt project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ropt/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ropt/vector_matrix_math/shell_matrix"
|
4
|
+
|
5
|
+
module Ropt
|
6
|
+
module AntagonisticGames
|
7
|
+
# class for matrix game solver with option of get report string message
|
8
|
+
class MatrixGame
|
9
|
+
attr_accessor :matrix, :count_rows, :count_columns
|
10
|
+
|
11
|
+
def initialize(rows)
|
12
|
+
@matrix = Ropt::VectorMatrixMath::ShellMatrix.create(rows)
|
13
|
+
@count_rows = rows.size
|
14
|
+
@count_columns = rows.first.size
|
15
|
+
end
|
16
|
+
|
17
|
+
def solve
|
18
|
+
hash_result
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def hash_result
|
24
|
+
{
|
25
|
+
first_gamer_optimal_strategy: opt_strategy(:first),
|
26
|
+
second_gamer_optimal_strategy: opt_strategy(:second),
|
27
|
+
lower_value:,
|
28
|
+
higher_value:,
|
29
|
+
equilibrium: lower_value == higher_value
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
def opt_strategy(gamer_position)
|
34
|
+
case gamer_position
|
35
|
+
when :first
|
36
|
+
index = find_vector(:row).index(lower_value)
|
37
|
+
matrix.row(index).to_a
|
38
|
+
when :second
|
39
|
+
index = find_vector(:column).index(higher_value)
|
40
|
+
matrix.column(index).to_a
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def lower_value
|
45
|
+
find_vector(:row).max
|
46
|
+
end
|
47
|
+
|
48
|
+
def higher_value
|
49
|
+
find_vector(:column).min
|
50
|
+
end
|
51
|
+
|
52
|
+
def find_vector(vector)
|
53
|
+
case vector
|
54
|
+
when :row
|
55
|
+
(0...count_rows).map { |number| find_extremum(number, :min) }
|
56
|
+
when :column
|
57
|
+
(0...count_columns).map { |number| find_extremum(number, :max) }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def find_extremum(number, direction)
|
62
|
+
case direction
|
63
|
+
when :min then matrix.row(number).min
|
64
|
+
when :max then matrix.column(number).max
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ropt
|
4
|
+
module CooperativeGames
|
5
|
+
# class for get coefficients of Shapley Value with limit of number of gamers equal 171
|
6
|
+
class CoalitionsCoefficients
|
7
|
+
attr_accessor :number_gamers
|
8
|
+
|
9
|
+
LIMIT_VALUE = 171
|
10
|
+
|
11
|
+
def initialize(number_gamers)
|
12
|
+
@number_gamers = number_gamers.to_f
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
validate_number
|
17
|
+
coefficients if number_gamers == coefficients.size
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def coefficients
|
23
|
+
(0...number_gamers).map do |coalition_power|
|
24
|
+
numerator = factorial(coalition_power) * factorial(number_gamers - coalition_power - 1)
|
25
|
+
numerator.to_f / factorial(number_gamers)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def factorial(number)
|
30
|
+
(1..number).inject(1, :*)
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate_number
|
34
|
+
return unless number_gamers >= LIMIT_VALUE
|
35
|
+
|
36
|
+
raise Error,
|
37
|
+
"The gamers is #{LIMIT_VALUE} or more. You should reduce the number of gamers"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ropt
|
4
|
+
module CooperativeGames
|
5
|
+
# class for calculating of Shapley Value something super-additive convex cooperative game
|
6
|
+
class ShapleyValue
|
7
|
+
attr_accessor :coalitions, :number_gamers, :coefficients, :validator, :round
|
8
|
+
|
9
|
+
DEFAULT_VALUE_OF_COALITION = 0
|
10
|
+
def initialize(coalitions, number_gamers, round = 3)
|
11
|
+
@coalitions = coalitions
|
12
|
+
@number_gamers = number_gamers
|
13
|
+
@coefficients = Ropt::CooperativeGames::CoalitionsCoefficients.new(number_gamers).call
|
14
|
+
@validator = Ropt::CooperativeGames::Validator.new
|
15
|
+
@round = round
|
16
|
+
end
|
17
|
+
|
18
|
+
def solve
|
19
|
+
validator.validate_arguments(coefficients,
|
20
|
+
coalitions,
|
21
|
+
number_gamers)
|
22
|
+
|
23
|
+
singletons = singleton_coalitions
|
24
|
+
|
25
|
+
validator.validate_coalitions_consistency(coalitions, singletons.keys)
|
26
|
+
|
27
|
+
vector = calculate_vector_shapley(singletons)
|
28
|
+
|
29
|
+
validator.validate_result(coalitions, singletons.keys, vector)
|
30
|
+
|
31
|
+
vector
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def singleton_coalitions
|
37
|
+
coalitions.select { |key, _value| key.size == 1 }
|
38
|
+
end
|
39
|
+
|
40
|
+
def calculate_vector_shapley(singletons)
|
41
|
+
vector = singletons.each_with_object({}) do |singleton, result|
|
42
|
+
name_gamer = singleton.first
|
43
|
+
coalitions_with_gamer = coalitions_with_gamer(name_gamer)
|
44
|
+
|
45
|
+
result.merge!(shapley_value_for_one_gamer(name_gamer, coalitions_with_gamer))
|
46
|
+
end
|
47
|
+
|
48
|
+
aggregate_components(vector)
|
49
|
+
end
|
50
|
+
|
51
|
+
def shapley_value_for_one_gamer(name_gamer, coalitions_with_gamer)
|
52
|
+
cost_gamer = coalitions_with_gamer.flat_map do |coalition, value|
|
53
|
+
if coalition.size == 1
|
54
|
+
coefficients[0] * value
|
55
|
+
else
|
56
|
+
coefficients[coalition.size - 1] * (value - value_coalition_without_gamer(coalition, name_gamer))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
{ name_gamer => cost_gamer }
|
61
|
+
end
|
62
|
+
|
63
|
+
def aggregate_components(vector)
|
64
|
+
vector.transform_values { |component| component.sum.round(round) }
|
65
|
+
end
|
66
|
+
|
67
|
+
def value_coalition_without_gamer(coalition, name_gamer)
|
68
|
+
value = coalitions[coalition.to_s.delete(name_gamer.to_s).to_sym]
|
69
|
+
return value unless value.nil?
|
70
|
+
|
71
|
+
DEFAULT_VALUE_OF_COALITION
|
72
|
+
end
|
73
|
+
|
74
|
+
def coalitions_with_gamer(name_gamer)
|
75
|
+
coalitions.select { |key, _value| key.to_s.include?(name_gamer.to_s) }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ropt
|
4
|
+
module CooperativeGames
|
5
|
+
# class for validation after and before calculating vector shapley
|
6
|
+
class Validator
|
7
|
+
def validate_arguments(coefficients, coalitions, number_gamers)
|
8
|
+
validate_number_gamers(number_gamers)
|
9
|
+
validate_coalitions(coalitions)
|
10
|
+
validate_coefficients(coefficients)
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate_coalitions_consistency(coalitions, names_singletons)
|
14
|
+
value_grand_coalition = value_grand_coalition(coalitions, names_singletons)
|
15
|
+
validate_value_grand_coalition(value_grand_coalition)
|
16
|
+
end
|
17
|
+
|
18
|
+
def validate_result(coalitions, names_singletons, vector)
|
19
|
+
value_grand_coalition = value_grand_coalition(coalitions, names_singletons)
|
20
|
+
validate_vector(value_grand_coalition, vector)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def validate_coefficients(coefficients)
|
26
|
+
raise Error, "Not find coefficients" if coefficients.nil? || coefficients.empty?
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_coalitions(coalitions)
|
30
|
+
raise Error, "Not find coalitions" if coalitions.nil? || coalitions.empty?
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate_number_gamers(number_gamers)
|
34
|
+
raise Error, "Not find number of gamers" if number_gamers.nil?
|
35
|
+
end
|
36
|
+
|
37
|
+
def value_grand_coalition(coalitions, names_singletons)
|
38
|
+
most_big_coalition = coalitions.keys.max_by(&:length)
|
39
|
+
most_big_coalition_by_singles = coalitions.select do |coalition|
|
40
|
+
coalition.size == names_singletons.count
|
41
|
+
end
|
42
|
+
|
43
|
+
return nil if most_big_coalition != most_big_coalition_by_singles.keys.first
|
44
|
+
|
45
|
+
most_big_coalition_by_singles.values.first
|
46
|
+
end
|
47
|
+
|
48
|
+
def validate_value_grand_coalition(value)
|
49
|
+
return if value
|
50
|
+
|
51
|
+
raise Error,
|
52
|
+
"Not find value of grand coalition or several singletons coalitions"
|
53
|
+
end
|
54
|
+
|
55
|
+
def validate_vector(value_grand_coalition, vector)
|
56
|
+
return if vector.values.sum.round(2) == value_grand_coalition.to_f.round(2)
|
57
|
+
|
58
|
+
raise Error,
|
59
|
+
"Sum components of Shapley vector is not equal value of grand coalition"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "matrix"
|
4
|
+
require "ropt/vector_matrix_math/shell_vector"
|
5
|
+
|
6
|
+
module Ropt
|
7
|
+
module VectorMatrixMath
|
8
|
+
# class-wrapper for converting arrays to objects of vectors with option of inner product
|
9
|
+
class OperationVectors
|
10
|
+
attr_accessor :vector1, :vector2
|
11
|
+
|
12
|
+
def initialize(vector1, vector2)
|
13
|
+
@vector1 = ShellVector.create(vector1)
|
14
|
+
@vector2 = ShellVector.create(vector2)
|
15
|
+
end
|
16
|
+
|
17
|
+
def inner_product
|
18
|
+
vector1.inner_product(vector2)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "matrix"
|
4
|
+
|
5
|
+
module Ropt
|
6
|
+
module VectorMatrixMath
|
7
|
+
# class-wrapper for converting numerics objects to vector
|
8
|
+
class ShellVector < Vector
|
9
|
+
def self.create(components_vector)
|
10
|
+
Vector.send(:new, components_vector)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/ropt/version.rb
ADDED
data/lib/ropt.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "ropt/version"
|
4
|
+
|
5
|
+
# module for integrate all solvers of optimization problems
|
6
|
+
module Ropt
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
require_relative "ropt/vector_matrix_math/operation_vectors"
|
10
|
+
require_relative "ropt/antagonistic_games/matrix_game"
|
11
|
+
require_relative "ropt/cooperative_games/coalitions_coefficients"
|
12
|
+
require_relative "ropt/cooperative_games/shapley_value"
|
13
|
+
require_relative "ropt/cooperative_games/validator"
|
14
|
+
end
|
data/ropt.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/ropt/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ropt"
|
7
|
+
spec.version = Ropt::VERSION
|
8
|
+
spec.authors = ["Lev Pruglo "]
|
9
|
+
spec.email = ["lev.pruglo@mail.ru"]
|
10
|
+
|
11
|
+
spec.summary = "Library for mathematical optimization"
|
12
|
+
spec.description = "The library for mathematical optimization in Ruby is designed to help solve many problems in the
|
13
|
+
computational, financial, social, and energy fields and, in theory,
|
14
|
+
should include components of game theory, combinatorics, probability theory,
|
15
|
+
linear and nonlinear optimization, cluster, regression, and other analysis."
|
16
|
+
spec.homepage = "https://github.com/Leozack-red"
|
17
|
+
spec.license = "MIT"
|
18
|
+
spec.required_ruby_version = ">= 2.7.0"
|
19
|
+
spec.metadata = { "rubygems_mfa_required" => "true" }
|
20
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
21
|
+
spec.metadata["source_code_uri"] = "https://github.com/Leozack-red/ropt"
|
22
|
+
spec.metadata["changelog_uri"] = "https://github.com/Leozack-red/ropt/blob/master/CHANGELOG.md"
|
23
|
+
|
24
|
+
# Specify which files should be added to the gem when it is released.
|
25
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
26
|
+
spec.files = Dir.chdir(__dir__) do
|
27
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
spec.extra_rdoc_files = ["README.md"]
|
35
|
+
|
36
|
+
spec.add_dependency "matrix"
|
37
|
+
|
38
|
+
# For more information and examples about making a new gem, check out our
|
39
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
40
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
41
|
+
end
|
data/sig/ropt.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ropt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- 'Lev Pruglo '
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-02-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: matrix
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: |-
|
28
|
+
The library for mathematical optimization in Ruby is designed to help solve many problems in the
|
29
|
+
computational, financial, social, and energy fields and, in theory,
|
30
|
+
should include components of game theory, combinatorics, probability theory,
|
31
|
+
linear and nonlinear optimization, cluster, regression, and other analysis.
|
32
|
+
email:
|
33
|
+
- lev.pruglo@mail.ru
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files:
|
37
|
+
- README.md
|
38
|
+
files:
|
39
|
+
- ".rspec"
|
40
|
+
- ".rubocop.yml"
|
41
|
+
- CHANGELOG.md
|
42
|
+
- CODE_OF_CONDUCT.md
|
43
|
+
- Gemfile
|
44
|
+
- Gemfile.lock
|
45
|
+
- LICENSE.txt
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- lib/ropt.rb
|
49
|
+
- lib/ropt/antagonistic_games/matrix_game.rb
|
50
|
+
- lib/ropt/cooperative_games/coalitions_coefficients.rb
|
51
|
+
- lib/ropt/cooperative_games/shapley_value.rb
|
52
|
+
- lib/ropt/cooperative_games/validator.rb
|
53
|
+
- lib/ropt/vector_matrix_math/operation_vectors.rb
|
54
|
+
- lib/ropt/vector_matrix_math/shell_matrix.rb
|
55
|
+
- lib/ropt/vector_matrix_math/shell_vector.rb
|
56
|
+
- lib/ropt/version.rb
|
57
|
+
- ropt.gemspec
|
58
|
+
- sig/ropt.rbs
|
59
|
+
homepage: https://github.com/Leozack-red
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata:
|
63
|
+
rubygems_mfa_required: 'true'
|
64
|
+
homepage_uri: https://github.com/Leozack-red
|
65
|
+
source_code_uri: https://github.com/Leozack-red/ropt
|
66
|
+
changelog_uri: https://github.com/Leozack-red/ropt/blob/master/CHANGELOG.md
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.7.0
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.5.6
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Library for mathematical optimization
|
86
|
+
test_files: []
|