-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.py
195 lines (129 loc) · 3.67 KB
/
Model.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
import itertools
framework = {}
descriptions = {}
arguments = set()
relations = set()
cfs = set()
grod = set()
prf = set()
def is_attacked(arg, relations):
for x in relations:
if x[1] == arg:
return True
def attacks(x):
return list(framework[x])
def get_arg_attackers(arg):
attackers = []
for i in relations:
if i[1] == arg:
attackers.append(i[0])
return(set(attackers))
def get_attacked_args(set_of_args):
attacked = set()
for arg in set_of_args:
for i in relations:
if i[0] == arg:
attacked.add(i[1])
return (set(attacked))
#Pasted from Python Iter Recipes
def powerset(iterable):
s = list(iterable)
return set(itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(len(s)+1)))
def compute_cfs():
pwr = powerset(arguments)
for x in relations:
x1 = x[0]
x2 = x[1]
todelete = []
for i in pwr:
if (x1 in i) and (x2 in i):
todelete.append(i)
for i in todelete:
pwr.remove(i)
cfs = pwr
return cfs
#return pwr
def compute_naive_extension(cfs):
naive = []
maxLen = 0
for i in cfs:
if len(i)>maxLen:
maxLen = len(i)
for i in cfs:
if len(i)==maxLen:
naive.append(i)
return set(naive)
# For each conflict free subset, if its attackers are
# attacked (exhaustively) by a subset of that cfs, it is admissible
def compute_admissibility(cfs):
admissible = []
for cfset in cfs:
attackers = set()
for cfsetmember in cfset:
attackers = attackers.union(get_arg_attackers(cfsetmember))
attackedbycfsmembers = []
for attacker in attackers:
atk = False
attackedby = get_arg_attackers(attacker)
for cfsetmember in cfset:
if cfsetmember in attackedby:
atk = True
attackedbycfsmembers.append(atk)
if all(attackedbycfsmembers):
admissible.append(cfset)
return set(admissible)
def compute_stable_extension(adm):
stb = []
for x in adm:
if set(x).union(get_attacked_args(x)) == arguments:
stb.append(x)
return set(stb)
def compute_grounded_extension(adm):
grd = []
rel = list(relations)
attackedbyin = []
someArg = True
if len(adm)== 1:
return grd
for x in arguments:
if is_attacked(x, rel) != True:
if x not in grd:
grd.append(x)
if len(grd)==0:
return grd
while someArg:
for x in grd:
if len(framework[x]) <= 1:
attackedbyin.append(*framework[x])
else :
attackedbyin.extend(framework[x])
for r in rel:
for atk in attackedbyin:
if r[0]== atk:
rel.remove(r)
for i in arguments:
if is_attacked(i, rel) != True:
if i not in grd:
grd.append(i)
else:
someArg = False
return grd
def compute_preferred_extensions(adm):
pref = []
maxLen = 0
for i in adm:
if len(i)>maxLen:
maxLen = len(i)
for i in adm:
if len(i)==maxLen:
pref.append(i)
return set(pref)
def compute_complete_extensions(adm):
grd = set(compute_grounded_extension(adm))
prf = compute_preferred_extensions(adm)
grd2 = str(grd).replace("'",'')
prf2 = str(prf).replace('(','').replace(',','').replace(')','').replace("'",'')
if prf2 == grd2:
return prf2
else:
return grd2+" "+prf2