-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbenchmarkfunctions.py
59 lines (48 loc) · 1.89 KB
/
benchmarkfunctions.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
'''
This module contains various test functions for the ZORO algorithm.
All of them exhibit gradient sparsity or compressibility.
'''
import numpy as np
#import sys
import math
class SparseQuadric(object):
'''An implementation of the sparse quadric function.'''
def __init__(self, n, s, noiseamp):
self.noiseamp = noiseamp/np.sqrt(n)
self.s = s
self.dim = n
self.rng = np.random.RandomState()
def __call__(self,x):
f_no_noise = np.dot(x[0:self.s],x[0:self.s])
return f_no_noise + self.noiseamp*self.rng.randn()
class MaxK(object):
'''An implementation of the max-k-squared-sum function.'''
def __init__(self, n, s, noiseamp):
self.noiseamp = noiseamp/np.sqrt(n)
self.dim = n
self.s = s
self.rng = np.random.RandomState()
def __call__(self, x):
idx = np.argsort(np.abs(x))
idx2 = idx[self.dim-self.s:self.dim]
f_no_noise = np.dot(x[idx2], x[idx2])/2
return f_no_noise + self.noiseamp*self.rng.randn()
class CompressibleQuadric(object):
'''An implementation of the sparse quadric function.'''
def __init__(self, n, decay_factor, noiseamp):
self.noiseamp = noiseamp/np.sqrt(n)
self.decay_factor = decay_factor
self.dim = n
self.rng = np.random.RandomState()
self.diag = np.zeros(n)
for i in range(0,n):
self.diag[i] = math.exp(-self.decay_factor*i)
def __call__(self,x):
#f_no_noise = 0
#for i in range(0,self.dim):
#f_no_noise += math.exp(-self.decay_factor*i)*x[i]**2
f_no_noise = np.dot(self.diag * x, x)
return f_no_noise + self.noiseamp*self.rng.randn()
#f_no_noise = np.dot(x[0:self.s],x[0:self.s])
#f_no_noise += 1e-4*np.dot(x[self.s:self.dim],x[self.s:self.dim])
#return f_no_noise + self.noiseamp*self.rng.randn()