def main(): bb.search(f=fun, # given function box=[[0., 1.], [0., 1.], [0., 1.], [0., 1.], [0., 1.], [0., 1.]], # range of values for each parameter n=40, # number of function calls on initial stage (global search) m=40, # number of function calls on subsequent stage (local search) batch=1, # number of calls that will be evaluated in parallel resfile='output.csv') # text file where results will be saved
def main(): start_time = time.time() thresholdWavgDistanceToMidpointPercent60minRange = [-1, 1] thresholdWavgDistanceToMidpointPercent5minRange = [-1, 1] thresholdWavgDistanceToMidpointPercent30minRange = [-1, 1] thresholdPowerImbalanceRange = [-1, 3] thresholdEstimatedFutureWavg5Range = [-1, 2] thresholdEstimatedFutureWavg30Range = [-1, 2] thresholdEstimatedFutureWavg60Range = [-1, 2] thresholdEstimatedFutureWavg120Range = [-1, 2] thresholdSumFeaturesRange = [5, 6] thresholdBuyProfitRange = [0.001, 0.0065] bb.search( f=blackbox_gekko, # given function box=[ thresholdWavgDistanceToMidpointPercent60minRange, thresholdWavgDistanceToMidpointPercent5minRange, thresholdWavgDistanceToMidpointPercent30minRange, thresholdPowerImbalanceRange, thresholdEstimatedFutureWavg5Range, thresholdEstimatedFutureWavg30Range, thresholdEstimatedFutureWavg60Range, thresholdEstimatedFutureWavg120Range, thresholdSumFeaturesRange, thresholdBuyProfitRange ], # range of values for each parameter n=50, # number of function calls on initial stage (global search) m=50, # number of function calls on subsequent stage (local search) batch=8, # number of calls that will be evaluated in parallel resfile=gekko_base_path + 'blackbox/' + gekko_strategy + '/output.csv') # text file where results will be saved print("--- %s seconds ---" % (time.time() - start_time))
def main(): bb.search( f=predicted_revenue, # given function box=[[0., 10.], [0., 12.], [0., 40.], [0., 50.], [0., 100.]], # range of values for each parameter (2D case) n=100, # number of function calls on initial stage (global search) m=100, # number of function calls on subsequent stage (local search) batch=16, # number of calls that will be evaluated in parallel resfile='revenue_output.csv') # text file where results will be saved
def main(): start_time = time.time() thresholdSumFeaturesForBuy = [3, 8] thresholdSumFeaturesForSell = [-3, 5] bb.search( f=blackbox_gekko, # given function box=[thresholdSumFeaturesForBuy, thresholdSumFeaturesForSell ], # range of values for each parameter n=100, # number of function calls on initial stage (global search) m=100, # number of function calls on subsequent stage (local search) batch=12, # number of calls that will be evaluated in parallel resfile=gekko_base_path + 'blackbox/' + gekko_strategy + '/output.csv') # text file where results will be saved print("--- %s seconds ---" % (time.time() - start_time))
def start_blackbox_optimise(minX, maxX): bb.search( f=func, # given function box=[[minX, maxX]], # range of values for each parameter (2D case) n=10, # number of function calls on initial stage (global search) m=40, # number of function calls on subsequent stage (local search) batch=2, # number of calls that will be evaluated in parallel resfile='output.csv') # text file where results will be saved print("@@ Done blackbox search") # End off by sending result with open('output.csv', 'r') as f: # line zero is the header, line one is the best result line1 = f.readlines()[1] x1, y1 = eval(line1) # remember to invert back the y value optimised = -y1 func_queue.put((optimised, False))
def test_2(): """This function tests that the BLACKBOX results are unaffected by the size of the batch.""" d, box, n, m, batch, strategy = get_valid_request() rslt_base = None for batch in range(1, 5): rslt = search(rosen, box, n, m, batch, strategy) if rslt_base is None: rslt_base = rslt np.testing.assert_almost_equal(rslt, rslt_base)
def test_6(): """This test function runs a subset of the regression tests.""" vault = pkl.load( open(TEST_RESOURCES + '/regression_vault.blackbox.pkl', 'rb')) for idx in np.random.choice(len(vault), size=5): request, rslt = vault[idx] # We need to adjust the strategy if there is no MPI available. if request[-1] not in EXECUTORS: request[-1] = 'mp' stat = search(*request) np.testing.assert_equal(rslt, stat)
def test_1(): """This function tests that the BLACKBOX results are unaffected by the parallelization strategy.""" d = np.random.randint(2, 5) box = [[-10., 10.]] * d n = d + np.random.randint(5, 10) batch = np.random.randint(1, 5) m = np.random.randint(5, 25) rslt_base = None for strategy in ['mpi', 'mp']: rslt = search(rosen, box, n, m, batch, strategy) if rslt_base is None: rslt_base = rslt np.testing.assert_almost_equal(rslt, rslt_base)
def test_3(): """This function tests that the results line up with the original algorithm.""" # Unfortunately, there will be small differences when using the FORTRAN implementation # of selected functions. However, this test always passes when the PYTHON versions are # used. I test all implementations against each other in other tests and as the interface # is identical, I am confident that the discrepancies here are only due to small numerical # differences in the PYTHON and FORTRAN calculations that accumulate if all are used at once. open(PYTHON_FNAME, 'a').close() d, box, n, m, batch, strategy = get_valid_request() np.random.seed(123) alg_original = bb_search(rosen, box, n, m, batch, resfile='output.csv') alg_revised = search(rosen, box, n, m, batch, strategy, legacy=True) np.testing.assert_almost_equal(alg_original, alg_revised) os.remove(PYTHON_FNAME)
def test_5(): """This test function ensures that the results are unaffected by using either the FORTRAN or PYTHON function.""" d, box, n, m, batch, strategy = get_valid_request() rslt_base = None for is_python in [True, False]: if is_python: open(PYTHON_FNAME, 'a').close() rslt = search(rosen, box, n, m, batch, strategy) if rslt_base is None: rslt_base = rslt np.testing.assert_almost_equal(rslt, rslt_base) if os.path.exists(PYTHON_FNAME): os.remove(PYTHON_FNAME)
# grid search """ forest_params = {'max_depth': DEPTH_RANGE,'max_features': range(1,feats)} forest_grid = GridSearchCV(forest, forest_params, cv=5, n_jobs=-1, verbose=True, scoring='f1') forest_grid.fit(X_train, y_train) # forest_grid.best_params_ best_scores_grid_search.append(forest_grid.best_score_ ) """ # blackbox optimization print("START") bb.search( f=bb_func, # given function box=[DEPTH_RANGE, range(1, feats)], # range of values for each parameter (2D case) n=4, # number of function calls on initial stage (global search) m=2, # number of function calls on subsequent stage (local search) batch=1, # number of calls that will be evaluated in parallel resfile=GRAPH_PATH + 'output.csv') # text file where results will be saved print("...") print(best_scores_grid_search) print(sample_range) plt.plot(sample_range, best_scores_grid_search, label='Grid search') plt.title('Optimization scores') plt.savefig(GRAPH_PATH + 'forest_optimization_scores.png', dpi=900) plt.legend()
from blackbox import search from scipy.optimize import rosen import cProfile import pstats d = 2 box = [[-10, 10]] * d n = 100 m = 10 strategy = 'mp' batch = 2 search(rosen, box, n, m, batch, strategy)
C0 = 299792458 f_0 = 6.4896e9 lambda0 = C0 / f_0 bounds = [ [6.0429e-03 / 1.15, 6.0429e-03 * 1.15], # radius [1.0871e-02 / 1.15, 1.0871e-02 * 1.15], #length [4.4621e-03 / 1.15, 4.4621e-03 * 1.15], #pitch2 [4.2901e-03 / 1.5, 4.2901e-03 * 1.5] #pitch1 ] class Mapper: def map(self, *args, **kwargs): global client return client.gather(client.map(*args, **kwargs)) mapper = Mapper() @contextmanager def executor(): yield mapper bb.search( f=fun, # given function box=bounds, # range of values for each parameter n=80, # number of function calls on initial stage (global search) m=24, # number of function calls on subsequent stage (local search) batch=8, # number of calls that will be evaluated in parallel resfile='output.csv', # text file where results will be saved executor=executor)
import os from scipy.optimize import rosen import numpy as np from blackbox.tests.auxiliary import get_valid_request from blackbox import search import blackbox is_creation = False if is_creation: num_tests = 100 tests = [] for seed in range(num_tests): request = [rosen] + list(get_valid_request())[1:] rslt = search(*request) tests.append([request, rslt]) pkl.dump(tests, open('regression_vault.blackbox.pkl', 'wb')) # TODO: These are only working on heracles at the moment, portability is still an issue. FNAME_VAULT = os.path.dirname( blackbox.__file__) + '/tests/material/regression_vault.blackbox.pkl' for i, test in enumerate(pkl.load(open(FNAME_VAULT, 'rb'))): print(" running test " + str(i)) request, rslt = test stat = search(*request) np.testing.assert_equal(rslt, stat)