当前位置:首页 » 编程软件 » 遗传算法编程

遗传算法编程

发布时间: 2022-06-19 10:15:27

Ⅰ 遗传算法的C语言实现

一个非常简单的遗传算法源代码,是由Denis Cormier (North Carolina State University)开发的,Sita S.Raghavan (University of North Carolina at Charlotte)修正。代码保证尽可能少,实际上也不必查错。对一特定的应用修正此代码,用户只需改变常数的定义并且定义“评价函数”即可。注意代码的设计是求最大值,其中的目标函数只能取正值;且函数值和个体的适应值之间没有区别。该系统使用比率选择、精华模型、单点杂交和均匀变异。如果用Gaussian变异替换均匀变异,可能得到更好的效果。代码没有任何图形,甚至也没有屏幕输出,主要是保证在平台之间的高可移植性。读者可以从ftp.uncc.e,目录 coe/evol中的文件prog.c中获得。要求输入的文件应该命名为‘gadata.txt’;系统产生的输出文件为‘galog.txt’。输入的文件由几行组成:数目对应于变量数。且每一行提供次序——对应于变量的上下界。如第一行为第一个变量提供上下界,第二行为第二个变量提供上下界,等等。

/**************************************************************************/
/* This is a simple genetic algorithm implementation where the */
/* evaluation function takes positive values only and the */
/* fitness of an indivial is the same as the value of the */
/* objective function */
/**************************************************************************/

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

/* Change any of these parameters to match your needs */

#define POPSIZE 50 /* population size */
#define MAXGENS 1000 /* max. number of generations */
#define NVARS 3 /* no. of problem variables */
#define PXOVER 0.8 /* probability of crossover */
#define PMUTATION 0.15 /* probability of mutation */
#define TRUE 1
#define FALSE 0

int generation; /* current generation no. */
int cur_best; /* best indivial */
FILE *galog; /* an output file */

struct genotype /* genotype (GT), a member of the population */
{
double gene[NVARS]; /* a string of variables */
double fitness; /* GT's fitness */
double upper[NVARS]; /* GT's variables upper bound */
double lower[NVARS]; /* GT's variables lower bound */
double rfitness; /* relative fitness */
double cfitness; /* cumulative fitness */
};

struct genotype population[POPSIZE+1]; /* population */
struct genotype newpopulation[POPSIZE+1]; /* new population; */
/* replaces the */
/* old generation */

/* Declaration of proceres used by this genetic algorithm */

void initialize(void);
double randval(double, double);
void evaluate(void);
void keep_the_best(void);
void elitist(void);
void select(void);
void crossover(void);
void Xover(int,int);
void swap(double *, double *);
void mutate(void);
void report(void);

/***************************************************************/
/* Initialization function: Initializes the values of genes */
/* within the variables bounds. It also initializes (to zero) */
/* all fitness values for each member of the population. It */
/* reads upper and lower bounds of each variable from the */
/* input file `gadata.txt'. It randomly generates values */
/* between these bounds for each gene of each genotype in the */
/* population. The format of the input file `gadata.txt' is */
/* var1_lower_bound var1_upper bound */
/* var2_lower_bound var2_upper bound ... */
/***************************************************************/

void initialize(void)
{
FILE *infile;
int i, j;
double lbound, ubound;

if ((infile = fopen("gadata.txt","r"))==NULL)
{
fprintf(galog,"\nCannot open input file!\n");
exit(1);
}

/* initialize variables within the bounds */

for (i = 0; i < NVARS; i++)
{
fscanf(infile, "%lf",&lbound);
fscanf(infile, "%lf",&ubound);

for (j = 0; j < POPSIZE; j++)
{
population[j].fitness = 0;
population[j].rfitness = 0;
population[j].cfitness = 0;
population[j].lower[i] = lbound;
population[j].upper[i]= ubound;
population[j].gene[i] = randval(population[j].lower[i],
population[j].upper[i]);
}
}

fclose(infile);
}

/***********************************************************/
/* Random value generator: Generates a value within bounds */
/***********************************************************/

double randval(double low, double high)
{
double val;
val = ((double)(rand()%1000)/1000.0)*(high - low) + low;
return(val);
}

/*************************************************************/
/* Evaluation function: This takes a user defined function. */
/* Each time this is changed, the code has to be recompiled. */
/* The current function is: x[1]^2-x[1]*x[2]+x[3] */
/*************************************************************/

void evaluate(void)
{
int mem;
int i;
double x[NVARS+1];

for (mem = 0; mem < POPSIZE; mem++)
{
for (i = 0; i < NVARS; i++)
x[i+1] = population[mem].gene[i];

population[mem].fitness = (x[1]*x[1]) - (x[1]*x[2]) + x[3];
}
}

/***************************************************************/
/* Keep_the_best function: This function keeps track of the */
/* best member of the population. Note that the last entry in */
/* the array Population holds a of the best indivial */
/***************************************************************/

void keep_the_best()
{
int mem;
int i;
cur_best = 0; /* stores the index of the best indivial */

for (mem = 0; mem < POPSIZE; mem++)
{
if (population[mem].fitness > population[POPSIZE].fitness)
{
cur_best = mem;
population[POPSIZE].fitness = population[mem].fitness;
}
}
/* once the best member in the population is found, the genes */
for (i = 0; i < NVARS; i++)
population[POPSIZE].gene[i] = population[cur_best].gene[i];
}

/****************************************************************/
/* Elitist function: The best member of the previous generation */
/* is stored as the last in the array. If the best member of */
/* the current generation is worse then the best member of the */
/* previous generation, the latter one would replace the worst */
/* member of the current population */
/****************************************************************/

void elitist()
{
int i;
double best, worst; /* best and worst fitness values */
int best_mem, worst_mem; /* indexes of the best and worst member */

best = population[0].fitness;
worst = population[0].fitness;
for (i = 0; i < POPSIZE - 1; ++i)
{
if(population[i].fitness > population[i+1].fitness)
{
if (population[i].fitness >= best)
{
best = population[i].fitness;
best_mem = i;
}
if (population[i+1].fitness <= worst)
{
worst = population[i+1].fitness;
worst_mem = i + 1;
}
}
else
{
if (population[i].fitness <= worst)
{
worst = population[i].fitness;
worst_mem = i;
}
if (population[i+1].fitness >= best)
{
best = population[i+1].fitness;
best_mem = i + 1;
}
}
}
/* if best indivial from the new population is better than */
/* the best indivial from the previous population, then */
/* the best from the new population; else replace the */
/* worst indivial from the current population with the */
/* best one from the previous generation */

if (best >= population[POPSIZE].fitness)
{
for (i = 0; i < NVARS; i++)
population[POPSIZE].gene[i] = population[best_mem].gene[i];
population[POPSIZE].fitness = population[best_mem].fitness;
}
else
{
for (i = 0; i < NVARS; i++)
population[worst_mem].gene[i] = population[POPSIZE].gene[i];
population[worst_mem].fitness = population[POPSIZE].fitness;
}
}
/**************************************************************/
/* Selection function: Standard proportional selection for */
/* maximization problems incorporating elitist model - makes */
/* sure that the best member survives */
/**************************************************************/

void select(void)
{
int mem, i, j, k;
double sum = 0;
double p;

/* find total fitness of the population */
for (mem = 0; mem < POPSIZE; mem++)
{
sum += population[mem].fitness;
}

/* calculate relative fitness */
for (mem = 0; mem < POPSIZE; mem++)
{
population[mem].rfitness = population[mem].fitness/sum;
}
population[0].cfitness = population[0].rfitness;

/* calculate cumulative fitness */
for (mem = 1; mem < POPSIZE; mem++)
{
population[mem].cfitness = population[mem-1].cfitness +
population[mem].rfitness;
}

/* finally select survivors using cumulative fitness. */

for (i = 0; i < POPSIZE; i++)
{
p = rand()%1000/1000.0;
if (p < population[0].cfitness)
newpopulation[i] = population[0];
else
{
for (j = 0; j < POPSIZE;j++)
if (p >= population[j].cfitness &&
p<population[j+1].cfitness)
newpopulation[i] = population[j+1];
}
}
/* once a new population is created, it back */

for (i = 0; i < POPSIZE; i++)
population[i] = newpopulation[i];
}

/***************************************************************/
/* Crossover selection: selects two parents that take part in */
/* the crossover. Implements a single point crossover */
/***************************************************************/

void crossover(void)
{
int i, mem, one;
int first = 0; /* count of the number of members chosen */
double x;

for (mem = 0; mem < POPSIZE; ++mem)
{
x = rand()%1000/1000.0;
if (x < PXOVER)
{
++first;
if (first % 2 == 0)
Xover(one, mem);
else
one = mem;
}
}
}
/**************************************************************/
/* Crossover: performs crossover of the two selected parents. */
/**************************************************************/

void Xover(int one, int two)
{
int i;
int point; /* crossover point */

/* select crossover point */
if(NVARS > 1)
{
if(NVARS == 2)
point = 1;
else
point = (rand() % (NVARS - 1)) + 1;

for (i = 0; i < point; i++)
swap(&population[one].gene[i], &population[two].gene[i]);

}
}

/*************************************************************/
/* Swap: A swap procere that helps in swapping 2 variables */
/*************************************************************/

void swap(double *x, double *y)
{
double temp;

temp = *x;
*x = *y;
*y = temp;

}

/**************************************************************/
/* Mutation: Random uniform mutation. A variable selected for */
/* mutation is replaced by a random value between lower and */
/* upper bounds of this variable */
/**************************************************************/

void mutate(void)
{
int i, j;
double lbound, hbound;
double x;

for (i = 0; i < POPSIZE; i++)
for (j = 0; j < NVARS; j++)
{
x = rand()%1000/1000.0;
if (x < PMUTATION)
{
/* find the bounds on the variable to be mutated */
lbound = population[i].lower[j];
hbound = population[i].upper[j];
population[i].gene[j] = randval(lbound, hbound);
}
}
}

/***************************************************************/
/* Report function: Reports progress of the simulation. Data */
/* mped into the output file are separated by commas */
/***************************************************************/
。。。。。
代码太多 你到下面呢个网站看看吧

void main(void)
{
int i;

if ((galog = fopen("galog.txt","w"))==NULL)
{
exit(1);
}
generation = 0;

fprintf(galog, "\n generation best average standard \n");
fprintf(galog, " number value fitness deviation \n");

initialize();
evaluate();
keep_the_best();
while(generation<MAXGENS)
{
generation++;
select();
crossover();
mutate();
report();
evaluate();
elitist();
}
fprintf(galog,"\n\n Simulation completed\n");
fprintf(galog,"\n Best member: \n");

for (i = 0; i < NVARS; i++)
{
fprintf (galog,"\n var(%d) = %3.3f",i,population[POPSIZE].gene[i]);
}
fprintf(galog,"\n\n Best fitness = %3.3f",population[POPSIZE].fitness);
fclose(galog);
printf("Success\n");
}

Ⅱ 遗传算法 编程

染色体长度问题应该很容易解决,因为都是动态的分配内存,然后再用一个变量来记录染色体有多长就可以了。不过这样也仅限于编码方式比较简单的问题。实际中,对于不同的问题,有可能编码方案截然不同,你能做到的就是让你的算法能够在某一类问题上做到通用。例如,对所有采用二进制编码的问题通用,或者对所有采用实数编码的问题通用。
VBscript,java Script应该都可以写遗传算法的。实际上只要这种语言可以产生随机数,只要能够处理数组,能够进行循环,那么就肯定可以写遗传算法。另外,为什么要vbscript和javascript配合呢?任何一种都可以了。

Ⅲ 如何用matlab遗传算法编程

有两种方法,一种是用matlab自带的遗传算法工具箱;还有一种是自己编写遗传算法解决问题。第二种方法的话,网上可以找到很多遗传算法的matlab代码,我也可以提供。第一种的话,有一定的局限性。

Ⅳ MATLAB中遗传算法编程中,二进制编码如何处理实数变量

假如你想要编码为x,设x的范围是【min,max】,二进制编码长度为10,那二进解码方式是:x*(max-min)/1023,这个不用开始编码,开始你可以用rand(n,10)产生n个样本的随机数,然后优化即可。
不是能把“数学模型中的目标函数和每一条约束函数分别编程Matlab里的M文件”,是你用遗传算法就必须要编进去,电脑怎么知道往哪个方向优化是好的,要不把你邮箱留下,我给你发个寻求最大值的遗传算法。

Ⅳ 遗传算法的迭代次数是怎么确定的,与什么有关

1. 遗传算法简介

遗传算法是用于解决最优化问题的一种搜索算法,算法的整体思路是建立在达尔文生物进化论“优胜劣汰”规律的基础上。它将生物学中的基因编码、染色体交叉、基因变异以及自然选择等概念引入最优化问题的求解过程中,通过不断的“种群进化”,最终得到问题的最优解。

2. 遗传算法实现步骤

在讲下面几个基于生物学提出的概念之前,首先我们需要理解为什么需要在最优化问题的求解中引入生物学中的各种概念。

假设我们需要求一个函数的最大值,但这个函数异常复杂以至于无法套用一般化的公式,那么就会想到:如果可以将所有可能的解代入方程,那么函数最大值所对应的那个解就是问题的最优解。但是,对于较复杂的函数来说,其可能的解的个数的数量级是我们所无法想象的。因此,我们只好退而求其次,只代入部分解并在其中找到最优解。那么这样做的核心就在于如何设定算法确定部分解并去逼近函数的最优解或者较好的局部最优解。

遗传算法就是为了解决上述问题而诞生的。假设函数值所对应的所有解是一个容量超级大的种群,而种群中的个体就是一个个解,接下去遗传算法的工作就是让这个种群中的部分个体去不断繁衍,在繁衍的过程中一方面会发生染色体交叉而产生新的个体。另一方面,基因变异也会有概率会发生并产生新的个体。接下去,只需要通过自然选择的方式,淘汰质量差的个体,保留质量好的个体,并且让这个繁衍的过程持续下去,那么最后就有可能进化出最优或者较优的个体。这么看来原来最优化问题居然和遗传变异是相通的,而且大自然早已掌握了这样的机制,这着实令人兴奋。为了将这种机制引入最优化问题并利用计算机求解,我们需要将上述提到的生物学概念转化为计算机能够理解的算法机制。

下面介绍在计算机中这种遗传变异的机制是如何实现的:

基因编码与解码:

在生物学中,交叉与变异能够实现是得益于染色体上的基因,可以想象每个个体都是一串超级长的基因编码,当两个个体发生交叉时,两条基因编码就会发生交换,产生的新基因同时包含父亲和母亲的基因编码。在交叉过程中或者完成后,某些基因点位又会因为各种因素发生突变,由此产生新的基因编码。当然,发生交叉和变异之后的个体并不一定优于原个体,但这给了进化(产生更加优秀的个体)发生的可能。

因此,为了在计算机里实现交叉和变异,就需要对十进制的解进行编码。对于计算机来说其最底层的语言是由二进制0、1构成的,而0、1就能够被用来表示每个基因点位,大量的0、1就能够表示一串基因编码,因此我们可以用二进制对十进制数进行编码,即将十进制的数映射到二进制上。但是我们并不关心如何将十进制转换为二进制的数,因为计算机可以随机生成大量的二进制串,我们只需要将办法将二进制转化为十进制就可以了。

二进制转换为十进制实现方式:

假设,我们需要将二进制映射到以下范围:

首先,将二进制串展开并通过计算式转化为[0,1]范围内的数字:

将[0,1]范围内的数字映射到我们所需要的区间内:

交叉与变异:

在能够用二进制串表示十进制数的基础上,我们需要将交叉与变异引入算法中。假设我们已经获得两条二进制串(基因编码),一条作为父亲,一条作为母亲,那么交叉指的就是用父方一半的二进制编码与母方一半的二进制编码组合成为一条新的二进制串(即新的基因)。变异则指的是在交叉完成产生子代的过程中,二进制串上某个数字发生了变异,由此产生新的二进制串。当然,交叉与变异并不是必然发生的,其需要满足一定的概率条件。一般来说,交叉发生的概率较大,变异发生的概率较小。交叉是为了让算法朝着收敛的方向发展,而变异则是为了让算法有几率跳出某种局部最优解。

自然选择:

在成功将基因编码和解码以及交叉与变异引入算法后,我们已经实现了让算法自动产生部分解并优化的机制。接下去,我们需要解决如何在算法中实现自然选择并将优秀的个体保留下来进而进化出更优秀的个体。

首先我们需要确定个体是否优秀,考虑先将其二进制串转化为十进制数并代入最初定义的目标函数中,将函数值定义为适应度。在这里,假设我们要求的是最大值,则定义函数值越大,则其适应度越大。那是否在每一轮迭代过程中只需要按照适应度对个体进行排序并选出更加优秀的个体就可以了呢?事实上,自然选择的过程中存在一个现象,并没有说优秀的个体一定会被保留,而差劲的个体就一定被会被淘汰。自然选择是一个概率事件,越适应环境则生存下去的概率越高,反之越低。为了遵循这样的思想,我们可以根据之前定义的适应度的大小给定每个个体一定的生存概率,其适应度越高,则在筛选时被保留下来的概率也越高,反之越低。

那么问题就来了,如何定义这种生存概率,一般来说,我们可以将个体适应度与全部个体适应度之和的比率作为生存概率。但我们在定义适应度时使用函数值进行定义的,但函数值是有可能为负的,但概率不能为负。因此,我们需要对函数值进行正数化处理,其处理方式如下:

定义适应度函数:

定义生存概率函数:

注:最后一项之所以加上0.0001是因为不能让某个个体的生存概率变为0,这不符合自然选择中包含的概率思想。

3. 遗传算例

在这里以一个比较简单的函数为例,可以直接判断出函数的最小值为0,最优解为(0,0)

若利用遗传算法进行求解,设定交叉概率为0.8,变异概率为0.005,种群内个体数为2000,十进制数基因编码长度为24,迭代次数为500次。

从遗传算法收敛的动态图中可以发现,遗传算法现实生成了大量的解,并对这些解进行试错,最终收敛到最大值,可以发现遗传算法的结果大致上与最优解无异,结果图如下:

4. 遗传算法优缺点

优点:

1、 通过变异机制避免算法陷入局部最优,搜索能力强

2、 引入自然选择中的概率思想,个体的选择具有随机性

3、 可拓展性强,易于与其他算法进行结合使用

缺点:

1、 遗传算法编程较为复杂,涉及到基因编码与解码

2、 算法内包含的交叉率、变异率等参数的设定需要依靠经验确定

3、 对于初始种群的优劣依赖性较强

Ⅵ MATLAB遗传算法编程(多目标优化)

多目标是通过分布性 和非劣解来进行评价的

Ⅶ matlab遗传算法编程求最大值的问题

(1)关于fitness value,你要自己定义一个函数,如你所说从25个x变量经过一系列运算得到y值 可以其作为fitness value
(2)由于x的取值是离散的 染色体不一定要是二进制 最简单的做法是一个5进制的长为25的串

Ⅷ 《Java遗传算法编程》pdf下载在线阅读全文,求百度网盘云资源

《Java遗传算法编程》网络网盘pdf最新全集下载:
链接: https://pan..com/s/1l6_14X1Yhcgv8kYwHqyY2g

?pwd=xv3v 提取码: xv3v
简介:本书简单、直接地介绍了遗传算法,并且针对所讨论的示例问题,给出了Java代码的算法实现。全书分为6章。第1章简单介绍了人工智能和生物进化的知识背景,这也是遗传算法的历史知识背景。第2章给出了一个基本遗传算法的实现;第4章和第5章,分别针对机器人控制器、旅行商问题、排课问题展开分析和讨论,并给出了算法实现。在这些章的末尾,还给出了一些练习供读者深入学习和实践。第6章专门讨论了各种算法的优化问题。

Ⅸ 遗传算法编程,求指教

你好 你最后这个问题解决了吗 我也是遇到了这个问题

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:645
制作脚本网站 发布:2025-10-20 08:17:34 浏览:936
python中的init方法 发布:2025-10-20 08:17:33 浏览:632
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:821
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:731
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1066
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:299
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:160
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:851
python股票数据获取 发布:2025-10-20 07:39:44 浏览:763