Skip to main content
Version: Next

Getting Started with Ax

Complex optimization problems where we wish to tune multiple parameters to improve metric performance, but the inter-parameter interactions are not fully understood, are common across various fields including machine learning, robotics, materials science, and chemistry. This category of problem is known as "black-box" optimization. The complexity of black-box optimization problems further increases if evaluations are expensive to conduct, time-consuming, or noisy.

We can use Ax to efficiently conduct an experiment in which we "ask" for candidate points to evaluate, "tell" Ax the results, and repeat. We'll uses Ax's Client, a tool for managing the state of our experiment, and we'll learn how to define an optimization problem, configure an experiment, run trials, analyze results, and persist the experiment for later use using the Client.

Because Ax is a black box optimizer, we can use it to optimize any arbitrary function. In this example we will minimize the Hartmann6 function, a complicated 6-dimensional function with multiple local minima. Hartmann6 is a challenging benchmark for optimization algorithms commonly used in the global optimization literature -- it tests the algorithm's ability to identify the true global minimum, rather than mistakenly converging on a local minimum. Looking at its analytic form we can see that it would be incredibly challenging to efficiently find the global minimum either by manual trial-and-error or traditional design of experiments like grid-search or random-search.

f(x)=i=14αiexp(j=16Aij(xjPij)2) f(\mathbf{x})=-\sum_{i=1}^4 \alpha_i \exp \left(-\sum_{j=1}^6 A_{i j}\left(x_j-P_{i j}\right)^2\right)

Learning Objectives

  • Understand the basic concepts of black box optimization
  • Learn how to define an optimization problem using Ax
  • Configure and run an experiment using Ax's Client
  • Analyze the results of the optimization

Prerequisites

Step 1: Import Necessary Modules

First, ensure you have all the necessary imports:

import numpy as np
from ax.api.client import Client
from ax.api.configs import RangeParameterConfig

Step 2: Initialize the Client

Create an instance of the Client to manage the state of your experiment.

client = Client()

Step 3: Configure the Experiment

The Client instance can be configured with a series of Configs that define how the experiment will be run.

The Hartmann6 problem is usually evaluated on the hypercube xi(0,1)x_i \in (0, 1), so we will define six identical RangeParameterConfigs with these bounds.

You may specify additional features like parameter constraints to further refine the search space and parameter scaling to help navigate parameters with nonuniform effects.

# Define six float parameters x1, x2, x3, ... for the Hartmann6 function, which is typically evaluated on the unit hypercube
parameters = [
RangeParameterConfig(
name="x1", parameter_type="float", bounds=(0, 1)
),
RangeParameterConfig(
name="x2", parameter_type="float", bounds=(0, 1)
),
RangeParameterConfig(
name="x3", parameter_type="float", bounds=(0, 1)
),
RangeParameterConfig(
name="x4", parameter_type="float", bounds=(0, 1)
),
RangeParameterConfig(
name="x5", parameter_type="float", bounds=(0, 1)
),
RangeParameterConfig(
name="x6", parameter_type="float", bounds=(0, 1)
),
]

client.configure_experiment(parameters=parameters)

Step 4: Configure Optimization

Now, we must configure the objective for this optimization, which we do using Client.configure_optimization. This method expects a string objective, an expression containing either a single metric to maximize, a linear combination of metrics to maximize, or a tuple of multiple metrics to jointly maximize. These expressions are parsed using SymPy. For example:

  • "score" would direct Ax to maximize a metric named score
  • "-loss" would direct Ax to Ax to minimize a metric named loss
  • "task_0 + 0.5 * task_1" would direct Ax to maximize the sum of two task scores, downweighting task_1 by a factor of 0.5
  • "score, -flops" would direct Ax to simultaneously maximize score while minimizing flops

See these recipes for more information on configuring objectives and outcome constraints.

metric_name = "hartmann6" # this name is used during the optimization loop in Step 5
objective = f"-{metric_name}" # minimization is specified by the negative sign

client.configure_optimization(objective=objective)

Step 5: Run Trials

Here, we will configure the ask-tell loop.

We begin by defining the Hartmann6 function as written above. Remember, this is just an example problem and any Python function can be substituted here.

# Hartmann6 function
def hartmann6(x1, x2, x3, x4, x5, x6):
alpha = np.array([1.0, 1.2, 3.0, 3.2])
A = np.array([
[10, 3, 17, 3.5, 1.7, 8],
[0.05, 10, 17, 0.1, 8, 14],
[3, 3.5, 1.7, 10, 17, 8],
[17, 8, 0.05, 10, 0.1, 14]
])
P = 10**-4 * np.array([
[1312, 1696, 5569, 124, 8283, 5886],
[2329, 4135, 8307, 3736, 1004, 9991],
[2348, 1451, 3522, 2883, 3047, 6650],
[4047, 8828, 8732, 5743, 1091, 381]
])

outer = 0.0
for i in range(4):
inner = 0.0
for j, x in enumerate([x1, x2, x3, x4, x5, x6]):
inner += A[i, j] * (x - P[i, j])**2
outer += alpha[i] * np.exp(-inner)
return -outer

hartmann6(0.1, 0.45, 0.8, 0.25, 0.552, 1.0)
Output:
np.float64(-0.4878737485613134)

Optimization Loop

We will iteratively call client.get_next_trials to "ask" Ax for a parameterization to evaluate, then call hartmann6 using those parameters, and finally "tell" Ax the result using client.complete_trial.

This loop will run multiple trials to optimize the function.

for _ in range(10): # Run 10 rounds of trials
# We will request three trials at a time in this example
trials = client.get_next_trials(max_trials=3)

for trial_index, parameters in trials.items():
x1 = parameters["x1"]
x2 = parameters["x2"]
x3 = parameters["x3"]
x4 = parameters["x4"]
x5 = parameters["x5"]
x6 = parameters["x6"]

result = hartmann6(x1, x2, x3, x4, x5, x6)

# Set raw_data as a dictionary with metric names as keys and results as values
raw_data = {metric_name: result}

# Complete the trial with the result
client.complete_trial(trial_index=trial_index, raw_data=raw_data)
Output:
[INFO 08-31 05:03:23] ax.api.client: GenerationStrategy(name='Center+Sobol+MBM:fast', nodes=[CenterGenerationNode(next_node_name='Sobol'), GenerationNode(node_name='Sobol', generator_specs=[GeneratorSpec(generator_enum=Sobol, model_key_override=None)], transition_criteria=[MinTrials(transition_to='MBM'), MinTrials(transition_to='MBM')]), GenerationNode(node_name='MBM', generator_specs=[GeneratorSpec(generator_enum=BoTorch, model_key_override=None)], transition_criteria=[])]) chosen based on user input and problem structure.
[INFO 08-31 05:03:23] ax.api.client: Generated new trial 0 with parameters {'x1': 0.5, 'x2': 0.5, 'x3': 0.5, 'x4': 0.5, 'x5': 0.5, 'x6': 0.5} using GenerationNode CenterOfSearchSpace.
[INFO 08-31 05:03:23] ax.api.client: Generated new trial 1 with parameters {'x1': 0.94576, 'x2': 0.079446, 'x3': 0.004584, 'x4': 0.273981, 'x5': 0.727706, 'x6': 0.936001} using GenerationNode Sobol.
[INFO 08-31 05:03:23] ax.api.client: Generated new trial 2 with parameters {'x1': 0.326466, 'x2': 0.723634, 'x3': 0.504097, 'x4': 0.646434, 'x5': 0.151632, 'x6': 0.120948} using GenerationNode Sobol.
[INFO 08-31 05:03:23] ax.api.client: Trial 0 marked COMPLETED.
[INFO 08-31 05:03:23] ax.api.client: Trial 1 marked COMPLETED.
[INFO 08-31 05:03:23] ax.api.client: Trial 2 marked COMPLETED.
[INFO 08-31 05:03:23] ax.api.client: Generated new trial 3 with parameters {'x1': 0.121794, 'x2': 0.396652, 'x3': 0.470293, 'x4': 0.1014, 'x5': 0.499134, 'x6': 0.283438} using GenerationNode Sobol.
[INFO 08-31 05:03:23] ax.api.client: Generated new trial 4 with parameters {'x1': 0.74469, 'x2': 0.783636, 'x3': 0.969806, 'x4': 0.979163, 'x5': 0.888136, 'x6': 0.726145} using GenerationNode Sobol.
[WARNING 08-31 05:03:23] ax.api.client: 3 trials requested but only 2 could be generated.
[INFO 08-31 05:03:23] ax.api.client: Trial 3 marked COMPLETED.
[INFO 08-31 05:03:23] ax.api.client: Trial 4 marked COMPLETED.
[INFO 08-31 05:03:24] ax.api.client: Generated new trial 5 with parameters {'x1': 0.294166, 'x2': 0.984644, 'x3': 0.553037, 'x4': 0.948434, 'x5': 0.0, 'x6': 0.0} using GenerationNode MBM.
[WARNING 08-31 05:03:24] ax.api.client: 3 trials requested but only 1 could be generated.
[INFO 08-31 05:03:24] ax.api.client: Trial 5 marked COMPLETED.
[INFO 08-31 05:03:25] ax.api.client: Generated new trial 6 with parameters {'x1': 0.270782, 'x2': 0.698745, 'x3': 0.484359, 'x4': 0.572578, 'x5': 0.080029, 'x6': 0.02366} using GenerationNode MBM.
[INFO 08-31 05:03:25] ax.api.client: Generated new trial 7 with parameters {'x1': 0.415569, 'x2': 0.682369, 'x3': 0.362747, 'x4': 0.683884, 'x5': 0.114715, 'x6': 0.247062} using GenerationNode MBM.
[INFO 08-31 05:03:25] ax.api.client: Generated new trial 8 with parameters {'x1': 0.273782, 'x2': 0.781377, 'x3': 0.678484, 'x4': 0.654898, 'x5': 0.260749, 'x6': 0.066181} using GenerationNode MBM.
[INFO 08-31 05:03:25] ax.api.client: Trial 6 marked COMPLETED.
[INFO 08-31 05:03:25] ax.api.client: Trial 7 marked COMPLETED.
[INFO 08-31 05:03:25] ax.api.client: Trial 8 marked COMPLETED.
[INFO 08-31 05:03:28] ax.api.client: Generated new trial 9 with parameters {'x1': 0.293189, 'x2': 0.719609, 'x3': 0.830212, 'x4': 0.568561, 'x5': 0.450192, 'x6': 0.115911} using GenerationNode MBM.
[INFO 08-31 05:03:28] ax.api.client: Generated new trial 10 with parameters {'x1': 0.046165, 'x2': 0.858423, 'x3': 0.392347, 'x4': 0.636817, 'x5': 0.191651, 'x6': 0.107313} using GenerationNode MBM.
[INFO 08-31 05:03:28] ax.api.client: Generated new trial 11 with parameters {'x1': 0.5471, 'x2': 0.641765, 'x3': 0.76186, 'x4': 0.656843, 'x5': 0.192718, 'x6': 0.069442} using GenerationNode MBM.
[INFO 08-31 05:03:28] ax.api.client: Trial 9 marked COMPLETED.
[INFO 08-31 05:03:28] ax.api.client: Trial 10 marked COMPLETED.
[INFO 08-31 05:03:28] ax.api.client: Trial 11 marked COMPLETED.
[INFO 08-31 05:03:31] ax.api.client: Generated new trial 12 with parameters {'x1': 0.353486, 'x2': 0.749576, 'x3': 0.840743, 'x4': 0.645046, 'x5': 0.325715, 'x6': 0.038334} using GenerationNode MBM.
[INFO 08-31 05:03:31] ax.api.client: Generated new trial 13 with parameters {'x1': 0.345049, 'x2': 0.892851, 'x3': 0.388534, 'x4': 0.62988, 'x5': 0.646423, 'x6': 0.051139} using GenerationNode MBM.
[INFO 08-31 05:03:31] ax.api.client: Generated new trial 14 with parameters {'x1': 0.312649, 'x2': 0.526705, 'x3': 1.0, 'x4': 0.668187, 'x5': 0.069139, 'x6': 0.075622} using GenerationNode MBM.
[INFO 08-31 05:03:31] ax.api.client: Trial 12 marked COMPLETED.
[INFO 08-31 05:03:31] ax.api.client: Trial 13 marked COMPLETED.
[INFO 08-31 05:03:31] ax.api.client: Trial 14 marked COMPLETED.
[INFO 08-31 05:03:33] ax.api.client: Generated new trial 15 with parameters {'x1': 0.400153, 'x2': 0.88135, 'x3': 0.646338, 'x4': 0.596693, 'x5': 0.496048, 'x6': 0.0} using GenerationNode MBM.
[INFO 08-31 05:03:33] ax.api.client: Generated new trial 16 with parameters {'x1': 0.386646, 'x2': 0.829242, 'x3': 0.0, 'x4': 0.626706, 'x5': 1.0, 'x6': 0.0} using GenerationNode MBM.
[INFO 08-31 05:03:33] ax.api.client: Generated new trial 17 with parameters {'x1': 0.392669, 'x2': 0.922736, 'x3': 1.0, 'x4': 0.589299, 'x5': 0.0, 'x6': 0.027378} using GenerationNode MBM.
[INFO 08-31 05:03:33] ax.api.client: Trial 15 marked COMPLETED.
[INFO 08-31 05:03:33] ax.api.client: Trial 16 marked COMPLETED.
[INFO 08-31 05:03:33] ax.api.client: Trial 17 marked COMPLETED.
[INFO 08-31 05:03:36] ax.api.client: Generated new trial 18 with parameters {'x1': 0.415277, 'x2': 0.952391, 'x3': 1.0, 'x4': 0.634546, 'x5': 0.0, 'x6': 0.0} using GenerationNode MBM.
[INFO 08-31 05:03:36] ax.api.client: Generated new trial 19 with parameters {'x1': 0.399444, 'x2': 0.940189, 'x3': 1.0, 'x4': 0.553463, 'x5': 0.913724, 'x6': 0.0} using GenerationNode MBM.
[INFO 08-31 05:03:36] ax.api.client: Generated new trial 20 with parameters {'x1': 0.395914, 'x2': 0.935037, 'x3': 0.389082, 'x4': 0.530887, 'x5': 0.0, 'x6': 0.0} using GenerationNode MBM.
[INFO 08-31 05:03:36] ax.api.client: Trial 18 marked COMPLETED.
[INFO 08-31 05:03:36] ax.api.client: Trial 19 marked COMPLETED.
[INFO 08-31 05:03:36] ax.api.client: Trial 20 marked COMPLETED.
[INFO 08-31 05:03:39] ax.api.client: Generated new trial 21 with parameters {'x1': 0.400568, 'x2': 0.868164, 'x3': 1.0, 'x4': 0.56713, 'x5': 0.0, 'x6': 0.0} using GenerationNode MBM.
[INFO 08-31 05:03:39] ax.api.client: Generated new trial 22 with parameters {'x1': 0.424639, 'x2': 0.911234, 'x3': 1.0, 'x4': 0.549122, 'x5': 0.049041, 'x6': 0.097141} using GenerationNode MBM.
[INFO 08-31 05:03:39] ax.api.client: Generated new trial 23 with parameters {'x1': 0.390579, 'x2': 1.0, 'x3': 1.0, 'x4': 0.489928, 'x5': 0.0, 'x6': 0.233799} using GenerationNode MBM.
[INFO 08-31 05:03:39] ax.api.client: Trial 21 marked COMPLETED.
[INFO 08-31 05:03:39] ax.api.client: Trial 22 marked COMPLETED.
[INFO 08-31 05:03:39] ax.api.client: Trial 23 marked COMPLETED.
[INFO 08-31 05:03:43] ax.api.client: Generated new trial 24 with parameters {'x1': 0.448095, 'x2': 0.757694, 'x3': 1.0, 'x4': 0.232369, 'x5': 0.0, 'x6': 0.024437} using GenerationNode MBM.
[INFO 08-31 05:03:43] ax.api.client: Generated new trial 25 with parameters {'x1': 0.667784, 'x2': 0.847231, 'x3': 1.0, 'x4': 0.206565, 'x5': 0.0, 'x6': 0.0} using GenerationNode MBM.
[INFO 08-31 05:03:43] ax.api.client: Generated new trial 26 with parameters {'x1': 0.412265, 'x2': 0.878027, 'x3': 1.0, 'x4': 0.570001, 'x5': 0.02182, 'x6': 0.0349} using GenerationNode MBM.
[INFO 08-31 05:03:43] ax.api.client: Trial 24 marked COMPLETED.
[INFO 08-31 05:03:43] ax.api.client: Trial 25 marked COMPLETED.
[INFO 08-31 05:03:43] ax.api.client: Trial 26 marked COMPLETED.

Step 6: Analyze Results

After running trials, you can analyze the results. Most commonly this means extracting the parameterization from the best performing trial you conducted.

Hartmann6 has a known global minimum of f(x)=3.322f(x*) = -3.322 at x=(0.201,0.150,0.477,0.273,0.312,0.657)x* = (0.201, 0.150, 0.477, 0.273, 0.312, 0.657). Ax is able to identify a point very near to this true optimum using just 30 evaluations. This is possible due to the sample-efficiency of Bayesian optimization, the optimization method we use under the hood in Ax.

best_parameters, prediction, index, name = client.get_best_parameterization()
print("Best Parameters:", best_parameters)
print("Prediction (mean, variance):", prediction)
Output:
Best Parameters: {'x1': 0.4005684378178244, 'x2': 0.8681638319276835, 'x3': 1.0, 'x4': 0.567130056413669, 'x5': 0.0, 'x6': 0.0}
Prediction (mean, variance): {'hartmann6': (np.float64(-3.1263589059829635), np.float64(0.0021860844504721405))}

Step 7: Compute Analyses

Ax can also produce a number of analyses to help interpret the results of the experiment via client.compute_analyses. Users can manually select which analyses to run, or can allow Ax to select which would be most relevant. In this case Ax selects the following:

  • Arm Effects Plots show the metric value for each arm on the experiment. Ax produces one plot using values from its internal surrogate model (this can be helpful for seeing the true effect of an arm when evaluations are noisy) and another using the raw metric values as observed.
  • Summary lists all trials generated along with their parameterizations, observations, and miscellaneous metadata
  • Sensitivity Analysis Plot shows which parameters have the largest affect on the objective using Sobol Indicies
  • Slice Plot shows how the model predicts a single parameter effects the objective along with a confidence interval
  • Contour Plot shows how the model predicts a pair of parameters effects the objective as a 2D surface
  • Cross Validation helps to visualize how well the surrogate model is able to predict out of sample points
# display=True instructs Ax to sort then render the resulting analyses
cards = client.compute_analyses(display=True)

Modeled Arm Effects on hartmann6

Modeled effects on hartmann6. This plot visualizes predictions of the true metric changes for each arm based on Ax's model. This is the expected delta you would expect if you (re-)ran that arm. This plot helps in anticipating the outcomes and performance of arms based on the model's predictions. Note, flat predictions across arms indicate that the model predicts that there is no effect, meaning if you were to re-run the experiment, the delta you would see would be small and fall within the confidence interval indicated in the plot.

loading...

Observed Arm Effects on hartmann6

Observed effects on hartmann6. This plot visualizes the effects from previously-run arms on a specific metric, providing insights into their performance. This plot allows one to compare and contrast the effectiveness of different arms, highlighting which configurations have yielded the most favorable outcomes.

loading...

Summary for Experiment

High-level summary of the Trial-s in this Experiment

trial_indexarm_nametrial_statusgeneration_nodehartmann6x1x2x3x4x5x6
000_0COMPLETEDCenterOfSearchSpace-0.5053150.50.50.50.50.50.5
111_0COMPLETEDSobol-0.0139870.945760.0794460.0045840.2739810.7277060.936001
222_0COMPLETEDSobol-2.036670.3264660.7236340.5040970.6464340.1516320.120948
333_0COMPLETEDSobol-0.556420.1217940.3966520.4702930.10140.4991340.283438
444_0COMPLETEDSobol-0.0006320.744690.7836360.9698060.9791630.8881360.726145
555_0COMPLETEDMBM-0.5748010.2941660.9846440.5530370.94843400
666_0COMPLETEDMBM-1.790340.2707820.6987450.4843590.5725780.0800290.02366
777_0COMPLETEDMBM-1.131390.4155690.6823690.3627470.6838840.1147150.247062
888_0COMPLETEDMBM-2.044380.2737820.7813770.6784840.6548980.2607490.066181
999_0COMPLETEDMBM-1.922040.2931890.7196090.8302120.5685610.4501920.115911
101010_0COMPLETEDMBM-0.3299610.0461650.8584230.3923470.6368170.1916510.107313
111111_0COMPLETEDMBM-1.319790.54710.6417650.761860.6568430.1927180.069442
121212_0COMPLETEDMBM-2.520970.3534860.7495760.8407430.6450460.3257150.038334
131313_0COMPLETEDMBM-2.798230.3450490.8928510.3885340.629880.6464230.051139
141414_0COMPLETEDMBM-0.9062280.3126490.52670510.6681870.0691390.075622
151515_0COMPLETEDMBM-3.068160.4001530.881350.6463380.5966930.4960480
161616_0COMPLETEDMBM-2.636320.3866460.82924200.62670610
171717_0COMPLETEDMBM-3.133830.3926690.92273610.58929900.027378
181818_0COMPLETEDMBM-2.897890.4152770.95239110.63454600
191919_0COMPLETEDMBM-2.846660.3994440.94018910.5534630.9137240
202020_0COMPLETEDMBM-2.969570.3959140.9350370.3890820.53088700
212121_0COMPLETEDMBM-3.122130.4005680.86816410.5671300
222222_0COMPLETEDMBM-2.987250.4246390.91123410.5491220.0490410.097141
232323_0COMPLETEDMBM-1.556920.390579110.48992800.233799
242424_0COMPLETEDMBM-0.8481620.4480950.75769410.23236900.024437
252525_0COMPLETEDMBM-0.2478870.6677840.84723110.20656500
262626_0COMPLETEDMBM-3.191280.4122650.87802710.5700010.021820.0349

Sensitivity Analysis for hartmann6

Understand how each parameter affects hartmann6 according to a second-order sensitivity analysis.

loading...

x1 vs. hartmann6

The slice plot provides a one-dimensional view of predicted outcomes for hartmann6 as a function of a single parameter, while keeping all other parameters fixed at their status_quo value (or mean value if status_quo is unavailable). This visualization helps in understanding the sensitivity and impact of changes in the selected parameter on the predicted metric outcomes.

loading...

x2, x6 vs. hartmann6

The contour plot visualizes the predicted outcomes for hartmann6 across a two-dimensional parameter space, with other parameters held fixed at their status_quo value (or mean value if status_quo is unavailable). This plot helps in identifying regions of optimal performance and understanding how changes in the selected parameters influence the predicted outcomes. Contour lines represent levels of constant predicted values, providing insights into the gradient and potential optima within the parameter space.

loading...

x2, x3 vs. hartmann6

The contour plot visualizes the predicted outcomes for hartmann6 across a two-dimensional parameter space, with other parameters held fixed at their status_quo value (or mean value if status_quo is unavailable). This plot helps in identifying regions of optimal performance and understanding how changes in the selected parameters influence the predicted outcomes. Contour lines represent levels of constant predicted values, providing insights into the gradient and potential optima within the parameter space.

loading...

Cross Validation for hartmann6

The cross-validation plot displays the model fit for each metric in the experiment. It employs a leave-one-out approach, where the model is trained on all data except one sample, which is used for validation. The plot shows the predicted outcome for the validation set on the y-axis against its actual value on the x-axis. Points that align closely with the dotted diagonal line indicate a strong model fit, signifying accurate predictions. Additionally, the plot includes 95% confidence intervals that provide insight into the noise in observations and the uncertainty in model predictions. A horizontal, flat line of predictions indicates that the model has not picked up on sufficient signal in the data, and instead is just predicting the mean.

loading...

Conclusion

This tutorial demonstrates how to use Ax's Client for ask-tell optimization of Python functions using the Hartmann6 function as an example. You can adjust the function and parameters to suit your specific optimization problem.