C语言-遗传算法(GA)

  遗传算法是比较经典的群体智能算法,属于随机搜索算法的一种。遗传算法属于全局性搜索算法,因为存在基因交叉互换和基因突变,因此比较不容易陷入局部最优。遗传算法的本质是随机生成一些合理的解,通过计算代入变量得出的值,优胜劣汰,保存优良解,淘汰不良解,再通过随机地两两交配互换对应变量的值以及改变某解中的某一变量来生成新的解向量,这些过程可以称之为适应度评估,交叉,突变。对于交配对象的分配,本算法使用轮盘赌选择法。

头文件如下

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

/*parameter*/
#define POPSIZE 100			/*population size*/
#define VARSNUM 3			/*variable number*/
#define PXOVER  0.7			/*probability of crossover*/   
#define PMUTATION 0.07			/*probability of mutation*/
#define MAXGENS 500			/*max number of generations*/
#define TRUE 1
#define FALSE 0

/*global variable*/
int generation;                         /*current generatons number*/   
int current_best;			/*current best individual*/
double val_best;			/*current best fitness*/
FILE *galog;                            /*output file*/                    
FILE *output;

struct popmem{
	double gene[VARSNUM];               /*a array to store variables' value*/
	double fitness;                     /*a value to refelct current variable's priority*/
	double upboundary[VARSNUM];         /*range of variables*/
	double lowboundary[VARSNUM];  
	double rfitness;                    /*relative fitness of current generation*/
	double cfitness;		    /*for roulette wheel selection strategy*/
};

struct popmem population[POPSIZE+1];        /*current generation*/
struct popmem newpopulation[POPSIZE+1];     /*next generation*/


void Initial(void);                     /*initialize the group*/
void Evaluate(void);			/*evalute the member's fitness*/
void RepBest(void);			/*save the best member*/
void Select(void
  • 6
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值