基于变色龙算法的线性规划问题求解matlab程序
1 变色龙算法
变色龙是爬行动物,是非常奇特的动物,它有适于树栖生活的种种特征和行为。避役的体长约15-25厘米,身体侧扁,背部有脊椎,头上的枕部有钝三角形突起。四肢很长,指和趾合并分为相对的两组,前肢前三指形成内组,四、五指形成外组;后肢一、二趾形成内组,奇特三趾形成外组,这样的特征非常适于握住树枝。它的尾巴长,能缠卷树枝。它有很长很灵敏的舌,伸出来要超过它的体长,舌尖上有腺体,能分泌大量粘液粘住昆虫。它一双眼睛十分奇特,眼帘很厚,呈环形,两只眼球突出,左右180度,上下左右转动自如,左右眼可以各自单独活动,不协调一致,这种现象在动物中是罕见的。双眼各自分工前后注视,既有利于捕食,又能及时发现后面的敌害。变色龙用长舌捕食是闪电式的,只需1/25秒便可以完成,而且它们的舌头的长度是自己身体的2倍。在树上一走一停的动作使天敌误以为是被风吹动的树叶。
变色龙算法(CSA)是一种仿生算法,在数学上模拟并实现了变色龙狩猎的行为步骤,包括搜索食物,眼睛360°旋转追踪食物及使用自身舌头定位并抓住猎物的行为。变色龙算法可以很好的运用在实际问题当中并取得好的表现。SAID等将CSA用于经济负荷调度问题的优化,并与其他算法进行了比较,仿真显示CSA计算结果更优。RIZK等设计了一种改进变色龙群算法,从而提取SOFC模型的可靠和精确参数。CSA对各类问题具有很强的计算能力和适应力。
2 算例
3 程序求解结果
4 程序
1)主程序
%% 基于变色龙算法的线性规划问题求解
clear
close all
clc
%% % Prepare the problem
dim = 3;
ub = 15*ones(1, dim);
lb = 0*ones(1, dim);
fobj = @Objfun;
%% % CSA parameters
noP = 500;
maxIter = 50;
[bestFitness, bestPosition, CSAConvCurve] =Chameleon(noP,maxIter,lb,ub,dim,fobj);
disp(['最优解为:', num2str(bestFitness)]);
disp(['最优变量为:', num2str(bestPosition)]);
2)子程序
%% Chameleon Swarm Algorithm (CSA) source codes version 1.0
%
% Developed in MATLAB R2018a
%
% Author and programmer: Malik Braik
%
% e-Mail: [email protected]
% [email protected]
%
% Main paper:
% Malik Sh. Braik,
% Chameleon Swarm Algorithm: A Bio-inspired Optimizer for Solving Engineering Design Problems
% Expert Systems with Applications
% DOI: https://doi.org/10.1016/j.eswa.2021.114685
%____________________________________________________________________________________
function [fmin0,gPosition,cg_curve]=Chameleon(searchAgents,iteMax,lb,ub,dim,fobj)
%%%%* 1
if size(ub,2)==1
ub=ones(1,dim)*ub;
lb=ones(1,dim)*lb;
end
% if size(ub,1)==1
% ub=ones(dim,1)*ub;
% lb=ones(dim,1)*lb;
% end
%% Convergence curve
cg_curve=zeros(1,iteMax);
%
% f1 = figure (1);
% set(gcf,'color','w');
% hold on
% xlabel('Iteration','interpreter','latex','FontName','Times','fontsize',10)
% ylabel('fitness value','interpreter','latex','FontName','Times','fontsize',10);
% grid;
%% Initial population
chameleonPositions=initialization(searchAgents,dim,ub,lb);% Generation of initial solutions
%% Evaluate the fitness of the initial population
fit=zeros(searchAgents,1);
for i=1:searchAgents
fit(i,1)=fobj(chameleonPositions(i,:));
end
%% Initalize the parameters of CSA
fitness=fit; % Initial fitness of the random positions
[fmin0,index]=min(fit);
chameleonBestPosition = chameleonPositions; % Best position initialization
gPosition = chameleonPositions(index,:); % initial global position
v=0.1*chameleonBestPosition;% initial velocity
v0=0.0*v;
%% Start CSA
% Main parameters of CSA
rho=1.0;
p1=2.0;
p2=2.0;
c1=2.0;
c2=1.80;
gamma=2.0;
alpha = 4.0;
beta=3.0;
%% Start CSA
for t=1:iteMax
a = 2590*(1-exp(-log(t)));
omega=(1-(t/iteMax))^(rho*sqrt(t/iteMax)) ;
p1 = 2* exp(-2*(t/iteMax)^2); %
p2 = 2/(1+exp((-t+iteMax/2)/100)) ;
mu= gamma*exp(-(alpha*t/iteMax)^beta) ;
ch=ceil(searchAgents*rand(1,searchAgents));
%% Update the position of CSA (Exploration)
for i=1:searchAgents
if rand>=0.1
chameleonPositions(i,:)= chameleonPositions(i,:)+ p1*(chameleonBestPosition(ch(i),:)-chameleonPositions(i,:))*rand()+...
+ p2*(gPosition -chameleonPositions(i,:))*rand();
else
for j=1:dim
chameleonPositions(i,j)= gPosition(j)+mu*((ub(j)-lb(j))*rand+lb(j))*sign(rand-0.50) ;
end
end
end
%% Rotation of the chameleons - Update the position of CSA (Exploitation)
%%% Rotation 180 degrees in both direction or 180 in each direction
%
% [chameleonPositions] = rotation(chameleonPositions, searchAgents, dim);
%% % Chameleon velocity updates and find a food source
for i=1:searchAgents
v(i,:)= omega*v(i,:)+ p1*(chameleonBestPosition(i,:)-chameleonPositions(i,:))*rand +....
+ p2*(gPosition-chameleonPositions(i,:))*rand;
chameleonPositions(i,:)=chameleonPositions(i,:)+(v(i,:).^2 - v0(i,:).^2)/(2*a);
end
v0=v;
%% handling boundary violations
for i=1:searchAgents
if chameleonPositions(i,:)<lb
chameleonPositions(i,:)=lb;
elseif chameleonPositions(i,:)>ub
chameleonPositions(i,:)=ub;
end
end
%% Relocation of chameleon positions (Randomization)
for i=1:searchAgents
ub_=sign(chameleonPositions(i,:)-ub)>0;
lb_=sign(chameleonPositions(i,:)-lb)<0;
chameleonPositions(i,:)=(chameleonPositions(i,:).*(~xor(lb_,ub_)))+ub.*ub_+lb.*lb_; %%%%%*2
fit(i,1)=fobj (chameleonPositions(i,:)) ;
if fit(i)<fitness(i)
chameleonBestPosition(i,:) = chameleonPositions(i,:); % Update the best positions
fitness(i)=fit(i); % Update the fitness
end
end
%% Evaluate the new positions
[fmin,index]=min(fitness); % finding out the best positions
% Updating gPosition and best fitness
if fmin < fmin0
gPosition = chameleonBestPosition(index,:); % Update the global best positions
fmin0 = fmin;
end
%% Print the results
% outmsg = ['Iteration# ', num2str(t) , ' Fitness= ' , num2str(fmin0)];
% disp(outmsg);
%% Visualize the results
cg_curve(t)=fmin0; % Best found value until iteration t
% if t>2
% set(0, 'CurrentFigure', f1)
%
% line([t-1 t], [cg_curve(t-1) cg_curve(t)],'Color','b');
% title({'Convergence characteristic curve'},'interpreter','latex','FontName','Times','fontsize',12);
% xlabel('Iteration');
% ylabel('Best score obtained so far');
% drawnow
% end
end
ngPosition=find(fitness== min(fitness));
g_best=chameleonBestPosition(ngPosition(1),:); % Solutin of the problem
fmin0 =fobj(g_best);
end
。。。。。。略