出力結果をみるとエリート選択は問題ないと思われます。デバックで値の動き等を見てみましたが原因が見つかりません。わかる方がいましたらご助言をいただけると嬉しいです。
const int G_number = 4, G_length = 10;//遺伝子の数、長さ
const int N = 4;//個体数
const int s = 2;//トーナメントサイズ
//重複しない乱数の生成
void shuffle(int array[], int size)
{
int i = size;
while (i > 1) {
int j = rand() % i;
i--;
int t = array[i];
array[i] = array[j];
array[j] = t;
}
}
void tournament(int gene_chrom[N * G_number][G_length], int gene_new_chrom[N * G_number][G_length], double v[N]) {
int tm[s * G_number][G_length];//一時的に選択した遺伝子を格納するもの
double vtm[s];//上記の評価値を格納
int tmp;
double vmp;
int i, j, k;
int p, q;
int data[N];//ランダム変数の格納
//エリート選択
for (p = 1; p <= N; p++) {
q = p;
while (q >= 1 && v[q - 1] < v[q]) {
for (i = 0; i < G_number; i++) {
for (j = 0; j < G_length; j++) {
tmp = gene_chrom[(q - 1) * G_number + i][j];
gene_chrom[(q - 1) * G_number + i][j] = gene_chrom[q * G_number + i][j];
gene_chrom[q * G_number + i][j] = tmp;
}
}
vmp = v[q - 1];
v[q - 1] = v[q];
v[q] = vmp;
q--;
}
}
//一番最初に評価値が一番高い個体を格納
for (i = 0; i < G_number; i++) {
for (j = 0; j < G_length; j++) {
gene_new_chrom[i][j] = gene_chrom[i][j];
}
}
//トーナメント選択
for (i = 0; i < N; i++) {
data[i] = i;
}
for (k = 1; k < N; k++) {
shuffle(data, N);//重複しない乱数を生成
//ランダムに選ばれた個体を一時的に格納
for (i = 0; i < s; i++) {
for (k = 0; k < G_number; k++) {
for (j = 0; j < G_length; j++) {
tm[i * G_number + k][j] = gene_chrom[data[i] * G_number + k][j];
}
}
vtm[i] = v[data[i]];
}
//評価値が高い順にする
for (p = 1; p <= s; p++) {
q = p;
while (q >= 1 && vtm[q - 1] < vtm[q]) {
for (i = 0; i < G_number; i++) {
for (j = 0; j < G_length; j++) {
tmp = tm[(q - 1) * G_number + i][j];
tm[(q - 1) * G_number + i][j] = tm[q * G_number + i][j];
tm[q * G_number + i][j] = tmp;
}
}
vmp = vtm[q - 1];
vtm[q - 1] = vtm[q];
vtm[q] = vmp;
q--;
}
}
//一番評価値が高いものを格納
for (i = 0; i < G_number; i++) {
for (j = 0; j < G_length; j++) {
gene_new_chrom[k * G_number + i][j] = tm[i][j];
}
}
v[k] = vtm[0];
}
}
int main() {
//int x;
int i;
int j;
int p;
int k;
int chrom[G_number][G_length];
int gene_chrom[N * G_number][G_length];
int gene_new_chrom[N * G_number][G_length];
int data[N];
double v[N];
srand((unsigned int)time(NULL));//乱数の種を変える
//トーナメント選択の試行
for (p = 0; p < N; p++) {
createInitialvalue(chrom);
for (i = 0; i < G_number; i++) {
for (j = 0; j < G_length; j++) {
gene_chrom[p * G_number + i][j] = chrom[i][j];
}
}
}
init_value(gene_chrom, v);
for (p = 0; p < N; p++) {
for (i = 0; i < G_number; i++) {
for (j = 0; j < G_length; j++) {
printf("%d ", gene_chrom[p * G_number + i][j]);
}
printf("\n");
}
printf("\n");
}
for (i = 0; i < N; i++) {
printf("%lf ", v[i]);
}
printf("\n");
tournament(gene_chrom, gene_new_chrom, v);
for (p = 0; p < N; p++) {
for (i = 0; i < G_number; i++) {
for (j = 0; j < G_length; j++) {
printf("%d ", gene_new_chrom[p * G_number + i][j]);
}
printf("\n");
}
printf("\n");
}
for (i = 0; i < N; i++) {
printf("%lf ", v[i]);
}
printf("\n");
return 0;
}
2 2 1 2 2 2 0 1 1 3
1 2 0 2 0 0 2 1 1 2
2 3 1 1 1 2 0 2 3 1
2 3 0 0 0 0 3 2 2 0
1 1 2 3 3 0 0 2 2 0
0 1 2 3 3 1 2 3 1 2
1 3 2 2 0 3 1 3 0 0
1 2 3 2 2 0 2 1 3 1
0 1 0 2 2 2 2 3 2 2
2 2 0 1 2 2 3 0 2 0
0 1 3 2 0 1 1 1 3 2
2 0 1 3 0 1 0 2 0 3
3 1 1 1 1 3 2 3 0 1
0 2 3 3 2 1 2 0 2 1
2 3 1 0 0 2 2 1 2 1
3 3 0 2 3 0 3 2 2 0
-6649.008980 -6959.004444 -7029.003460 -8389.022222
2 2 1 2 2 2 0 1 1 3
1 2 0 2 0 0 2 1 1 2
2 3 1 1 1 2 0 2 3 1
2 3 0 0 0 0 3 2 2 0
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
0.000000 0.000000 0.000000 0.000000