-
Notifications
You must be signed in to change notification settings - Fork 4
/
benchmarkfunctions.py
158 lines (136 loc) · 5.52 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
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
'''This module contains the following:
BenignEllipse
A moderately conditioned function.
BenignEllipseNoisyThres
A moderately conditioned function with additive noise above a certain
(function value) threshold.
BenignEllipseAddNoise
A moderately conditioned function with additive noise of a specified
strength applied.
BenignEllipseMultNoise
A moderately conditioned function with multiplicative noise of a specified
strength applied.
Ellipse
A badly conditioned function.
EllipseAddNoise
A badly conditioned function with additive noise of a specified
strength applied.
EllipseMultNoise
A badly conditioned function with multiplicative noise of a specified
strength applied.
sphere
The standard spherical quadratic function.
SphereAddNoise
The standard spherical quadratic function with additive noise of a
specified streght applied.
SphereMultNoise
The standard spherical quadratic function with multiplicative noise of a
specified streght applied.
sigmoidal
A sigmoidal function thresholding large values.
'''
import numpy as np
import sys
class BenignEllipse(object):
'''Implements a quadratic function of condition number 1e2.'''
def __init__(self, n):
self.elli_factors = np.zeros(n)
for i in range(n):
self.elli_factors[i] = np.power(1e2, i / (n - 1))
def __call__(self, x):
return np.sqrt(np.dot(x, self.elli_factors * x))
class BenignEllipseNoisyThres(object):
'''Implements a quadratic function of condition number 1e2
with a noise strength proportional to the problem dimension
The noise is applied only above a threshold of 3.5 (function value).'''
def __init__(self, n, rng=np.random.RandomState()):
self.n = n
self.rng = rng
self.elli_factors = np.zeros(n)
for i in range(n):
self.elli_factors[i] = np.power(1e2, i / (n - 1))
def __call__(self, x):
f = np.sqrt(np.dot(x, self.elli_factors * x))
if f > 3.5:
f += (self.rng.randn()*(200/self.n))
return f
class BenignEllipseAddNoise(object):
'''An implementation of a quadratic function of condition number 1e6
on which additive noise is applied.'''
def __init__(self, n, noiseamp, rng=np.random.RandomState()):
self.noiseamp = noiseamp
self.rng = rng
self.elli_factors = np.zeros(n)
for i in range(n):
self.elli_factors[i] = np.power(1e2, i / (n - 1))
def __call__(self, x):
f_no_noise = np.sqrt(np.dot(x, self.elli_factors * x))
return f_no_noise*self.rng.randn()*self.noiseamp
class BenignEllipseMultNoise(object):
'''An implementation of a quadratic function of condition number 1e6
on which multiplicative noise is applied.'''
def __init__(self, n, noiseamp, rng=np.random.RandomState()):
self.noiseamp = noiseamp
self.rng = rng
self.elli_factors = np.zeros(n)
for i in range(n):
self.elli_factors[i] = np.power(1e2, i / (n - 1))
def __call__(self, x):
f_no_noise = np.sqrt(np.dot(x, self.elli_factors * x))
return f_no_noise*sigmoidal(self.rng.randn()*self.noiseamp)
class Ellipse(object):
'''Implements a quadratic function of condition number 1e6.'''
def __init__(self, n):
self.elli_factors = np.zeros(n)
for i in range(n):
self.elli_factors[i] = np.power(1e6, i / (n - 1))
def __call__(self, x):
return np.sqrt(np.dot(x, self.elli_factors * x))
class EllipseAddNoise(object):
'''An implementation of a quadratic function of condition number 1e6
on which additive noise is applied.'''
def __init__(self, n, noiseamp, rng=np.random.RandomState()):
self.noiseamp = noiseamp
self.rng = rng
self.elli_factors = np.zeros(n)
for i in range(n):
self.elli_factors[i] = np.power(1e6, i / (n - 1))
def __call__(self, x):
f_no_noise = np.sqrt(np.dot(x, self.elli_factors * x))
return f_no_noise*self.rng.randn()*self.noiseamp
class EllipseMultNoise(object):
'''An implementation of a quadratic function of condition number 1e6
on which multiplicative noise is applied.'''
def __init__(self, n, noiseamp, rng=np.random.RandomState()):
self.noiseamp = noiseamp
self.rng = rng
self.elli_factors = np.zeros(n)
for i in range(n):
self.elli_factors[i] = np.power(1e6, i / (n - 1))
def __call__(self, x):
f_no_noise = np.sqrt(np.dot(x, self.elli_factors * x))
return f_no_noise*sigmoidal(self.rng.randn()*self.noiseamp)
def sphere(x):
'''Implements a quadratic function of condition number 1e0.'''
return np.sqrt(np.dot(x, x))
class SphereAddNoise(object):
'''An implementation of a quadratic function of condition number 1
on which additive noise is applied.'''
def __init__(self, n, noiseamp, rng=np.random.RandomState()):
self.noiseamp = noiseamp
self.rng = rng
def __call__(self, x):
f_no_noise = np.sqrt(np.dot(x, x))
return f_no_noise*self.rng.randn()*self.noiseamp
class SphereMultNoise(object):
'''An implementation of a quadratic function of condition number 1
on which multiplicative noise is applied.'''
def __init__(self, n, noiseamp, rng=np.random.RandomState()):
self.noiseamp = noiseamp
self.rng = rng
def __call__(self, x):
f_no_noise = np.sqrt(np.dot(x, x))
return f_no_noise*sigmoidal(rng.randn()*self.noiseamp)
def sigmoidal(x):
'''A sigmoidal function thresholding large values.'''
return 2 / (1 + np.exp(-x))