-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathsa_demo.m
145 lines (121 loc) · 2.65 KB
/
sa_demo.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
145
function [x,N]=random_search(funcname,xnew,options);
%Simulated annealing demo
% random_search(funcname,xnew,options);
% print = options(1);
% gamma = options(15);
% alpha = options(18);
if nargin ~= 3
options = [];
if nargin ~= 2
disp('Wrong number of arguments.');
return;
end
end
if length(options) >= 14
if options(14)==0
options(14)=1000*length(xnew);
end
else
options(14)=1000*length(xnew);
end
if length(options) < 15
options(15)=5.0; %
end
if options(15)==0
options(15)=5.0; %
end
if length(options) < 18
options(18)=0.5; %optional step size
end
%clc;
format compact;
format short e;
options = foptions(options);
print = options(1);
epsilon_x = options(2);
epsilon_g = options(3);
max_iter=options(14);
alpha = options(18);
gamma = options(15);
k0=2;
if funcname == 'f_r',
ros_cnt
elseif funcname == 'f_p',
pks_cnt;
end %if
if length(xnew) == 2
plot(xnew(1),xnew(2),'o')
text(xnew(1),xnew(2),'Start Point')
xlower = [-2;-1];
xupper = [2;3];
end
f_0=feval(funcname,xnew);
xbestcurr = xnew;
xbestold = xnew;
xcurr = xnew;
f_best=feval(funcname,xnew);
f_best=10^(sign(f_best))*f_best;
for k = 1:max_iter,
f_curr=feval(funcname,xcurr);
xnew = xcurr + alpha*(2*rand(length(xcurr),1)-1);
for i=1:length(xnew),
xnew(i) = max(xnew(i),xlower(i));
xnew(i) = min(xnew(i),xupper(i));
end %for
f_new=feval(funcname,xnew);
%if abs(f_best-f_new) <= epsilon_g*abs(f_best),
%disp('Terminating: relative function difference less than');
%disp(epsilon_g);
%k=k-1;
%break;
%end %if
if f_new < f_curr,
xcurr = xnew;
f_curr = f_new;
else
cointoss = rand(1);
Temp = gamma/log(k+k0);
Prob = exp(-(f_new-f_curr)/Temp);
if cointoss < Prob,
xcurr = xnew;
f_curr = f_new;
end
end
if f_new < f_best,
xbestold = xbestcurr;
xbestcurr = xnew;
f_best = f_new;
end
if print,
disp('Iteration number k =')
disp(k); %print iteration index k
disp('alpha =');
disp(alpha); %print alpha
disp('New point =');
disp(xnew'); %print new point
disp('Function value =');
disp(f_new); %print func value at new point
end %if
if norm(xnew-xbestold) <= epsilon_x*norm(xbestold)
disp('Terminating: Norm of difference between iterates less than');
disp(epsilon_x);
break;
end %if
pltpts(xbestcurr,xbestold);
if k == max_iter
disp('Terminating with maximum number of iterations');
end %if
end %for
if nargout >= 1
x=xnew;
if nargout == 2
N=k;
end
else
disp('Final point =');
disp(xbestcurr');
disp('Objective function value =');
disp(f_best);
disp('Number of iterations =');
disp(k);
end %if