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