-
Notifications
You must be signed in to change notification settings - Fork 2
/
ordertheory.py
99 lines (81 loc) · 3.64 KB
/
ordertheory.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
import itertools
import pickle
class Powerset(object):
def powerset(self, iterable):
l = list(iterable)
return itertools.chain.from_iterable(itertools.combinations(l, r) for r in range(len(l)+1))
####################################################################################################################
####################################################################################################################
class FunctionalSpace(object):
def __rel(self, list0, list1):
prod = itertools.product(list0, list1)
pset = Powerset().powerset(prod)
for rel in pset:
yield dict(list(rel))
def funcs(self, list0, list1):
dom = list(list0)
codom = list(list1)
if dom == [] and codom == []:
return []
elif dom != [] and codom == []:
return []
elif dom == [] and codom != []:
return [[]]
elif dom != [] and codom != []:
rel = self.__rel(dom, codom)
return [dict(tupleized).items() for tupleized in set(tuple(item.items()) for item in rel) if len(dict(tupleized).keys()) == len(dom)]
####################################################################################################################
####################################################################################################################
class StrictTotalOrders(object):
def __recurse(self, l, orders=[]):
if l:
orders = orders + list(itertools.product([l[0]], list(l[1:])))
return self.__recurse(list(l[1:]), orders)
else:
return orders
def orders(self, iterable):
l = list(iterable)
permutations = itertools.permutations(l)
for permutation in permutations:
yield frozenset(self.__recurse(permutation))
####################################################################################################################
####################################################################################################################
class StrictOrders(object):
lattice = {}
def __is_not_transitive(self, relation):
product = itertools.product(list(relation), list(relation))
for x in product:
if x[0][1] == x[1][0]:
if (x[0][0], x[1][1]) not in list(relation):
return True
def __orders(self, l):
torders = list(StrictTotalOrders().orders(l))
for order in torders:
pset = Powerset().powerset(list(order))
for relation in pset:
if self.__is_not_transitive(relation) != True:
yield frozenset(relation)
def get_orders(self, iterable):
l = list(iterable)
torders = list(StrictTotalOrders().orders(l))
for s, t in itertools.product(self.__orders(l), self.__orders(l)):
try:
self.lattice[s]
except:
self.lattice.update({s:{'max':set([]), 'up':set([]), 'down':set([])}})
if s.issuperset(t):
self.lattice[s]['down'].add(t)
if s.issubset(t):
self.lattice[s]['up'].add(t)
if t in torders:
self.lattice[s]['max'].add(t)
elif s.issubset(t):
self.lattice[s]['up'].add(t)
if t in torders:
self.lattice[s]['max'].add(t)
return self.lattice
def write_to_pickle(self, iterable):
l = list(iterable)
length = len(l)
pickle.dump(self.get_orders(l), open('gspace_%scons.p' %(length), 'wb'))
return 'Orders written.'