-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobinmax_cover_generator.py
249 lines (195 loc) · 8.03 KB
/
robinmax_cover_generator.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
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
"""Generate minimal activation covers.
This module contains all functions relative to the generation of
activation covers for a linear threshold graph.
"""
import numpy as np
from itertools import chain, combinations
def powerset(indices):
"""Generate power set of a given list of indices
Parameters
----------
indices : List[int]
The list of indices for which we should generate the powerset.
Returns
-------
iterator
An iterator over the powerset, where each element is given as
a tuple.
"""
return chain.from_iterable(combinations(indices, n)
for n in range(1, len(indices)+1))
# -- end function
def generate_minimal_covers_brute_force(graph):
"""Generate minimal activation covers.
Parameters
----------
graph : `robinmax_graph_reader.LinearThresholdGraph`
Graph for which activation covers should be generated.
Returns
-------
List[List[List[int]]]
List of activation covers for each node.
"""
list_covers = [list() for i in range(graph.num_nodes)]
for i in range(graph.num_nodes):
size = len(graph.instar[i])
# Generate all combinations of indices from 0 to size-1
for cover in chain.from_iterable(combinations(range(size), n)
for n in range(1, size + 1)):
if (sum(graph.arc_weight_in[i][j] for j in cover) >=
graph.node_threshold[i]):
# This is a cover. Check minimality
minimal = True
for k in cover:
if (sum(graph.arc_weight_in[i][k] for j in cover
if j != k) >= graph.node_threshold[i]):
minimal = False
break
if (minimal):
list_covers[i].append(sorted(graph.instar[i][j]
for j in cover))
# -- end if
# -- end if
# -- end for
# -- end for
return list_covers
# -- end function
def generate_covers_with_thresh(graph, max_cover_size, thresh_budget,
max_thresh_dev, weight_budget, max_weight_dev):
"""Generate activation covers. All covers up to max_cover_size + 1
will be generated, but only those up to max_cover_size will be
returned. An additional list contains a the thresholds for
minimality and validity.
Parameters
----------
graph : `robinmax_graph_reader.LinearThresholdGraph`
Graph for which activation covers should be generated.
max_cover_size : int
Maximum size of activation covers to consider.
thresh_budget : float
Maximum threshold budget available to the opponent.
max_thresh_dev : float
Maximum node threshold deviation.
weight_budget : float
Maximum weight budget available to the opponent.
max_weight_dev : float
Maximum arc weight deviation.
Returns
-------
List[List[List[int]]]
List of activation covers for each node.
List[List[(float, float)]]
The thresholds (min_minimality, max_validity) for each cover.
"""
covers = [list() for _ in range(graph.num_nodes)]
# Generate covers of a slightly larger size.
covers, thresholds = generate_minimal_covers(
graph, max_cover_size, thresh_budget,
max_thresh_dev, weight_budget, max_weight_dev)
return covers, thresholds
def generate_minimal_covers(graph, max_cover_size, thresh_budget,
max_thresh_dev, weight_budget, max_weight_dev):
"""Generate minimal activation covers.
Parameters
----------
graph : `robinmax_graph_reader.LinearThresholdGraph`
Graph for which activation covers should be generated.
max_cover_size : int
Maximum size of activation covers to consider. Any activation
cover larger than this size will not be generated.
thresh_budget : float
Maximum threshold budget available to the opponent.
max_thresh_dev : float
Maximum node threshold deviation.
weight_budget : float
Maximum weight budget available to the opponent.
max_weight_dev : float
Maximum arc weight deviation.
Returns
-------
List[List[List[int]]]
List of activation covers for each node.
"""
list_covers = [list() for i in range(graph.num_nodes)]
thresholds = [list() for i in range(graph.num_nodes)]
for i in range(graph.num_nodes):
# Sort the arcs by decreasing weight
sort_order = [i for i in
reversed(np.argsort(graph.arc_weight_in[i]))]
covers = []
# Generate covers recursively
recursive_generate_covers(graph.arc_weight_in[i], sort_order,
graph.node_threshold[i], [], 0,
max_cover_size, thresh_budget, max_thresh_dev,
covers, weight_budget, max_weight_dev)
# Translate the arc indices in the sorted list, to the index
# of the node that these arcs originate from
list_covers[i] = [sorted(graph.instar[i][sort_order[j]]
for j in cover) for cover in covers]
# Skip if we did not find any cover
if len(covers) == 1 and len(covers[0]) == 0:
continue
for cover in covers:
value = sum(graph.arc_weight_in[i][sort_order[j]] for j in cover)
minimality = value - min(graph.arc_weight_in[i][sort_order[j]] for j in cover)
thresholds[i].append((minimality, value))
return list_covers, thresholds
# -- end function
def recursive_generate_covers(weights, sort_order, threshold, curr_set,
curr_index, max_cover_size, thresh_budget,
max_thresh_dev, all_sets, weight_budget,
max_weight_dev):
"""Generate all minimal activation covers for given node.
Recursive function to generate all minimal activation covers, in a
dynamic programming fashion.
Parameters
----------
weights : List[float]
List of arc weights.
sort_order : List[int]
Indices of weights sorted by decreasing value.
threshold : float
Activation threshold for the node.
curr_set : List[int]
Current set of arcs in the cover.
curr_index : int
Position of arc weights yet to be considered.
max_cover_size : int
Maximum size of covers to consider.
thresh_budget : float
Maximum threshold budget available to the opponent.
max_thresh_dev : float
Maximum node threshold deviation.
all_sets : List[List[int]]
The list of all covers discovered.
weight_budget : float
Maximum weight budget available to the opponent.
max_weight_dev : float
Maximum arc weight deviation.
Returns
-------
bool
True if a cover was generated, False otherwise
"""
if (curr_index > len(weights) or len(curr_set) > max_cover_size):
return False
any_generated = False
if (sum(weights[sort_order[i]] for i in curr_set) >= threshold):
all_sets.append([i for i in curr_set])
any_generated = True
max_curr_set_dev = sum(weights[sort_order[i]] * max_weight_dev for i in curr_set)
if (sum(weights[sort_order[i]] for i in curr_set) >=
threshold + min(thresh_budget, threshold * max_thresh_dev) + min(weight_budget, max_curr_set_dev)):
return True
for j in range(curr_index, len(weights)):
curr_set.append(j)
result = recursive_generate_covers(weights, sort_order, threshold,
curr_set, j + 1, max_cover_size, thresh_budget,
max_thresh_dev, all_sets, weight_budget,
max_weight_dev)
curr_set.pop()
if not result:
break
else:
any_generated = True
return any_generated