Statistics
| Branch: | Revision:

iof-tools / mrai_setter / test_mrai_setter.py @ 62798f6e

History | View | Annotate | Download (8.22 KB)

1
import networkx as nx
2
import mrai_setter as pg
3

    
4

    
5
def almost_equal(x, y, eps=0.05):
6
    if abs(x - y) < eps:
7
        return True
8
    return False
9

    
10

    
11
class TestStrategyGenerator():
12
    @classmethod
13
    def setup_class(cls):
14
        cls.small_saw = nx.Graph()
15
        cls.small_saw.add_edge('x1', 'x2', type='transit', customer='x1')
16
        cls.small_saw.add_edge('x2', 'x3', type='transit', customer='x2')
17

    
18
        cls.small_saw.add_edge('x1', 'y1', type='transit', customer='x1')
19
        cls.small_saw.add_edge('y1', 'x2', type='transit', customer='y1')
20
        cls.small_saw.add_edge('x2', 'y2', type='transit', customer='x2')
21
        cls.small_saw.add_edge('y2', 'x3', type='transit', customer='y2')
22

    
23
        cls.saw = nx.Graph()
24
        cls.saw.add_edge('x1', 'x2', type='transit', customer='x1')
25
        cls.saw.add_edge('x2', 'x3', type='transit', customer='x2')
26
        cls.saw.add_edge('x3', 'x4', type='transit', customer='x3')
27

    
28
        cls.saw.add_edge('x1', 'y1', type='transit', customer='x1')
29
        cls.saw.add_edge('y1', 'x2', type='transit', customer='y1')
30
        cls.saw.add_edge('x2', 'y2', type='transit', customer='x2')
31
        cls.saw.add_edge('y2', 'x3', type='transit', customer='y2')
32
        cls.saw.add_edge('x3', 'y3', type='transit', customer='x3')
33
        cls.saw.add_edge('y3', 'x4', type='transit', customer='y3')
34

    
35
        cls.chain = nx.Graph()
36
        cls.chain.add_edge('x1', 'x2', type='transit', customer='x1')
37
        cls.chain.add_edge('x2', 'x3', type='transit', customer='x2')
38
        cls.chain.add_edge('x3', 'x4', type='transit', customer='x3')
39

    
40
        cls.chain.add_edge('x1', 'y12', type='transit', customer='x1')
41
        cls.chain.add_edge('y12', 'y11', type='transit', customer='y12')
42
        cls.chain.add_edge('y11', 'y10', type='transit', customer='y11')
43
        cls.chain.add_edge('y10', 'x2', type='transit', customer='y10')
44

    
45
        cls.chain.add_edge('x1', 'z13', type='transit', customer='x1')
46
        cls.chain.add_edge('z13', 'y12', type='transit', customer='z13')
47
        cls.chain.add_edge('y12', 'z12', type='transit', customer='y12')
48
        cls.chain.add_edge('z12', 'y11', type='transit', customer='z12')
49
        cls.chain.add_edge('y11', 'z11', type='transit', customer='y11')
50
        cls.chain.add_edge('z11', 'y10', type='transit', customer='z11')
51
        cls.chain.add_edge('y10', 'z10', type='transit', customer='y10')
52
        cls.chain.add_edge('z10', 'x2', type='transit', customer='z10')
53

    
54
        cls.chain.add_edge('x2', 'y22', type='transit', customer='x2')
55
        cls.chain.add_edge('y22', 'y21', type='transit', customer='y22')
56
        cls.chain.add_edge('y21', 'y20', type='transit', customer='y21')
57
        cls.chain.add_edge('y20', 'x3', type='transit', customer='y20')
58

    
59
        cls.chain.add_edge('x2', 'z23', type='transit', customer='x2')
60
        cls.chain.add_edge('z23', 'y22', type='transit', customer='z23')
61
        cls.chain.add_edge('y22', 'z22', type='transit', customer='y22')
62
        cls.chain.add_edge('z22', 'y21', type='transit', customer='z22')
63
        cls.chain.add_edge('y21', 'z21', type='transit', customer='y21')
64
        cls.chain.add_edge('z21', 'y20', type='transit', customer='z21')
65
        cls.chain.add_edge('y20', 'z20', type='transit', customer='y20')
66
        cls.chain.add_edge('z20', 'x3', type='transit', customer='z20')
67

    
68
        cls.chain.add_edge('x3', 'y32', type='transit', customer='x3')
69
        cls.chain.add_edge('y32', 'y31', type='transit', customer='y32')
70
        cls.chain.add_edge('y31', 'y30', type='transit', customer='y31')
71
        cls.chain.add_edge('y30', 'x4', type='transit', customer='y30')
72

    
73
        cls.chain.add_edge('x3', 'z33', type='transit', customer='x3')
74
        cls.chain.add_edge('z33', 'y32', type='transit', customer='z33')
75
        cls.chain.add_edge('y32', 'z32', type='transit', customer='y32')
76
        cls.chain.add_edge('z32', 'y31', type='transit', customer='z32')
77
        cls.chain.add_edge('y31', 'z31', type='transit', customer='y31')
78
        cls.chain.add_edge('z31', 'y30', type='transit', customer='z31')
79
        cls.chain.add_edge('y30', 'z30', type='transit', customer='y30')
80
        cls.chain.add_edge('z30', 'x4', type='transit', customer='z30')
81

    
82
    def test_fabrikant_levels(self):
83
        levels = pg.fabrikant_levels(self.small_saw, 'x1')
84
        assert (levels['x1'] == 0)
85
        assert (levels['x2'] == 1)
86
        assert (levels['x3'] == 2)
87
        assert (levels['y1'] == 0)
88
        assert (levels['y2'] == 1)
89
        levels = pg.fabrikant_levels(self.saw, 'x1')
90
        assert (levels['x1'] == 0)
91
        assert (levels['x2'] == 1)
92
        assert (levels['x3'] == 2)
93
        assert (levels['x4'] == 3)
94
        assert (levels['y1'] == 0)
95
        assert (levels['y2'] == 1)
96
        assert (levels['y3'] == 2)
97
        levels = pg.fabrikant_levels(self.chain, 'x1')
98
        assert (levels['x1'] == 0)
99
        assert (levels['x2'] == 1)
100
        assert (levels['x3'] == 2)
101
        assert (levels['x4'] == 3)
102
        assert (levels['y12'] == 0)
103
        assert (levels['y11'] == 0)
104
        assert (levels['y10'] == 0)
105
        assert (levels['z13'] == 0)
106
        assert (levels['z12'] == 0)
107
        assert (levels['z11'] == 0)
108
        assert (levels['z10'] == 0)
109
        assert (levels['y22'] == 1)
110
        assert (levels['y21'] == 1)
111
        assert (levels['y20'] == 1)
112
        assert (levels['z23'] == 1)
113
        assert (levels['z22'] == 1)
114
        assert (levels['z21'] == 1)
115
        assert (levels['z20'] == 1)
116
        assert (levels['y32'] == 2)
117
        assert (levels['y31'] == 2)
118
        assert (levels['y30'] == 2)
119
        assert (levels['z33'] == 2)
120
        assert (levels['z32'] == 2)
121
        assert (levels['z31'] == 2)
122
        assert (levels['z30'] == 2)
123

    
124
    def get_node_mrai(self, G, node):
125
        ss = 0
126
        cnt = 0
127
        for j in nx.neighbors(G, node):
128
            e = G.edges[(node, j)]
129
            if e['termination1'] == node:
130
                ss += e['mrai1']
131
            else:
132
                ss += e['mrai2']
133
            cnt += 1
134
        return ss / cnt
135

    
136
    def test_fabrikant_strategy(self):
137
        pg.apply_fabrikant_strategy(self.small_saw, 'x1')
138
        almost_equal(self.get_node_mrai(self.small_saw, 'x1'), 30)
139
        almost_equal(self.get_node_mrai(self.small_saw, 'x2'), 15)
140
        almost_equal(self.get_node_mrai(self.small_saw, 'x3'), 7.5)
141
        almost_equal(self.get_node_mrai(self.small_saw, 'y1'), 30)
142
        almost_equal(self.get_node_mrai(self.small_saw, 'y2'), 15)
143

    
144
        pg.apply_fabrikant_strategy(self.saw, 'x1')
145
        almost_equal(self.get_node_mrai(self.saw, 'x1'), 30)
146
        almost_equal(self.get_node_mrai(self.saw, 'x2'), 15)
147
        almost_equal(self.get_node_mrai(self.saw, 'x3'), 7.5)
148
        almost_equal(self.get_node_mrai(self.saw, 'x4'), 3.75)
149
        almost_equal(self.get_node_mrai(self.saw, 'y1'), 30)
150
        almost_equal(self.get_node_mrai(self.saw, 'y2'), 15)
151
        almost_equal(self.get_node_mrai(self.saw, 'y3'), 7.5)
152

    
153
    def test_constantfabrikant_strategy(self):
154
        pg.apply_constantfabrikant_strategy(self.small_saw, 'x1')
155
        almost_equal(self.get_node_mrai(self.small_saw, 'x1'), 30)
156
        almost_equal(self.get_node_mrai(self.small_saw, 'x2'), 24)
157
        almost_equal(self.get_node_mrai(self.small_saw, 'x3'), 19.2)
158
        almost_equal(self.get_node_mrai(self.small_saw, 'y1'), 30)
159
        almost_equal(self.get_node_mrai(self.small_saw, 'y2'), 24)
160

    
161
    def test_constantinversefabrikant_strategy(self):
162
        pg.apply_constantinversefabrikant_strategy(self.small_saw, 'x1')
163
        almost_equal(self.get_node_mrai(self.small_saw, 'x1'), 15.36)
164
        almost_equal(self.get_node_mrai(self.small_saw, 'x2'), 19.2)
165
        almost_equal(self.get_node_mrai(self.small_saw, 'x3'), 24)
166
        almost_equal(self.get_node_mrai(self.small_saw, 'y1'), 15.36)
167
        almost_equal(self.get_node_mrai(self.small_saw, 'y2'), 19.2)
168

    
169
    def test_simpleheuristic_strategy(self):
170
        pg.apply_simpleheuristic_strategy(self.saw, 'x1')
171
        almost_equal(self.get_node_mrai(self.saw, 'x1'), 0)
172
        almost_equal(self.get_node_mrai(self.saw, 'x2'), 0.5)
173
        almost_equal(self.get_node_mrai(self.saw, 'x3'), 1)
174
        almost_equal(self.get_node_mrai(self.saw, 'x4'), 1.5)
175
        almost_equal(self.get_node_mrai(self.saw, 'y1'), 0.5)
176
        almost_equal(self.get_node_mrai(self.saw, 'y2'), 1)
177
        almost_equal(self.get_node_mrai(self.saw, 'y3'), 1.5)