def solve(self, step, stop): # solves using gradient descent self.init_solver() for i in range(self.max_iters): g = self.gradient() nu = simplex_projection(self.nu - step() * g, self.b) obj, a = self.objective(nu) self.update(nu, obj, a) if stop(): break print 'iter: ', i print 'obj: ', obj return {'attack_rates': self.nu, 'obj_values': self.obj_values}
def online_gradient_descent(batch_data, T, init, G, D): ''' :param batch_data: ratios data of size (n_ratios, T) :param T: Horizon of repeated game :param init: Initial point x_1 :param G: Lipschitz coeeficient :param D: Diameter :return: ''' xs = [init] eta = D / (G * np.sqrt(T)) # fixed step size rt = batch_data[:, 0] # initial ratios for t in range(T-1): # adapting step size # eta = D / (G * np.sqrt(t+1)) # compute online gradient grad = utl.get_grad(xs[-1], rt) # perform OGD update (with projection) xs.append(utl.simplex_projection(xs[-1] - eta * grad)) # observe next data point rt = batch_data[:, t + 1] return xs
def test_simplex_projection(self): v = np.array([1, 0, 0, 0, 0, 0]) self.assertTrue(is_equal(simplex_projection(v), v)) v = np.array([0.1, 0.2, 0.3, 0.4]) self.assertTrue(is_equal(simplex_projection(v), v)) v = np.array([10, 10]) w = np.array([0.5, 0.5]) self.assertTrue(is_equal(simplex_projection(v), w)) v = np.array([-10, -10]) w = np.array([0.5, 0.5]) self.assertTrue(is_equal(simplex_projection(v), w)) v = np.array([-10, 10]) w = np.array([0, 1]) self.assertTrue(is_equal(simplex_projection(v), w)) for _ in range(20): v = (np.random.rand(20) - np.ones(20) * 0.5) * 100 self.assertTrue(is_equal(np.sum(simplex_projection(v)), 1)) self.assertTrue(is_equal(np.sum(simplex_projection(v, 5)), 5))