lnaive_scheduler.py~ 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # schedulers/lnaive_scheduler.py
  2. from lnaive_nb import naive_network_benchmarking_with_budget # 既存実装を利用
  3. def lnaive_budget_scheduler(
  4. node_path_list, # 例: [2, 2, 2] … 各ペアのパス本数
  5. importance_list, # 例: [0.3, 0.5, 0.7] … 長さは node_path_list と同じ(ここでは未使用)
  6. bounces, # 例: [1,2,3,4](重複なし)
  7. C_total, # 総予算(切り捨て配分、超過しない)
  8. network_generator, # callable: (path_num, pair_idx) -> network
  9. ):
  10. num_pairs = len(node_path_list)
  11. assert num_pairs == len(importance_list), "length mismatch: node_path_list vs importance_list"
  12. if num_pairs == 0:
  13. return [], 0
  14. # bounces は重複なし・正の整数を仮定
  15. assert len(bounces) == len(set(bounces)), "bounces must be unique"
  16. assert all(isinstance(w, int) and w > 0 for w in bounces), "bounces must be positive ints"
  17. C_per_pair = int(C_total) // num_pairs # 等分(余りは未使用)
  18. per_pair_results, total_cost = [], 0
  19. for pair_idx, path_num in enumerate(node_path_list):
  20. if path_num <= 0:
  21. per_pair_results.append((False, 0, None))
  22. continue
  23. network = network_generator(path_num, pair_idx)
  24. path_list = list(range(1, path_num + 1))
  25. correctness, cost, best_path_fidelity = naive_network_benchmarking_with_budget(
  26. network, path_list, list(bounces), C_per_pair
  27. )
  28. per_pair_results.append((bool(correctness), int(cost), best_path_fidelity))
  29. total_cost += int(cost)
  30. return per_pair_results, total_cost