import sys import numpy as np from branin import branin """ Example for the use of SMAC through the commandline. To run this example, change to the branin-directory and type: python ../../scripts/smac --scenario branin_scenario.txt We optimize the branin-function, which has two parameters: x1 and x2. To use the cmd-line, we need two files: a scenario-file, located in examples/branin/branin_scenario.txt a pcs-file, located in examples/branin/branin_pcs.pcs The code below will be called by SMAC with parameters as cmd-line arguments and prints the results so SMAC can interpret them again. """ if __name__ == '__main__': seed = sys.argv[5] x = float(sys.argv[7]) y = float(sys.argv[9]) tmp = branin((x, y)) print('Result for SMAC: SUCCESS, -1, -1, %f, %s' % (tmp, seed))
def branin_f(p_vector): x, y = p_vector x = x - 5. y = y return branin(x, y)
import branin """ Jasper Snoek This is a simple script to help demonstrate the functionality of spearmintlite. It will read in results.dat and fill in 'pending' experiments. """ if __name__ == '__main__': resfile = open('results.dat', 'r') newlines = [] for line in resfile.readlines(): values = line.split() if len(values) < 3: continue val = values.pop(0) dur = values.pop(0) X = [float(values[0]), float(values[1])] print X if (val == 'P'): val = branin.branin(X) newlines.append( str(val) + " 0 " + str(float(values[0])) + " " + str(float(values[1])) + "\n") else: newlines.append(line) resfile.close() outfile = open('results.dat', 'w') for line in newlines: outfile.write(line)
import sys from branin import branin """ Example for the use of SMAC through the commandline. To run this example, change to the branin-directory and type: python ../../scripts/smac --scenario branin_scenario.txt We optimize the branin-function, which has two parameters: x1 and x2. To use the cmd-line, we need two files: a scenario-file, located in examples/branin/branin_scenario.txt a pcs-file, located in examples/branin/branin_pcs.pcs The code below will be called by SMAC with parameters as cmd-line arguments and prints the results so SMAC can interpret them again. """ if __name__ == '__main__': # <algo> <instance> <instance specific> <cutoff time> <runlength> <seed> <algorithm parameters> instance = sys.argv[1] instance_specs = sys.argv[2] seed = sys.argv[5] x = float(sys.argv[7]) y = float(sys.argv[9]) tmp = branin((x, y), inst=instance, inst_spec=instance_specs) print('Result for SMAC: SUCCESS, -1, -1, %f, %s' % (tmp, seed))
- a scenario-file: located in "examples/branin/scenario.txt" specifies SMAC-parameters, e.g. runtime, output, etc. - a pcs-file: located in "examples/branin/param_config_space.pcs" specifies the parameter configuration space (here: x1, x2) SMAC calls this wrapper during optimization, because it is specified in the "algo"-parameter of the scenario-file. SMAC calls this file via the commandline, passing information in additional commandline-arguments. The target algorithm parameters (here: x1, x2) are also passed as commandline-arguments. A full call by SMAC looks like this: <algo> <instance> <instance specific> <cutoff time> <runlength> <seed> <algorithm parameters> python branin.py 0 0 99999999999999 0 12345 -x1 0 -x2 0 SMAC processes results from the commandline, therefore the print-statement is crucial. The format of the results must be: Result for SMAC: <STATUS>, <runtime>, <runlength>, <quality>, <seed>, <instance-specifics> """ if __name__ == '__main__': # Unused in this example: # instance, instance_specific, cutoff, runlength = sys.argv[1:5] seed = sys.argv[5] # sys.argv[6] and sys.argv[8] are the names of the target algorithm # parameters (here: "-x1", "-x2") x = float(sys.argv[7]) y = float(sys.argv[9]) result = branin((x, y)) print('Result for SMAC: SUCCESS, -1, -1, %f, %s' % (result, seed))
from argparse import Namespace import numpy as np from probo import NelderMeadAcqOptimizer, SimpleBo from branin import branin, get_branin_domain_nd from penn_sklearn import SklearnPenn # define function ndimx = 40 f = lambda x: np.sum([branin(x[2 * i:2 * i + 2]) for i in range(ndimx // 2)]) # define model model = SklearnPenn() # define acqfunction acqfunction = {'acq_str': 'ts', 'n_gen': 500} # define acqoptimizer domain = get_branin_domain_nd(ndimx) acqoptimizer = NelderMeadAcqOptimizer( { 'rand_every': 10, 'max_iter': 200, 'jitter': True }, domain) # define initial dataset n_init = 80 data = Namespace() data.x = domain.unif_rand_sample(n_init) data.y = [f(x) for x in data.x]
def branin_f(p_vector): x, y = p_vector x = x - 5.0 y = y return branin(x, y)
spearmint-lite. It will read in results.dat and fill in 'pending' experiments. """ if __name__ == '__main__': resfile = open('results.dat','r') newlines = [] for line in resfile.readlines(): values = line.split() if len(values) < 3: continue val = values.pop(0) dur = values.pop(0) X = [float(values[0]), float(values[1])] Y = [float(values[2]), float(values[3])] print X print Y if (val == 'P'): val = branin.branin(X, Y) newlines.append(str(val) + " 0 " + str(float(values[0])) + " " + str(float(values[1])) + " " + str(float(values[2])) + " " + str(float(values[3])) + "\n") else: newlines.append(line) resfile.close() outfile = open('results.dat','w') for line in newlines: outfile.write(line)
def scoring_function(p_dict): x,y = p_dict['x'], p_dict['y'] x = x -5. y= y return branin(x,y)
def plot_contour(trialList, nameList, save = "", title=""): # constraints: # -5 <= x <= 10, 0 <= y <= 15 # three global optima: (-pi, 12.275), (pi, 2.275), (9.42478, 2.475), where # branin = 0.397887 markers = itertools.cycle(['o', 's', '^', 'x']) colors = itertools.cycle(['b', 'g', 'r', 'k']) # linestyles = itertools.cycle(['-']) size = 5 # Get handles ratio=5 gs = gridspec.GridSpec(ratio,1) fig = plt.figure(1, dpi=100) fig.suptitle(title) ax = plt.subplot(gs[0:ratio, :]) ax.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) xopt = [-np.pi, np.pi, 9.42478] yopt = [12.275, 2.275, 2.475] # Plot Branin step = 0.1 xi = np.arange(-5, 10 + step, step) yi = np.arange(-0, 15 + step, step) z = np.zeros([len(xi), len(yi)]) for i in range(len(xi)): for j in range(len(yi)): #z[j, i] = np.power(np.e, branin.branin({"x":xi[i], "y":yi[j]})) z[j, i] = branin.branin({"x":xi[i], "y":yi[j]}) XI, YI = np.meshgrid(xi, yi) cax = ax.contourf(XI, YI, z, 50, cmap=cm.gray) fig.colorbar(cax) # Plot Optimums after all work is done d = plt.scatter(xopt, yopt, marker="o", facecolor='w', edgecolor='w', s = 20*size, label="Optimum") # Get values for opt in range(len(nameList)): print nameList[opt], "has", len(trialList[opt]['trials']), "samples" m = markers.next() c = colors.next() x = np.zeros(len(trialList[opt]["trials"])) y = np.zeros(len(trialList[opt]["trials"])) for i in range(len(x)): x[i] = trialList[opt]["trials"][i]["params"]["x"] y[i] = trialList[opt]["trials"][i]["params"]["y"] a = plt.scatter(x[0:10], y[0:10], marker=m, s=size, facecolors=c, linewidth=0.1) # label=nameList[opt]) # + " first 10 points") b = plt.scatter(x[10:-10], y[10:-10], marker=m, linewidth=0.1, s=4*size, facecolors=c) # label="points in the middle") c = plt.scatter(x[-10:-1], y[-10:-1], marker=m, linewidth=0.1, s=6*size, facecolors=c, label=nameList[opt]) # label="last 10 points") plt.xlim([-5, 10]) plt.ylim([-0, 15]) plt.xlabel("X") plt.ylabel("Y") if [trial["result"] for trial in trialList[opt]["trials"]]: minimum = str(min([trial["result"] for trial in trialList[opt]["trials"]])) else: minimum = "NaN" # Describe the plot plt.title(title) leg = plt.legend(loc="best", fancybox=True) leg.get_frame().set_alpha(0.5) if save != "": plt.subplots_adjust(top=0.85) plt.savefig(save, dpi=600, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches="tight", pad_inches=0.1) else: plt.show()
import branin """ Jasper Snoek This is a simple script to help demonstrate the functionality of spearmint-lite. It will read in results.dat and fill in 'pending' experiments. """ if __name__ == '__main__': resfile = open('results.dat','r') newlines = [] for line in resfile.readlines(): values = line.split() if len(values) < 3: continue val = values.pop(0) dur = values.pop(0) X = [float(values[0]), float(values[1])] print X if (val == 'P'): val = branin.branin(X) newlines.append(str(val) + " 0 " + str(float(values[0])) + " " + str(float(values[1])) + "\n") else: newlines.append(line) resfile.close() outfile = open('results.dat','w') for line in newlines: outfile.write(line)
def scoring_function(p_dict): x, y = p_dict['x'], p_dict['y'] x = x - 5. y = y return branin(x, y)
def plot_contour(trialList, nameList, save="", title=""): # constraints: # -5 <= x <= 10, 0 <= y <= 15 # three global optima: (-pi, 12.275), (pi, 2.275), (9.42478, 2.475), where # branin = 0.397887 markers = itertools.cycle(['o', 's', '^', 'x']) colors = itertools.cycle(['b', 'g', 'r', 'k']) # linestyles = itertools.cycle(['-']) size = 5 # Get handles ratio = 5 gs = gridspec.GridSpec(ratio, 1) fig = plt.figure(1, dpi=100) fig.suptitle(title) ax = plt.subplot(gs[0:ratio, :]) ax.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) xopt = [-np.pi, np.pi, 9.42478] yopt = [12.275, 2.275, 2.475] # Plot Branin step = 0.1 xi = np.arange(-5, 10 + step, step) yi = np.arange(-0, 15 + step, step) z = np.zeros([len(xi), len(yi)]) for i in range(len(xi)): for j in range(len(yi)): #z[j, i] = np.power(np.e, branin.branin({"x":xi[i], "y":yi[j]})) z[j, i] = branin.branin({"x": xi[i], "y": yi[j]}) XI, YI = np.meshgrid(xi, yi) cax = ax.contourf(XI, YI, z, 50, cmap=cm.gray) fig.colorbar(cax) # Plot Optimums after all work is done d = plt.scatter(xopt, yopt, marker="o", facecolor='w', edgecolor='w', s=20 * size, label="Optimum") # Get values for opt in range(len(nameList)): print nameList[opt], "has", len(trialList[opt]['trials']), "samples" m = markers.next() c = colors.next() x = np.zeros(len(trialList[opt]["trials"])) y = np.zeros(len(trialList[opt]["trials"])) for i in range(len(x)): x[i] = trialList[opt]["trials"][i]["params"]["x"] y[i] = trialList[opt]["trials"][i]["params"]["y"] a = plt.scatter( x[0:10], y[0:10], marker=m, s=size, facecolors=c, linewidth=0.1) # label=nameList[opt]) # + " first 10 points") b = plt.scatter(x[10:-10], y[10:-10], marker=m, linewidth=0.1, s=4 * size, facecolors=c) # label="points in the middle") c = plt.scatter(x[-10:-1], y[-10:-1], marker=m, linewidth=0.1, s=6 * size, facecolors=c, label=nameList[opt]) # label="last 10 points") plt.xlim([-5, 10]) plt.ylim([-0, 15]) plt.xlabel("X") plt.ylabel("Y") if [trial["result"] for trial in trialList[opt]["trials"]]: minimum = str( min([trial["result"] for trial in trialList[opt]["trials"]])) else: minimum = "NaN" # Describe the plot plt.title(title) leg = plt.legend(loc="best", fancybox=True) leg.get_frame().set_alpha(0.5) if save != "": plt.subplots_adjust(top=0.85) plt.savefig(save, dpi=600, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches="tight", pad_inches=0.1) else: plt.show()