made sobo and randombo more consistent

This commit is contained in:
2026-07-30 15:05:59 +09:00
parent ba4ebfd999
commit 02293389a1
5 changed files with 36 additions and 70 deletions
+23 -53
View File
@@ -9,71 +9,44 @@ class RandomBOEngine(BOEngine):
def __init__(self, config): def __init__(self, config):
super().__init__(config) super().__init__(config)
bo_params = [ self.params = [key for key, spec in config["bo"]["params"].items() if spec is not None]
key for key, spec in config["bo"]["params"].items() if spec is not None self.param_types = {key: config["bo"]["params"][key].get("type", "float") for key in self.params}
] self.integer_indices = [i for i, param in enumerate(self.params) if self.param_types[param] == 'int']
self.bounds = np.empty((2, len(self.params)))
bo_param_types = { for i, param in enumerate(self.params):
key: config["bo"]["params"][key].get("type", "float") for key in bo_params
}
integer_params = [
key for key in bo_params if bo_param_types[key] == "int"
]
integer_indices = [
bo_params.index(key) for key in integer_params
]
bounds = np.empty((2, len(bo_params)))
for i, param in enumerate(bo_params):
center = config["ptycho"]["params"][param] center = config["ptycho"]["params"][param]
radius = config["bo"]["params"][param]["radius"] radius = config["bo"]["params"][param]["radius"]
bounds[0, i] = center - radius self.bounds[0, i] = center - radius
bounds[1, i] = center + radius self.bounds[1, i] = center + radius
state = { self.train_x = np.empty((0, len(self.params))) # shape: (BOiter, BOparam)
"method": "random", self.train_y = np.empty((0,)) # shape: (BOiter,)
"acquisition": "",
"params": bo_params,
"param_types": bo_param_types,
"integer_params": integer_params,
"integer_indices": integer_indices,
"bounds": bounds, # shape: (2, BOparam)
"train_x": np.empty((0, len(bo_params))), # shape: (BOiter, BOparam)
"train_y": np.empty((0,)), # shape: (BOiter,)
"train_info": [] # shape: (BOiter,)
}
train_x_path = config["bo"].get("train_x") train_x_path = config["bo"].get("train_x")
train_y_path = config["bo"].get("train_y") train_y_path = config["bo"].get("train_y")
if train_x_path is not None and train_y_path is not None: if train_x_path is not None and train_y_path is not None:
if os.path.exists(train_x_path) and os.path.exists(train_y_path): if os.path.exists(train_x_path) and os.path.exists(train_y_path):
train_x = np.load(train_x_path) train_x = np.load(train_x_path)
train_y = np.load(train_y_path) train_y = np.load(train_y_path)
assert train_x.ndim == 2, "loaded train_x must be 2D" assert train_x.ndim == 2, "loaded train_x must be 2D"
assert train_x.shape[1] == len(bo_params), "loaded train_x shape(1) does not match number of variable parameters" assert train_x.shape[1] == len(self.params), "loaded train_x shape(1) does not match number of variable parameters"
assert train_y.ndim == 1, "loaded train_y must be 1D" assert train_y.ndim == 1, "loaded train_y must be 1D"
assert train_y.shape[0] == train_x.shape[0], "loaded train_x and train_y shape(0) have unequal iterations" assert train_y.shape[0] == train_x.shape[0], "loaded train_x and train_y shape(0) have unequal iterations"
state["train_x"] = train_x self.train_x = train_x
state["train_y"] = train_y self.train_y = train_y
self.state = state
def ask(self): def ask(self):
config = self.config config = self.config
state = self.state
next_config = copy.deepcopy(config) next_config = copy.deepcopy(config)
for param in state['params']: for param in self.params:
radius = config['bo']['params'][param]['radius'] radius = config['bo']['params'][param]['radius']
center = config['ptycho']['params'][param] center = config['ptycho']['params'][param]
modulation = radius * (np.random.rand() - 0.5) * 2 modulation = radius * (np.random.rand() - 0.5) * 2
next_value = center + modulation next_value = center + modulation
if state['param_types'][param] == 'int': if self.param_types[param] == 'int':
next_value = round(next_value) next_value = round(next_value)
next_config['ptycho']['params'][param] = next_value next_config['ptycho']['params'][param] = next_value
@@ -82,31 +55,28 @@ class RandomBOEngine(BOEngine):
def tell(self, job_config, y_value): def tell(self, job_config, y_value):
config = self.config config = self.config
state = self.state
x_value = [] x_value = []
for param in state['params']: for param in self.params:
x_value.append(job_config['ptycho']['params'][param]) x_value.append(job_config['ptycho']['params'][param])
state['train_x'] = np.vstack([ self.train_x = np.vstack([
state['train_x'], self.train_x,
np.array(x_value).reshape(1, -1) np.array(x_value).reshape(1, -1)
]) ])
state['train_y'] = np.concatenate([ self.train_y = np.concatenate([
state['train_y'], self.train_y,
np.array([y_value]) np.array([y_value])
]) ])
state['train_info'].append(state['method'])
train_x_path = config['bo'].get('train_x') train_x_path = config['bo'].get('train_x')
train_y_path = config['bo'].get('train_y') train_y_path = config['bo'].get('train_y')
if train_x_path is not None and train_y_path is not None: if train_x_path is not None and train_y_path is not None:
np.save(train_x_path, state['train_x']) np.save(train_x_path, self.train_x)
np.save(train_y_path, state['train_y']) np.save(train_y_path, self.train_y)
else: else:
result_dir = config['io']['result_dir'] result_dir = config['io']['result_dir']
np.save(os.path.join(result_dir, 'train_x.npy'), state['train_x']) np.save(os.path.join(result_dir, 'train_x.npy'), self.train_x)
np.save(os.path.join(result_dir, 'train_y.npy'), state['train_y']) np.save(os.path.join(result_dir, 'train_y.npy'), self.train_y)
+1 -2
View File
@@ -38,7 +38,6 @@ class SingleObjectiveBOEngine(BOEngine):
train_x_path = config["bo"].get("train_x") train_x_path = config["bo"].get("train_x")
train_y_path = config["bo"].get("train_y") train_y_path = config["bo"].get("train_y")
if train_x_path is not None and train_y_path is not None: if train_x_path is not None and train_y_path is not None:
if os.path.exists(train_x_path) and os.path.exists(train_y_path): if os.path.exists(train_x_path) and os.path.exists(train_y_path):
train_x = np.load(train_x_path) train_x = np.load(train_x_path)
@@ -168,7 +167,7 @@ class SingleObjectiveBOEngine(BOEngine):
]) ])
train_x_path = config['bo'].get('train_x') train_x_path = config['bo'].get('tain_x')
train_y_path = config['bo'].get('train_y') train_y_path = config['bo'].get('train_y')
if train_x_path is not None and train_y_path is not None: if train_x_path is not None and train_y_path is not None:
np.save(train_x_path, self.train_x) np.save(train_x_path, self.train_x)
+2 -2
View File
@@ -1,7 +1,7 @@
io: io:
input_data_path: '/home/swim/Si_project/data/Si2V1_1.mat' input_data_path: '/home/swim/Si_project/data/Si2V1_1.mat'
result_dir: '/home/swim/Si_project/wrapper/results/260721' result_dir: '/home/swim/bo-ptycho/results/260730'
verbosity: 1 verbosity: 0
ptycho: ptycho:
engine: 'fold_slice' engine: 'fold_slice'
+2 -2
View File
@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
#SBATCH --job-name=jobname #SBATCH --job-name=Si2V1
#SBATCH --nodes=1 #SBATCH --nodes=1
#SBATCH --ntasks=1 #SBATCH --ntasks=1
#SBATCH --cpus-per-task=1 #SBATCH --cpus-per-task=1
@@ -7,7 +7,7 @@
#SBATCH --time=100:00:00 #SBATCH --time=100:00:00
#SBATCH --output=/home/swim/slurm-logs/job_%j.log #SBATCH --output=/home/swim/slurm-logs/job_%j.log
echo "JOBNAME" echo "Si_2V_1"
pwd pwd
hostname hostname
date date
+7 -10
View File
@@ -16,30 +16,27 @@ def main(config_yaml):
os.makedirs(result_dir, exist_ok=True) os.makedirs(result_dir, exist_ok=True)
shutil.copy(config_yaml, os.path.join(result_dir, os.path.basename(config_yaml))) shutil.copy(config_yaml, os.path.join(result_dir, os.path.basename(config_yaml)))
# RANDOM BO SAMPLING PREPARATIONS; 20 SAMPLES
randombo = bo.RandomBOEngine(config) randombo = bo.RandomBOEngine(config)
for j in range(20):
for j in range(10): print(f"RANDOM sampling iteration {j+1}")
job_config = randombo.ask() job_config = randombo.ask()
ptycho_engine = ptycho.FoldSlicePtychoEngine(job_config) ptycho_engine = ptycho.FoldSlicePtychoEngine(job_config)
ptycho_engine.run() ptycho_engine.run()
y_value = -np.log(ptycho_engine.metric()) y_value = -np.log(ptycho_engine.metric())
randombo.tell(job_config, y_value) randombo.tell(job_config, y_value)
print('[random] TRAIN_X\n', randombo.state['train_x'])
print('[random] TRAIN_Y\n', randombo.state['train_y'])
# MAIN SINGLE OBJECTIVE BAYESIAN OPTIMIZATION
sobo = bo.SingleObjectiveBOEngine(config) sobo = bo.SingleObjectiveBOEngine(config)
sobo.train_x = randombo.state['train_x'] sobo.train_x = randombo.train_x
sobo.train_y = randombo.state['train_y'] sobo.train_y = randombo.train_y
for j in range(config['bo']['max_iterations']): for j in range(config['bo']['max_iterations']):
print(f"SOBO sampling iteration {j+1}")
job_config = sobo.ask() job_config = sobo.ask()
ptycho_engine = ptycho.FoldSlicePtychoEngine(job_config) ptycho_engine = ptycho.FoldSlicePtychoEngine(job_config)
ptycho_engine.run(header=f"[BO {j:03d}] ") ptycho_engine.run(header=f"[BO {j:03d}] ")
y_value = -np.log(ptycho_engine.metric()) y_value = -np.log(ptycho_engine.metric())
sobo.tell(job_config, y_value) sobo.tell(job_config, y_value)
print('[sobo] TRAIN_X\n', sobo.train_x)
print('[sobo] TRAIN_Y\n', sobo.train_y)
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) != 2: if len(sys.argv) != 2: