高分求遗传算法程序

作者&投稿:罗泻 (若有异议请与网页底部的电邮联系)
高分求大神帮编个程序,遗传算法的C语言程序~

我的天呐!这么难啊!

ee

遗传算法作为一种快速有效的寻优算法,在工业控制、经济决策和交通规划等居多领域得到广泛应用。其特点就是其不同于传统的搜索寻优方式而是模拟自然界生物进化过程,通过基因的变异交叉重组使整体得到进化,不断的进化最终得到最优的组群,即最优解。
下面是一个具体实例,是小可在别人程序的基础上改写优化而成。这里首先要建立数学模型,即问题的数学公式描述,数学建模是一门对数学能力有相当要求的课程,一个人的数学能力往往决定其从事工作的层次。下面的程序是求多项式y=x^6-10x^5-26x^4+344x^3+193x^2-1846x-1680在区间[-8,8]的最小值,误差不超过0.001。对于这个复杂的多项式,可先用matlab绘制函数的大概曲线,确认函数的最小值大概处于[-8,8]之间,再用本程序求出精确解。
程序已在Dev-c++下运行通过,结果正确。
如有什么疑问,给我消息。

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

#define SUM 20 /* 总共的染色体数量 */
#define MAXloop 1200 /* 最大循环次数 */
#define error 0.01 /* 若两次最优值之差小于此数则认为结果没有改变 */
#define crossp 0.7 /* 交叉概率 */
#define mp 0.04 /* 变异概率 */

struct gen /* 定义染色体结构 */
{
int info;
double suitability;
};
struct gen gen_group[SUM];/* 定义一个含有20个染色体的组 */
struct gen gen_new[SUM];

struct gen gen_result; /* 记录最优的染色体 */
int result_unchange_time; /* 记录在error前提下最优值未改变的循环次数 */

struct log /* 形成链表,记录每次循环所产生的最优的适应度 */
{
double suitability;
struct log *next;
}llog,*head,*end;
int log_num; /* 链表长度 */

void initiate(); /*初始化函数*/
void evaluation(int flag); /*适应度评估和排序函数*/
void cross(); /*交叉函数*/
void selection(); /*选择函数*/
int record(); /*记录每次最优解和判断循环是否终止*/
void mutation(); /*变异函数*/
void showresult(int); /*显示结果*/

int randsign(double p); /*按概率p产生0,1随机数,p表示产生值1的概率*/
int randbit(int i,int j);/*产生i,j间的随机整数*/
int randnum(); /*随机产生14个位(基因)组成的染色体*/
int convertionD2B(double x); /*编码*/
double convertionB2D(int x); /*解码*/
int createmask(int a); /*交叉操作*/

int main()
{
int i,flag;
flag=0;
initiate();
evaluation( 0 );
for( i = 0 ; i < MAXloop ; i++ )
{
cross();
evaluation( 1 );
selection();
if( record() == 1 )
{
flag = 1;
break;
}
mutation();
}
showresult( flag );
system("pause");
return 0
}

void initiate()
{
int i , stime;
long ltime;
ltime=time(NULL);
stime=(unsigned)ltime/2;
srand(stime);
for( i = 0 ; i < SUM ; i++ )
{
gen_group[i].info = randnum();
}
gen_result.suitability=1000;
result_unchange_time=0;
head=end=(struct log *)malloc(sizeof(llog));
if(head==NULL)
{
printf("\n内存不够!\n");
exit(0);
}
end->next = NULL;
log_num = 1;
}

void evaluation(int flag)
{
int i,j,k;
struct gen *genp;
int gentinfo;
double gentsuitability;
double x;
if( flag == 0 )
genp = gen_group;
else genp = gen_new;
for(i = 0 ; i < SUM ; i++)/* 计算各染色体对应的表达式值 */
{
x = convertionB2D( genp[i].info );
genp[i].suitability = x*(x*(x*(x*(x*(x-10)-26)+344)+193)-1846)-1680;
}
for(i = 0 ; i < SUM - 1 ; i++)/* 按表达式的值进行排序 */
{ k=i;
for(j = i + 1 ; j < SUM ; j++)
if( genp[i].suitability > genp[j].suitability ) k=j;
if( k!=i )
{
gentinfo = genp[i].info;
genp[i].info = genp[k].info;
genp[k].info = gentinfo;

gentsuitability = genp[i].suitability;
genp[i].suitability = genp[k].suitability;
genp[k].suitability = gentsuitability;
}
}
}

void cross()
{
int i , j , k ;
int mask1 , mask2;
int a[SUM];
for(i = 0 ; i < SUM ; i++) a[i] = 0;
k = 0;
for(i = 0 ; i < SUM ; i++)
{
if( a[i] == 0)
{
for( ; ; )/* 随机找到一组未进行过交叉的染色体与a[i]交叉 */
{
j = randbit(i + 1 , SUM - 1);
if( a[j] == 0) break;
}
if(randsign(crossp) == 1)
{
mask1 = createmask(randbit(0 , 14-1));
mask2 = ~mask1;
gen_new[k].info = (gen_group[i].info) & mask1 + (gen_group[j].info) & mask2;
gen_new[k+1].info=(gen_group[i].info) & mask2 + (gen_group[j].info) & mask1;
k = k + 2;
}
else
{
gen_new[k].info=gen_group[i].info;
gen_new[k+1].info=gen_group[j].info;
k=k+2;
}
a[i] = a[j] = 1;
}
}
}

void selection()
{
int i , j , k;
j = 0;
i = SUM/2-1;
if(gen_group[i].suitability < gen_new[i].suitability)
{
for(j = 1 ; j < SUM / 2 ; j++)
{
if(gen_group[i+j].suitability > gen_new[i-j].suitability)
break;
}
}
else
if(gen_group[i].suitability>gen_new[i].suitability)
{
for(j=-1;j>-SUM/2;j--)
{
if(gen_group[i+j].suitability<=gen_new[i-j].suitability)
break;
}
}
for(k=j;k<SUM/2+1;k++)
{
gen_group[i+k].info = gen_new[i-k].info;
gen_group[i+k].suitability = gen_new[i-k].suitability;
}
}

int record()
{
double x;
struct log *r;
r=(struct log *)malloc(sizeof(llog));
if(r==NULL)
{
printf("\n内存不够!\n");
exit(0);
}
r->next = NULL;
end->suitability = gen_group[0].suitability;
end->next = r;
end = r;
log_num++;

x = gen_result.suitability - gen_group[0].suitability;
if(x < 0)x = -x;
if(x < error)
{
result_unchange_time++;
if(result_unchange_time >= 20)return 1;
}
else
{
gen_result.info = gen_group[0].info;
gen_result.suitability = gen_group[0].suitability;
result_unchange_time=0;
}
return 0;
}

void mutation()
{
int i , j , k, m;
double x;
double gmp;
int gentinfo;
double gentsuitability;
gmp = 1 - pow(1 - mp , 11);/* 在基因变异概率为mp时整条染色体的变异概率 */
for(i = 0 ; i < SUM ; i++)
{
if(randsign(gmp) == 1)
{
j = randbit(0 , 14);
m = 1 << j;
gen_group[i].info = gen_group[i].info^m;
x = convertionB2D(gen_group[i].info);
gen_group[i].suitability = x*(x*(x*(x*(x*(x-10)-26)+344)+193)-1846)-1680;
}
}
for(i = 0 ; i < SUM - 1 ; i++)
{ k=i;
for(j = i + 1 ; j < SUM ; j++)
if(gen_group[i].suitability > gen_group[j].suitability) k=j;
if(k!=i)
{
gentinfo = gen_group[i].info;
gen_group[i].info = gen_group[k].info;
gen_group[k].info = gentinfo;

gentsuitability = gen_group[i].suitability;
gen_group[i].suitability = gen_group[k].suitability;
gen_group[k].suitability = gentsuitability;
}
}
}

void showresult(int flag)/* 显示搜索结果并释放内存 */
{
int i , j;
struct log *logprint,*logfree;
FILE *logf;
if(flag == 0)
printf("已到最大搜索次数,搜索失败!");
else
{
printf("当取值%f时表达式达到最小值为%f\n",convertionB2D(gen_result.info),gen_result.suitability);
printf("收敛过程记录于文件log.txt");
if((logf = fopen("log.txt" , "w+")) == NULL)
{
printf("Cannot create/open file");
exit(1);
}
logprint=head;
for(i = 0 ; i < log_num ; i = i + 5)/* 对收敛过程进行显示 */
{
for(j = 0 ; (j < 5) & ((i + j) < log_num-1) ; j++)
{
fprintf(logf , "%20f" , logprint->suitability);
logprint=logprint->next;
}
fprintf(logf,"\n\n");
}
}
for(i = 0 ; i< log_num ; i++)/* 释放内存 */
{
logfree=head;
head=head->next;
free(logfree);
}
fclose(logf);
getch();
}

int randsign(double p)/* 按概率p返回1 */
{
if(rand() > (p * 32768))
return 0;
else return 1;
}
int randbit(int i, int j)/* 产生在i与j之间的一个随机数 */
{
int a , l;
l = j - i + 1;
a = i + rand() * l / 32768;
return a;
}
int randnum()
{
int x;
x = rand() / 2;
return x;
}
double convertionB2D(int x)
{
double y;
y = x;
y = (y - 8192) / 1000;
return y;

}
int convertionD2B(double x)
{
int g;
g = (x * 1000) + 8192;
return g;
}
int createmask(int a)
{
int mask;
mask=(1 << a) - 1;
return mask;
}

我这有不少代码
不过我没运行过
因为要写论文才去整的
只是还没去研究过
不如一起试试吧
我邮箱:lilei3974@163.com

遗传算法是算法,要用到你具体的领域里才能根据具体情况写代码,你这样要注释,要运行的。。。
你在网上搜搜,有关于用遗传算法就解方程的问题,你下载下来看看,然后改到你要用的领域。

这个问题估计不好回答,建议你不如从网上搜搜相关的问题吧。

#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 individual */
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 procedures 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 copy of the best individual */
/***************************************************************/

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

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, copy 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 individual from the new population is better than */
/* the best individual from the previous population, then */
/* copy the best from the new population; else replace the */
/* worst individual 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, copy 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 procedure 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 */
/* dumped into the output file are separated by commas */
/***************************************************************/

void report(void)
{
int i;
double best_val; /* best population fitness */
double avg; /* avg population fitness */
double stddev; /* std. deviation of population fitness */
double sum_square; /* sum of square for std. calc */
double square_sum; /* square of sum for std. calc */
double sum; /* total population fitness */

sum = 0.0;
sum_square = 0.0;

for (i = 0; i < POPSIZE; i++)
{
sum += population[i].fitness;
sum_square += population[i].fitness * population[i].fitness;
}

avg = sum/(double)POPSIZE;
square_sum = avg * avg * POPSIZE;
stddev = sqrt((sum_square - square_sum)/(POPSIZE - 1));
best_val = population[POPSIZE].fitness;

fprintf(galog, "\n%5d, %6.3f, %6.3f, %6.3f \n\n", generation,
best_val, avg, stddev);
}

/**************************************************************/
/* Main function: Each generation involves selecting the best */
/* members, performing crossover & mutation and then */
/* evaluating the resulting population, until the terminating */
/* condition is satisfied */
/**************************************************************/

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");
}
/***************************************************************/
参考资料:http://blog.csdn.net/AmiRural/archive/2005/07/07/416333.aspx

难道你要别人自己写一个给你?太麻烦了吧~~


高分求遗传算法程序
下面的程序是求多项式y=x^6-10x^5-26x^4+344x^3+193x^2-1846x-1680在区间[-8,8]的最小值,误差不超过0.001。对于这个复杂的多项式,可先用matlab绘制函数的大概曲线,确认函数的最小值大概处于[-8,8]之间,再用本程序求出精确解。程序已在Dev-c++下运行通过,结果正确。如有什么疑问,给...

遗传算法求解tsp问题的matlab程序
程序一:主程序 TSP问题(又名:旅行商问题,货郎担问题)遗传算法通用matlab程序 %D是距离矩阵,n为种群个数 %参数a是中国31个城市的坐标 C为停止代数,遗传到第 C代时程序停止,C的具体取值视问题的规模和耗费的时间而定 %m为适应值归一化淘汰加速指数,最好取为1,2,3,4,不宜太大 alpha为淘汰...

遗传算法求解tsp问题的matlab程序
把下面的(1)-(7)依次存成相应的.m文件,在(7)的m文件下运行就可以了 (1) 适应度函数fit.m function fitness=fit(len,m,maxlen,minlen)fitness=len;for i=1:length(len)fitness(i,1)=(1-(len(i,1)-minlen)\/(maxlen-minlen+0.0001)).^m;end (2)个体距离计算函数 myleng...

急求 遗传算法 java程序
button.setText("求最小值");button.setBounds(323, 27, 99, 23);getContentPane().add(button);final JLabel label_2 = new JLabel();label_2.setText("利用标准遗传算法求解函数f(x)=(x-5)*(x-5)的最小值:");label_2.setBounds(23, 31, 318, 15);getContentPane().add(label_2...

...遗传算法 计算出来的? 请资深人士帮忙写一下程序。这是怎么出来的...
function pop = create_permutations(NVARS,FitnessFcn,options)totalPopulationSize = sum(options.PopulationSize);n = NVARS;pop = cell(totalPopulationSize,1);for i = 1:totalPopulationSize pop{i} = randperm(n);end --- crossover_permutation.m function xoverKids = crossover_permutation(...

求遗传算法的matlab程序
options,'CrossoverFcn',@crossoverarithmetic);选择变异函数 options=gaoptimset(options,'MutationFcn',@mutationuniform);设置绘图:解的变化、种群平均值的变化 options=gaoptimset(options,'PlotFcns',{@gaplotbestf});执行遗传算法,fitness.m是函数文件 [x,fval]=ga(@fitness,1,options)...

...目标函数和约束条件在matlab中编写遗传算法的程序
1、首先建立带有约束条件的目标函数 function f =ga_fun0(x)if (1.5+x(1)*x(2)-x(1)-x(2)>0 | -x(1)*x(2)>10)f=100;else f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);end end 2、在命令窗口中执行下列命令 >>[x,fval] = ga(@ga_fun...

求matlab的遗传算法程序 ,希望得到您的帮助 ,谢谢,邮箱:568403518@qq...
这个很简单啊 ,我给你我论文里面的遗传算法程序。是我集成的,参数很容易调整 该代码为基于遗传算法神经网络的预测代码 清空环境变量 clc clear 网络结构建立 读取数据 load data input output 节点个数 inputnum=2;hiddennum=5;outputnum=1;训练数据和预测数据 input_train=input(1:1900,:)';input_...

遗传算法的matlab代码实现是什么?
遗传算法我懂,我的论文就是用着这个算法,具体到你要遗传算法是做什么?优化什么的。。。我给你一个标准遗传算法程序供你参考:该程序是遗传算法优化BP神经网络函数极值寻优:该代码为基于神经网络遗传算法的系统极值寻优 清空环境变量 clc clear 初始化遗传算法参数 初始化参数 maxgen=100; %进化代数...

vc程序设计(遗传算法)
main() \/* 主程序 *\/{ \/*当前代的个体遗传基因型与新一代的个体遗传基因型 *\/ unsigned char gene[POP_SIZE][G_LENGTH], new_gene[POP_SIZE][G_LENGTH]; \/*当前代的个体适应度与新一代个体的适应度 *\/ double fitness[POP_SIZE], new_fitness[POP_SIZE]; \/* 优化函数 *\/ double func[XMAX+1...

马尾区15794914878: 高分求遗传算法程序 -
袁垄世扶: 遗传算法作为一种快速有效的寻优算法,在工业控制、经济决策和交通规划等居多领域得到广泛应用.其特点就是其不同于传统的搜索寻优方式而是模拟自然界生物进化过程,通过基因的变异交叉重组使整体得到进化,不断的进化最终得到最优...

马尾区15794914878: 求遗传算法的matlab程序
袁垄世扶: function my_ga() options=gaoptimset; %设置变量范围 options=gaoptimset(options,'PopInitRange',[0;9]); %设置种群大小 options=gaoptimset(options,'PopulationSize',100); %设置迭代次数 options=gaoptimset(options,'Generations',100); %...

马尾区15794914878: 求MATLAB编遗传算法的程序? -
袁垄世扶: figure(1); fplot('variable.*sin(10*pi*variable)+2.0',[-1,2]); NIND=40; MAXGEN=25; PRECI=20; GGAP=0.9; trace=zeros(2,MAXGEN); FieldD=[20;-1;2;1;0;1;1]; Chrom=CRTBP(NIND,PRECI); gen=0; variable=BS2RV(Chrom,FieldD); ObjV=variable.*sin...

马尾区15794914878: 求遗传算法的matlab程序 -
袁垄世扶: maxgenerations,是迭代次数,是用循环语句实现的,如果求解空间不大,改小点就好,population 是每次迭代的并行运算数量.我个人在求解中发现,该值对结果影响较大,一般不要改变.crossoverfraction,变异系数,与运算复杂度无关.如果程序运行时间很长,检查一下是不是你把遗传算法又放入另一层循环了,一般这样运算时间就会呈现指数级增长.如果这样,就修改算法.

马尾区15794914878: 100分,求c++实现遗传编程 -
袁垄世扶: 遗传算法是吧,哈哈, 我刚好最近写了的.如果你需要,我可以直接发程序给你.不过先说明,我写的是用遗传算法基本步骤的,没有采用特殊的交叉和变异操作(当然,对初学者可能更好,我也是初学者).我写的是求函数最大值问题和TSP问题.我的程序有注释,如果你看了还不懂,可以直接找我交流,因为程序就是我写的,嘿嘿.

马尾区15794914878: 遗传算法求最短路径的matlab程序,急求!!
袁垄世扶: function [path, totalCost, farthestPreviousHop, farthestNextHop] = dijkstra(n, netCostMatrix, s, d, farthestPreviousHop, farthestNextHop) % path: the list of nodes in the path from source to destination; % totalCost: the total cost of the path; % ...

马尾区15794914878: 求C代码:遗传算法求函数最大值f(x)=xsin(10Pix)+1.0 -
袁垄世扶: %输出参数:x:求的最优解% endpop:最终的种群% bpop:最优种群的一个搜索轨迹% 输出参数:% bounds:代表变量的上下界的矩阵% eevalFN:适应度函数% startPop:初始群体% termFN:终止函数的名字% termOps: 终止函数的参数...

马尾区15794914878: 求C代码:遗传算法求函数最大值f(x)=x^2 x 从0到30 -
袁垄世扶: |#include <stdio.h>#include <math.h>#include <stdlib.h>#include <time.h>float f(float x){ return x * x;}void main() { float x[10]; float f1, f2; int i, j; float fmax; int xfmax; srand(time(NULL)); xfmax = 0; x[0] = 15.0f; f1 = f(x[0]); f2 = f1 + 1.0f; for (j = 0; fabs(f1 - f...

马尾区15794914878: 寻求matlab高人用遗传算法编程求解一个函数最优值,若成功追加50分 -
袁垄世扶: clear clc f=@(t)sin(100*pi*t)+sin(200*pi*t)+sin(400*pi*t); t=0:1e-3:2; subplot 211 plot(t,f(t));xlabel('t/s') y=fft(f(t),2^20); P=y.*conj(y)/2^20; subplot 212 plot(1000*(0:2^18)/2^20,P(1:2^18+1)) xlabel('f/Hz')

马尾区15794914878: 可不可以给给遗传算法的实例,有matlab实现的最好!谢谢咯! -
袁垄世扶: 点matlab里面的start菜单,从toolbox里面选取optimization toolbox,在早期版本里面不叫这个名字,从2010b以后的版本把非线性方程组求解的算法都放在一起了.里面第一个下拉菜单选择最后一个,里面写的ga.fitness function是你的函数.遗传算法求解的goal是0,如果你要求趋近某一个值,就要看你的fitness function怎么写了.下面number of variables是你要求解的维数.接着点击运行就行了.因为不知道你的具体目的是什么,也没有代码发上来,所以我只能说这种很没用的话.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网