antigravityでm0.50が正しいのか確認した

main
KoyoNoguchi 2 months ago
parent 71dc0d33d2
commit 91292e04ed
  1. 13
      compare/em_summary_v2.dat
  2. 62
      run_experiments.ps1
  3. 47
      tsp.c
  4. 11
      tsp.h

@ -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

@ -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."

47
tsp.c

@ -1,4 +1,4 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <time.h> #include <time.h>
@ -39,6 +39,10 @@ int main(int argc, char *argv[])
Gene_set gene_group[GENE_NUM]; /* 遺伝子集団 */ Gene_set gene_group[GENE_NUM]; /* 遺伝子集団 */
FILE *fp; /* 座標用ファイル */ FILE *fp; /* 座標用ファイル */
char string[BUFSIZ]; /* ファイル入力用バッファ */ char string[BUFSIZ]; /* ファイル入力用バッファ */
int trial; /* 試行回数カウンタ */
int best_index; /* 最良個体のインデックス */
double best_length; /* 最良経路長 */
int gen_to_best; /* 最良値に達した世代数 */
srand((unsigned int)time(NULL)); /* rand() の種 */ srand((unsigned int)time(NULL)); /* rand() の種 */
@ -78,43 +82,40 @@ int main(int argc, char *argv[])
fclose(fp); fclose(fp);
} }
/* CSVヘッダーの出力 */
printf("# Trial, MutationRate, SelectNum, BestLength, GenToBest\n");
for(trial = 0; trial < 10; trial++) {
/* 各種データの初期化 */ /* 各種データの初期化 */
initial_gene(gene_group); /* 遺伝子集団の生成 */ initial_gene(gene_group); /* 遺伝子集団の生成 */
initial_fitness(gene_group, city); /* 初期適合度の計算 */ initial_fitness(gene_group, city); /* 初期適合度の計算 */
/* 初期データの表示 */ /* 初期状態での最良経路長を取得 */
print_city(city); best_index = get_best_gene(gene_group);
#if PRINT_GENE /* 1/2 */ best_length = gene_group[best_index].length;
print_gene_group(gene_group); gen_to_best = 0; /* 初期状態で最良 */
#endif /* PRINT_GENE 1/2 */
printf("INITIAL RESULT\n");
print_best_data(gene_group);
for(i = 0; i < GENERATION; i++) { for(i = 0; i < GENERATION; i++) {
/* 選択 */ /* 選択 */
selection(gene_group); /* この関数を実験で作成 */ selection(gene_group);
/* 突然変異を起こす */ /* 突然変異を起こす */
mutation(gene_group); /* この関数を実験で作成 */ mutation(gene_group);
/* 適合度計算 */ /* 適合度計算 */
calculate_fitness(gene_group, city); calculate_fitness(gene_group, city);
#if PRINT_PROCESS /* 最良値の更新チェック */
/* 進化中のデータ表示 */ best_index = get_best_gene(gene_group);
printf("GENERATION %d\n", i); if (gene_group[best_index].length < best_length) {
print_data(gene_group); best_length = gene_group[best_index].length;
/* print_best_data(gene_group); */ gen_to_best = i + 1; /* 1-indexed で世代数を記録 */
#endif /* PRINT_PROCESS */ }
} }
/* 進化終了時のデータ表示 */ /* 試行結果をCSVフォーマットで出力 */
printf("FINAL RESULT\n"); printf("%d, %.2f, %d, %.2f, %d\n", trial, MUTATION_RATIO, SELECT_NUM, best_length, gen_to_best);
#if PRINT_GENE /* 2/2 */ }
print_gene_group(gene_group);
#endif /* PRINT_GENE 2/2 */
print_best_data(gene_group);
return 0; return 0;
} }

11
tsp.h

@ -1,4 +1,4 @@
#ifndef TSP_H_INCLUDED #ifndef TSP_H_INCLUDED
#define TSP_H_INCLUDED #define TSP_H_INCLUDED
#define YES 1 #define YES 1
@ -7,10 +7,10 @@
#define POSITION_MAX 10 /* 都市座標の最大値 */ #define POSITION_MAX 10 /* 都市座標の最大値 */
/* 以下のパラメータを調節する */ /* 以下のパラメータを調節する */
#define GENE_NUM 200 /* 個体数 */ #define GENE_NUM 100 /* 個体数 */
#define ELETE 1 /* エリート戦略用 */ #define ELETE 1 /* エリート戦略用 */
#define GENERATION 50 /* 進化世代数 */ #define GENERATION 100 /* 進化世代数 */
#define MUTATION_RATIO 0.2 /* 突然変異確率 */ #define MUTATION_RATIO 0.20 /* 突然変異確率 */
#define CROSSOVER_RATIO 0.3 /* 交叉確率 */ /* 本実験では不使用 */ #define CROSSOVER_RATIO 0.3 /* 交叉確率 */ /* 本実験では不使用 */
#define SELECT_NUM 2 /* トーナメント方式で選ぶ遺伝子の数 */ #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]); void print_data(Gene_set gene_group[GENE_NUM]);
/* 最良個体のインデックスを取得 */
int get_best_gene(Gene_set gene_group[GENE_NUM]);
#endif /* define TSP_H_INCLUDED */ #endif /* define TSP_H_INCLUDED */

Loading…
Cancel
Save