-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
220 lines (198 loc) · 7.17 KB
/
algorithms.py
File metadata and controls
220 lines (198 loc) · 7.17 KB
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
import numpy as np
from utils import greedy
def indicator(S, n):
x = np.zeros(n)
x[list(S)] = 1
return x
def multi_to_set(f, n):
'''
Takes as input a function defined on indicator vectors of sets, and returns
a version of the function which directly accepts sets
'''
def f_set(S):
return f(indicator(S, n))
return f_set
def make_weighted(f, weights, *args):
def weighted(x):
return np.dot(weights, f(x, *args))
return weighted
def make_normalized(f, targets):
def normalized(x, *args):
return f(x, *args)@np.diag(1./targets)
return normalized
def make_contracted_function(f, S):
'''
For any function defined on the multilinear extension which takes x as its
first argument, return a function which conditions on all items in S being
chosen.
'''
def contracted(x, *args):
x = x.copy()
x[list(S)] = 1
return f(x, *args)
return contracted
def mirror_sp(x, grad_oracle, k, group_indicator, group_targets, num_iter, step_size = 1, batch_size = 200, verbose=False):
'''
Uses stochastic saddle point mirror descent to solve the inner maxmin linear
optimization problem.
'''
#random initialization
startstep = step_size
m = group_indicator.shape[1]
v = x + 0.01*np.random.rand(*x.shape)
v[v > 1] = 1
v = k*v/v.sum()
y = (1./m) * np.ones(m)
group_weights = 1./group_targets
group_weights[group_targets <= 0 ] = 0
#historical mixed strategy for defender
for t in range(num_iter):
step_size = startstep/np.sqrt(t+1)
#get a stochastic estimate of the gradient for each player
g = grad_oracle(x, batch_size)
group_grad = g @ np.diag(group_weights)
grad_v = group_grad@y
grad_y = v@group_grad
#gradient step
v = v * np.exp(step_size*grad_v)
y = y*np.exp(-step_size*grad_y)
#bregman projection
v[v > 1] = 1
v = k*v/v.sum()
y = y/y.sum()
return v
def lp_minmax(x, grad_oracle, k, group_indicator, group_targets):
import gurobipy as gp
m = gp.Model()
m.setParam( 'OutputFlag', False )
v = m.addVars(range(len(x)), lb = 0, ub = 1)
obj = m.addVar()
m.update()
m.addConstr(gp.quicksum(v) <= k)
g = grad_oracle(x, 5000)
for i in range(len(group_targets)):
if group_targets[i] > 0:
m.addConstr(obj <= gp.quicksum(v[j]*g[j, i] for j in range(len(v)))/group_targets[i])
m.setObjective(obj, gp.GRB.MAXIMIZE)
m.optimize()
return np.array([v[i].x for i in range(len(v))])
def rounding(x):
'''
Rounding algorithm that does not require decomposition of x into bases
of the matroid polytope
'''
import random
i = 0
j = 1
x = x.copy()
for t in range(len(x)-1):
if x[i] == 0 and x[j] == 0:
i = max((i,j)) + 1
elif x[i] + x[j] < 1:
if random.random() < x[i]/(x[i] + x[j]):
x[i] = x[i] + x[j]
x[j] = 0
j = max((i,j)) + 1
else:
x[j] = x[i] + x[j]
x[i] = 0
i = max((i,j)) + 1
else:
if random.random() < (1 - x[j])/(2 - x[i] - x[j]):
x[j] = x[i] + x[j] - 1
x[i] = 1
i = max((i,j)) + 1
else:
x[i] = x[i] + x[j] - 1
x[j] = 1
j = max((i,j)) + 1
return x
def multiobjective_fw(grad_oracle, val_oracle, k, group_indicator, group_targets, num_iter, solver = 'md'):
'''
Uses FW updates to find a fractional point meeting a threshold value for each
of the objectives
'''
# startstep = stepsize
x = np.zeros((num_iter, group_indicator.shape[0]))
for t in range(num_iter-1):
#how far each group currently is from the target
iter_targets = group_targets - val_oracle((1/num_iter)*x[0:t].sum(axis=0), 5000)
if np.all(iter_targets < 0):
# print('all targets met')
iter_targets = np.ones(len(group_targets))
# iter_targets = group_targets - val_oracle(x[t], 1000)
# print(iter_targets)
# iter_targets[iter_targets < 0] = 0
# print(iter_targets.min(), iter_targets.max())
#Frank-Wolfe update
# stepsize = startstep/np.sqrt(2*t+1)
if solver == 'md':
x[t+1] = mirror_sp((1./(t+1))*x[0:(t+1)].sum(axis=0), grad_oracle, k, group_indicator, iter_targets, num_iter=1000)
elif solver == 'gurobi':
x[t+1] = lp_minmax((1./(t+1))*x[0:(t+1)].sum(axis=0), grad_oracle, k, group_indicator, iter_targets)
else:
raise Exception('solver must be either md or gurobi')
return x
def greedy_top_k(grad, elements, budget):
'''
Greedily select budget number of elements with highest weight according to
grad
'''
import numpy as np
inx = np.argpartition(grad, -budget)[-budget:]
indicator = np.zeros(len(grad))
indicator[inx[:budget]] = 1
return indicator
def fw(grad_oracle, val_oracle, threshold, k, group_indicator, group_targets, num_iter, stepsize= 0.3):
'''
Run the normal frank-wolf algorithm to maximize a single submodular function
'''
x = np.zeros((num_iter, group_indicator.shape[0]))
for t in range(num_iter-1):
grad = grad_oracle((1./num_iter)*x[0:t].sum(axis=0), 1000).sum(axis=1)
x[t+1] = greedy_top_k(grad, None, k)
return x
def threshold_include(n_items, val_oracle, threshold):
'''
Makes a single pass through the items and returns a set including every
item whose singleton value is at least threshold for some objective
'''
to_include = []
x = np.zeros((n_items))
for i in range(n_items):
x[i] = 1
vals = val_oracle(x, 1000)
if vals.max() >= threshold:
to_include.append(i)
x[i] = 0
return to_include
def algo(grad_oracle, val_oracle, threshold, k, group_indicator, group_targets, num_iter, solver):
'''
Combine algorithm that runs the first thresholding stage and then FW
'''
S = threshold_include(group_indicator.shape[0], val_oracle, threshold)
grad_oracle = make_contracted_function(grad_oracle, S)
val_oracle = make_contracted_function(val_oracle, S)
x = multiobjective_fw(grad_oracle, val_oracle, k - len(S), group_indicator, group_targets, num_iter, solver)
x[:, list(S)] = 1
return x
def maxmin_algo(grad_oracle, val_oracle, threshold, k, group_indicator, num_iter, num_bin_iter, eps, solver):
S = threshold_include(group_indicator.shape[0], val_oracle, threshold)
grad_oracle = make_contracted_function(grad_oracle, S)
val_oracle = make_contracted_function(val_oracle, S)
ub = 1.
lb = 0
iternum = 0
while ub - lb > eps and iternum < num_bin_iter:
target = (ub + lb)/2
# print(target)
group_targets = np.zeros(group_indicator.shape[1])
group_targets[:] = target
x = algo(grad_oracle, val_oracle, threshold, k, group_indicator, group_targets, num_iter, solver)[1:]
vals = val_oracle(x.mean(axis=0), 1000)
if vals.min() > target:
lb = target
else:
ub = target
iternum += 1
return x