-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheval_res.py
210 lines (155 loc) · 4.25 KB
/
eval_res.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
'''
This program analyzes the output of infmax, vacc and senti.
It assumes there are files with the name 'fname' in the directories
res/{vacc,infmax,senti}/ that contains output from all three programs.
These files should contain output from all sets of active nodes. For the
example of one link and n = 1, this would be achieved by.
./infmax 1 0 1 0 >> res/infmax/file
./infmax 1 0 1 1 >> res/infmax/file
./vacc 1 0 1 0 >> res/vacc/file
./vacc 1 0 1 1 >> res/vacc/file
./senti 1 0 1 0 >> res/senti/file
./senti 1 0 1 1 >> res/senti/file
python eval_res.py file
the output shows the intervals of beta with distinct optimal sets of vertices
(both by exact algebraic expressions and numerical values, separated by "=")
'''
from sys import argv
from sympy import S, oo, Interval, Poly, N
from sympy.abc import x
from sympy.polys.polytools import poly_from_expr
from sympy.solvers.inequalities import solve_poly_inequality
from itertools import combinations
tol = 1e-5
senses = ['infmax','vacc','senti']
large = 10000
larger = large + 10000
# # # # # # # # # # # # # # # # #
def eliminate (s, t):
global maxsol, maximize
a = maxsol[s]['num'].mul(maxsol[t]['den'])
b = maxsol[t]['num'].mul(maxsol[s]['den'])
p = a.sub(b)
if maximize:
a = solve_poly_inequality(p, '>')
else:
a = solve_poly_inequality(p, '<')
# make a union of intervals
c = []
for b in a:
if b.measure > tol:
if b.sup > 0:
c.append(b)
if len(c) == 0:
maxsol[s]['interval'] = S.EmptySet
return
b = c[0]
for i in range(1,len(c)):
b = b.union(c[i])
# delete the suboptimal regions
a = b.complement(S.Reals)
maxsol[s]['interval'] = maxsol[s]['interval'].intersection(b)
maxsol[t]['interval'] = maxsol[t]['interval'].intersection(a)
# # # # # # # # # # # # # # # # #
# being a bit more generous than just poly_from_expr
def make_poly (s):
try:
a,no = poly_from_expr(s)
except:
if not 'x' in s:
s = s.strip('()')
if s.isdigit():
return Poly(int(s),x)
print "couldn't convert", s, 'to polynomial'
return a
# # # # # # # # # # # # # # # # #
def different (p, s, ii):
for m in senses:
if len(p[m]) == 0:
return True
if ii[m] < len(s[m]):
if set(p[m]) != set(s[m][ii[m]]['nodes']):
return True
return False
# # # # # # # # # # # # # # # # #
def get_sol (d,fname):
global maximize, maxsol
f = open('res/' + d + '/' + fname)
if d == 'infmax':
maximize = True
else:
maximize = False
polys = {}
maxsol = {}
for l in f:
a = l.split(',')
s = a[0].strip()
y = a[1].strip()
if s not in polys.values(): # ignore duplicates
if y in polys:
polys[y].append(s)
else:
polys[y] = [s]
a = y.split('/')
maxsol[y] = {}
maxsol[y]['num'] = make_poly(a[0])
if len(a) > 1:
maxsol[y]['den'] = make_poly(a[1])
else:
maxsol[y]['den'] = Poly(1,x)
maxsol[y]['interval'] = Interval.Ropen(0,oo)
f.close()
for s,t in combinations(maxsol,2):
eliminate(s,t)
solint = []
for s in maxsol:
if maxsol[s]['interval'].is_Union:
a = list(maxsol[s]['interval'].args)
else:
a = [maxsol[s]['interval']]
for b in a:
if b.measure > tol:
solint.append({})
solint[-1]['nodes'] = polys[s]
solint[-1]['beginning'] = b.inf
solint.sort(key = lambda c: c['beginning'])
return solint
# # # # # # # # # # # # # # # # #
if __name__ == "__main__":
if len(argv) < 2:
print 'usage python eval_res.py [file name]'
exit(1)
if '/' in argv[1]:
print 'just give name of file inside directory'
exit()
s = {}
i = {}
prn = {}
for m in senses:
s[m] = get_sol(m,argv[1])
prn[m] = []
i[m] = 0
smallest = 0
while True:
if different(prn,s,i):
print str(smallest), '=', str(N(smallest))
print 'I', str(s['infmax'][i['infmax']]['nodes']) + ', V', str(s['vacc'][i['vacc']]['nodes']) + ', S', str(s['senti'][i['senti']]['nodes'])
for m in senses:
prn[m] = s[m][i[m]]['nodes']
# step up smallest
smallest = larger
adv = []
for m in senses:
k = i[m] + 1
if k < len(s[m]):
a = s[m][k]['beginning']
if a == smallest:
adv.append(m)
elif a < smallest:
smallest = a
adv = [m]
if smallest == larger:
break
for m in adv:
i[m] += 1
# # # # # # # # # # # # # # # # #