-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import networkx as nx | ||
import numpy as np | ||
from fractions import Fraction | ||
|
||
|
||
def choquet_value_frac(g, v, r: Fraction): | ||
# Copy graph, because we will make changes in it's structure | ||
g = g.copy() | ||
|
||
# Количество вершин и количество ребер | ||
N = len(g.nodes) | ||
E = len(g.edges) | ||
|
||
# Def f | ||
# In our case it's r in power of closes path | ||
paths = dict(nx.shortest_path(g)) | ||
|
||
def f(s, t): | ||
return r ** Fraction(len(paths[s][t]) - 1) | ||
|
||
# Calculate f for all nodes | ||
Fv = {n: f(v, n) for n in sorted(g.nodes)} | ||
|
||
# Sort values asc, saving nodes names | ||
Fv = dict(sorted(Fv.items(), key=lambda item: (item[1], item[0]))) | ||
|
||
# Re-mapping nodes according to Fv | ||
mapping = dict(zip( | ||
Fv.keys(), | ||
sorted(g.nodes) | ||
)) | ||
|
||
g = nx.relabel_nodes(g, mapping) | ||
Fv = {mapping[i]: Fv[i] for i in Fv} | ||
|
||
# Calc all mu values | ||
muAs = [] | ||
for i in Fv.keys(): | ||
muAs.append(Fraction(len(g.edges), E)) | ||
g.remove_node(i) | ||
|
||
# Adding case f(0) = 0 | ||
Fv[0] = 0 | ||
|
||
# Calculate Shoquet Integral values | ||
R = sum([(Fv[i] - Fv[i - 1]) * muAs[i - 1] for i in range(1, N)]) | ||
return R | ||
|
||
|
||
def choquet_value_frac_all(g): | ||
return [choquet_value_frac(g, i, Fraction(1, 4)) for i in g.nodes] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import numpy as np | ||
from scipy.stats import rankdata | ||
|
||
|
||
def kirchhoff_tournament(a: np.ndarray, delta, round_to=5): | ||
n = len(a) | ||
one = np.full((n, n), 1.0, dtype=float) | ||
w = np.divide(one, a, out=np.zeros_like(one), where=a != 0) | ||
d = np.sum(w, axis=1) | ||
l = -w | ||
np.fill_diagonal(l, d + delta) | ||
|
||
l_inv = np.linalg.inv(l) | ||
t_matrix = np.zeros((n, n)) | ||
b = np.zeros((n, 1), dtype=float) | ||
|
||
for i in range(0, n): | ||
if i > 0: | ||
b[i - 1][0] = 0 | ||
b[i][0] = 1 | ||
phi = np.dot(l_inv, b).sum(axis=1) | ||
|
||
t_matrix[i] = rankdata( | ||
list(map(lambda it: np.round(-it, round_to), phi)), | ||
method='dense' | ||
) | ||
|
||
return t_matrix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import numpy as np | ||
|
||
|
||
def myerson(a, k): | ||
""" | ||
Calculate Modified Myerson-value for given graph | ||
http:https://dx.doi.org/10.1134/S0005117921010100 | ||
a - Adjacency matrix of graph | ||
k - path length | ||
""" | ||
|
||
n = len(a) | ||
a_k = np.empty(k + 1, dtype=np.ndarray) | ||
a_k[0] = a | ||
a_k[1] = a | ||
|
||
for i in range(2, k + 1): | ||
a_k[i] = np.matmul(a_k[i - 1], a) | ||
|
||
a_sums = np.empty(n, dtype=np.ndarray) | ||
for i in range(k + 1): | ||
a_sums[i] = np.sum(a_k[i], axis=1) | ||
|
||
s_k = np.empty(n, dtype=np.int32) | ||
l_sum = np.sum(a_k[k], axis=0) | ||
|
||
for i in range(n): | ||
l_inside = np.empty(n, dtype=np.int32) | ||
for l in range(n): | ||
l_inside[l] = sum( | ||
a_k[r][l, i] * a_sums[k - r][i, 0] | ||
for r in range(1, k) | ||
) | ||
main = l_inside.sum() + l_sum[0, i] | ||
s_k[i] = a_sums[k][i, 0] + main | ||
return s_k |