Experiment¶
In NEXTorch, We use Experiment
objects to store the data and the setting of the optimization loop. We integrate
most acquisition functions and GP models from the upstream BoTorch with Experiment
objects. Experiment objects
are the core of NEXTorch where data are preprocessed, GP models are trained, used for prediction, and the optima are located.
There are several variants of the Experiment class depending on the application. At the higher level, users interact with the
Experiment
class, WeightedMOOExperiment
class, and EHVIMOOExperiment
for sequential single-objective
optimization, MOO with the weighted sum method, and MOO with EHVI, respectively. Besides, two additional classes, COMSOLExperiment
and COMSOLMOOExperiment
are provided to perform automatic optimization by integrating the NEXTorch with COMSOL Multiphysics®.
Example¶
Experiment
class¶
Initialize a Experiment
object with input_data
to specify the inputs of parameters and response,
the range and name of them, and their conditions.
from nextorch import bo
X_names = ["param_1", "param_2", "param_3"]
X_ranges = [[1.0, 25.0], [1.0, 25.0], [1.0, 25.0]]
X_units = ["mm", "mm", "mm"]
X_real = np.array([[1.0, 1.0, 1.0], [5.0, 5.0, 5.0], [25.0, 25.0, 25.0]])
Y_real = np.array([[0.5], [0.2], [0.7]])
exp = bo.Experiment("Experiment")
exp.input_data(X_real=X_real, Y_real=Y_real)
Additionally, we can specify more conditions:
exp.input_data(X_real=X_real, Y_real=Y_real, X_ranges=X_ranges, X_names=X_names, standardized=True)
Note
X_real
and Y_real
are the required inputs for Experiment
object.
Set the optimization conditions using set_optim_specs
def simple_1d(X):
"""
1D function y = (6x-2)^2 * sin(12x-4)
"""
try:
X.shape[1]
except:
X = np.array(X)
if len(X.shape)<2:
X = np.array([X])
y = np.array([],dtype=float)
for i in range(X.shape[0]):
ynew = (X[i]*6-2)**2*np.sin((X[i]*6-2)*2)
y = np.append(y, ynew)
y = y.reshape(X.shape)
return y
objective_func = simple_1d
exp.set_optim_specs(objective_func=objective_func)
Note
set_optim_specs
by default maximizing the objective function. To minimize, set maximize
to False
.
exp.set_optim_specs(objective_func=objective_func, maximize=False)
WeightedMOOExperiment
class¶
In WeightedMOOExperiment
, an additional weights
for different objectives needs to be specify in set_optim_specs
.
weights_obj = np.linspace(0, 1, 21)
exp_weighted = bo.WeightedMOOExperiment("Weighted Experiment")
exp_weighted.input_data(X_real=X_real, Y_real=Y_real, X_ranges=X_ranges, X_names=X_names)
exp_weighted.set_optim_specs(objective_func=objective_func, maximize=True, weights=weights_obj)
EHVIMOOExperiment
class¶
On the other hand, we can use an EHVIMOOExperiment
object to perform optimization using expected hypervolume
improvement as aquisition funciton. It requires all key components as Experiment
. Additionally, ref_point
is required for set_ref_point
function.
ref_point = [10.0, 10.0]
exp_ehvi = bo.EHVIMOOExperiment("MOO Experiment")
exp_ehvi.input_data(X_real=X_real, Y_real=Y_real, X_ranges=X_ranges, X_names=X_names)
exp_ehvi.set_ref_point(ref_point)
exp_ehvi.set_optim_specs(objective_func=objective_func, maximize=True)
Note
ref_point
defines a list of values that are slightly worse than the lower bound of objective values, where
the lower bound is the minimum acceptable value of interest for each objective. It would be helpful if the user
know the rough values using domain knowledge prior to optimization.
COMSOLExperiment
class¶
The COMSOLExperiment
and COMSOLMOOExperiment
are designed for the integration with COMSOL Multiphysics®.
It requires all key components as Experiment
. Additionally, the X_names
and X_units
are required.
Instead of specifying objective function, the optimized file name,the installed location of COMSOL Multiphysics®,
the output location of the simulation, and the selected column in the output file are needed.
file_name = "comsol_example"
comsol_location = "~/comsol54/multiphysics/bin/comsol"
output_file = "simulation_result.csv"
exp_comsol = bo.COMSOLExperiment("COMSOL Experiment")
exp_comsol.input_data(X_real=X_real, Y_real=Y_real, X_ranges=X_ranges, X_names=X_names, X_units=X_units)
exp_comsol.set_optim_specs(file_name, comsol_location, output_file, comsol_output_col=2, maximize=True)
Note
To use COMSOLExperiment
and COMSOLMOOExperiment
class, you need to have or purchase your own valid
license for COMSOL Multiphysics®.
Run trials and get optimal¶
For all of discussed classes, run_trials_auto
can automatically perform the optimization.
n_trials=30
exp.run_trials_auto(n_trials=n_trials)
exp_weighted.run_trials_auto(n_trials=n_trials, acq_func_name='EI')
exp_ehvi.run_trials_auto(n_trials=n_trials)
exp_comsol.run_trials_auto(n_trials=n_trials)
Note
The acquisition function used can be specified for run_trials_auto
using acq_func_name
.
Get the final optimal using get_optim
for all classes
y_opt, X_opt, index_opt = exp.get_optim()
For more details, see nextorch.bo
.
Experiment class Base: BasicExperiment Experiment consists of a set of trial points For single objective optimization |
|
MOOExperiment class Base: Database For multi-objective optimization (MOO) Currently, only supports two objectives Used for generating Pareto front |
|
EHVIMOOExperiment class Base: Experiment For multi-objective optimization (MOO) |
|
COMSOLExperiment class Base: Experiment Experiment consists of a set of trial points using COMSOL For single objective optimization |
|
COMSOLMOOExperiment class Base: EHVIMOOExperiment For multi-objective optimization (MOO) Used for generating Pareto front |