Last active
January 13, 2020 15:26
-
-
Save Sagebati/b84db3e31e0f4021ea75b6e7d2da5358 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| """ | |
| source : https://www.irit.fr/~Leila.Amgoud/IJAR-2018.pdf | |
| """ | |
| from enum import auto | |
| def h_product(x1, x2): | |
| return x1 * x2 | |
| def g_sum(neighbors, h): | |
| res = 0.0 | |
| for (weight_node, weight_edge) in neighbors: | |
| res += h(weight_node, weight_edge) | |
| return res | |
| def f_h_cat(poid_noeud, neighbors, g, h): | |
| return poid_noeud / (1 + g(neighbors, h)) | |
| class H(auto): | |
| PRODUCT = h_product | |
| class G(auto): | |
| SUM = g_sum | |
| class F(auto): | |
| H_Cat = f_h_cat | |
| def calculate(graph): | |
| current_iter = {} | |
| # Init | |
| for node in list(graph.nodes): | |
| current_iter[node] = graph.nodes[node]['weight'] | |
| for i in range(15): | |
| next_iter = {} | |
| print(current_iter) | |
| for node in list(graph.nodes): | |
| predecessors_weights = list( | |
| map( | |
| # (node weight, edge weight) for each predecessor to node | |
| lambda n: (current_iter[n], graph.edges[n, node]['weight']), graph.predecessors(node) | |
| ) | |
| ) | |
| next_iter[node] = F.H_Cat(graph.nodes[node]['weight'], predecessors_weights, | |
| G.SUM, H.PRODUCT) | |
| current_iter = next_iter | |
| if __name__ == '__main__': | |
| import networkx as nx | |
| g = nx.DiGraph() | |
| g.add_node('a', weight=1) | |
| g.add_node('b', weight=1) | |
| g.add_node('c', weight=1) | |
| g.add_node('d', weight=1) | |
| g.add_node('e', weight=1) | |
| g.add_edge('a', 'b', weight=1) | |
| g.add_edge('a', 'e', weight=1) | |
| g.add_edge('e', 'a', weight=1) | |
| g.add_edge('e', 'c', weight=1) | |
| g.add_edge('e', 'd', weight=1) | |
| calculate(g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment