展开全部

/* ABC algorithm coded using C programming language */

/* Artificial Bee Colony (ABC) is one of the most recently defined algorithms by Dervis Karaboga in 2005,

motivated by the intelligent behavior of honey bees. */

/* Referance Papers*/

/*D. Karaboga, AN IDEA BASED ON HONEY BEE SWARM FOR NUMERICAL OPTIMIZATION,TECHNICAL REPORT-TR06, Erciyes University, Engineering Faculty, Computer Engineering Department 2005.*/

/*D. Karaboga, B. Basturk, A powerful and Efficient Algorithm for Numerical Function Optimization: Artificial Bee Colony (ABC) Algorithm, Journal of Global Optimization, Volume:39, Issue:3,pp:459-171, November 2007,ISSN:0925-5001 , doi: 10.1007/s10898-007-9149-x */

/*D. Karaboga, B. Basturk, On The Performance Of Artificial Bee Colony (ABC) Algorithm, Applied Soft Computing,Volume 8, Issue 1, January 2008, Pages 687-697. */

/*D. Karaboga, B. Akay, A Comparative Study of Artificial Bee Colony Algorithm, Applied Mathematics and Computation, 214, 108-132, 2009. */

/*Copyright © 2009 Erciyes University, Intelligent Systems Research Group, The Dept. of Computer Engineering*/

/*Contact:

Dervis Karaboga (karaboga@62616964757a686964616fe59b9ee7ad9431333264626562erciyes.edu.tr )

Bahriye Basturk Akay (bahriye@erciyes.edu.tr)

*/

#include

#include

#include

#include

#include

/* Control Parameters of ABC algorithm*/

#define NP 20 /* The number of colony size (employed bees+onlooker bees)*/

#define FoodNumber NP/2 /*The number of food sources equals the half of the colony size*/

#define limit 100 /*A food source which could not be improved through "limit" trials is abandoned by its employed bee*/

#define maxCycle 2500 /*The number of cycles for foraging {a stopping criteria}*/

/* Problem specific variables*/

#define D 100 /*The number of parameters of the problem to be optimized*/

#define lb -100 /*lower bound of the parameters. */

#define ub 100 /*upper bound of the parameters. lb and ub can be defined as arrays for the problems of which parameters have different bounds*/

#define runtime 30 /*Algorithm can be run many times in order to see its robustness*/

double Foods[FoodNumber][D]; /*Foods is the population of food sources. Each row of Foods matrix is a vector holding D parameters to be optimized. The number of rows of Foods matrix equals to the FoodNumber*/

double f[FoodNumber]; /*f is a vector holding objective function values associated with food sources */

double fitness[FoodNumber]; /*fitness is a vector holding fitness (quality) values associated with food sources*/

double trial[FoodNumber]; /*trial is a vector holding trial numbers through which solutions can not be improved*/

double prob[FoodNumber]; /*prob is a vector holding probabilities of food sources (solutions) to be chosen*/

double solution [D]; /*New solution (neighbour) produced by v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) j is a randomly chosen parameter and k is a randomlu chosen solution different from i*/

double ObjValSol; /*Objective function value of new solution*/

double FitnessSol; /*Fitness value of new solution*/

int neighbour, param2change; /*param2change corrresponds to j, neighbour corresponds to k in equation v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij})*/

double GlobalMin; /*Optimum solution obtained by ABC algorithm*/

double GlobalParams[D]; /*Parameters of the optimum solution*/

double GlobalMins[runtime]; /*GlobalMins holds the GlobalMin of each run in multiple runs*/

double r; /*a random number in the range [0,1)*/

/*a function pointer returning double and taking a D-dimensional array as argument */

/*If your function takes additional arguments then change function pointer definition and lines calling "...=function(solution);" in the code*/

typedef double (*FunctionCallback)(double sol[D]);

/*benchmark functions */

double sphere(double sol[D]);

double Rosenbrock(double sol[D]);

double Griewank(double sol[D]);

double Rastrigin(double sol[D]);

/*Write your own objective function name instead of sphere*/

FunctionCallback function = &sphere;

/*Fitness function*/

double CalculateFitness(double fun)

{

double result=0;

if(fun>=0)

{

result=1/(fun+1);

}

else

{

result=1+fabs(fun);

}

return result;

}

/*The best food source is memorized*/

void MemorizeBestSource()

{

int i,j;

for(i=0;i

{

if (f[i]

{

GlobalMin=f[i];

for(j=0;j

GlobalParams[j]=Foods[i][j];

}

}

}

/*Variables are initialized in the range [lb,ub]. If each parameter has different range, use arrays lb[j], ub[j] instead of lb and ub */

/* Counters of food sources are also initialized in this function*/

void init(int index)

{

int j;

for (j=0;j

{

r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );

Foods[index][j]=r*(ub-lb)+lb;

solution[j]=Foods[index][j];

}

f[index]=function(solution);

fitness[index]=CalculateFitness(f[index]);

trial[index]=0;

}

/*All food sources are initialized */

void initial()

{

int i;

for(i=0;i

{

init(i);

}

GlobalMin=f[0];

for(i=0;i

GlobalParams[i]=Foods[0][i];

}

void SendEmployedBees()

{

int i,j;

/*Employed Bee Phase*/

for (i=0;i

{

/*The parameter to be changed is determined randomly*/

r = ((double)rand() / ((double)(RAND_MAX)+(double)(1)) );

param2change=(int)(r*D);

/*A randomly chosen solution is used in producing a mutant solution of the solution i*/

r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );

neighbour=(int)(r*FoodNumber);

/*Randomly selected solution must be different from the solution i*/

while(neighbour==i)

{

r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );

neighbour=(int)(r*FoodNumber);

}

for(j=0;j

solution[j]=Foods[i][j];

/*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */

r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );

solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;

/*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/

if (solution[param2change]

solution[param2change]=lb;

if (solution[param2change]>ub)

solution[param2change]=ub;

ObjValSol=function(solution);

FitnessSol=CalculateFitness(ObjValSol);

/*a greedy selection is applied between the current solution i and its mutant*/

if (FitnessSol>fitness[i])

{

/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/

trial[i]=0;

for(j=0;j

Foods[i][j]=solution[j];

f[i]=ObjValSol;

fitness[i]=FitnessSol;

}

else

{ /*if the solution i can not be improved, increase its trial counter*/

trial[i]=trial[i]+1;

}

}

/*end of employed bee phase*/

}

/* A food source is chosen with the probability which is proportioal to its quality*/

/*Different schemes can be used to calculate the probability values*/

/*For example prob(i)=fitness(i)/sum(fitness)*/

/*or in a way used in the metot below prob(i)=a*fitness(i)/max(fitness)+b*/

/*probability values are calculated by using fitness values and normalized by dividing maximum fitness value*/

void CalculateProbabilities()

{

int i;

double maxfit;

maxfit=fitness[0];

for (i=1;i

{

if (fitness[i]>maxfit)

maxfit=fitness[i];

}

for (i=0;i

{

prob[i]=(0.9*(fitness[i]/maxfit))+0.1;

}

}

void SendOnlookerBees()

{

int i,j,t;

i=0;

t=0;

/*onlooker Bee Phase*/

while(t

{

r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );

if(r

{

t++;

/*The parameter to be changed is determined randomly*/

r = ((double)rand() / ((double)(RAND_MAX)+(double)(1)) );

param2change=(int)(r*D);

/*A randomly chosen solution is used in producing a mutant solution of the solution i*/

r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );

neighbour=(int)(r*FoodNumber);

/*Randomly selected solution must be different from the solution i*/

while(neighbour==i)

{

r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );

neighbour=(int)(r*FoodNumber);

}

for(j=0;j

solution[j]=Foods[i][j];

/*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */

r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );

solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;

/*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/

if (solution[param2change]

solution[param2change]=lb;

if (solution[param2change]>ub)

solution[param2change]=ub;

ObjValSol=function(solution);

FitnessSol=CalculateFitness(ObjValSol);

/*a greedy selection is applied between the current solution i and its mutant*/

if (FitnessSol>fitness[i])

{

/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/

trial[i]=0;

for(j=0;j

Foods[i][j]=solution[j];

f[i]=ObjValSol;

fitness[i]=FitnessSol;

}

else

{ /*if the solution i can not be improved, increase its trial counter*/

trial[i]=trial[i]+1;

}

} /*if */

i++;

if (i==FoodNumber-1)

i=0;

}/*while*/

/*end of onlooker bee phase */

}

/*determine the food sources whose trial counter exceeds the "limit" value. In Basic ABC, only one scout is allowed to occur in each cycle*/

void SendScoutBees()

{

int maxtrialindex,i;

maxtrialindex=0;

for (i=1;i

{

if (trial[i]>trial[maxtrialindex])

maxtrialindex=i;

}

if(trial[maxtrialindex]>=limit)

{

init(maxtrialindex);

}

}

/*Main program of the ABC algorithm*/

int main()

{

int iter,run,j;

double mean;

mean=0;

srand(time(NULL));

for(run=0;run

{

initial();

MemorizeBestSource();

for (iter=0;iter

{

SendEmployedBees();

CalculateProbabilities();

SendOnlookerBees();

MemorizeBestSource();

SendScoutBees();

}

for(j=0;j

{

printf("GlobalParam[%d]: %f\n",j+1,GlobalParams[j]);

}

printf("%d. run: %e \n",run+1,GlobalMin);

GlobalMins[run]=GlobalMin;

mean=mean+GlobalMin;

}

mean=mean/runtime;

printf("Means of %d runs: %e\n",runtime,mean);

getch();

}

double sphere(double sol[D])

{

int j;

double top=0;

for(j=0;j

{

top=top+sol[j]*sol[j];

}

return top;

}

double Rosenbrock(double sol[D])

{

int j;

double top=0;

for(j=0;j

{

top=top+100*pow((sol[j+1]-pow((sol[j]),(double)2)),(double)2)+pow((sol[j]-1),(double)2);

}

return top;

}

double Griewank(double sol[D])

{

int j;

double top1,top2,top;

top=0;

top1=0;

top2=1;

for(j=0;j

{

top1=top1+pow((sol[j]),(double)2);

top2=top2*cos((((sol[j])/sqrt((double)(j+1)))*M_PI)/180);

}

top=(1/(double)4000)*top1-top2+1;

return top;

}

double Rastrigin(double sol[D])

{

int j;

double top=0;

for(j=0;j

{

top=top+(pow(sol[j],(double)2)-10*cos(2*M_PI*sol[j])+10);

}

return top;

}

本回答由提问者推荐

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

人工蜂群算法的java代码_求人工蜂群算法的c程序源代码``````谢谢各位大神了``````...相关推荐

  1. 冒泡排序java代码_看动画学算法之:排序冒泡排序

    点击上方的蓝字关注我吧 程序那些事 简介 排序可能是所有的算法中最最基础和最最常用的了.排序是一个非常经典的问题,它以一定的顺序对一个数组(或一个列表)中的项进行重新排序. 排序算法有很多种,每个都有 ...

  2. 判断三角形java代码_打基础之LeetCode算法题第72篇:最大的三角形周长问题

    一直很纠结算法的文章应该怎么写.最后觉得还是从最简单的level开始写吧,一开始就弄些重量级的,什么人工智能,机器学习的算法,还要有大量的数学以及优化的知识,小白们估计会很郁闷,当然我也不一定能做出来 ...

  3. 售票java代码_初探12306售票算法(二)-java代码实践

    周五闲来无事,基于上一篇关于初探12306售票算法(一)-理论,进行了java编码实践供各位读者参考(以下为相关代码的简单描述) 1.订票工具类 1.1初始化一列车厢的票据信息 /** * 生成Tic ...

  4. 苹果手机通讯录java代码_求通讯录系统

    求通讯录系统 老师要求我们做作业,题目是做一个同学通讯录系统,用java语言写. 功能:控制台上进行同学信息的增删改查,比如录入:张三,1365541646,上海,乒乓球.唱K:王五,12345455 ...

  5. 距离矢量路由算法的java代码_八大排序算法比较(附Java代码)

    冒泡排序 /*** 冒泡排序 比较好理解* 两两相比 较大的放后面* 时间复杂度O(n^2)*//*** 改进前的冒泡排序算法进行100,000数据排序运行时间为:3829ms* 优化后的冒泡排序算法 ...

  6. 体质测试java代码_求java代码,要求做一个测试类,实现以下功能之一。最好三个功能都有。...

    展开全部 import java.util.*; public class Admin {//管理类 Scanner in=new Scanner(System.in); String msg=&qu ...

  7. 蝗虫算法java代码_蝗虫搜索算法 蝗虫算法:蝗虫优化算法是模拟自然界蝗虫种群捕食行为而提出的一 联合开发网 - pudn.com...

    蝗虫搜索算法 所属分类:其他 开发工具:matlab 文件大小:347KB 下载次数:5 上传日期:2020-07-26 16:31:25 上 传 者:西柚不加冰 说明:  蝗虫算法:蝗虫优化算法是模 ...

  8. 有趣的java代码_求一些有趣的java小程序?

    不请自来,用eclipse写的一个输出爱心的小程序,应该算是比较有趣的吧 下面是程序内容------------------------------------ public class love { ...

  9. 简单的五子棋java代码_求一个最简单的JAVA五子棋程序。。

    展开全部 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt ...

最新文章

  1. 终于有人解救Python人了!
  2. Django使用Social-Auth实现微信第三方网站扫码登录
  3. [洛谷P3181] [HAOI2016]找相同字符
  4. 测试isEmpty null 方法
  5. 第八章 流量复制/AB测试/协程
  6. wxpython菜单的位置_wxpython教程:[5]次级菜单
  7. hibernate......1、2级缓存
  8. C# mysql导入文件报错:The used command is not allowed with this MySQL version
  9. Unity实现安卓虚拟摇杆多点触控
  10. java 如何执行dig 命令_dig命令简介
  11. 容器-2018百战程序员JAVA全系列终结版第07阶:容器和数据结构
  12. 湖南出台不动产登记新规 “小产权房”不予办理
  13. android画布橡皮,Android 橡皮擦功能的实现
  14. 主流芯片解决方案Ambarella的高清网络摄像机、德州仪器和控制海思
  15. 提升思维品质,不可不知的5个工具
  16. 焦虑 程序员_我如何克服焦虑和沮丧来完成freeCodeCamp的前端开发程序
  17. 自媒体运营抖音快手怎么快速涨粉
  18. 可视化大屏幕的适配方法-vw/vh
  19. 藏在今日头条、喜马拉雅背后的神秘天使:龚挺 | 捕捉隐秘猎手
  20. 漫画 | 为什么程序员的女朋友或老婆颜值普遍都偏高?

热门文章

  1. crf graph matlab_如何评价 Vicarious 在 Science 上提出基于概率图模型(PGM)的 RCN 模型?...
  2. Iog4j2漏洞相关技术分析
  3. 最大公约数和最小公倍数实现
  4. numeric比较大小 数据库_数据库基础知识个人整理版-强烈推荐
  5. 除了迅雷还有谁在“偷”你的信息?
  6. 计算机不能启动任务管理器,win10系统无法打开任务管理器怎么办
  7. 数据分析在互联网金融的应用
  8. [EGNN] Exploiting Edge Features for Graph Neural Networks 利用图神经网络的边特征 论文详解 CVPR 2019
  9. 【网络】TOE、RDMA、smartNIC 是什么和区别|DPU
  10. paper学习笔记 - PLE