📄 ga_crossover.c
字号:
void ga_crossover_boolean_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_boolean_chromosome( pop, (boolean *)father->chromosome[i], (boolean *)mother->chromosome[i], (boolean *)son->chromosome[i], (boolean *)daughter->chromosome[i]); } return; }/********************************************************************** ga_crossover_boolean_doublepoints() synopsis: `Mates' two genotypes by double-point crossover of each chromosome. parameters: return: last updated: 29 Jun 2003 **********************************************************************/void ga_crossover_boolean_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_boolean_chromosome( pop, (boolean *)father->chromosome[i], (boolean *)mother->chromosome[i], (boolean *)son->chromosome[i], (boolean *)daughter->chromosome[i]); } return; }/********************************************************************** ga_crossover_boolean_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: 27/04/00 **********************************************************************/void ga_crossover_boolean_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()) { memcpy(son->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(boolean)); memcpy(daughter->chromosome[i], mother->chromosome[i], pop->len_chromosomes*sizeof(boolean)); ga_copy_data(pop, son, father, i); ga_copy_data(pop, daughter, mother, i); } else { memcpy(daughter->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(boolean)); memcpy(son->chromosome[i], mother->chromosome[i], pop->len_chromosomes*sizeof(boolean)); ga_copy_data(pop, daughter, father, i); ga_copy_data(pop, son, mother, i); } } return; }/********************************************************************** ga_crossover_boolean_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/04/00 **********************************************************************/void ga_crossover_boolean_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()) { ((boolean *)son->chromosome[i])[j] = ((boolean *)father->chromosome[i])[j]; ((boolean *)daughter->chromosome[i])[j] = ((boolean *)mother->chromosome[i])[j]; } else { ((boolean *)daughter->chromosome[i])[j] = ((boolean *)father->chromosome[i])[j]; ((boolean *)son->chromosome[i])[j] = ((boolean *)mother->chromosome[i])[j]; } } } return; }/********************************************************************** ga_crossover_char_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: 16/06/01 **********************************************************************/void ga_crossover_char_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()) { memcpy( son->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(char) ); memcpy( daughter->chromosome[i], mother->chromosome[i], pop->len_chromosomes*sizeof(char) ); ga_copy_data(pop, son, father, i); ga_copy_data(pop, daughter, mother, i); } else { memcpy( daughter->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(char) ); memcpy( son->chromosome[i], mother->chromosome[i], pop->len_chromosomes*sizeof(char) ); ga_copy_data(pop, daughter, father, i); ga_copy_data(pop, son, mother, i); } } return; }/********************************************************************** ga_crossover_char_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: 16/06/01 **********************************************************************/void ga_crossover_char_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()) { ((char *)son->chromosome[i])[j] = ((char *)father->chromosome[i])[j]; ((char *)daughter->chromosome[i])[j] = ((char *)mother->chromosome[i])[j]; } else { ((char *)daughter->chromosome[i])[j] = ((char *)father->chromosome[i])[j]; ((char *)son->chromosome[i])[j] = ((char *)mother->chromosome[i])[j]; } } } return; }/********************************************************************** ga_crossover_double_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: 16/06/01 **********************************************************************/void ga_crossover_double_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()) { memcpy( son->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(double) ); memcpy( daughter->chromosome[i], mother->chromosome[i], pop->len_chromosomes*sizeof(double) ); ga_copy_data(pop, son, father, i); ga_copy_data(pop, daughter, mother, i); } else { memcpy( daughter->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(double) ); memcpy( son->chromosome[i], mother->chromosome[i], pop->len_chromosomes*sizeof(double) ); ga_copy_data(pop, daughter, father, i); ga_copy_data(pop, son, mother, i); } } return; }/********************************************************************** ga_crossover_double_mean() synopsis: `Mates' two genotypes by averaging the parents alleles. Keeps no chromosomes intact, and therefore will need to recreate all structural data. FIXME: Children are identical! parameters: return: last updated: 18 Jun 2004 **********************************************************************/void ga_crossover_double_mean( 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++) { ((double *)son->chromosome[i])[j] = 0.5 * (((double *)father->chromosome[i])[j] + ((double *)mother->chromosome[i])[j]); ((double *)daughter->chromosome[i])[j] = 0.5 * (((double *)father->chromosome[i])[j] + ((double *)mother->chromosome[i])[j]); } } return; }/********************************************************************** ga_crossover_double_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: 16/06/01 **********************************************************************/void ga_crossover_double_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()) { ((double *)son->chromosome[i])[j] = ((double *)father->chromosome[i])[j]; ((double *)daughter->chromosome[i])[j] = ((double *)mother->chromosome[i])[j]; } else { ((double *)daughter->chromosome[i])[j] = ((double *)father->chromosome[i])[j]; ((double *)son->chromosome[i])[j] = ((double *)mother->chromosome[i])[j]; } } } return; }/********************************************************************** ga_crossover_char_singlepoints() synopsis: `Mates' two genotypes by single-point crossover of each chromosome. parameters: return: last updated: 16/07/01 **********************************************************************/void ga_crossover_char_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); memcpy( son->chromosome[i], mother->chromosome[i], location*sizeof(char) ); memcpy( daughter->chromosome[i], father->chromosome[i], location*sizeof(char)); memcpy( &(((char *)son->chromosome[i])[location]), &(((char *)father->chromosome[i])[location]), (pop->len_chromosomes-location)*sizeof(char) ); memcpy( &(((char *)daughter->chromosome[i])[location]), &(((char *)mother->chromosome[i])[location]), (pop->len_chromosomes-location)*sizeof(char) ); } return; }/**********************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -