|
| 1 | +import itertools |
| 2 | + |
| 3 | + |
| 4 | +framework = {} |
| 5 | +descriptions = {} |
| 6 | +arguments = set() |
| 7 | +relations = set() |
| 8 | +cfs = set() |
| 9 | +grod = set() |
| 10 | +prf = set() |
| 11 | + |
| 12 | +def is_attacked(arg, relations): |
| 13 | + for x in relations: |
| 14 | + if x[1] == arg: |
| 15 | + return True |
| 16 | + |
| 17 | +def attacks(x): |
| 18 | + return list(framework[x]) |
| 19 | + |
| 20 | +def get_arg_attackers(arg): |
| 21 | + attackers = [] |
| 22 | + for i in relations: |
| 23 | + if i[1] == arg: |
| 24 | + attackers.append(i[0]) |
| 25 | + return(set(attackers)) |
| 26 | + |
| 27 | +def get_attacked_args(set_of_args): |
| 28 | + |
| 29 | + attacked = set() |
| 30 | + for arg in set_of_args: |
| 31 | + for i in relations: |
| 32 | + if i[0] == arg: |
| 33 | + attacked.add(i[1]) |
| 34 | + return (set(attacked)) |
| 35 | + |
| 36 | +#Pasted from Python Iter Recipes |
| 37 | +def powerset(iterable): |
| 38 | + s = list(iterable) |
| 39 | + return set(itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(len(s)+1))) |
| 40 | + |
| 41 | +def compute_cfs(): |
| 42 | + |
| 43 | + pwr = powerset(arguments) |
| 44 | + for x in relations: |
| 45 | + x1 = x[0] |
| 46 | + x2 = x[1] |
| 47 | + todelete = [] |
| 48 | + for i in pwr: |
| 49 | + if (x1 in i) and (x2 in i): |
| 50 | + todelete.append(i) |
| 51 | + for i in todelete: |
| 52 | + pwr.remove(i) |
| 53 | + cfs = pwr |
| 54 | + return cfs |
| 55 | + |
| 56 | + |
| 57 | + #return pwr |
| 58 | + |
| 59 | +def compute_naive_extension(cfs): |
| 60 | + naive = [] |
| 61 | + maxLen = 0 |
| 62 | + |
| 63 | + for i in cfs: |
| 64 | + if len(i)>maxLen: |
| 65 | + maxLen = len(i) |
| 66 | + |
| 67 | + for i in cfs: |
| 68 | + if len(i)==maxLen: |
| 69 | + naive.append(i) |
| 70 | + |
| 71 | + return set(naive) |
| 72 | + |
| 73 | + |
| 74 | + # For each conflict free subset, if its attackers are |
| 75 | + # attacked (exhaustively) by a subset of that cfs, it is admissible |
| 76 | + |
| 77 | +def compute_admissibility(cfs): |
| 78 | + |
| 79 | + admissible = [] |
| 80 | + |
| 81 | + for cfset in cfs: |
| 82 | + attackers = set() |
| 83 | + |
| 84 | + for cfsetmember in cfset: |
| 85 | + attackers = attackers.union(get_arg_attackers(cfsetmember)) |
| 86 | + |
| 87 | + attackedbycfsmembers = [] |
| 88 | + |
| 89 | + for attacker in attackers: |
| 90 | + atk = False |
| 91 | + attackedby = get_arg_attackers(attacker) |
| 92 | + |
| 93 | + |
| 94 | + for cfsetmember in cfset: |
| 95 | + if cfsetmember in attackedby: |
| 96 | + atk = True |
| 97 | + attackedbycfsmembers.append(atk) |
| 98 | + |
| 99 | + if all(attackedbycfsmembers): |
| 100 | + admissible.append(cfset) |
| 101 | + |
| 102 | + |
| 103 | + return set(admissible) |
| 104 | + |
| 105 | +def compute_stable_extension(adm): |
| 106 | + |
| 107 | + stb = [] |
| 108 | + |
| 109 | + for x in adm: |
| 110 | + if set(x).union(get_attacked_args(x)) == arguments: |
| 111 | + stb.append(x) |
| 112 | + return set(stb) |
| 113 | + |
| 114 | +def compute_grounded_extension(adm): |
| 115 | + |
| 116 | + grd = [] |
| 117 | + rel = list(relations) |
| 118 | + attackedbyin = [] |
| 119 | + someArg = True |
| 120 | + |
| 121 | + if len(adm)== 1: |
| 122 | + return grd |
| 123 | + |
| 124 | + |
| 125 | + for x in arguments: |
| 126 | + if is_attacked(x, rel) != True: |
| 127 | + if x not in grd: |
| 128 | + grd.append(x) |
| 129 | + |
| 130 | + if len(grd)==0: |
| 131 | + return grd |
| 132 | + |
| 133 | + while someArg: |
| 134 | + for x in grd: |
| 135 | + if len(framework[x]) <= 1: |
| 136 | + attackedbyin.append(*framework[x]) |
| 137 | + else : |
| 138 | + attackedbyin.extend(framework[x]) |
| 139 | + |
| 140 | + for r in rel: |
| 141 | + for atk in attackedbyin: |
| 142 | + if r[0]== atk: |
| 143 | + rel.remove(r) |
| 144 | + |
| 145 | + for i in arguments: |
| 146 | + if is_attacked(i, rel) != True: |
| 147 | + if i not in grd: |
| 148 | + grd.append(i) |
| 149 | + else: |
| 150 | + someArg = False |
| 151 | + |
| 152 | + |
| 153 | + return grd |
| 154 | + |
| 155 | +def compute_preferred_extensions(adm): |
| 156 | + |
| 157 | + pref = [] |
| 158 | + maxLen = 0 |
| 159 | + |
| 160 | + for i in adm: |
| 161 | + if len(i)>maxLen: |
| 162 | + maxLen = len(i) |
| 163 | + |
| 164 | + for i in adm: |
| 165 | + if len(i)==maxLen: |
| 166 | + pref.append(i) |
| 167 | + |
| 168 | + return set(pref) |
| 169 | + |
| 170 | +def compute_complete_extensions(adm): |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + grd = set(compute_grounded_extension(adm)) |
| 175 | + prf = compute_preferred_extensions(adm) |
| 176 | + |
| 177 | + grd2 = str(grd).replace("'",'') |
| 178 | + prf2 = str(prf).replace('(','').replace(',','').replace(')','').replace("'",'') |
| 179 | + |
| 180 | + |
| 181 | + if prf2 == grd2: |
| 182 | + return prf2 |
| 183 | + else: |
| 184 | + return grd2+" "+prf2 |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
0 commit comments