用浮点数编码解决遗传算法问题

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

遗传算法作业

一:用浮点数编码解决以下问题。

])1cos[(*])1cos[(),(25

1

15121i x i i i x i i x x f i i ++++=∑∑== 10,1021≤≤-x x 其中

),(min 21x x f 求

二:构造过程。

第一步:确定决策变量和约束条件。

题目中已给出了该问题的决策变量及约束条件。

第二步:建立优化模型。

既是题目给出所要求的。

第三步:确定编码方法。

第四步:确定解码方法。

第五步:确定个体评价方法。

这里可将个体的适应度直接取为对应的目标函数值,并且不再对它做其他变化处理,即有:F(X)=f(x1,x2)

第六步:设计遗传算子。

选择运算使用比例运算算子;

交叉运算是用单点交叉算子;

变异运算使用基本位变异算子。

第七步:确定遗传算法的运行参数。

对于本例,设计基本遗传算法的运行参数如下:

MAXGENS=80 /* 最大世代数 */

POPSIZE=20 /* 种群大小*/

NV ARS=2 /* 变量个数 */

PXOVER=0.2 /* 交叉概率 */

PMUTATION=0.1 /* 变异概率 */

三:代码如下。

#include "stdafx.h"

#include

#include

#include

#define MAXGENS 80 /* 最大世代数 */

#define POPSIZE 20 /* 种群大小*/

#define NV ARS 2 /* 变量个数 */

#define PXOVER 0.2 /* 交叉概率 */

#define PMUTATION 0.1 /* 变异概率 */

#define FALSE 0

int cur_best; /* 最佳个体*/

struct genotype

{

double gene[NV ARS];

double fitness;

double upper[NV ARS];

double lower[NV ARS];

double rfitness;

double cfitness;

};

struct genotype population[POPSIZE+1];

struct genotype newpopulation[POPSIZE+1];

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);

void initialize(void)

{

int i, j;

double lbound, ubound;

for (i = 0; i < NV ARS; i++)

{

scanf("%lf",&lbound);

scanf("%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]);

}

}

}

double randval(double low, double high)

{

double val;

val = ((double)(rand()%1000)/1000.0)*(high - low) + low;

void evaluate(void)

{

int mem;

int i;

int X=0;

int Y=0;

double x[NV ARS+1];

for (mem = 0; mem < POPSIZE; mem++)

{

for (i = 0; i < NV ARS; i++)

x[i+1] = population[mem].gene[i];

{

for(i=1;i<=5;i++)

{

X=X+(i*cos((i+1)*x[1]+i));

Y=Y+(i*cos((i+1)*x[2]+i));

}

population[mem].fitness = (-1)*X*Y;

}

}

}

void keep_the_best()

{

int mem;

int i;

cur_best = 0;

for (mem = 0; mem < POPSIZE; mem++)

{

if (population[mem].fitness > population[POPSIZE].fitness)

{

cur_best = mem;

population[POPSIZE].fitness = population[mem].fitness;

}

}

for (i = 0; i < NV ARS; i++)

population[POPSIZE].gene[i] = population[cur_best].gene[i]; }

void elitist()

{

int i;

double best, worst;

int best_mem, worst_mem;

best = population[0].fitness;

worst = population[0].fitness;

for (i = 0; i < POPSIZE - 1; ++i)

{

相关文档
最新文档