pso c语言程序,C语言实现基本PSO算法
粒子群算法的流程图如上,看了好多版本,这个最靠谱,我的main函数完全按照这个来,好理解过程:
int main(int argc, const char *argv[])
{
int n=0;
//printf("Random Initialization of the swarm:\n\n");
RandInitofSwarm();
//printf("Computation of the fitness of each particle:\n");
ComputFitofSwarm();
//printf("FirstComputPandGbest:\n");
FirstComputPandGbest();
while(n++!=N)
{
printf("The %dth time to calculate .\n",n );
//printf("Updated of the swarm:\n\n");
UpdateofVandX();
//printf("Updated of the swarm's Fitness:\n");
ComputFitofSwarm();
//printf("Replaced of P and Gbest:\n\n");
UpdatePandGbest();
}
getchar();
return 0;
}各种printf用来调试自己的信息,可以去掉看看中间值~~~
先来pso.h
现在执行最简单的功能,用粒子群搜索二维空间:y=x^2+y^+3的最大值~~~x,y的范围属于(-100,100),那最大值就是20003,伪随机粒子数20个,最大迭代次数为40次,最大搜索速度为2,惯性权重W设为1.4,学习因子c1=c2=2,看能不能找到最大值~~~
#ifndef _PSO_H_
#define _PSO_H_
#define Dim 2
#define PNum 20
#define N 40
typedef struct PARTICLE{
double X[Dim];
double P[Dim];
double V[Dim];
double Fitness;
}particle;
typedef struct SWARM{
particle Particle[PNum];
int GBestIndex;
double GBest[Dim];
double W;
double C1;
double C2;
double Xup[Dim];
double Xdown[Dim];
double Vmax[Dim];
}swarm;
void RandInitofSwarm(void);
void ComputFitofSwarm(void);
void FirstComputPandGbest(void);
void UpdateofVandX(void);
void UpdatePandGbest(void);
double InertWeight(void);
int CriteriaofStop(void);
#endif
pso.c
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "pso.h"
#include "stdio.h"
swarm *s=(swarm *)malloc(sizeof(swarm));
particle *p=(particle *)malloc(sizeof(particle));
int main(int argc, const char *argv[])
{
int n=0;
//printf("Random Initialization of the swarm:\n\n");
RandInitofSwarm();
//printf("Computation of the fitness of each particle:\n");
ComputFitofSwarm();
//printf("FirstComputPandGbest:\n");
FirstComputPandGbest();
while(n++!=N)
{
printf("The %dth time to calculate .\n",n );
//printf("Updated of the swarm:\n\n");
UpdateofVandX();
//printf("Updated of the swarm's Fitness:\n");
ComputFitofSwarm();
//printf("Replaced of P and Gbest:\n\n");
UpdatePandGbest();
}
getchar();
return 0;
}
//Random Initialization of the swarm
void RandInitofSwarm(void)
{
int i,j;
s->W=1.4;
s->C1=2.0;
s->C2=2.0;
for(j=0;j
{
s->Xdown[j] = -100;
s->Xup[j] = 100;
s->Vmax[j] = 2;
}
srand((unsigned)time(NULL));
for(i=0; i
{
//printf(" The %dth of X is: ",i);
for(j=0; j
{
s->Particle[i].X[j] = rand()/(double)RAND_MAX*(s->Xup[j]-s->Xdown[j])+s->Xdown[j];//-100~100
s->Particle[i].V[j] = rand()/(double)RAND_MAX*s->Vmax[j]*2-s->Vmax[j];//-2~2
//printf(" %.2f \n ",s->Particle[i].X[j]);
}
}
}
//Computation of the fitness of each particle
void ComputFitofSwarm(void)
{
int i;
srand((unsigned)time(NULL));
for(i=0; i
{
//printf(" The Fitness of %dth Particle: ",i);
s->Particle[i].Fitness = s->Particle[i].X[0]*s->Particle[i].X[0]+s->Particle[i].X[1]*s->Particle[i].X[1]+3;
//printf(" %.2f\n",s->Particle[i].Fitness);
}
}
void FirstComputPandGbest(void)
{
int i,j;
//P=X;
for(i=0; i
{
for(j=0;j
{
s->Particle[i].P[j]=s->Particle[i].X[j];
}
}
//Computation of GBest
s->GBestIndex = 0;
for(i=0; i
if(s->Particle[i].Fitness>=s->Particle[s->GBestIndex].Fitness)
s->GBestIndex = i;
for(j=0;j
{
s->GBest[j]=s->Particle[s->GBestIndex].P[j];
}
printf("GBestIndex , GBest , Fitness of GBest:%d ,%.2f ,%.2f ,%.2f \n",
s->GBestIndex ,s->GBest[0],s->GBest[1],
s->Particle[s->GBestIndex].Fitness);
}
//update V and X
void UpdateofVandX(void)
{
int i,j;
srand((unsigned)time(NULL));
for(i=0; i
{
//printf(" The %dth of X is: ",i);
for(j=0; j
s->Particle[i].V[j] = s->W*s->Particle[i].V[j]+
rand()/(double)RAND_MAX*s->C1*(s->Particle[i].P[j] - s->Particle[i].X[j])+
rand()/(double)RAND_MAX*s->C2*(s->GBest[j] - s->Particle[i].X[j]);
for(j=0; j
{
if(s->Particle[i].V[j]>s->Vmax[j])
s->Particle[i].V[j] = s->Vmax[j];
if(s->Particle[i].V[j]Vmax[j])
s->Particle[i].V[j] = -s->Vmax[j];
}
for(j=0; j
{
s->Particle[i].X[j] += s->Particle[i].V[j];
if(s->Particle[i].X[j]>s->Xup[j])
s->Particle[i].X[j]=s->Xup[j];
if(s->Particle[i].X[j]Xdown[j])
s->Particle[i].X[j]=s->Xdown[j];
}
//printf(" %.2f %.2f \n",s->Particle[i].X[0],s->Particle[i].X[1]);
}
}
static double ComputAFitness(double X[])
{
return X[0]*X[0]+X[1]*X[1]+3;
}
void UpdatePandGbest(void)
{
int i,j;
//update of P if the X is bigger than current P
for (i = 0; i < PNum; i++)
{
//printf(" The %dth of P is: ",i);
if (s->Particle[i].Fitness > ComputAFitness(s->Particle[i].P))
{
for(j=0;j
{
s->Particle[i].P[j] = s->Particle[i].X[j];
}
}
//printf(" %.2f %.2f \n",s->Particle[i].P[0],s->Particle[i].P[1]);
}
for (i = 0; i < PNum; i++)
{
//printf("The %dth of P's Fitness is : %.2f \n",i,ComputAFitness(s->Particle[i].P));
}
//update of GBest
for(i=0; i
if(ComputAFitness(s->Particle[i].P) >= s->Particle[s->GBestIndex].Fitness)
s->GBestIndex = i;
for(j=0;j
{
s->GBest[j]=s->Particle[s->GBestIndex].P[j];
}
printf("GBestIndex , GBest , Fitness of GBest:%d ,%.2f ,%.2f ,%.2f \n",
s->GBestIndex ,s->GBest[0],s->GBest[1],
ComputAFitness(s->GBest));
}
double InertWeight(void)
{
return 1.0;
}
int CriteriaofStop()
{
int n=N;
return (n--==0);
}
ps:以上全英文的原因是因为,Subline Test2不支持中文输入,坑啊~~~
C语言实现部分没有复杂的语法,要优化下,不过现在我用printf一步一步打印出来的,基本上实现了,关键是理解流程图,幸亏流程图写的简单明了~~~小bug估计还不少~~~
至于内存申请木有释放,就先放着吧,也不多,呵呵~~~但是要释放得需要先释放particle,再释放swarm~~~不然就内存泄漏了~~~
实验结果:
前天刚看到几个非常变态的函数,什么有276个局部最小点的,最大值在最小值周围的~~~可以各种虐啊~~~
Version0.0——基本PSO
论文:在google学术里面搜particle swarm optimization 被引用20000+的论文就是鼻祖了~~~是的,本文就是实现最基本的~~~
pso c语言程序,C语言实现基本PSO算法相关推荐
- 棱形旋转c语言程序_C 语言时隔 5 年重回巅峰,这 20 个热门项目拿去练手!
在上个月的 TIOBE 编程语言排名中,C 语言和 Java 的差距只有 0.01%.在近日 TIOBE 公布的 2020 年 5 月编程语言排行榜中,C 语言成功超越了 Java,重返第一的王者宝座 ...
- 蜂鸣器发出7种音阶c语言程序_C语言编程新思路
第一章 单元测试 1.单选题: 关于一个C语言程序执行的起点和终点,以下选项正确的是 ( ). 选项: A: main 任意 B: main 最后一个 C: main main D: 第一个 最后一个 ...
- 文件的记录c语言程序,c语言程序学生籍贯信息记录簿设计.docx
c 语言程序学生籍贯信息记录簿设计 学生籍贯信息记录簿 课程设计报告书 班 级: 方 0909-1 学 号:姓 名: 苑 小 叶 指导教师 : 康 亚 男 石家庄铁道大学四方学院 2010年 07月 ...
- 学生实验平台搭建c语言程序,c语言程序设计实验学生用.doc
c语言程序设计实验学生用 C语言程序设计 实验指导 (学生用) 计算机基础教研室 <C语言程序设计>课程组 2012年9月 前 言 <C语言程序设计>是计算机科学技术系面向全校 ...
- 插入法排序c语言程序,C语言之插入排序算法
一.什么是直接插入算法? 直接插入排序是一种简单的插入排序法,其基本思想是:把待排序的纪录按其关键码值的大小逐个插入到一个已经排好序的有序序列中,直到所有的纪录插入完为止,得到一个新的有序序列. 选择 ...
- 青花瓷音乐的单片机c语言程序,c语言曲谱_单片机c语言音乐简谱代码
51单片机曲谱编写音乐程序,我看不懂曲谱,谁能教下我! //<世上只有妈妈好>51单片机C语言程序和音乐采灯程序 //此程序在硬件上调试通过 //本程序的单片机晶振采用11.0592MHZ ...
- 框图c语言程序,C语言程序设计框图
<C语言程序设计框图>由会员分享,可在线阅读,更多相关<C语言程序设计框图(86页珍藏版)>请在人人文库网上搜索. 1.第三章控制结构,返回总目录,目录,3.1节目结构框,3. ...
- 简单谱子C语言程序,c语言曲谱_单片机c语言音乐简谱代码
51单片机曲谱编写音乐程序,我看不懂曲谱,谁能教下我! //<世上只有妈妈好>51单片机C语言程序和音乐采灯程序 //此程序在硬件上调试通过 //本程序的单片机晶振采用11.0592MHZ ...
- 用直接分解法求方程组的C语言程序,c语言编程求解线性方程组论文
计算机编程求解线性方程组 第一章 绪 论 在自然科学.工程技术.经济和医学各领域中产生的许多实际问题都可以通过数学语言描述为数学问题,也就是说,由实际问题建立数学模型,然后应用各种数学方法和技巧来求解 ...
- 记得每天锻炼身体c语言程序,c语言程序
一实验名称计算出1000以内10个最大素数之和二.实验目的1.熟练掌握if.if-else.if-else if语句和witch语句格式及使用方法,掌握if语句中的嵌套关系和匹配原则,利用if语句和s ...
最新文章
- ExecutorService与Executors例子的简单剖析(转)
- 三十六亿的《哪吒》历时五年,如何用AI解决动画创作难题?
- 把热带雨林搬进办公室!这样的互联网公司!我愿意加班至死!
- 【2017上半年中国AI融资英雄榜】TOP10融资50亿元,二八定律明显
- 安装itunes需要管理员身份_Windows 10 在microsoft store 微软商店里安装的itunes如何更改备份位置...
- wk一sm5时间温度控制器_新能源汽车电机控制器温度计算及其模型—DC电容篇
- hdu4869 费马小+快速幂
- WaitForSingleObject的用法举例
- 互联网架构,究竟为啥要做服务化?
- Qt Creator添加套件
- Mongodb和redis书籍调研
- 一种通过变量插值读取属性的方法
- PLSQL触发器随笔
- centos7 端口相关操作
- 6.表单提交,input键盘变搜索,有关自定义属性input操作
- @所有人 Flink Forward Asia 2020 议题征集倒计时!
- 计算机与科学 研究生考试内容,计算机科学与技术考研考哪些科目 备考技巧有哪些...
- 将微信数据提取为exel表格(2022年版)免root 保姆级教程
- 操作系统学习笔记:大容量存储器的结构
- 怎样提高计算机内存,电脑物理内存不足怎么提高 电脑物理内存占用过高的解决方法...
热门文章
- python2安装tkinter_Ubuntu下安装Python2.6.1以及Tkinter
- VMware Workstation下载与安装(适用于在官网注册好账号的朋友,许可证秘钥请自行网上搜索获取)
- Licode入门学习:MediaStream源码分析(三)
- 戴尔 XPS 13安装Ubuntu 16.04和Windows 10双系统
- linux xampp 环境变量,XAMPP相关:Linux学习-环境变量和可执行属性
- Node.js安装教程(Windows)
- 赛思互动:CRM呼叫中心如何实现高效运营管理
- 图像倾斜校正 Hough校正 原理及函数
- html三角形坐标图怎么看,地理中三角图_高中地理题那种三角形的坐标图要怎么看啊_淘题吧...
- java小练习(人品计算机)