forked from stelmo/LoopRanking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btest1.py
157 lines (119 loc) · 4.93 KB
/
btest1.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 13 19:51:59 2012
@author: St Elmo Wilken
"""
"""System Description:
This is a simple 2 input 2 output system"""
"""All the imports"""
from RGABristol import RGA
from gainRank import gRanking
from localGainCalculator import localgains
import networkx as nx
import matplotlib.pyplot as plt
from numpy import array, transpose
"""Test system one: pairings should be obvious"""
localdata = localgains("btest1.csv","btest1ObviousConnections.txt",3)
G = nx.DiGraph()
connectionmatrix = localdata.connectionmatrix
localgainmatrix = localdata.linlocalgainmatrix
variablenames = localdata.variables
localgaindict = dict()
"""Displays the causality with calculated local gains"""
for u in range(localdata.n):
for v in range(localdata.n):
if (connectionmatrix[u,v]==1):
G.add_edge(variablenames[v], variablenames[u])
localgaindict[(variablenames[u],variablenames[v])] = localgainmatrix[u,v]
posdict = {variablenames[0]: array([1,1]), variablenames[1]: array([1,2]), variablenames[2]: array([4,1]), variablenames[3]: array([4,2])} #position dictionary
plt.figure(1)
plt.subplot(311)
plt.suptitle("RGA Implementation",size='x-large')
nx.draw_networkx(G, pos=posdict)
nx.draw_networkx_edge_labels(G,pos=posdict,edge_labels=localgaindict,label_pos=0.3)
nx.draw_networkx_edges(G,pos=posdict,width=5.0,edge_color='k', style='solid',alpha=0.5)
nx.draw_networkx_nodes(G,pos=posdict, node_color='y',node_size=900)
plt.axis('off') #it refuses to plot the ylabels if the grid is off... but it looks better with the grid off...
plt.ylabel("Open Loop Local Gains")
"""Displays the Bristol Matrix"""
"""First plot will show the connections of the max in each column"""
bristol = RGA(variablenames,localdata.localdiffmatrix,2)
spam = bristol.bristolmatrix
print(spam)
G1 = nx.DiGraph()
G1 = G.copy() #this is to prevent displaying a mix of max/0.5 pairs
pairlist = []
for row in bristol.pairedvariablesMax:
pairlist.append((row[0],row[1]))
G1.add_edge(row[0],row[1])
print(pairlist)
edgecolorlist = []
for element in G1.edges():
found = 0
for pair in pairlist:
if element==pair:
found = 1
if found==1:
edgecolorlist.append("r")
else:
edgecolorlist.append("k")
plt.subplot(312)
nx.draw_networkx(G1, pos=posdict)
nx.draw_networkx_edges(G1,pos=posdict,width=5.0,edge_color=edgecolorlist, style='solid',alpha=0.5)
nx.draw_networkx_nodes(G1,pos=posdict, node_color='y',node_size=900)
plt.axis('off')
plt.ylabel("Separation point = maximum value in column")
"""Second plot will show the connections if and only if the the column has an entry bigger than 0.5"""
pairlist = []
for row in bristol.pairedvariablesHalf:
pairlist.append((row[0],row[1]))
G.add_edge(row[0],row[1])
edgecolorlist = []
for element in G.edges():
found = 0
for pair in pairlist:
if element==pair:
found = 1
if found==1:
edgecolorlist.append("r")
else:
edgecolorlist.append("k")
plt.subplot(313)
nx.draw_networkx(G, pos=posdict)
nx.draw_networkx_edges(G,pos=posdict,width=5.0,edge_color=edgecolorlist, style='solid',alpha=0.5)
nx.draw_networkx_nodes(G,pos=posdict, node_color='y',node_size=900)
plt.axis('off')
plt.ylabel("Separation point = 0.5")
plt.figure("RGA")
plt.imshow(bristol.bristolmatrix, interpolation='nearest',extent=[0,1,0,1]) #need to fix this part!!! it looks ugly
plt.axis('off')
plt.colorbar()
"""************************************************************************************************************************************"""
"""Eigenvector Approach Time"""
"""Local Gain Ranking"""
forwardgain = gRanking(localdata.normaliseGainMatrix(localgainmatrix), variablenames)
backwardgain = gRanking(localdata.normaliseGainMatrix(transpose(localgainmatrix)),variablenames)
gfgain = gRanking(localdata.normaliseGainMatrix(connectionmatrix), variablenames)
gbgain = gRanking(localdata.normaliseGainMatrix(transpose(connectionmatrix)),variablenames)
frankdict = forwardgain.rankDict
brankdict = backwardgain.rankDict
gfdict = gfgain.rankDict
gbdict = gbgain.rankDict
print(frankdict)
print(brankdict)
print(gfdict)
print(gbdict)
plt.figure("Weight")
H = nx.DiGraph()
transconnect = transpose(connectionmatrix)
weights = dict()
for u in range(localdata.n):
for v in range(localdata.n):
if transconnect[u,v] == 1:
H.add_edge(variablenames[v],variablenames[u], weight = abs(frankdict[variablenames[v]]/gfdict[variablenames[v]]-brankdict[variablenames[u]]/gbdict[variablenames[u]]))
weights[(variablenames[v],variablenames[u])] = abs(frankdict[variablenames[v]]/gfdict[variablenames[v]]-brankdict[variablenames[u]]/gbdict[variablenames[u]])
nx.draw_networkx(H,pos=posdict)
nx.draw_networkx_edge_labels(H, pos=posdict,edge_labels=weights, style='solid',alpha=0.5, width = 0.5, label_pos= 0.3)
nx.draw_networkx_nodes(G,pos=posdict, node_color='y',node_size=900)
plt.axis("off")
plt.show()