From 91292e04ed29db72b581a54819136d403b97df43 Mon Sep 17 00:00:00 2001 From: KoyoNoguchi Date: Sat, 23 May 2026 00:33:58 +0900 Subject: [PATCH] =?UTF-8?q?antigravity=E3=81=A7m0.50=E3=81=8C=E6=AD=A3?= =?UTF-8?q?=E3=81=97=E3=81=84=E3=81=AE=E3=81=8B=E7=A2=BA=E8=AA=8D=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compare/em_summary_v2.dat | 13 ++++++++ run_experiments.ps1 | 62 ++++++++++++++++++++++++++++++++++++++ tsp.c | 63 ++++++++++++++++++++------------------- tsp.h | 11 ++++--- 4 files changed, 114 insertions(+), 35 deletions(-) create mode 100644 compare/em_summary_v2.dat create mode 100644 run_experiments.ps1 diff --git a/compare/em_summary_v2.dat b/compare/em_summary_v2.dat new file mode 100644 index 0000000..42c7972 --- /dev/null +++ b/compare/em_summary_v2.dat @@ -0,0 +1,13 @@ +# 突然変異確率の影響 比較表(エリート選択、10回試行の平均) +# 列定義: +# 1: MutationRate - 突然変異確率 (MUTATION_RATIO) +# 2: MeanBest - 平均最終距離 +# 3: MinBest - 最短距離 (Best) +# 4: OptRate(%) - 最適解到達率 (最適解: 35.63) +# +# MutationRate, MeanBest, MinBest, OptRate(%) +0.01, 42.37, 38.69, 0 +0.05, 38.41, 35.63, 30 +0.10, 39.44, 35.63, 10 +0.20, 37.16, 35.63, 40 +0.50, 36.82, 35.63, 50 diff --git a/run_experiments.ps1 b/run_experiments.ps1 new file mode 100644 index 0000000..fa90edb --- /dev/null +++ b/run_experiments.ps1 @@ -0,0 +1,62 @@ +$ratesStr = @("0.01", "0.05", "0.10", "0.20", "0.50") +$outputFile = "compare\em_summary_v2.dat" + +# ヘッダー書き込み +@" +# 突然変異確率の影響 比較表(エリート選択、10回試行の平均) +# 列定義: +# 1: MutationRate - 突然変異確率 (MUTATION_RATIO) +# 2: MeanBest - 平均最終距離 +# 3: MinBest - 最短距離 (Best) +# 4: OptRate(%) - 最適解到達率 (最適解: 35.63) +# +# MutationRate, MeanBest, MinBest, OptRate(%) +"@ | Out-File -FilePath $outputFile -Encoding ASCII + +$tsp_h = "tsp.h" +$content = Get-Content -Path $tsp_h -Raw + +foreach ($rateStr in $ratesStr) { + # tsp.h の MUTATION_RATIO 行を置換 + $newContent = $content -replace '#define MUTATION_RATIO [\d\.]+', "#define MUTATION_RATIO $rateStr" + Set-Content -Path $tsp_h -Value $newContent -Encoding Default + + # コンパイル + gcc tsp.c tsp_jikken.c tsp_public.c tsp_private.c -o tsp.exe -lm + + # 実行 + $output = .\tsp.exe check.dat + + # 出力の解析 + $bestLengths = @() + foreach ($line in $output) { + if ($line -match '^#') { continue } + $parts = $line -split ',' + if ($parts.Length -ge 4) { + $bestLengths += [double]$parts[3].Trim() + } + } + + if ($bestLengths.Count -gt 0) { + $meanBest = ($bestLengths | Measure-Object -Average).Average + $minBest = ($bestLengths | Measure-Object -Minimum).Minimum + $optCount = ($bestLengths | Where-Object { $_ -le 35.631 }).Count + $optRate = ($optCount / $bestLengths.Count) * 100 + + # ファイルに追記 (カルチャ依存を回避して文字列操作) + $meanBestStr = [math]::Round($meanBest, 2).ToString("0.00", [System.Globalization.CultureInfo]::InvariantCulture) + $minBestStr = [math]::Round($minBest, 2).ToString("0.00", [System.Globalization.CultureInfo]::InvariantCulture) + $optRateStr = [math]::Round($optRate, 0).ToString("0", [System.Globalization.CultureInfo]::InvariantCulture) + + $lineOut = "{0}, {1}, {2}, {3}" -f $rateStr, $meanBestStr, $minBestStr, $optRateStr + Add-Content -Path $outputFile -Value $lineOut -Encoding ASCII + Write-Host "Completed rate $rateStr" + } else { + Write-Host "Error parsing output for rate $rateStr" + } +} + +# tsp.h を 0.20 に戻す +$newContent = $content -replace '#define MUTATION_RATIO [\d\.]+', "#define MUTATION_RATIO 0.20" +Set-Content -Path $tsp_h -Value $newContent -Encoding Default +Write-Host "All experiments completed." diff --git a/tsp.c b/tsp.c index 595a37a..5b5a028 100644 --- a/tsp.c +++ b/tsp.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -39,6 +39,10 @@ int main(int argc, char *argv[]) Gene_set gene_group[GENE_NUM]; /* 遺伝子集団 */ FILE *fp; /* 座標用ファイル */ char string[BUFSIZ]; /* ファイル入力用バッファ */ + int trial; /* 試行回数カウンタ */ + int best_index; /* 最良個体のインデックス */ + double best_length; /* 最良経路長 */ + int gen_to_best; /* 最良値に達した世代数 */ srand((unsigned int)time(NULL)); /* rand() の種 */ @@ -78,43 +82,40 @@ int main(int argc, char *argv[]) fclose(fp); } - /* 各種データの初期化 */ - initial_gene(gene_group); /* 遺伝子集団の生成 */ - initial_fitness(gene_group, city); /* 初期適合度の計算 */ + /* CSVヘッダーの出力 */ + printf("# Trial, MutationRate, SelectNum, BestLength, GenToBest\n"); - /* 初期データの表示 */ - print_city(city); -#if PRINT_GENE /* 1/2 */ - print_gene_group(gene_group); -#endif /* PRINT_GENE 1/2 */ + for(trial = 0; trial < 10; trial++) { + /* 各種データの初期化 */ + initial_gene(gene_group); /* 遺伝子集団の生成 */ + initial_fitness(gene_group, city); /* 初期適合度の計算 */ - printf("INITIAL RESULT\n"); - print_best_data(gene_group); + /* 初期状態での最良経路長を取得 */ + best_index = get_best_gene(gene_group); + best_length = gene_group[best_index].length; + gen_to_best = 0; /* 初期状態で最良 */ - for(i = 0; i < GENERATION; i++) { - /* 選択 */ - selection(gene_group); /* この関数を実験で作成 */ + for(i = 0; i < GENERATION; i++) { + /* 選択 */ + selection(gene_group); - /* 突然変異を起こす */ - mutation(gene_group); /* この関数を実験で作成 */ + /* 突然変異を起こす */ + mutation(gene_group); - /* 適合度計算 */ - calculate_fitness(gene_group, city); + /* 適合度計算 */ + calculate_fitness(gene_group, city); -#if PRINT_PROCESS - /* 進化中のデータ表示 */ - printf("GENERATION %d\n", i); - print_data(gene_group); - /* print_best_data(gene_group); */ -#endif /* PRINT_PROCESS */ - } + /* 最良値の更新チェック */ + best_index = get_best_gene(gene_group); + if (gene_group[best_index].length < best_length) { + best_length = gene_group[best_index].length; + gen_to_best = i + 1; /* 1-indexed で世代数を記録 */ + } + } - /* 進化終了時のデータ表示 */ - printf("FINAL RESULT\n"); -#if PRINT_GENE /* 2/2 */ - print_gene_group(gene_group); -#endif /* PRINT_GENE 2/2 */ - print_best_data(gene_group); + /* 試行結果をCSVフォーマットで出力 */ + printf("%d, %.2f, %d, %.2f, %d\n", trial, MUTATION_RATIO, SELECT_NUM, best_length, gen_to_best); + } return 0; } \ No newline at end of file diff --git a/tsp.h b/tsp.h index 3619677..b2be18b 100644 --- a/tsp.h +++ b/tsp.h @@ -1,4 +1,4 @@ -#ifndef TSP_H_INCLUDED +#ifndef TSP_H_INCLUDED #define TSP_H_INCLUDED #define YES 1 @@ -7,10 +7,10 @@ #define POSITION_MAX 10 /* 都市座標の最大値 */ /* 以下のパラメータを調節する */ -#define GENE_NUM 200 /* 個体数 */ +#define GENE_NUM 100 /* 個体数 */ #define ELETE 1 /* エリート戦略用 */ -#define GENERATION 50 /* 進化世代数 */ -#define MUTATION_RATIO 0.2 /* 突然変異確率 */ +#define GENERATION 100 /* 進化世代数 */ +#define MUTATION_RATIO 0.20 /* 突然変異確率 */ #define CROSSOVER_RATIO 0.3 /* 交叉確率 */ /* 本実験では不使用 */ #define SELECT_NUM 2 /* トーナメント方式で選ぶ遺伝子の数 */ @@ -85,4 +85,7 @@ void print_best_data(Gene_set gene_group[GENE_NUM]); /* 最良値、平均値、最悪値を表示 */ void print_data(Gene_set gene_group[GENE_NUM]); +/* 最良個体のインデックスを取得 */ +int get_best_gene(Gene_set gene_group[GENE_NUM]); + #endif /* define TSP_H_INCLUDED */