This commit is contained in:
2026-07-30 20:58:21 +09:00
parent 19f3cff188
commit 72f9cbb154
3 changed files with 26 additions and 14 deletions
+20 -8
View File
@@ -17,26 +17,38 @@ def main(config_yaml):
shutil.copy(config_yaml, os.path.join(result_dir, os.path.basename(config_yaml)))
# RANDOM BO SAMPLING PREPARATIONS; 20 SAMPLES
J = 20
randombo = bo.RandomBOEngine(config)
for j in range(20):
print(f"RANDOM sampling iteration {j+1}")
bo_txt = os.path.join(result_dir, "bo.txt")
with open(bo_txt, "w") as f:
f.write(f" iter\tmetric\t{"\t".join([p[:7] for p in randombo.params])}\n")
for j in range(J):
print(f"RANDOM sampling iteration {j}")
job_config = randombo.ask()
ptycho_engine = ptycho.FoldSlicePtychoEngine(job_config)
ptycho_engine.run()
y_value = -np.log(ptycho_engine.metric())
ptycho_engine.run(run_id=f"bo-{j:03d}")
y_value = ptycho_engine._metric
randombo.tell(job_config, y_value)
with open(bo_txt, "a") as f:
p = [f'{job_config['ptycho']['params'][key]:.2f}' for key in randombo.params]
f.write(f"{j: 8d}\t{y_value:.4f}\t{"\t".join(p)}\n")
# MAIN SINGLE OBJECTIVE BAYESIAN OPTIMIZATION
sobo = bo.SingleObjectiveBOEngine(config)
sobo.train_x = randombo.train_x
sobo.train_y = randombo.train_y
for j in range(config['bo']['max_iterations']):
print(f"SOBO sampling iteration {j+1}")
for j in range(J, J + config['bo']['max_iterations'] + 1):
print(f"SOBO sampling iteration {j}")
job_config = sobo.ask()
ptycho_engine = ptycho.FoldSlicePtychoEngine(job_config)
ptycho_engine.run(header=f"[BO {j:03d}] ")
y_value = -np.log(ptycho_engine.metric())
ptycho_engine.run(run_id=f"bo-{j:03d}")
y_value = ptycho_engine._metric
sobo.tell(job_config, y_value)
with open(bo_txt, "a") as f:
p = [f'{job_config['ptycho']['params'][key]:.2f}' for key in sobo.params]
f.write(f"{j: 8d}\t{y_value:.4f}\t{"\t".join(p)}\n")
if __name__ == "__main__":
if len(sys.argv) != 2: