-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMSet_DB.sage
277 lines (230 loc) · 10.3 KB
/
MMSet_DB.sage
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Class: MMSet_DB
#
# Author: Jacob Hartzer, Timo de wolff
# Last Edited: 9 - September - 2017
#
# Class written for the analysis of maximal mediated set databases
#
###############################################################################
import os
base_path = "/media/sf_GIT/Mediated-Sets/DB/"
class MMSet_DB:
'''Class for creating maximal mediated sets databases'''
def __init__(self):
"""Construct a Mediated Set database object."""
def load_data_table(self,filename):
if not os.path.isfile(os.path.join(base_path, filename)):
return 'Error: File does not exist'
else:
self.path = os.path.join(base_path, filename)
self.open_db()
self.conn.commit()
self.close_db()
self.database_computed = true
def create_new_table(self, dim, degree):
self.dim = dim
self.degree = degree
filename = "DB_dimension_%s_degree_%s.db" % (dim,degree)
self.path = os.path.join(base_path, filename)
if not os.path.isfile(os.path.join(base_path, filename)):
self.open_db()
c = self.conn.cursor()
c.execute('''CREATE TABLE MMSet (Vertices TEXT, MMSet TEXT, 'Max Set Size' INTEGER, 'MMSet size' INTEGER, 'Min Set Size' INTEGER, Percent TEXT, Degree INTEGER, Orbits INTEGER)''')
self.conn.commit()
self.database_computed = false
else:
print 'Table already exists'
self.load_data_table(filename)
print 'Existing table loaded'
self.close_db()
def open_db(self):
self.conn = sqlite3.connect(self.path)
def close_db(self):
self.conn.close()
def add_mmset(self,Vertices,MMSet,Max,SetSize,Min,Degree,Orbits):
'''Adds element to the MMSet database table'''
c = self.conn.cursor()
if Max == Min:
percent = str('N/A')
else:
percent = str(float((SetSize-Min)/(Max-Min)*100))
addSet = (str(Vertices),str(MMSet),int(Max),int(SetSize),int(Min),\
percent,int(Degree),int(Orbits))
c.execute("INSERT INTO MMSet VALUES (?,?,?,?,?,?,?,?)", addSet)
self.conn.commit()
def compute_dimension_table(self):
'''Creates a database table for a given dimension up to a certain degree.
Generates power set and removes all rotations and counts orbits. Lists
each unique Mediated Set and its maximal set, maximal set size, minimal set
size, and orbits.'''
if self.database_computed == false:
files = os.listdir(base_path)
min_degree = 0
self.open_db()
c = self.conn.cursor()
for file in files:
dim_index = file.find('dimension_')
deg_index = file.find('degree_')
if self.dim == int(file[dim_index+10:deg_index-1]):
file_deg = int(file[deg_index+7:len(file)-3])
if self.degree < file_deg:
print 'Warning: Table of equal or higher degree already exists.'
return
elif self.degree != file_deg and min_degree < file_deg:
min_degree = file_deg
if min_degree != 0:
filename = "DB_dimension_%s_degree_%s.db" % (self.dim,min_degree)
filepath = os.path.join(base_path, filename)
dbconn = sqlite3.connect(filepath)
c2 = dbconn.cursor()
c2.execute('SELECT * FROM MMSet')
for set in c2.fetchall():
c.execute("INSERT INTO MMSet VALUES (?,?,?,?,?,?,?,?)", set)
t_start = time.time()
standard_points = []
for i in range(1,self.dim+1):
point = [0,]*self.dim
point[i-1] = self.degree/2
standard_points.append(point)
standard_points.append([0,]*self.dim)
all_points = np.array(LatticePolytope_PPL(standard_points).integral_points()).tolist()
for i in range(len(all_points)):
for j in range(len(all_points[0])):
all_points[i][j] = (all_points[i][j] * 2)
all_points.remove([0]*self.dim)
self._power_set(all_points,min_degree)
c.execute('SELECT Sum(Orbits) FROM MMSet')
num_sets = c.fetchone()
total_time = time.time()-t_start
print num_sets[0], 'MMSets analyzed in', total_time, 'seconds'
self.close_db()
self.database_computed = true
def _power_set(self,all_points,min_degree):
'''Creates the power set of all possible simplicies in dimension
n up to a given degree in any direction along with the number of repeats for
each unique set of points.'''
ticker = range(0,self.dim)
n = 1
c = self.conn.cursor()
t0 = time.time()
while true:
if n % 100 == 0:
if time.time()-t0 > 60:
print '%i Point Sets Checked' %n
t0 = t0+60
n = n + 1
base_simplex = self._find_base_simplex(ticker,all_points)
points = self._index_to_points(list(set(base_simplex)),all_points)
if matrix_rank(points) == self.dim:
points.sort()
degree = max(map(sum, points))
if degree > min_degree:
entry = str([[0,]*self.dim,] + points)
c.execute('SELECT Orbits FROM MMSet WHERE Vertices =?', (entry,))
new_orbit = c.fetchone()
if new_orbit is None:
M = MaxMediatedSet([[0,]*self.dim,] + points)
MMSet = M.compute_maxmediatedset()
self.add_mmset(str([[0,]*self.dim,] + points),str(MMSet),len(M.lattice),len(MMSet),M.minimal_set_size(),degree,1)
else:
new_orbit = int(new_orbit[0] + 1)
c.execute('UPDATE MMSet SET Orbits = ? WHERE Vertices=?',(new_orbit,entry))
self.conn.commit()
if ticker[self.dim-1] == len(all_points) -1:
for i in range(self.dim-2,-1,-1):
if ticker[i] + 1 != ticker[i+1]:
break
if i == 0 and ticker[0] +1 == ticker[1]:
break
ticker[i] = ticker[i] + 1
ticker[i+1:self.dim] = range(ticker[i]+1,ticker[i] + self.dim - i)
ticker[self.dim-1] = ticker[self.dim-1] -1
ticker[self.dim-1] = ticker[self.dim-1] +1
def _find_base_simplex(self, ticker,all_points):
'''Checks if the given simlex is the base simplex out of all possible permutations about lines of symmetry'''
points = [0,]*len(ticker)
for i in range(len(ticker)):
points[i] = all_points[ticker[i]]
point_matrix = np.array(points)
column_vectors = []
for j in range(len(points)):
column_vectors.append(point_matrix[:,j])
indices = []
column_permutations = list(itr.permutations(column_vectors))
for column_permutation in column_permutations:
key = np.column_stack(column_permutation)
purmutation_index = [0,]*len(key)
for i in range(len(key)):
purmutation_index[i] = all_points.index(key[i].tolist())
indices.append(purmutation_index)
sorted_indices = []
for index in indices:
index.sort()
sorted_indices.append(index)
return min(sorted_indices)
def _index_to_points(self, ticker,all_points):
'''Converts the list of indices back to a list of points'''
points = [0,]*len(ticker)
for i in range(len(ticker)):
points[i] = all_points[ticker[i]]
return points
def percent_fractional_set(self,max_degree):
self.compute_dimension_table()
self.open_db()
c = self.conn.cursor()
entry = str('N/A')
c.execute('SELECT Percent,Degree FROM MMSet WHERE Percent !=?',(entry,))
percents = c.fetchall()
self.close_db()
elements = []
for i in range(len(percents)-1,0,-1):
if percents[i][1] > max_degree:
percents.remove(percents[i])
print len(percents),'Possibly Fractional Base Sets'
print ' '
elements = list(set(percents))
elements.sort()
if len(elements) >= 2:
elements.append(elements[1])
elements.remove(elements[1])
count = []
for i in range(len(elements)):
count.append(percents.count(elements[i]))
print elements[i][0], count[i]
print ' '
def percent_non_sos(self,max_degree):
self.compute_dimension_table()
self.open_db()
c = self.conn.cursor()
entry = str('N/A')
c.execute('SELECT `MMSet Size`,`Max Set Size`,Orbits,Degree FROM MMSet WHERE Percent !=?',(entry,))
set_sizes = c.fetchall()
self.close_db()
sum_max = 0
sum_mms = 0
for i in range(len(set_sizes)):
if set_sizes[i][3] <= max_degree:
sum_mms = sum_mms + set_sizes[i][0] * set_sizes[i][2]
sum_max = sum_max + set_sizes[i][1] * set_sizes[i][2]
return float(sum_mms*100)/sum_max
def delete_table(self):
self.close_db()
os.remove(self.path)
def percent_non_sos_vs_degree(dim, max_degree):
bars = []
db = MMSet_DB()
db.create_new_table(dim,max_degree)
db.compute_dimension_table()
for i in range(4,max_degree+1,2):
bars.append(db.percent_non_sos(i))
print bars
return bar_chart(bars)
def percent_non_sos_vs_dim(max_dim,degree):
bars = []
for i in range(2,max_dim+1):
db = MMSet_DB()
db.create_new_table(i,degree)
db.compute_dimension_table()
bars.append(db.percent_non_sos())
print bars
return bar_chart(bars)