Skip to content
Snippets Groups Projects
Commit 896fc999 authored by shamith2's avatar shamith2
Browse files

update: changed compare.py, runBO.py, run_BO.py, sobol.py

parent dad68c3a
No related branches found
No related tags found
No related merge requests found
import argparse import argparse
import os.path import os
import pickle import pickle
import sys import sys
import time import time
from datetime import datetime from datetime import datetime
import pathlib
# current working directory
cwd = os.path.split(pathlib.Path(__file__).parent.absolute())[0]
parent = os.path.split(cwd)[0]
if str(parent) not in sys.path:
sys.path.append(parent)
sys.path.append(r'/::host::/BayesianOptimization/')
from target import * from target import *
EXPERIMENT_DIR = '/::host::/BayesianOptimization/HyperSphere/experiments/' EXPERIMENT_DIR = os.path.join(cwd, 'experiments/')
import torch import torch
from torch.autograd import Variable from torch.autograd import Variable
...@@ -236,4 +242,6 @@ if __name__ == '__main__': ...@@ -236,4 +242,6 @@ if __name__ == '__main__':
assert not args.origin assert not args.origin
assert not args.warping assert not args.warping
print(BO(**vars(args))) hs_file = BO(**vars(args))
print(hs_file)
\ No newline at end of file
import os import os, sys, stat
import tempfile import tempfile
import numpy as np import numpy as np
import torch import torch
import pathlib
def sobol_generate(n_dim, n_point, n_skip=0): def sobol_generate(n_dim, n_point, n_skip=0):
if n_dim > 1111: if n_dim > 1111:
...@@ -11,8 +11,11 @@ def sobol_generate(n_dim, n_point, n_skip=0): ...@@ -11,8 +11,11 @@ def sobol_generate(n_dim, n_point, n_skip=0):
try: try:
sequence_file = tempfile.NamedTemporaryFile('r') sequence_file = tempfile.NamedTemporaryFile('r')
filename = sequence_file.name filename = sequence_file.name
cmd = os.path.join(os.path.split(os.path.abspath(os.path.realpath(__file__)))[0], 'sobol_c/sobol') cmd_file = os.path.join(pathlib.Path(__file__).parent.absolute(), 'sobol_c/sobol')
cmd += ' ' + str(int(n_dim)) + ' ' + str(int(n_point)) + ' ' + str(int(n_skip)) + ' ' + filename cmd = cmd_file + ' ' + str(int(n_dim)) + ' ' + str(int(n_point)) + ' ' + str(int(n_skip)) + ' ' + filename
if (os.error(os.system(cmd))):
os.chmod(cmd_file, stat.S_IRWXU) # stat.S_IRWXU : read, write and execute permission for owner
print(f"\n{cmd_file}: Changed File Permissions to stat.S_IRWXU")
os.system(cmd) os.system(cmd)
sequence = np.fromfile(sequence_file.file, dtype=np.float64).astype(np.float32) sequence = np.fromfile(sequence_file.file, dtype=np.float64).astype(np.float32)
sequence = sequence.reshape([n_point, n_dim]) sequence = sequence.reshape([n_point, n_dim])
......
File mode changed from 100644 to 100755
import sys
from matplotlib import pyplot as my_plt from matplotlib import pyplot as my_plt
from matplotlib import patches as my_patch from matplotlib import patches as my_patch
import numpy as np import numpy as np
import pickle import pickle
import json import json
import pandas as pd import pandas as pd
import subprocess
from HyperSphere.BO.run_BO import BO
from fmfnBO.runBO import fmfnBO
from HyperSphere.test_functions.benchmarks import *
from target import *
data_config_filename = r'/::host::/BayesianOptimization/HyperSphere/experiments/::func_name::/data_config.pkl' def compare_plots(hs_files, fmfn_files):
log_file = r'/::host::/BayesianOptimization/fmfnBO/logs/::func_name::.json' # data file
plot_file = r'/::host::/BayesianOptimization/fmfnBO/plots/::func_name::.json' data_config_filename = hs_files + '/data_config.pkl'
f_name, log_file, plot_file = fmfn_files
# HyperSphere # HyperSphere
data_config_file = open(data_config_filename, 'rb') with open(data_config_filename, 'rb') as data_config_file:
for key, val in pickle.load(data_config_file).items(): for key, val in pickle.load(data_config_file).items():
if key == 'output': if key == 'output':
y1 = val.data.numpy() y1 = val.data.numpy()
if key == 'x_input': if key == 'x_input':
x1 = val.data.numpy() x1 = val.data.numpy()
# fmfn/BayesianOptimization
# read from file
with open(log_file, "r") as f:
file_data = f.read()
# add trailing commas for each line
with open(log_file, "r") as f:
lines = f.readlines()
first_line = lines[0]
file_lines = [''.join([',', line.strip(), '\n']) for line in lines[1:]]
data_config_file.close() # encaspulate the data
with open(plot_file, "w+") as f:
f.write('{' + '\n' + ' ' + '"logs"' + ':' + ' ' + '[' + '\n')
f.writelines(first_line)
f.writelines(file_lines)
f.write(" " + "]" + "\n" + "}")
# fmfn/BayesianOptimization # load data for visualization
with open(log_file, "r") as f: with open(plot_file) as json_file:
file_data = f.read() data = json.load(json_file)
with open(log_file, "r") as f: # number of evaluations
lines = f.readlines() x2 = np.arange(1, len(lines)+1, 1)
first_line = lines[0] # y = f(x)
file_lines = [''.join([',', line.strip(), '\n']) for line in lines[1:]] y2 = pd.DataFrame(data['logs']).to_numpy()[:,0]
with open(plot_file, "w+") as f: # visualization
f.write('{' + '\n' + ' ' + '"logs"' + ':' + ' ' + '[' + '\n') my_plt.figure('HyperSphere vs fmfn')
f.writelines(first_line) my_plt.title(f"function = {f_name} :: evaluations = 50")
f.writelines(file_lines) red = my_patch.Patch(color='red', label='HyperSphere')
f.write(" " + "]" + "\n" + "}") blue = my_patch.Patch(color='blue', label='fmfn')
my_plt.legend(handles=[red, blue])
with open(plot_file) as json_file: my_plt.plot(x1[:,0], y1, 'r')
data = json.load(json_file) my_plt.plot(x2, y2, 'b')
x2 = np.arange(1, len(lines)+1, 1) my_plt.show()
y2 = pd.DataFrame(data['logs']).to_numpy()[:,0]
if __name__ == '__main__':
# parameters
geometry = 'sphere'
func = 'branin'
d = '2'
e = '2'
my_plt.figure('HyperSphere vs fmfn') # subprocess
my_plt.title("function = 'bird Function' :: evaluations = 50") hs = subprocess.Popen(args=['python', 'HyperSphere/BO/run_BO.py', '-g', geometry, '--origin', '--warping',
red = my_patch.Patch(color='red', label='HyperSphere') '--parallel', '-f', func, '-d', d, '-e', e], stdout=subprocess.PIPE, universal_newlines=True)
blue = my_patch.Patch(color='blue', label='fmfn')
my_plt.legend(handles=[red, blue])
my_plt.plot(x1[:,0], y1, 'r') while True:
my_plt.plot(x2, y2, 'b') output = hs.stdout.readline()
if output not in [' ', '\n', '']:
hs_files = output
print(output.strip())
return_code = hs.poll()
if return_code is not None:
break
my_plt.show() fmfn = subprocess.Popen(args=['python', 'fmfnBO/runBO.py', '-f', str(func), '-d', str(d),
'-e', str(e)], stdout=subprocess.PIPE, universal_newlines=True)
while True:
output = fmfn.stdout.readline()
if output not in [' ', '\n', '']:
fmfn_files = output
print(output.strip())
return_code = fmfn.poll()
if return_code is not None:
break
# clean up hs_files
hs_files = hs_files.strip('\n')
# clean up fmfn_files
fmfn_files = [line.strip().strip("(").strip(")").strip("'") for line in fmfn_files.strip("\n").split(",")]
# compare
compare_plots(hs_files, fmfn_files)
from .bayesian_optimization import BayesianOptimization, Events from .bayesian_optimization import BayesianOptimization, Events
from .util import UtilityFunction from .util import UtilityFunction
from .logger import ScreenLogger, JSONLogger from .logger import ScreenLogger, JSONLogger
from .plotter import JSONPlotter
__all__ = [ __all__ = [
"BayesianOptimization", "BayesianOptimization",
...@@ -8,4 +9,5 @@ __all__ = [ ...@@ -8,4 +9,5 @@ __all__ = [
"Events", "Events",
"ScreenLogger", "ScreenLogger",
"JSONLogger", "JSONLogger",
] "JSONPlotter",
]
\ No newline at end of file
import os import os
import sys import sys
import argparse
import pathlib
from bayes_opt import BayesianOptimization from bayes_opt import BayesianOptimization, Events, JSONLogger
from bayes_opt.logger import JSONLogger
from bayes_opt.plotter import JSONPlotter # current working directory
from bayes_opt.event import Events cwd = pathlib.Path(__file__).parent.absolute()
parent = os.path.split(cwd)[0]
if str(parent) not in sys.path:
sys.path.append(parent)
sys.path.append('/::host::/')
from target import * from target import *
func = birdy def fmfnBO(n_eval, ndim, func=None):
assert func is not None
logs = os.path.join(cwd, 'logs/')
plots = os.path.join(cwd, 'plots/')
if not os.path.isdir(logs):
os.mkdir(logs)
logs = r'/::host::/BayesianOptimization/fmfnBO/logs/' if not os.path.isdir(plots):
plots = r'/::host::/BayesianOptimization/fmfnBO/plots/' os.mkdir(plots)
if not os.path.isdir(logs): log_name = os.path.join(logs + str(func) + ".json")
os.mkdir(logs) plot_name = os.path.join(plots + str(func) + ".json")
if not os.path.isdir(plots): optimizer = BayesianOptimization(func, func.pbounds, random_state=1)
os.mkdir(plots)
log_name = os.path.join(logs + str(func) + ".json") logger = JSONLogger(log_name)
plot_name = os.path.join(plots + str(func) + ".json") optimizer.subscribe(Events.OPTIMIZATION_STEP, logger)
optimizer = BayesianOptimization(func, func.pbounds, random_state=1) steps = n_eval
optimizer.maximize(init_points=2, n_iter=0, acq='ei', kappa=5)
optimizer.maximize(init_points=0, n_iter=steps, acq='ei', kappa=5)
print("Output Files:")
return (func, log_name, plot_name)
logger = JSONLogger(path) if __name__ == '__main__':
optimizer.subscribe(Events.OPTIMIZATION_STEP, logger) parser = argparse.ArgumentParser(description='FmFn Bayesian Optimization runner')
parser.add_argument('-e', '--n_eval', dest='n_eval', type=int, default=1)
parser.add_argument('-d', '--dim', dest='ndim', type=int, default=1)
parser.add_argument('-f', '--func', dest='func_name')
steps = 50 args = parser.parse_args()
optimizer.maximize(init_points=2, n_iter=0, acq='ei', kappa=5) fmfn_args = vars(args)
optimizer.maximize(init_points=0, n_iter=steps, acq='ei', kappa=5) if args.func_name is not None:
exec('func=' + 'fmfn_' + args.func_name)
fmfn_args['func'] = func
del fmfn_args['func_name']
JSONPlotter(log_name=log_name, plot_name=plot_name, func=str(func), x_eval=True) fmfn_files = fmfnBO(**fmfn_args)
print(fmfn_files)
\ No newline at end of file
...@@ -3,7 +3,6 @@ import math ...@@ -3,7 +3,6 @@ import math
import numpy as np import numpy as np
# HyperSphere Functions # HyperSphere Functions
def neg_birdy(x): def neg_birdy(x):
flat = x.dim() == 1 flat = x.dim() == 1
if flat: if flat:
...@@ -23,12 +22,12 @@ def neg_birdy(x): ...@@ -23,12 +22,12 @@ def neg_birdy(x):
neg_birdy.dim = 0 neg_birdy.dim = 0
def bf(x): # bohachevsky_function def bf(x): # bohachevsky_function
return x[0]**2 + 2*(x[1]**2) - 0.3*torch.cos(3*math.pi*x[0]) - 0.4*torch.cos(4*math.pi*x[1]) + 0.7 return x[0]**2 + 2*(x[1]**2) - 0.3*torch.cos(3*math.pi*x[0]) - 0.4*torch.cos(4*math.pi*x[1]) + 0.7
bf.dim = 0 bf.dim = 0
def nbf(x): # bohachevsky_function def nbf(x): # bohachevsky_function
return -1.0*(x[0]**2 + 2*(x[1]**2) - 0.3*torch.cos(3*math.pi*x[0]) - 0.4*torch.cos(4*math.pi*x[1]) + 0.7) return -1.0*(x[0]**2 + 2*(x[1]**2) - 0.3*torch.cos(3*math.pi*x[0]) - 0.4*torch.cos(4*math.pi*x[1]) + 0.7)
nbf.dim = 0 nbf.dim = 0
...@@ -120,12 +119,12 @@ sq.dim = 0 ...@@ -120,12 +119,12 @@ sq.dim = 0
# FmFn Functions # FmFn Functions
def sphere(x1, x2): def fmfn_sphere(x1, x2):
return (x1**2 + x2**2) return (x1**2 + x2**2)
sphere.pbounds = {'x1': (-5.12, 5.12), 'x2': (-5.12, 5.12)} fmfn_sphere.pbounds = {'x1': (-5.12, 5.12), 'x2': (-5.12, 5.12)}
def branin(x1, x2): def fmfn_branin(x1, x2):
a = 1 a = 1
b = 5.1 / (4 * math.pi ** 2) b = 5.1 / (4 * math.pi ** 2)
c = 5.0 / math.pi c = 5.0 / math.pi
...@@ -135,7 +134,7 @@ def branin(x1, x2): ...@@ -135,7 +134,7 @@ def branin(x1, x2):
return a * (x2 - b * x1**2 + c * x1 - r)**2 + s * (1 - t) * np.cos(x1) + s return a * (x2 - b * x1**2 + c * x1 - r)**2 + s * (1 - t) * np.cos(x1) + s
branin.pbounds = {'x1': (-5, 10), 'x2': (0, 15)} fmfn_branin.pbounds = {'x1': (-5, 10), 'x2': (0, 15)}
def fmfn_neg_branin(x1, x2): def fmfn_neg_branin(x1, x2):
a = 1 a = 1
...@@ -147,14 +146,16 @@ def fmfn_neg_branin(x1, x2): ...@@ -147,14 +146,16 @@ def fmfn_neg_branin(x1, x2):
return -1.0 * (a * (x2 - b * x1**2 + c * x1 - r)**2 + s * (1 - t) * np.cos(x1) + s) return -1.0 * (a * (x2 - b * x1**2 + c * x1 - r)**2 + s * (1 - t) * np.cos(x1) + s)
neg_branin.pbounds = {'x1': (-5, 10), 'x2': (0, 15)} fmfn_neg_branin.pbounds = {'x1': (-5, 10), 'x2': (0, 15)}
def fmfn_birdy(x1, x2):
def birdy(x1, x2):
return (np.sin(x1) * np.exp((1 - np.cos(x2))**2) + np.cos(x2) * np.exp((1 - np.sin(x1)**2)) + (x1 - x2)**2) return (np.sin(x1) * np.exp((1 - np.cos(x2))**2) + np.cos(x2) * np.exp((1 - np.sin(x1)**2)) + (x1 - x2)**2)
birdy.pbounds = {'x1': (-2*np.pi, 2*np.pi), 'x2': (-2*np.pi, 2*np.pi)} fmfn_birdy.pbounds = {'x1': (-2*np.pi, 2*np.pi), 'x2': (-2*np.pi, 2*np.pi)}
def fmfn_neg_birdy(x1, x2): def fmfn_neg_birdy(x1, x2):
return -1.0 * (np.sin(x1) * np.exp((1 - np.cos(x2))**2) + np.cos(x2) * np.exp((1 - np.sin(x1)**2)) + (x1 - x2)**2) return -1.0 * (np.sin(x1) * np.exp((1 - np.cos(x2))**2) + np.cos(x2) * np.exp((1 - np.sin(x1)**2)) + (x1 - x2)**2)
neg_birdy.pbounds = {'x1': (-2*np.pi, 2*np.pi), 'x2': (-2*np.pi, 2*np.pi)} fmfn_neg_birdy.pbounds = {'x1': (-2*np.pi, 2*np.pi), 'x2': (-2*np.pi, 2*np.pi)}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment