// 1-max-sga.c // Kazutoshi Sakakibara (June 13, 2010) #include #include #include #define N 15 // 遺伝子長 #define M 15 // 個体数 #define T 100 // 世代数 #define Pc 0.30 // 交叉確率 #define Pm 0.10 // 突然変異確率 // 遺伝子型の定義 struct genotype { int gene[N]; // 遺伝子 float fitness; // 適応度 }; float evaluation(int *a); void one_point_crossover(struct genotype *ind); void mutation(struct genotype *ind); void roulette_selection(struct genotype *ind); int flip(float prob); void print_process(struct genotype *ind, int generation); int main(int argc, char *argv[]) { int i; // 個体インデックス int j; // 遺伝子座インデックス int t; // 世代インデックス struct genotype individual[M]; // 個体 // 乱数seedの設定 if(argc < 2) { // プログラムの引数が足りない場合 printf("Usage: %s [SEED_NUMBER]\n", argv[0]); exit(1); } else { srandom(atoi(argv[1])); } // ステップ1 (0世代目) for(i=0; i0) { ib=(ib+1)%M; for(; test[ib]==1; ib=(ib+1)%M); r--; } test[ib] = 1; // 個体iaとibの遺伝子を入れ替える if(flip(Pc)) { c = random() % N; for(j=0; j wheel && h < M-1) { h++; wheel += ind[h].fitness / total_fitness; } ind_new[i] = ind[h]; } // 個体集合の更新 for(i=0; i ind[i].fitness) min_fit = ind[i].fitness; avg_fit += ind[i].fitness / M; } printf("max: %.2f min: %.2f avg: %.2f\n", max_fit, min_fit, avg_fit); } // End of print_process()