Ejemplo n.º 1
0
void evaluation(int flag)
{
	int i,j;
	struct gen *genp;
	int gentinfo;
	float gentsuitability;
	float x;
	if( flag == 0 )			// 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++)//按表达式的值进行排序,
	{
		for(j = i + 1 ; j < SUM ; j++)
		{
			if( genp[i].suitability > genp[j].suitability )
			{
				gentinfo = genp[i].info;
				genp[i].info = genp[j].info;
				genp[j].info = gentinfo;
				
				gentsuitability = genp[i].suitability;
				genp[i].suitability = genp[j].suitability;
				genp[j].suitability = gentsuitability;		
			}
		}
	}
}
Ejemplo n.º 2
0
void mutation() {
  int i, j, m;
  int gentinfo;
  float x;
  float cmp;
  float gentsuitability;
  cmp = 1 - pow (1 - mp, 11); // 基因变异概率为mp时染色体中有基因发生变异的概率

  for (i = 0; i < SUM; i++) {
    if (randsign(cmp) == 1) {
      j = randbit(0, 14);
      m = 1 << j;
      gen_group[i].info = gen_group[i].info ^ m;
      x = convertionB2D(gen_group[i].info);
      x = x * (x * (x * (x * (x * (x - 10) - 26) + 344) + 193) - 1846) - 1680;
      gen_group[i].suitability = x;
    }
  }
  for (i = 0; i < SUM - 1; i++) {
    for (j = i + 1; j < SUM; j++) {
      if (gen_group[i].suitability > gen_group[j].suitability) {
        gentinfo = gen_group[i].info;
        gen_group[i].info = gen_group[j].info;
        gen_group[j].info = gentinfo;
        gentsuitability = gen_group[i].suitability;
        gen_group[i].suitability = gen_group[j].suitability;
        gen_group[j].suitability = gentsuitability;
      }
    }
  }
}
Ejemplo n.º 3
0
void showresult(int flag) {  // 显示搜索结果并释放内存
  int i, j;
  struct log * logfree = head;
  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);
    }
    for (i = 0; i < log_num; i = i + 5) { // 对收敛过程进行显示
      for (j = 0; (j < 5) & ( (i + j) < log_num - 1); j++) {
        //fprintf(logf, "%20f", logfree->suitability);
        //logfree = logfree->next;
      }
      fprintf(logf, "\n \n");
    }
  }
  for (i = 0; i < log_num; i++) {
    logfree = head;
    head = head->next;
    free(logfree);
    fclose(logf);
  }
  getchar();
}
Ejemplo n.º 4
0
void evaluation(int flag) { // 按照flag的指示分别对父种群、子种群进行评估、排序
  int i, j;
  struct gen * genp;
  int gentinfo;
  float gentsuitability;
  float x;
  if (flag == 0)    // 当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++) {   // 按照适应度值的大小进行排序
    for (j = i + 1; j < SUM; j++) {
      if (genp[i].suitability > genp[j].suitability) {
        gentinfo = genp[i].info;
        genp[i].info = genp[j].info;
        genp[j].info = gentinfo;
        gentsuitability = genp[i].suitability;
        genp[i].suitability = genp[j].suitability;
        genp[j].suitability = gentsuitability;
      }
    }
  }
}
Ejemplo n.º 5
0
void mutation()
{
	int i , j , m;
	float x;
	float gmp;
	int gentinfo;
	float 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++)
	{
		for(j = i + 1 ; j < SUM ; j++)
		{
			if(gen_group[i].suitability > gen_group[j].suitability)
			{
				gentinfo = gen_group[i].info;
				gen_group[i].info = gen_group[j].info;
				gen_group[j].info = gentinfo;
				
				gentsuitability = gen_group[i].suitability;
				gen_group[i].suitability = gen_group[j].suitability;
				gen_group[j].suitability = gentsuitability;
			}
		}
	}
	/*
	*为了提高执行速度,在进行变异操作的时候并没有直接确定需要进行变异的位
	*而是先以cmp概率确定将要发生变异的染色体,再从染色体中随进选择一个基因进行变异
	*由于进行选择和变异后父代种群的次序已被打乱,因此,在变异前后对种群进行一次排序 
	*/ 
}