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) //按照crossp的概率对选择的染色体进行交叉操作 { mask1 = createmask(randbit(0 , 14)); //由ranbit选择交叉位 mask2 = ~mask1; //形成一个类似 111000 000111之类的二进制码编码 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; } } }
// 1>随机选择将要进行交叉的染色体对,保证种群中的每个染色体都有唯一一次交叉的机会 // 2>按crossp的概率对选择的染色体对进行交叉操作 // 3>未进行交叉的染色体直接复制作为子染色体 void cross() { // 将父种群按概率crossp做交叉操作 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)); 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; } } }
static void testutil_newrand(const size_t bit_sz, const unsigned int seed) { // If we somehow managed to avoid freeing test_bitarray after a previous // test, go free it now. if (test_bitarray != NULL) { bitarray_free(test_bitarray); } test_bitarray = bitarray_new(bit_sz); assert(test_bitarray != NULL); // Reseed the RNG with whatever we were passed; this ensures that we can // repeat the test deterministically by specifying the same seed. srand(seed); for (size_t i = 0; i < bit_sz; i++) { bitarray_set(test_bitarray, i, randbit()); } // If we were asked to be verbose, go ahead and show the bit array and // the random seed. if (test_verbose) { bitarray_fprint(stdout, test_bitarray); fprintf(stdout, " newrand sz=%zu, seed=%u\n", bit_sz, seed); } }
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; } } } }
/* Create a new bitarray in test_ba of the specified size and fill it with random data based on the seed given. For a given seed number, the pseudorandom data will be the same (at least on the same glibc implementation). */ static void testutil_newrand(size_t bit_sz, unsigned int seed) { size_t i; if (test_ba != NULL) bitarray_free(test_ba); test_ba = bitarray_new(bit_sz); assert(test_ba != NULL); srand(seed); for (i = 0; i < bit_sz; i++) { bool temp = randbit() ; bitarray_set(test_ba, i, temp); } if (test_verbose) { bitarray_fprint(stdout, test_ba); fprintf(stdout, "\n"); fprintf(stdout, " newrand sz=%llu, seed=%u\n", (unsigned long long) bit_sz, seed); } }
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概率确定将要发生变异的染色体,再从染色体中随进选择一个基因进行变异 *由于进行选择和变异后父代种群的次序已被打乱,因此,在变异前后对种群进行一次排序 */ }