lnaive_nb.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # lnaive_nb.py
  2. def naive_network_benchmarking_with_budget(network, path_list, bounces, C_budget):
  3. """
  4. 目的:
  5. 総予算 C_budget を各パスへ均等割り当てし、均等サンプリングで NB を実行。
  6. 実行コストは常に予算内(超過しない)。
  7. 出力:
  8. (correctness, cost, best_path_fidelity)
  9. correctness … 推定最良パスが真の最良と一致したか
  10. cost … 実測で消費した総コスト
  11. best_path_fidelity … 推定最良パスの推定忠実度(naive変換後)
  12. """
  13. fidelity, cost = {}, 0
  14. n_paths = len(path_list)
  15. if n_paths == 0:
  16. return False, 0, None
  17. per_sample_cost = sum(bounces) or 1
  18. per_path_budget = int(C_budget) // n_paths
  19. Ns = per_path_budget // per_sample_cost # 各パスのサンプル数
  20. if Ns <= 0:
  21. return False, 0, None
  22. # 各 hop に同じ Ns を配る(既存 naive と同じ割当表)
  23. sample_times = {h: int(Ns) for h in bounces}
  24. # 各パスを均等回数でベンチマーク
  25. for path in path_list:
  26. p, used = network.benchmark_path(path, bounces, sample_times)
  27. fidelity[path] = p + (1 - p) / 2 # 既存 naive と同じ忠実度変換
  28. cost += used
  29. if not fidelity:
  30. return False, cost, None
  31. best_path = max(fidelity, key=fidelity.get)
  32. correctness = (best_path == getattr(network, "best_path", None))
  33. best_path_fidelity = fidelity[best_path]
  34. return correctness, cost, best_path_fidelity