📄 ars.m
字号:
function [x_min,f_min]=ARS(ARSOptions)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ARS->function for accelerated random search! %
% %
%USAGE: [x_min,f_min]=ARS(ARSOptions) %
% %
% Arguments : ARSOptions--> A Matlab stucture containing all ARS %
% related options. (see also: get_ARSOptions) %
%Return values: [f_min,x_min] %
% | | %
% | |_________________the minimum coordinates %
% |_______________________the minimum function values %
% %
%History : Author : caodongsheng-orient %
% created on : 2007.6.12 %
% comments :the accelerated random search(ARS) %
% %
% see also: get_ARSOptions %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initializations
if nargin == 0
arsOptions = get_ARSOptions;
end
Dim=ARSOptions.Vars.Dim;
var_flags=ARSOptions.ARSpara.flags;
if length(ARSOptions.Obj.lb)==1
low=repmat(ARSOptions.Obj.lb,Dim,1);
up=repmat(ARSOptions.Obj.ub,Dim,1);
else
low=ARSOptions.Obj.lb;
up=ARSOptions.Obj.ub;
end
if all(low<up)==0
disp('the interval is wrong!')
disp('Please input the correct interval!')
end
factor_contract=ARSOptions.ARSpara.factor_contract;
Nrestart_max=ARSOptions.ARSpara.Nrestart_max;
limit=ARSOptions.Vars.limit;
% Dim=size(low,1);
xx_min=[];ff_min=[];%save the best value and coordinates of every iteration!
iter_max=ARSOptions.Vars.Iterations;
tic;
% iter_max=20;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% BEGINNING OF ITERATION %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for iter=1:iter_max
if var_flags
current_state=zeros(Dim,1);
for i=1:Dim
current_state(i)=rand*(up(i)-low(i))+low(i);
end
else
if size(ARSOptions.Vars.initialvalue,1)==Dim
current_state=ARSOptions.Vars.initialvalue;
else
% error('You have a wrong intial value,Please repeat input!')
disp('You have a wrong intial value!')
disp('Please repeat to input!')
break;
end
end
if all(current_state>=low¤t_state<=up)
original_state=current_state;%get the initial result!
f2eval=ARSOptions.Obj.f2eval;
f_current=feval(f2eval,original_state);
else
disp('the initial value is not in the interval!')
disp('Please input the correct initial value!')
break;
end
% f_current=obj_fun(original_state)%get the intial obj value!
% xx_min=[xx_min,current_state];
% ff_min=[ff_min,f_current];
% limit=ARSOptions.Vars.limit;
% limit=0.000000001;
radius=ARSOptions.ARSpara.radius;
% radius=2;%initalize the radius equals 1!
time=1;
% Nrestart_max=ARSOptions.ARSpara.Nrestart_max;
% Nrestart_max=4;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%restart
while time<Nrestart_max
flag=1;
while flag
vr=2*rand(Dim,1)-1;
vr=vr/norm(vr,2);
x_cycle=vr.*radius+current_state;
lamda=rand;
x_current=lamda*current_state+(1-lamda)*x_cycle;
% x_cycle=current_state+vr;
% x_current=(1-lamda).*current_state+lamda.*x_cycle;
% if radius==1
% x_cycle=current_state+vr;
% x_current=lamda*current_state+(radius-lamda)*x_cycle;
% else
% x_cycle=current_state+vr;
% x_current=lamda*current_state+(1-lamda)*x_cycle;
% x_current=x_current.* radius;
% end
% x_current=current_state+vr.*radius;
%ensure the new coordinate between the low and up!
% for i=1:Dim
if all(x_current>=low&x_current<=up)
flag=0;
else
flag=1;
end
% end
end
% f2eval=ARSOptions.Obj.f2eval;
f_x=feval(f2eval,x_current);%obtain the new coordinate's functional values!
% f_x=obj_fun(x_current);%%get the current obj value!
if f_x<f_current
current_state=x_current;
xxx_min=x_current;
fff_min=f_x;
xx_min=[xx_min,xxx_min];
ff_min=[ff_min,fff_min];
radius=ARSOptions.ARSpara.radius;
% radius=1;
% f2eval=ARSOptions.Obj.f2eval;
f_current=feval(f2eval,current_state);
% f_current=obj_fun(current_state);
else
% current_state=current_state;
% xx_min=[xx_min,current_state];
% ff_min=[ff_min,f_current];
% factor_contract=ARSOptions.ARSpara.factor_contract;
% factor_contract=2;
radius=radius/factor_contract;%generate a (0.1] radius,
if radius<limit
radius=1;
time=time+1;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%print the every iteration's results!
if rem(iter_max,1)==0
disp(sprintf('%4d\t\t\t%.8g\t\t\t%5d', iter, fff_min, iter*time));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% END OF ITERATION %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%compute the time of iteration
run_time=toc
%obtain the best value and the coordinate'sopsitions.
[f_min,opsition]=min(ff_min);
%obtain the the best value's coordinate.
x_min=xx_min(:,opsition);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -