-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsupports_with_causal_sym.py
220 lines (196 loc) · 9.89 KB
/
supports_with_causal_sym.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
211
212
213
214
215
216
217
218
219
220
from supports import SupportTester
import numpy as np
import methodtools
class SupportTester_PartyCausalSymmetry(SupportTester):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.latent_cardinality = self.nof_observed * self.nof_events
self.observed_cardinality = self.observed_cardinalities[0]
self.visible_outcomes = list(range(self.observed_cardinality))
self.latent_outcomes = list(range(self.latent_cardinality))
del self.var
def var(self, i, j, val):
return self.vpool.id(f"A_[{i:02},{j:02}]=={val}")
@property
def at_least_one_outcome(self):
clauses = []
for (i, j) in itertools.permutations(self.latent_outcomes, 2):
clauses.append([self.var(i, j, val) for val in self.visible_outcomes])
for val1, val2 in itertools.permutations(self.visible_outcomes, 2):
clauses.append([-self.var(i, j, val1), -self.var(i, j, val2)])
return clauses
@methodtools.lru_cache(maxsize=None, typed=False)
def forbidden_event_clauses(self, event: int):
"""Get the clauses associated with a particular event not occurring anywhere in the off-diagonal worlds."""
forbidden_event_as_row = self.from_list_to_matrix(event)
[val1, val2, val3] = forbidden_event_as_row
forbidden_event_clauses = set() # np.empty((self.latent_cardinality ** 3, 3), dtype=int)
for idx, (i, j, k) in enumerate(itertools.permutations(self.latent_outcomes, 3)):
no_go_clause = tuple([-self.var(i, j, val1), -self.var(j, k, val2), -self.var(k, i, val3)])
forbidden_event_clauses.add(no_go_clause)
# forbidden_event_clauses.sort(axis=-1)
# forbidden_event_clauses = np.unique(forbidden_event_clauses, axis=0).tolist()
forbidden_event_clauses = list(map(list, forbidden_event_clauses))
return forbidden_event_clauses
@methodtools.lru_cache(maxsize=None, typed=False)
def positive_outcome_clause(self,
world: int,
outcomes: tuple):
i = world * self.nof_observed
clauses = []
for p, val in enumerate(outcomes):
if p == 0:
left_s = i
right_s = i + 1
elif p == 1:
left_s = i + 1
right_s = i + 2
elif p == 2:
left_s = i + 2
right_s = i
clauses.append( [self.var(left_s, right_s, val)] )
wrong_values = set(range(self.observed_cardinality))
wrong_values.remove(val)
for wrong_val in wrong_values:
clauses.append( [-self.var(left_s, right_s, wrong_val)] )
return clauses
class SupportTester_FullCausalSymmetry(SupportTester_PartyCausalSymmetry):
def var(self, i, j, val):
[new_i, new_j] = sorted([i, j])
return self.vpool.id(f"A_[{new_i},{new_j}]=={val}")
if __name__ == '__main__':
import itertools
# Special problem for Victor Gitton
parents_of = np.array(([3, 4], [4, 5], [5, 3]))
observed_cardinalities = (3, 3, 3)
occurring_events_card3 = np.array([(0, 0, 0), (1, 1, 1), (2, 2, 2)]
+ list(itertools.permutations(range(3))))
st = SupportTester(parents_of=parents_of,
observed_cardinalities=observed_cardinalities,
nof_events=len(occurring_events_card3))
print("3 outcome with no symmetry: ",
st.feasibleQ_from_matrix(occurring_events_card3, name='mgh'))
st = SupportTester_PartyCausalSymmetry(parents_of=parents_of,
observed_cardinalities=observed_cardinalities,
nof_events=len(occurring_events_card3))
# for clause in st._sat_solver_clauses(occurring_events_card3):
# print(st.reverse_vars(clause))
print("3 outcome with only party symmetry: ",
st.feasibleQ_from_matrix(occurring_events_card3, name='mgh'))
# AT_LEAST_ONE_CLAUSES = st.reverse_vars(st.at_least_one_outcome)
# MUST_OCCUR_CLAUSES = st.reverse_vars(st.array_of_positive_outcomes(occurring_events_card3))
# FORBIDDEN_EVENT_CLAUSES = st.reverse_vars(st.forbidden_events_clauses(occurring_events_card3))
observed_cardinalities = (4, 4, 4)
occurring_events_card4 = np.array([(0, 0, 0),
(1, 1, 1),
(2, 2, 2),
(3, 3, 3)]
+ list(
itertools.permutations(range(4), 3)))
# victor_definite_events = np.array([
# (0, 1, 2),
# (0, 1, 3),
# (0, 2, 3),
# (1, 2, 3),
# (0, 0, 0),
# (1, 1, 1)])
# print("Studying the 6-event inflation for cardinality 4...")
# st = SupportTester_PartyCausalSymmetry(parents_of=parents_of,
# observed_cardinalities=observed_cardinalities,
# nof_events=6)
# AT_LEAST_ONE_CLAUSES = st.reverse_vars(st.at_least_one_outcome)
# MUST_OCCUR_CLAUSES = st.reverse_vars(st.array_of_positive_outcomes(victor_definite_events))
# FORBIDDEN_EVENT_CLAUSES = st.reverse_vars(st.forbidden_events_clauses(occurring_events_card4))
# model = st.potentially_feasibleQ_from_matrix_pair(
# definitely_occurring_events_matrix=victor_definite_events,
# potentially_occurring_events_matrix=occurring_events_card4,
# return_model=True)
# print(model, " !")
# if model:
# grid_values = []
# for int_clause in model:
# if int_clause >= 0:
# grid_values.append(st.reverse_vars(int_clause))
# triple_vals = [(int(s[3:5]), int(s[6:8]), int(s[-1])) for s in grid_values]
# grid = np.zeros((st.latent_cardinality, st.latent_cardinality), dtype=int)
# for (i, j, v) in triple_vals:
# grid[i, j] = v+1
# print(grid)
st = SupportTester_PartyCausalSymmetry(parents_of=parents_of,
observed_cardinalities=observed_cardinalities,
nof_events=0)
print(st.feasibleQ_from_matrix_CONSERVATIVE(occurring_events_card4,
min_definite=3,
max_definite=6,
always_include=((0, 0, 0),),
return_model=True))
#
# rejected_yet = False
# definite_events_which_triggered_nonSAT = []
# for n in range(2, 6):
# print(f"Working on {n} events...")
# if rejected_yet:
# break
# st = SupportTester_PartyCausalSymmetry(parents_of=parents_of,
# observed_cardinalities=observed_cardinalities,
# nof_events=n)
# max_to_check = comb(len(occurring_events_card4)-1, n-1)
# with ProgressBar(max_value=max_to_check) as bar:
# for i, other_definite_events in enumerate(itertools.combinations(occurring_events_card4[1:], n-1)):
# definite_events = np.zeros((n, 3), dtype=int)
# definite_events[1:] = other_definite_events
# rejected_yet = not st.potentially_feasibleQ_from_matrix_pair(
# definitely_occurring_events_matrix=definite_events,
# potentially_occurring_events_matrix=occurring_events_card4)
# bar.update(i)
# if rejected_yet:
# definite_events_which_triggered_nonSAT = definite_events
# break
# print(definite_events_which_triggered_nonSAT)
#
#
#
# definite_events = [(0, 0, 0), (1, 1, 1), (2, 2, 2)]
#
# model = st.potentially_feasibleQ_from_matrix_pair(
# definitely_occurring_events_matrix=definite_events,
# potentially_occurring_events_matrix=occurring_events_card4,
# return_model=True)
# print(model, " !")
# if model:
# grid_values = []
# for int_clause in model:
# if int_clause >= 0:
# grid_values.append(st.reverse_vars(int_clause))
# triple_vals = [(int(s[3:5]), int(s[6:8]), int(s[-1])) for s in grid_values]
# grid = np.zeros((st.latent_cardinality, st.latent_cardinality), dtype=int)
# for (i, j, v) in triple_vals:
# grid[i, j] = v+1
# print(grid)
# print("Studying the maximal inflation for cardinality 4...")
# st = SupportTester_PartyCausalSymmetry(parents_of=parents_of,
# observed_cardinalities=observed_cardinalities,
# nof_events=len(occurring_events_card4))
#
# print("4 outcome with Party symmetry: ",
# st.feasibleQ_from_matrix(occurring_events_card4, name='mgh'))
# max_n = len(occurring_events_card4)
# rejected_yet = False
# for n in range(2, max_n+1):
# if rejected_yet:
# break
# print(f"Working on cardinality 4 with {n} definite events...")
# st = SupportTester_PartyCausalSymmetry(parents_of=parents_of,
# observed_cardinalities=(4, 4, 4),
# nof_events=n)
# # for definitely_occurring_events in itertools.combinations(occurring_events_card_4, n):
# definitely_occurring_events = occurring_events_card4[:n]
# rejected_yet = not st.potentially_feasibleQ_from_matrix_pair(
# definitely_occurring_events_matrix=definitely_occurring_events,
# potentially_occurring_events_matrix=occurring_events_card4
# )
# if rejected_yet:
# print(f"search completed at {n} events")
# print(definitely_occurring_events)
# break
# print("In the end, were we able to prove infeasibility? ", rejected_yet)