def testRGA7x7(self): test = RGA("testThreeConnections.csv", "testThreeIG.txt", 5, 2) gainmatrix = test.openloopgainmatrix.transpose() variables = test.variablecorrection expectedgainmatrix = [ 1, 0, 0, 0, 11, 0, 0, 1, 0, 0, 0, 13, 0, 0, 2, 5, 0, 0, 0, 0, 3, 7, 0, 0, 0, 0, 0, 0, 19, 17 ] expectedvariables = [0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0] for testelement, element in zip(gainmatrix.flat, expectedgainmatrix): self.assertAlmostEquals(testelement, element, 1) for testelement, element in zip(variables.flat, expectedvariables): self.assertAlmostEquals(testelement, element, 1)
def __init__(self, variables, localdiff, numberofinputs, fgainmatrix, fconnectionmatrix, fvariablenames, bgainmatrix, bconnectionmatrix, bvariablenames, normalgains, normalconnections, controlvarsforRGA=None): """This constructor will create an RGABristol object so that you simply have to call the display method to see which pairings should be made. It will also create 6 different ranking systems. Note that variablenames is not the same as variables!! There is a formatting difference. ASSUME: the first rows are the inputs up to numberofinputs""" self.bristol = RGA(variables, localdiff, numberofinputs, controlvarsforRGA) self.forwardgain = gRanking(self.normaliseMatrix(fgainmatrix), fvariablenames) self.gfgain = gRanking(self.normaliseMatrix(fconnectionmatrix), fvariablenames) self.backwardgain = gRanking(self.normaliseMatrix(bgainmatrix), bvariablenames) self.gbgain = gRanking(self.normaliseMatrix(bconnectionmatrix), bvariablenames) self.normalforwardgain = gRanking(self.normaliseMatrix(normalgains), variables) self.normalbackwardgain = gRanking( self.normaliseMatrix(transpose(normalgains)), variables) self.normalforwardgoogle = gRanking( self.normaliseMatrix(normalconnections), variables) self.listofinputs = variables[:numberofinputs] self.listofoutputs = variables[numberofinputs:]
def testRGA4x4(self): #this test a very simple 4x4 system #this is the only system where i could verify that the RGA is correctly calculated #mostly because the inverse is easy to calculate test = RGA("testOneConnections.csv", "testOneIG.txt", 4, 2) gainmatrix = test.openloopgainmatrix.transpose( ) #this is transposed for convenience variables = test.variablecorrection rga = test.bristolmatrix expectedgainmatrix = [2, 5, 3, 7] expectedvariables = [0, 1, 0, 1, 1, 0, 1, 0] expectedrga = [-14, 15, 15, -14] for testelement, element in zip(gainmatrix.flat, expectedgainmatrix): self.assertAlmostEquals(testelement, element, 1) for testelement, element in zip(variables.flat, expectedvariables): self.assertAlmostEquals(testelement, element, 1) for testelement, element in zip(rga.flat, expectedrga): self.assertAlmostEquals(testelement, element, 1)
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""" temp = localgains("btest3.csv", "btest3fix.txt", 3) specialdiff = temp.localdiffmatrix bristol = RGA(variablenames, specialdiff, 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]) edgecolorlist = [] for element in G1.edges(): found = 0 for pair in pairlist:
localgaindict[(variablenames[u],variablenames[v])] = localgainmatrix[u,v] posdict = {variablenames[0]: array([0.5,2]), variablenames[1]: array([0.5,1]), variablenames[2]: array([7,2]), variablenames[3]: array([7,1]), variablenames[4]: array([10,1.5]), variablenames[5]: array([0.5,3]), variablenames[6]: array([0.5,0]), variablenames[7]: array([7,3]), variablenames[8]: array([7,0])} #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]) edgecolorlist = [] for element in G1.edges(): found = 0 for pair in pairlist:
@author: St Elmo Wilken """ from localGainCalculator import localgains from gainRank import gRanking from RGABristol import RGA import numpy as np import matplotlib.pyplot as plt test1 = localgains("testFourConnections.csv", "testFourIG.txt", 5) gainm = test1.normaliseGainMatrix(test1.linlocalgainmatrix) varm = test1.variables googlem = test1.normaliseGainMatrix(test1.connectionmatrix) test2 = gRanking(googlem, varm) #print(test2.sortedRankingsKey) #print(test2.sortedRankingsValue) #test2.showConnectRank() localdiffsm = test1.localdiffmatrix test3 = RGA(varm, localdiffsm, 5, 3) #remember to change me for each case!!! #print(test3.pairedvariables) haha = test3.bristolmatrix print(haha) #print(test3.openloopmatrix) np.savetxt("rgatest.txt",haha) plt.matshow(haha) plt.show()