def draw_basic_plot(mark_incumbent=True, show_objective=False): fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[1.0, 2.0, 3.0], custom_x=x, ax=ax) if show_objective: boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=mark_incumbent, highlight_datapoint=None, highlight_label=None, ax=ax) if mark_incumbent: boplot.highlight_output(y=np.array([ymin]), label=['$c_{inc}$'], lloc='left', ax=ax, disable_ticks=True) boplot.annotate_y_edge(label='$c_{inc}$', xy=(x[ymin_arg], ymin), ax=ax, align='left', yoffset=0.8) return fig, ax
def visualize_ei(initial_design, init=None): """ Visualize one-step of EI. :param initial_design: Method for initializing the GP, choice between 'uniform', 'random', and 'presentation' :param init: Number of datapoints to initialize GP with. :return: None """ # 1. Plot GP fit on initial dataset # 2. Mark current incumbent # 3. Mark Zone of Probable Improvement # 4. Mark Hypothetical Real cost of a random configuration # 5. Display I(lambda) # 6. Display Vertical Normal Distribution # boplot.set_rcparams(**{'legend.loc': 'lower left'}) logging.debug("Visualizing EI with initial design {} and init {}".format(initial_design, init)) # Initialize dummy dataset x, y = initialize_dataset(initial_design=initial_design, init=init) ymin_arg = np.argmin(y) ymin = y[ymin_arg] logging.debug("Initialized dataset with:\nsamples {0}\nObservations {1}".format(x, y)) # Fit GP to the currently available dataset gp = GPR(kernel=Matern()) logging.debug("Fitting GP to\nx: {}\ny:{}".format(x, y)) gp.fit(x, y) # fit the model mu_star_t_xy = get_mu_star(gp) logging.info("Mu-star at time t: {}".format(mu_star_t_xy)) # noinspection PyStringFormat logging.debug("Model fit to dataset.\nOriginal Inputs: {0}\nOriginal Observations: {1}\n" "Predicted Means: {2}\nPredicted STDs: {3}".format(x, y, *(gp.predict(x, return_std=True)))) # 1. Plot GP fit on initial dataset # -------------Plotting code ----------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[2.0], custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, highlight_datapoint=None, highlight_label=None, ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig("ei_1.pdf") else: plt.show() # ------------------------------------------- # 2. Mark current incumbent # -------------Plotting code ----------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[2.0], custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig("ei_2.pdf") else: plt.show() # ------------------------------------------- # 3. Mark Zone of Probable Improvement # -------------Plotting code ----------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[2.0], custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax) boplot.darken_graph(y=ymin, ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') ax.legend().remove() plt.tight_layout() if TOGGLE_PRINT: plt.savefig("ei_3.pdf") else: plt.show() # ------------------------------------------- # 4. Forget the underlying objective function # ------------------------------------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[2.0], custom_x=x, ax=ax) # boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax) boplot.darken_graph(y=ymin, ax=ax) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig("ei_4.pdf") else: plt.show() # ------------------------------------------- # 5. Mark Hypothetical Real cost of a random configuration # ------------------------------------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[2.0], custom_x=x, ax=ax) # boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax) boplot.darken_graph(y=ymin, ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') candidate = 11.0 cost = -3.5 boplot.highlight_configuration(x=np.array([candidate]), label=r'$\lambda$', lloc='bottom', ax=ax) boplot.highlight_output(y=np.array([cost]), label='', lloc='left', ax=ax) boplot.annotate_y_edge(label=r'$c(\lambda)$', xy=(candidate, cost), align='left', ax=ax) ax.legend().remove() plt.tight_layout() if TOGGLE_PRINT: plt.savefig("ei_5.pdf") else: plt.show() # ------------------------------------------- # 6. Display I(lambda) # ------------------------------------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[2.0], custom_x=x, ax=ax) # boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax) boplot.darken_graph(y=ymin, ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') boplot.highlight_configuration(x=np.array([candidate]), label=r'$\lambda$', lloc='bottom', ax=ax) boplot.highlight_output(y=np.array([cost]), label='', lloc='left', ax=ax) boplot.annotate_y_edge(label=r'$c(\lambda)$', xy=(candidate, cost), align='left', ax=ax) xmid = (x[ymin_arg][0] + candidate) / 2. ax.annotate(s='', xy=(xmid, cost), xytext=(xmid, ymin), arrowprops={'arrowstyle': '<|-|>',}) textx = xmid + (ax.get_xlim()[1] - ax.get_xlim()[0]) / 40 ax.text(textx, (ymin + cost) / 2, r'$I^{(t)}(\lambda)$', weight='heavy') ax.legend().remove() plt.tight_layout() if TOGGLE_PRINT: plt.savefig("ei_6.pdf") else: plt.show() # ------------------------------------------- # 7. Display Vertical Normal Distribution # ------------------------------------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[2.0], custom_x=x, ax=ax) # boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax) boplot.darken_graph(y=ymin, ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') boplot.highlight_configuration(x=np.array([candidate]), label=r'$\lambda$', lloc='bottom', ax=ax) boplot.highlight_output(y=np.array([cost]), label='', lloc='left', ax=ax) boplot.annotate_y_edge(label=r'$c(\lambda)$', xy=(candidate, cost), align='left', ax=ax) xmid = (x[ymin_arg][0] + candidate) / 2. ax.annotate(s='', xy=(xmid, cost), xytext=(xmid, ymin), arrowprops={'arrowstyle': '<|-|>',}) textx = xmid + (ax.get_xlim()[1] - ax.get_xlim()[0]) / 40 ax.text(textx, (ymin + cost) / 2, r'$I^{(t)}(\lambda)$', weight='heavy') vcurve_x, vcurve_y, mu = boplot.draw_vertical_normal( gp=gp, incumbenty=ymin, ax=ax, xtest=candidate, xscale=2.0, yscale=1.0 ) ann_x = candidate + 0.3 * (np.max(vcurve_x) - candidate) / 2 ann_y = ymin - (mu - ymin) / 2 arrow_x = ann_x + 0.5 arrow_y = ann_y - 3.0 # label = "{:.2f}".format(candidate) label = '\lambda' ax.annotate( s=r'$PI^{(t)}(%s)$' % label, xy=(ann_x, ann_y), xytext=(arrow_x, arrow_y), arrowprops={'arrowstyle': 'fancy'}, weight='heavy', color='darkgreen', zorder=15 ) ax.legend().remove() plt.tight_layout() if TOGGLE_PRINT: plt.savefig("ei_7.pdf") else: plt.show()
def visualize_lcb(initial_design, init=None): """ Visualize one-step of LCB. :param initial_design: Method for initializing the GP, choice between 'uniform', 'random', and 'presentation' :param init: Number of datapoints to initialize GP with. :return: None """ # 1. Plot 3 different confidence envelopes # 2. Plot only LCB for one confidence envelope # 3. Mark next sample boplot.set_rcparams(**{'legend.loc': 'lower right'}) logging.debug("Visualizing PI with initial design {} and init {}".format( initial_design, init)) # Initialize dummy dataset x, y = initialize_dataset(initial_design=initial_design, init=init) logging.debug( "Initialized dataset with:\nsamples {0}\nObservations {1}".format( x, y)) # Fit GP to the currently available dataset gp = GPR(kernel=Matern()) logging.debug("Fitting GP to\nx: {}\ny:{}".format(x, y)) gp.fit(x, y) # fit the model # noinspection PyStringFormat logging.debug( "Model fit to dataset.\nOriginal Inputs: {0}\nOriginal Observations: {1}\n" "Predicted Means: {2}\nPredicted STDs: {3}".format( x, y, *(gp.predict(x, return_std=True)))) # 1. Plot 3 different confidence envelopes # -------------Plotting code ----------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[1.0, 2.0, 3.0], type='both', custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, highlight_datapoint=None, highlight_label=None, ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig("lcb_1.pdf") else: plt.show() # ------------------------------------------- # 2. Plot only LCB for one confidence envelope # -------------Plotting code ----------------- boplot.set_rcparams(**{'legend.loc': 'upper left'}) kappa = 3.0 fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[kappa], type='lower', custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig("lcb_2.pdf") else: plt.show() # ------------------------------------------- # 3. Show LCB in parallel # -------------Plotting code ----------------- if TOGGLE_PRINT: fig, (ax1, ax2) = plt.subplots(2, 1, squeeze=True, figsize=(18, 9)) else: fig, (ax1, ax2) = plt.subplots(2, 1, squeeze=True) ax1.set_xlim(bounds["x"]) ax1.set_ylim(bounds["gp_y"]) ax1.grid() boplot.plot_gp(model=gp, confidence_intervals=[kappa], type='lower', custom_x=x, ax=ax1) boplot.plot_objective_function(ax=ax1) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax1) lcb_max = get_lcb_maximum(gp, kappa) logging.info("LCB Maximum at:{}".format(lcb_max)) boplot.highlight_configuration(x=lcb_max[0], label=None, lloc='bottom', ax=ax1) ax1.set_xlabel(labels['xlabel']) ax1.set_ylabel(labels['gp_ylabel']) ax1.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') ax2.set_xlim(bounds["x"]) ax2.set_ylim(bounds["acq_y"]) ax2.grid() ax2.set_xlabel(labels['xlabel']) ax2.set_ylabel(labels['acq_ylabel']) ax2.set_title(r"Visualization of $LCB$", loc='left') boplot.highlight_configuration(x=lcb_max[0], label=None, lloc='bottom', ax=ax2) boplot.plot_acquisition_function(acquisition_functions['LCB'], 0.0, gp, kappa, ax=ax2) plt.tight_layout() if TOGGLE_PRINT: plt.savefig("lcb_3.pdf") else: plt.show() # ------------------------------------------- # 4. Mark next sample # -------------Plotting code ----------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[3.0], type='lower', custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax) lcb_max = get_lcb_maximum(gp, 3.0) logging.info("LCB Maximum at:{}".format(lcb_max)) boplot.highlight_configuration(x=lcb_max[0], label=None, lloc='bottom', ax=ax) boplot.highlight_output(y=lcb_max[1], label='', lloc='left', ax=ax) boplot.annotate_y_edge(label=r'${\hat{c}}^{(t)}(%.2f)$' % lcb_max[0], xy=lcb_max, align='left', ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') ax.legend().remove() plt.tight_layout() if TOGGLE_PRINT: plt.savefig("lcb_4.pdf") else: plt.show()
def visualize_look_ahead(initial_design, init=None): """ Visualize one-step of look-ahead. :param initial_design: Method for initializing the GP, choice between 'uniform', 'random', and 'presentation' :param init: Number of datapoints to initialize GP with. :return: None """ # boplot.set_rcparams(**{'legend.loc': 'lower left'}) logging.debug( "Visualizing Look-Ahead with initial design {} and init {}".format( initial_design, init)) # Initialize dummy dataset x, y = initialize_dataset(initial_design=initial_design, init=init) logging.debug( "Initialized dataset with:\nsamples {0}\nObservations {1}".format( x, y)) # Fit GP to the currently available dataset gp = GPR(kernel=Matern()) logging.debug("Fitting GP to\nx: {}\ny:{}".format(x, y)) gp.fit(x, y) # fit the model mu_star_t_xy = get_mu_star(gp) logging.info("Mu-star at time t: {}".format(mu_star_t_xy)) # noinspection PyStringFormat logging.debug( "Model fit to dataset.\nOriginal Inputs: {0}\nOriginal Observations: {1}\n" "Predicted Means: {2}\nPredicted STDs: {3}".format( x, y, *(gp.predict(x, return_std=True)))) # Assume next evaluation location x_ = np.array([[5.0]]) print(x_) y_ = f(x_[0]) # Update dataset with new observation X2_ = np.append(x, x_, axis=0) Y2_ = y + [y_] logging.info("x: {}, y: {}".format(x_, y_)) # Fit GP to the updated dataset gp2 = GPR(kernel=Matern()) logging.debug("Fitting GP to\nx: {}\ny:{}".format(X2_, Y2_)) gp2.fit(X2_, Y2_) # fit the model mu_star_t1_xy = get_mu_star(gp2) logging.info("Mu-star at time t+1: {}".format(mu_star_t1_xy)) # -------------------------Plotting madness begins--------------------------- def draw_basic_figure(tgp=gp, tx=x, tX_=x, tY_=y, title='', highlight_datapoint=None, highlight_label="", ax=None): if ax is None: fig, ax = plt.subplots(1, 1, squeeze=True) plt.subplots_adjust(0.05, 0.15, 0.95, 0.85) figflag = True else: figflag = False ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) if title: ax.set_title(title, loc='left') ax.grid() boplot.plot_objective_function(ax=ax) boplot.plot_gp(model=tgp, confidence_intervals=[1.0, 2.0, 3.0], ax=ax, custom_x=tx) if highlight_datapoint: boplot.mark_observations(X_=tX_, Y_=tY_, mark_incumbent=False, ax=ax, highlight_datapoint=highlight_datapoint, highlight_label=highlight_label) else: boplot.mark_observations(X_=tX_, Y_=tY_, mark_incumbent=False, ax=ax) if figflag: return fig, ax else: return ax def perform_finishing_tasks(ax, filename="", remove_legend=True): ax.legend().set_zorder(zorders['legend']) ax.set_xlabel(labels['xlabel']) if remove_legend: ax.legend().remove() # plt.tight_layout() if TOGGLE_PRINT: # plt.savefig(f"{OUTPUT_DIR}/{filename}", bbox_inches='tight') plt.savefig(f"{OUTPUT_DIR}/{filename}") else: plt.show() # --------------------------------------- # Draw look ahead 1. labels['gp_mean'] = r'Mean: $\mu^{(t)}(\cdot)$' fig, ax = draw_basic_figure(title="") perform_finishing_tasks(ax=ax, filename="look_ahead_1.pdf", remove_legend=False) # --------------------------------------- # Draw look ahead 2 fig, ax = draw_basic_figure(title="") logging.debug("Placing vertical on configuration: {}".format(x_)) # boplot.highlight_configuration(x=x_, label='', lloc='bottom', ax=ax, ha='center') boplot.highlight_configuration(x=x_, label='', lloc='bottom', ax=ax, disable_ticks=True) boplot.annotate_x_edge(label=r'$\lambda$', xy=(x_, y_), align='bottom', ax=ax) perform_finishing_tasks(ax=ax, filename="look_ahead_2.pdf", remove_legend=True) # --------------------------------------- # Draw look ahead 3 fig, ax = draw_basic_figure(title="") boplot.highlight_configuration(x=x_, label='', lloc='bottom', ax=ax, ha='right') boplot.annotate_x_edge(label=r'$\lambda$', xy=(x_, y_), align='bottom', ax=ax) boplot.highlight_output(y_, label='', lloc='right', ax=ax, fontsize=28) boplot.annotate_y_edge(label=r'$c(\lambda)$', xy=(x_, y_), align='right', ax=ax) ax.scatter(x_, y_, color=colors['highlighted_observations'], marker='X', label=r"Hypothetical Observation $<\lambda, c(\lambda)>$", zorder=zorders['annotations_normal']) perform_finishing_tasks(ax=ax, filename="look_ahead_3.pdf", remove_legend=True) # --------------------------------------- # Draw look ahead 4. labels['gp_mean'] = r'Mean: $\mu^{(t+1)}(\cdot)|_\lambda$' fig, ax = draw_basic_figure( tgp=gp2, tx=x, tX_=X2_, tY_=Y2_, title='', highlight_datapoint=np.where(np.isclose(X2_, x_))[0], highlight_label=r"Hypothetical Observation $<\lambda, c(\lambda)>$") perform_finishing_tasks(ax=ax, filename="look_ahead_4.pdf", remove_legend=False) # --------------------------------------- # Vertical comparison of look-ahead at any given x def draw_vertical_comparison(imaginary_lambda, ax1, ax2): tx_ = np.array([[imaginary_lambda]]) ty_ = f(tx_[0]) # Update dataset with new observation tX_ = np.append(x, tx_, axis=0) tY_ = y + [ty_] logging.info("x: {}, y: {}".format(tx_, ty_)) # Fit GP to the updated dataset tgp = GPR(kernel=Matern()) logging.debug("Fitting GP to\nx: {}\ny:{}".format(tX_, tY_)) tgp.fit(tX_, tY_) # fit the model tmu_star_t1_xy = get_mu_star(tgp) # Draw the left hand figure using the old gp on ax1 draw_basic_figure(tgp=gp, title=r"$\hat{c}^{(t)}$", ax=ax1) logging.debug("Placing vertical on configuration: {}".format(tx_)) ax1.scatter(tx_, ty_, color=colors['highlighted_observations'], marker='X', label=r"Hypothetical Observation $<\lambda, c(\lambda)>$", zorder=zorders["annotations_normal"]) ax1.legend().remove() # Draw the right hand figure using the hypothetical gp tgp on ax2 draw_basic_figure( tgp=tgp, tx=tX_, tX_=tX_, tY_=tY_, title=r"$\hat{c}^{(t+1)}|_\lambda$", highlight_datapoint=np.where(np.isclose(tX_, tx_))[0], highlight_label=r"Hypothetical Observation $<\lambda, c(\lambda)>$", ax=ax2) def finishing_touches_parallel(ax1, ax2, filename=""): ax1.set_xlabel(labels['xlabel']) ax2.set_xlabel(labels['xlabel']) plt.tight_layout() if TOGGLE_PRINT: plt.savefig(f"{OUTPUT_DIR}/{filename}") else: plt.show() # --------------------------------------- # Draw look ahead 5 fig, (ax1, ax2) = plt.subplots(1, 2, squeeze=True, figsize=(22, 9)) draw_vertical_comparison(imaginary_lambda=5.0, ax1=ax1, ax2=ax2) finishing_touches_parallel(ax1=ax1, ax2=ax2, filename="look_ahead_5.pdf") # --------------------------------------- # Draw look ahead 6 fig, (ax1, ax2) = plt.subplots(1, 2, squeeze=True, figsize=(22, 9)) draw_vertical_comparison(imaginary_lambda=5.5, ax1=ax1, ax2=ax2) finishing_touches_parallel(ax1=ax1, ax2=ax2, filename="look_ahead_6.pdf") # --------------------------------------- # Draw look ahead 5 fig, (ax1, ax2) = plt.subplots(1, 2, squeeze=True, figsize=(22, 9)) draw_vertical_comparison(imaginary_lambda=3.5, ax1=ax1, ax2=ax2) finishing_touches_parallel(ax1=ax1, ax2=ax2, filename="look_ahead_7.pdf") # --------------------------------------- # Draw KG 1 labels['gp_mean'] = r'Mean: $\mu^{(t)}(\cdot)$' fig, ax = draw_basic_figure(title="") perform_finishing_tasks(ax=ax, filename="kg_1.pdf", remove_legend=False) # --------------------------------------- # Draw kg 2 fig, ax = draw_basic_figure(title="") boplot.highlight_configuration(mu_star_t_xy[0], lloc='bottom', ax=ax, disable_ticks=True) boplot.annotate_x_edge(label="%.2f" % mu_star_t_xy[0], xy=mu_star_t_xy, ax=ax, align='bottom', offset_param=1.5) boplot.highlight_output(mu_star_t_xy[1], label='', lloc='right', ax=ax, fontsize=30, disable_ticks=True) boplot.annotate_y_edge(label=r'${(\mu^*)}^{(t)}$', xy=mu_star_t_xy, align='right', ax=ax, yoffset=1.5) perform_finishing_tasks(ax=ax, filename="kg_2.pdf", remove_legend=True) # --------------------------------------- # Draw kg 3 fig, ax = draw_basic_figure( tgp=gp2, tx=x, tX_=X2_, tY_=Y2_, title='', highlight_datapoint=np.where(np.isclose(X2_, x_))[0], highlight_label=r"Hypothetical Observation $<\lambda, c(\lambda)>$") perform_finishing_tasks(ax=ax, filename="kg_3.pdf", remove_legend=True) # --------------------------------------- # Draw kg 4 fig, ax = draw_basic_figure( tgp=gp2, tx=x, tX_=X2_, tY_=Y2_, title='', highlight_datapoint=np.where(np.isclose(X2_, x_))[0], highlight_label=r"Hypothetical Observation $<\lambda, c(\lambda)>$") boplot.highlight_configuration(mu_star_t1_xy[0], lloc='bottom', ax=ax, disable_ticks=True) boplot.annotate_x_edge(label="%.2f" % mu_star_t1_xy[0], xy=mu_star_t1_xy, ax=ax, align='bottom', offset_param=1.5) boplot.highlight_output(mu_star_t1_xy[1], label='', lloc='right', ax=ax, fontsize=28) boplot.annotate_y_edge(label=r'${(\mu^*)}^{(t+1)}|_\lambda$', xy=mu_star_t1_xy, align='right', ax=ax, yoffset=1.5) perform_finishing_tasks(ax=ax, filename="kg_4.pdf", remove_legend=True) # --------------------------------------- # Draw kg 5 fig, (ax1, ax2) = plt.subplots(1, 2, squeeze=True, figsize=(22, 9)) draw_vertical_comparison(imaginary_lambda=x_.squeeze(), ax1=ax1, ax2=ax2) boplot.highlight_output(mu_star_t_xy[1], label='', lloc='right', ax=ax1, fontsize=30) boplot.annotate_y_edge(label='${(\mu^*)}^{(t)}$', xy=mu_star_t_xy, align='right', ax=ax1, yoffset=1.5) boplot.highlight_output(mu_star_t1_xy[1], label='', lloc='right', ax=ax2, fontsize=28) boplot.annotate_y_edge(label='${(\mu^*)}^{(t+1)}|_\lambda$', xy=mu_star_t1_xy, align='left', ax=ax2, yoffset=1.5) finishing_touches_parallel(ax1=ax1, ax2=ax2, filename="kg_5.pdf") return
def visualize_ts(initial_design, init=None): """ Visualize one-step of TS. :param initial_design: Method for initializing the GP, choice between 'uniform', 'random', and 'presentation' :param init: Number of datapoints to initialize GP with. :return: None """ # 1. Plot GP fit on initial dataset # 2. Plot one sample # 3. Mark minimum of sample boplot.set_rcparams(**{'legend.loc': 'upper left'}) logging.debug("Visualizing EI with initial design {} and init {}".format( initial_design, init)) # Initialize dummy dataset x, y = initialize_dataset(initial_design=initial_design, init=init) ymin_arg = np.argmin(y) ymin = y[ymin_arg] logging.debug( "Initialized dataset with:\nsamples {0}\nObservations {1}".format( x, y)) # Fit GP to the currently available dataset gp = GPR(kernel=Matern()) logging.debug("Fitting GP to\nx: {}\ny:{}".format(x, y)) gp.fit(x, y) # fit the model # noinspection PyStringFormat logging.debug( "Model fit to dataset.\nOriginal Inputs: {0}\nOriginal Observations: {1}\n" "Predicted Means: {2}\nPredicted STDs: {3}".format( x, y, *(gp.predict(x, return_std=True)))) # 1. Plot GP fit on initial dataset # -------------Plotting code ----------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[1.0, 2.0, 3.0], custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, highlight_datapoint=None, highlight_label=None, ax=ax) ax.legend().set_zorder(zorders['legend']) ax.set_xlabel(labels['xlabel']) plt.tight_layout() if TOGGLE_PRINT: plt.savefig(f"{OUTPUT_DIR}/ts_1.pdf") else: plt.show() # ------------------------------------------- # 2. Plot one sample # -------------Plotting code ----------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[1.0, 2.0, 3.0], custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, highlight_datapoint=None, highlight_label=None, ax=ax) # Sample from the GP nsamples = 1 seed2 = 2 seed3 = 1375 X_ = boplot.get_plot_domain(precision=None) mu = gp.sample_y(X=X_, n_samples=nsamples, random_state=seed3) boplot.plot_gp_samples(mu=mu, nsamples=nsamples, precision=None, custom_x=X_, show_min=False, ax=ax, seed=seed2) ax.legend().set_zorder(zorders['legend']) ax.set_xlabel(labels['xlabel']) plt.tight_layout() if TOGGLE_PRINT: plt.savefig(f"{OUTPUT_DIR}/ts_2.pdf") else: plt.show() # ------------------------------------------- # 3. Mark minimum of sample # -------------Plotting code ----------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() # boplot.plot_gp(model=gp, confidence_intervals=[2.0], custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, highlight_datapoint=None, highlight_label=None, ax=ax) # Sample from the GP nsamples = 1 X_ = boplot.get_plot_domain(precision=None) mu = gp.sample_y(X=X_, n_samples=nsamples, random_state=seed3) colors['highlighted_observations'] = 'red' # Special for Thompson Sampling boplot.plot_gp_samples(mu=mu, nsamples=nsamples, precision=None, custom_x=X_, show_min=True, ax=ax, seed=seed2) min_idx = np.argmin(mu, axis=0) candidate = X_[min_idx] cost = mu[min_idx] boplot.highlight_configuration(x=np.array([candidate]), label='', lloc='bottom', disable_ticks=True, ax=ax) boplot.annotate_x_edge(label=r'$\lambda^{(t)}$', xy=(candidate, cost), align='top', ax=ax) boplot.highlight_output(y=np.array([cost]), label='', lloc='left', disable_ticks=True, ax=ax) boplot.annotate_y_edge(label=r'$g(\lambda^{(t)})$', xy=(candidate, cost), align='left', ax=ax) ax.legend().set_zorder(zorders['legend']) ax.set_xlabel(labels['xlabel']) plt.tight_layout() if TOGGLE_PRINT: plt.savefig(f"{OUTPUT_DIR}/ts_3.pdf") else: plt.show()