-
Notifications
You must be signed in to change notification settings - Fork 2
/
grafo.py
77 lines (66 loc) · 1.96 KB
/
grafo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import numpy as np
import math
import pygraphviz as pgv
import functools
from PIL import Image
def fuzzy( A , B , t):
C = A
for i in range( t ):
#Multiplicacion vector de estados y matriz
C = np.dot(C,B)
#Aplicacion de la normalizacion o parametrizacion
C = np.array([ 1/(1+math.exp(-1*x)) for x in C ])
return C
def outDegree(graph):
result = []
for i in graph:
od = functools.reduce(lambda x,y: abs(x) + abs(y), i )
result.append(od)
return result
def inDegree(graph):
result = np.zeros((1,len(graph)))
for i in graph:
result = np.absolute(result)+np.absolute(i)
return result
def saveDot(grafo, label, name):
tf = open(name, 'w')
tf.write("digraph G {\n")
# imprime nodos
# A [label="King Arthur"]
for i in range(len(label)):
tf.write( "A"+str(i)+ " [label=\""+label[i]+"\"];\n" )
tf.write("\n")
for i in range (len(grafo)):
for j in range (len(grafo)):
if grafo[i][j]!=0:
tf.write("A"+str(i)+" -> A"+ str(j)+" [label=\""+str(grafo[i][j]) +"\"];\n" )
tf.write("\n}")
tf.close()
def loadGraph(path):
B = pgv.AGraph(path)
B.layout(prog = 'circo') # layout with default (neato)
B.draw(path[:-3]+'png') # draw png
label = ["AoF","FP","P","L","EoL"]
file = "Fuzzy"
C = np.array([1,1,1,1,1])
B = np.array([[ 0.0 , 1.0 ,-0.1, 0.8 , 0.0 ],
[ 0.0 , 0.0 , 0.0, 1.0 , 0.0 ],
[-0.2 ,-1.0 , 0.0,-0.2 , 0.0 ],
[ 0.0 , 0.0 , 0.0, 0.0 , 0.0 ],
[ 0.2 , 0.5 ,-0.5,-0.2 , 0.0 ]
])
C = fuzzy( C ,B , t = 20 )
saveDot(B, label, name = file+".dot")
loadGraph(file+".dot")
print("\nInput : Mapa Cognitivo Difuso ")
print(B)
print("Output : Dregree ")
od = outDegree(B)
id = inDegree(B)
cd = od+id
print("Out Degree", od)
print("In Degree", id)
print("Centrality Degree", cd)
print("\n\n Vector de estados",C)
im = Image.open(file+".png")
im.show()