数学建模--粒子群算法模版

来源:互联网 发布:中国网络战部队 编辑:程序博客网 时间:2024/06/06 21:44

数学建模

数学建模中常用的启发式算法,这里整合一下模版,方便直接使用

原文:
http://blog.csdn.net/qq_34861102/article/details/77900277


求解下列非线性规划最优解

min f(x) = x(1)^2 + x(2)^2 + 8;         s.t.         x(1)^2 - x(2) >= 0;         -x(1) - x(2)^2 + 2 = 0;         x(1),x(2) >=0 

主函数:

clc;format long;%------给定初始化条件---------------------------------------c1=2;             %学习因子1c2=2;             %学习因子2w=0.7;            %惯性权重MaxDT=1000;       %最大迭代次数D = 1;M=40;             %初始化群体个体数目eps=1;      %设置精度(在已知最小值时候用)%------初始化种群的个体(可以在这里限定位置和速度的范围)----------for i=1:M    for j=1:D        x(i,j)=rand; %随机初始化位置        v(i,j)=rand; %随机初始化速度    endend%------先计算各个粒子的适应度,并初始化p(i)和gbest-------------for i=1:M    p(i)=fitness(x(i,:));    y(i,:)=x(i,:);endgbest=x(1,:);             %gbest为全局最优for i=2:M    if fitness(x(i,:)) < fitness(gbest)        gbest=x(i,:);    endend%------进入主要循环,按照公式依次迭代,直到满足精度要求----------for t=1:MaxDT    for i=1:M        v(i,:)=w*v(i,:)+c1*rand*(y(i,:)-x(i,:))+c2*rand*(gbest-x(i,:));        x(i,:)=x(i,:)+v(i,:);        if fitness(x(i,:))<p(i)            p(i)=fitness(x(i,:));            y(i,:)=x(i,:);        end        if p(i)<fitness(gbest)            gbest=y(i,:);        end    endendSolution=gbest'Result=fitness(gbest)

适应度函数:

function objvalue = fitness(X)x2 = X;x1 = 2-X^2;    if x1^2-x2>=0 && -x1-x2^2+2==0 && x1>=0 &&x2>=0            objvalue = x1^2+x2^2+8;        else            objvalue = 1000;    end end
原创粉丝点击