-
Notifications
You must be signed in to change notification settings - Fork 0
/
localSearchCircle.m
50 lines (40 loc) · 1.71 KB
/
localSearchCircle.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
function x=localSearchCircle(funcToOptimize,proposalFunc,startingX,epsilon,lowerBoundOnX,upperBoundOnX,timeDelta,funcDelta)
% This general-purpose optimization routine aims to minimize funcToOptimize
% starting from the initial guess startingX, via proposals generated by
% proposalFunc (the two functions are specified via function handles). When
% optimizing a function whose inputs have lower and upper bounds, the
% proposal xp=proposalFunc(x) has all of its entries less than
% lowerBoundOnX rounded up to lowerBoundOnX, and all of its entries greater
% than upperBoundOnX rounded up to upperBoundOnX before funcToOptimize is
% evaluted on xp (these bounds can be -inf and +inf to make them effectively
% ignored. The proposal is accepted if it improves the function value, or does
% not make it worse by more than epsilon. The routine terminates when timeDelta
% seconds have passed without the best value found so far of funcToOptimize
% improving by funcDelta.
x = startingX;
UB = upperBoundOnX;
LB = lowerBoundOnX;
while true
t0 = now*60*60*24;
t10 = t0;
x_t0 = x;
while (abs(t10-t0)<=timeDelta)
xp = proposalFunc(x);
acceptBound = funcToOptimize(x,0) + epsilon;
% to sanitise the input xp
for i = 1:length(xp), xp(i) = min(max(xp(i), LB), UB); end
f_xp = funcToOptimize(xp,0);
if (f_xp <= acceptBound), x = xp; end
t10 = now*60*60*24;
end
if (abs(funcToOptimize(x,0) - funcToOptimize(x_t0,0))<funcDelta)
disp('we are done.');
break;
else
disp('shalimar');
% xcoords = x(1:2:19);
% ycoords = x(2:2:20);
UB = max(x);
LB = min(x);
end
end