def run_optimizer(optimizer, dim, fID, instance, logfile, lb, ub, max_FEs, data_path, bbob_opt): """Parallel BBOB/COCO experiment wrapper """ # Set different seed for different processes start = time() seed = np.mod(int(start) + os.getpid(), 1000) np.random.seed(seed) data_path = os.path.join(data_path, str(instance)) max_FEs = eval(max_FEs) f = fgeneric.LoggingFunction(data_path, **bbob_opt) f.setfun(*bn.instantiate(fID, iinstance=instance)) opt = optimizer(dim, f.evalfun, f.ftarget, max_FEs, lb, ub, logfile) opt.run() f.finalizerun() with open(logfile, 'a') as fout: fout.write( "{} on f{} in {}D, instance {}: FEs={}, fbest-ftarget={:.4e}, " "elapsed time [m]: {:.3f}\n".format(optimizer, fID, dim, instance, f.evaluations, f.fbest - f.ftarget, (time() - start) / 60.))
def perform_bbob_benchmarks(iinstances, trials): # creating the function object f = fgeneric.LoggingFunction(datapath, **opts) # cumulative sum of the errors of each 'run' of the heuristic result = 0.0 # for each function instance for iinstance in iinstances: # for each trial for trial in trials: # initializing the becnhmark in the selected function f.setfun( *bn.instantiate(objective_function_id, iinstance=iinstance)) # running the heuristic # _, _, = random_search(f.evalfun, dimension, maxfunevals, box_restr, f.ftarget) _, _, = pso(f.evalfun, dimension, maxfunevals, box_restr, f.ftarget) # printing information related to each 'run' print( ' f%d in %d-D, instance %d: FEs=%d, ' 'fbest-ftarget=%.4e, elapsed time [h]: %.2f\n' % (objective_function_id, dimension, iinstance, f.evaluations, f.fbest - f.ftarget, (time.time() - t0) / 60. / 60.)) # there's no need to store the best solution found in each 'run' because the benchmark # saves it ('the fitness') in 'f.fbest'. the value of the 'global optimum' found by our # heuristic is stored in 'f.ftarget' and the 'error' can be measured by 'f.fbest - f.ftarget'. # computing cumulative sum for each 'run' result += f.fbest - f.ftarget # finilizing benchmark for the current 'run' f.finalizerun() return result
# Test if we reached the optimum of the function # Remember that ">" for fitness means better (not greater) if best.fitness > opt: return best return best if __name__ == "__main__": # Maximum number of restart for an algorithm that detects stagnation maxrestarts = 1000 # Create a COCO experiment that will log the results under the # ./output directory e = fgeneric.LoggingFunction("output") # Iterate over all desired test dimensions for dim in (2, 3, 5, 10, 20, 40): # Set the maximum number function evaluation granted to the algorithm # This is usually function of the dimensionality of the problem maxfuncevals = 100 * dim**2 minfuncevals = dim + 2 # Iterate over a set of benchmarks (noise free benchmarks here) for f_name in bn.nfreeIDs: # Iterate over all the instance of a single problem # Rotation, translation, etc. for instance in chain(list(range(1, 6)), list(range(21, 31))):
xpop = 10. * np.random.rand(popsize, dim) - 5. fvalues = fun(xpop) idx = np.argsort(fvalues) if fbest > fvalues[idx[0]]: fbest = fvalues[idx[0]] xbest = xpop[idx[0]] if fbest < ftarget: # task achieved break return xbest t0 = time.time() np.random.seed(int(t0)) f = fgeneric.LoggingFunction(datapath, **opts) for dim in dimensions: # small dimensions first, for CPU reasons for fun_id in function_ids: for iinstance in instances: f.setfun(*bbobbenchmarks.instantiate(fun_id, iinstance=iinstance)) # independent restarts until maxfunevals or ftarget is reached for restarts in xrange(maxrestarts + 1): if restarts > 0: f.restart('independent restart') # additional info run_optimizer(f.evalfun, dim, eval(maxfunevals) - f.evaluations, f.ftarget) if (f.fbest < f.ftarget or f.evaluations + eval(minfunevals) > eval(maxfunevals)): break
xbest = alg.best_solution[0] if fbest < ftarget: # task achieved break # compute the next population alg.run(1) return xbest timings = [] runs = [] dims = [] for dim in (2, 3, 5, 10, 20, 40, 80, 160): # 320, 640, 1280, 2560, 5120, 10240, 20480): nbrun = 0 f = fgeneric.LoggingFunction('tmp').setfun(*bn.instantiate(8, 1)) t0 = time.time() while time.time() - t0 < 30: # at least 30 seconds run_optimizer(f.evalfun, dim, eval(MAX_FUN_EVALS), f.ftarget) # adjust maxfunevals nbrun = nbrun + 1 timings.append((time.time() - t0) / f.evaluations) dims.append(dim) # not really needed runs.append(nbrun) # not really needed f.finalizerun() print '\nDimensions:', for i in dims: print ' %11d ' % i, print '\n runs:', for i in runs: print ' %11d ' % i, print '\n times [s]:',
BSF_Epoch = [] FId = int(sys.argv[1]) D = int(sys.argv[2]) # Number of Dimensions F_flag = sys.argv[3] # "Vector" # Cte, Scalar, Vector ms_type = sys.argv[4] # 'population' # population static individual NP = int(sys.argv[5]) # 20 # Population Number ms_indx = sys.argv[6] # index of mutation scheme Bound = int(sys.argv[7]) Cr = 0.5 # CrossOver Rate VTR = 1e-8 # Value to Reach N_Epoch = 30 # Number of Epochs NFC = 10000 * D # Number of Function Calls LB = -Bound # UB = Bound # Upper bound f_gen = fgeneric.LoggingFunction('tmp').setfun(*bn.instantiate(FId)) OGV = f_gen.ftarget # Optimal Global Value to Reach readme_log = 'D:' + str(D) + ' NP:' + str(NP) + ' NFC:' + str(NFC) + \ ' MF:' + F_flag + ' MST:' + ms_type + ' MSI:' + str(ms_indx) + \ ' Limit:' + str(Bound) + 'N_Epoch:' + str(N_Epoch) + ' Cr:' + str(Cr) + ' VTR:' + str(VTR) logging.basicConfig(filename=script_name[:-3] + '.log', level=logging.INFO) logging.info(readme_log) ms_list = ['rand1', 'best1', 'tbest1', 'best2', 'rand2'] if ms_indx == 'null': ms_indx = 'null' else: ms_indx = int(ms_indx)
fileLocation = "FunctionData" if not os.path.exists(fileLocation): os.makedirs(fileLocation) def DataLog(fun_id, dim): filename = fileLocation + "/DATA-FunID-%d-DIM-%d" % (fun_id, dim) file_ = open(filename, 'w') for _ in range(dataLength): xpop = 10. * np.random.rand(dim) - 5 line = "" for num in xpop: line = line + str(num) + ", " line = line + str(f.evalfun(xpop)) + "\n" file_.write(line) def DataLogAllFun(dimNum): for fid in range(1, 25): f.setfun(*bbobbenchmarks.instantiate(fid, 1)) DataLog(fid, dimNum) f = fgeneric.LoggingFunction(datapath) #f.setfun(*bbobbenchmarks.instantiate(fun_id, 0)) for dimNum in dim: DataLogAllFun(dimNum)
CAPITALIZATION indicates code adaptations to be made. This script as well as files bbobbenchmarks.py and fgeneric.py need to be in the current working directory. Under unix-like systems: nohup nice python exampleexperiment.py [data_path [dimensions [functions [instances]]]] > output.txt & """ import sys # in case we want to control what to run via command line args import time import numpy as np import fgeneric as fg import bbobbenchmarks from neuralnetwork import NeuralNetwork e = fg.LoggingFunction( datapath='ellipsoid', algid='BFGS', comments='x0 uniformly sampled in [0, 1]^5, default settings') argv = sys.argv[1:] # shortcut for input arguments copy = [] datapath = '/home/cs4/ma1017/Desktop/bbob.v15.03/python/bbob_pproc/results' if len( argv) < 1 else argv[0] #'PUT_MY_BBOB_DATA_PATH' dimensions = [2] if len(argv) < 2 else eval(argv[1]) function_ids = [1 ] #bbobbenchmarks.nfreeIDs if len(argv) < 3 else eval(argv[2]) # function_ids = bbobbenchmarks.noisyIDs if len(argv) < 3 else eval(argv[2]) instances = range(1, 6) + range(41, 51) if len(argv) < 4 else eval(argv[3]) opts = dict( algid='tester', #'PUT ALGORITHM NAME', comments='PUT MORE DETAILED INFORMATION, PARAMETER SETTINGS ETC')