⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ga_crossover.c

📁 http://gaul.sourceforge.net/ 这里大部分人讨论的是在Matlab里实现GA的toolbox.以上为一个GA的C语言的软件包.如果你想利用GA做优化算法,非常有用.而且有
💻 C
📖 第 1 页 / 共 3 页
字号:
  ga_crossover_char_doublepoints()  synopsis:	`Mates' two genotypes by double-point crossover of		each chromosome.  parameters:  return:  last updated: 16/07/01 **********************************************************************/void ga_crossover_char_doublepoints( population *pop,                                     entity *father, entity *mother,                                     entity *son, entity *daughter )  {  int	i;			/* Loop variable over all chromosomes. */  int	location1, location2;	/* Points of crossover. */  int	tmp;			/* For swapping crossover loci. */  /* Checks */  if (!father || !mother || !son || !daughter)    die("Null pointer to entity structure passed");  for (i=0; i<pop->num_chromosomes; i++)    {    /* Choose crossover point and perform operation */    location1=random_int(pop->len_chromosomes);    do      {      location2=random_int(pop->len_chromosomes);      } while (location2==location1);    if (location1 > location2)      {      tmp = location1;      location1 = location2;      location2 = tmp;      }    memcpy( son->chromosome[i], father->chromosome[i],            location1*sizeof(char) );    memcpy( daughter->chromosome[i], mother->chromosome[i],            location1*sizeof(char) );    memcpy( &(((char *)son->chromosome[i])[location1]),            &(((char *)mother->chromosome[i])[location1]),            (location2-location1)*sizeof(char) );    memcpy( &(((char *)daughter->chromosome[i])[location1]),            &(((char *)father->chromosome[i])[location1]),            (location2-location1)*sizeof(char) );    memcpy( &(((char *)son->chromosome[i])[location2]),            &(((char *)father->chromosome[i])[location2]),            (pop->len_chromosomes-location2)*sizeof(char) );    memcpy( &(((char *)daughter->chromosome[i])[location2]),            &(((char *)mother->chromosome[i])[location2]),            (pop->len_chromosomes-location2)*sizeof(char) );    }  return;  }/**********************************************************************  ga_crossover_bitstring_singlepoints()  synopsis:	`Mates' two genotypes by single-point crossover of		each chromosome.  parameters:  return:  last updated: 30/06/01 **********************************************************************/void ga_crossover_bitstring_singlepoints(population *pop, entity *father, entity *mother, entity *son, entity *daughter)  {  int		i;		/* Loop variable over all chromosomes. */  int		location;	/* Point of crossover. */  /* Checks */  if (!father || !mother || !son || !daughter)    die("Null pointer to entity structure passed");  for (i=0; i<pop->num_chromosomes; i++)    {    /* Choose crossover point and perform operation */    location=random_int(pop->len_chromosomes);    ga_bit_copy(son->chromosome[i], mother->chromosome[i],                  0, 0, location);    ga_bit_copy(daughter->chromosome[i], father->chromosome[i],                  0, 0, location);    ga_bit_copy(daughter->chromosome[i], mother->chromosome[i],                  location, location, pop->len_chromosomes-location);    ga_bit_copy(son->chromosome[i], father->chromosome[i],                  location, location, pop->len_chromosomes-location);    }  return;  }/**********************************************************************  ga_crossover_bitstring_doublepoints()  synopsis:	`Mates' two genotypes by double-point crossover of		each chromosome.  parameters:	population *		Population structure.		entity *father, *mother	Parent entities.		entity *son, *daughter	Child entities.  return:  last updated:	23 Jun 2003 **********************************************************************/void ga_crossover_bitstring_doublepoints( population *pop,                                        entity *father, entity *mother,                                        entity *son, entity *daughter )  {  int	i;			/* Loop variable over all chromosomes. */  int	location1, location2;	/* Points of crossover. */  int	tmp;			/* For swapping crossover loci. */  /* Checks */  if (!father || !mother || !son || !daughter)    die("Null pointer to entity structure passed");  for (i=0; i<pop->num_chromosomes; i++)    {    /* Choose crossover point and perform operation */    location1=random_int(pop->len_chromosomes);    do      {      location2=random_int(pop->len_chromosomes);      } while (location2==location1);    if (location1 > location2)      {      tmp = location1;      location1 = location2;      location2 = tmp;      }    ga_bit_copy(son->chromosome[i], mother->chromosome[i],                  0, 0, location1);    ga_bit_copy(daughter->chromosome[i], father->chromosome[i],                  0, 0, location1);    ga_bit_copy(son->chromosome[i], father->chromosome[i],                  location1, location1, location2-location1);    ga_bit_copy(daughter->chromosome[i], mother->chromosome[i],                  location1, location1, location2-location1);    ga_bit_copy(son->chromosome[i], mother->chromosome[i],                  location2, location2, pop->len_chromosomes-location2);    ga_bit_copy(daughter->chromosome[i], father->chromosome[i],                  location2, location2, pop->len_chromosomes-location2);    }  return;  }/**********************************************************************  ga_crossover_bitstring_mixing()  synopsis:	`Mates' two genotypes by mixing parents chromsomes.		Keeps all chromosomes intact, and therefore do not		need to recreate structural data.  parameters:  return:  last updated: 30/06/01 **********************************************************************/void ga_crossover_bitstring_mixing(population *pop, entity *father, entity *mother, entity *son, entity *daughter)  {  int		i;		/* Loop variable over all chromosomes */  /* Checks */  if (!father || !mother || !son || !daughter)    die("Null pointer to entity structure passed");  for (i=0; i<pop->num_chromosomes; i++)    {    if (random_boolean())      {      ga_bit_clone(son->chromosome[i], father->chromosome[i], pop->len_chromosomes);      ga_bit_clone(daughter->chromosome[i], mother->chromosome[i], pop->len_chromosomes);      ga_copy_data(pop, son, father, i);      ga_copy_data(pop, daughter, mother, i);      }    else      {      ga_bit_clone(daughter->chromosome[i], father->chromosome[i], pop->len_chromosomes);      ga_bit_clone(son->chromosome[i], mother->chromosome[i], pop->len_chromosomes);      ga_copy_data(pop, daughter, father, i);      ga_copy_data(pop, son, mother, i);      }    }  return;  }/**********************************************************************  ga_crossover_bitstring_allele_mixing()  synopsis:	`Mates' two genotypes by randomizing the parents		alleles.		Keeps no chromosomes intact, and therefore will		need to recreate all structural data.  parameters:  return:  last updated: 30/06/01 **********************************************************************/void ga_crossover_bitstring_allele_mixing( population *pop,                                 entity *father, entity *mother,                                 entity *son, entity *daughter )  {  int		i, j;		/* Loop over all chromosomes, alleles. */  /* Checks. */  if (!father || !mother || !son || !daughter)    die("Null pointer to entity structure passed.");  for (i=0; i<pop->num_chromosomes; i++)    {    for (j=0; j<pop->len_chromosomes; j++)      {      if (random_boolean())        {        if (ga_bit_get(father->chromosome[i],j))          ga_bit_set(son->chromosome[i],j);        else          ga_bit_clear(son->chromosome[i],j);        if (ga_bit_get(mother->chromosome[i],j))          ga_bit_set(daughter->chromosome[i],j);        else          ga_bit_clear(daughter->chromosome[i],j);        }      else        {        if (ga_bit_get(father->chromosome[i],j))          ga_bit_set(daughter->chromosome[i],j);        else          ga_bit_clear(daughter->chromosome[i],j);        if (ga_bit_get(mother->chromosome[i],j))          ga_bit_set(son->chromosome[i],j);        else          ga_bit_clear(son->chromosome[i],j);        }      }    }  return;  }/**********************************************************************  ga_singlepoint_crossover_double_chromosome()  synopsis:	`Mates' two chromosomes by single-point crossover.  parameters:  return:  last updated: 07 Nov 2002 **********************************************************************/static void ga_singlepoint_crossover_double_chromosome( population *pop,                                         double *father, double *mother,                                         double *son, double *daughter )  {  int	location;	/* Point of crossover */  /* Checks */  if (!father || !mother || !son || !daughter)    die("Null pointer to chromosome structure passed.");  /* Choose crossover point and perform operation */  location=random_int(pop->len_chromosomes);  memcpy(son, mother, location*sizeof(double));  memcpy(daughter, father, location*sizeof(double));  memcpy(&(son[location]), &(father[location]), (pop->len_chromosomes-location)*sizeof(double));  memcpy(&(daughter[location]), &(mother[location]), (pop->len_chromosomes-location)*sizeof(double));  return;  }/**********************************************************************  ga_doublepoint_crossover_double_chromosome()  synopsis:	`Mates' two chromosomes by double-point crossover.  parameters:  return:  last updated: 07 Nov 2002 **********************************************************************/static void ga_doublepoint_crossover_double_chromosome(population *pop,                              double *father, double *mother,                              double *son, double *daughter)  {  int	location1, location2;	/* Points of crossover. */  int	tmp;			/* For swapping crossover loci. */  /* Checks */  if (!father || !mother || !son || !daughter)    die("Null pointer to chromosome structure passed.");  /* Choose crossover point and perform operation */  location1=random_int(pop->len_chromosomes);  do    {    location2=random_int(pop->len_chromosomes);    } while (location2==location1);    if (location1 > location2)      {      tmp = location1;      location1 = location2;      location2 = tmp;      }  memcpy(son, father, location1*sizeof(double));  memcpy(daughter, mother, location1*sizeof(double));  memcpy(&(son[location1]), &(mother[location1]), (location2-location1)*sizeof(double));  memcpy(&(daughter[location1]), &(father[location1]), (location2-location1)*sizeof(double));  memcpy(&(son[location2]), &(father[location2]), (pop->len_chromosomes-location2)*sizeof(double));  memcpy(&(daughter[location2]), &(mother[location2]), (pop->len_chromosomes-location2)*sizeof(double));  return;  }/**********************************************************************  ga_crossover_double_singlepoints()  synopsis:	`Mates' two genotypes by single-point crossover of		each chromosome.  parameters:  return:  last updated: 07 Nov 2002 **********************************************************************/void ga_crossover_double_singlepoints(population *pop, entity *father, entity *mother, entity *son, entity *daughter)  {  int		i;	/* Loop variable over all chromosomes */  /* Checks */  if (!father || !mother || !son || !daughter)    die("Null pointer to entity structure passed");  for (i=0; i<pop->num_chromosomes; i++)    {    ga_singlepoint_crossover_double_chromosome( pop,                        (double *)father->chromosome[i],			(double *)mother->chromosome[i],			(double *)son->chromosome[i],			(double *)daughter->chromosome[i]);    }  return;  }/**********************************************************************  ga_crossover_double_doublepoints()  synopsis:	`Mates' two genotypes by double-point crossover of		each chromosome.  parameters:  return:  last updated: 07 Nov 2002 **********************************************************************/void ga_crossover_double_doublepoints( population *pop,                                        entity *father, entity *mother,                                        entity *son, entity *daughter )  {  int		i;	/* Loop variable over all chromosomes */  /* Checks */  if (!father || !mother || !son || !daughter)    die("Null pointer to entity structure passed");  for (i=0; i<pop->num_chromosomes; i++)    {    ga_doublepoint_crossover_double_chromosome( pop,                        (double *)father->chromosome[i],			(double *)mother->chromosome[i],			(double *)son->chromosome[i],			(double *)daughter->chromosome[i]);    }  return;  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -