-
Notifications
You must be signed in to change notification settings - Fork 1
/
SFO.m
144 lines (97 loc) · 4.18 KB
/
SFO.m
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
function grad = SFO(d, x, sigma, type, algo, eta)
% Stochastic First Order Oracles
% type 1 = Multimodal function
temp = zeros(d,1);
if type == 1 % Multimodal function
if strcmp(algo,'ub') % Unbiased gradient
% g_2(x) = (sin((pi*x)/20)^5*(ln(2)*(x-10)*sin((pi*x)/20)-480*pi*cos((pi*x)/20)))/(25*2^((x^2-20*x+19300)/3200))
%Gaussian zero mean noise
noise = sigma*randn(d,1);
for i = 1:d
temp(i) = (sin((pi*x(i))/20)^5*(log(2)*(x(i)-10)*sin((pi*x(i))/20)-480*pi*cos((pi*x(i))/20)))/(25*2^((x(i)^2-20*x(i)+19300)/3200));
end
grad = temp + noise;
elseif strcmp(algo,'gs') % Biased gradient using Gaussian smoothing
% Generating perturbation
delta = randn(d,1);
x_plus = x + eta.*delta;
x_minus = x;
y_plus = SZO(d, x_plus, sigma, type);
y_minus = SZO(d, x_minus, sigma, type);
grad = ((y_plus - y_minus)/eta)*delta;
elseif strcmp(algo,'spsa') % Biased gradient using 1SPSA
% Generating perturbation
delta = 2*round(rand(d,1))-1;
x_plus = x + eta*delta;
x_minus = x - eta*delta;
y_plus = SZO(d, x_plus, sigma, type);
y_minus = SZO(d, x_minus, sigma, type);
grad = (y_plus - y_minus)./(2*eta.*delta);
elseif strcmp(algo,'rdsa_u') % Biased gradient using 1RDSA_Uniform
u = 1;
% Generate uniform [-u,u] perturbations
delta = unifrnd(-u,u,d,1);
x_plus = x + eta*delta;
x_minus = x - eta*delta;
y_plus = SZO(d, x_plus, sigma, type);
y_minus = SZO(d, x_minus, sigma, type);
grad = 3*((y_plus - y_minus)/(2*eta))*delta;
elseif strcmp(algo,'rdsa_ab') % Biased gradient using 1RDSA_AsymBer
% Generating Asymmetric Bernoulli perturbation
epsilon = 0.0001;
delta = zeros(d,1);
unifrands = unifrnd(0,1,d,1);
for j=1:d
if unifrands(j,1) < ((1+epsilon)/(2+epsilon))
delta(j,1) = -1;
else
delta(j,1) = 1+epsilon;
end
end
x_plus = x + eta*delta;
x_minus = x - eta*delta;
y_plus = SZO(d, x_plus, sigma, type);
y_minus = SZO(d, x_minus, sigma, type);
grad = (1/(1+epsilon))*((y_plus - y_minus)/(2*eta))*delta;
elseif strcmp(algo,'rdsa_lex') % Biased gradient using 1RDSA_Lex_DP
% Generating lexicograpic sequence
delta = zeros(3^d,d);
for t = 1:d
temp = [-1*ones(2*3^(d-t),1); 2*ones(3^(d-t),1)];
delta(:,t) = repmat(temp,3^(t-1),1);
end
grad = zeros(d,1);
for j = 1:3^d
x_plus = x + eta*delta(j,:)';
x_minus = x - eta*delta(j,:)';
y_plus = SZO(d, x_plus, sigma, type);
y_minus = SZO(d, x_minus, sigma, type);
grad = grad + delta(j,:)'*((y_plus - y_minus)/(2*eta));
end
grad = grad/(2*3^d);
elseif strcmp(algo,'rdsa_perm') % Biased gradient using 1RDSA_Perm_DP
% Generating permutation matrix
delta = eye(d);
delta = delta(randperm(d),:);
grad = zeros(d,1);
for j = 1:d
x_plus = x + eta*delta(j,:)';
x_minus = x - eta*delta(j,:)';
y_plus = SZO(d, x_plus, sigma, type);
y_minus = SZO(d, x_minus, sigma, type);
grad = grad + delta(j,:)'*((y_plus - y_minus)/(2*eta));
end
elseif strcmp(algo,'rdsa_kw') % Biased gradient using 1RDSA_kw_DP
% Generating permutation matrix
delta = eye(d);
grad = zeros(d,1);
for j = 1:d
x_plus = x + eta*delta(j,:)';
x_minus = x - eta*delta(j,:)';
y_plus = SZO(d, x_plus, sigma, type);
y_minus = SZO(d, x_minus, sigma, type);
grad(j,1) = (y_plus - y_minus)/(2*eta);
end
end
end
end