Example 01: computer experiments¶
Script: examples/example01_computer_experiment.py
Purpose¶
The script shows two equivalent ways to describe a constrained computer experiment: separate objective/constraint callables and one combined callable with output metadata. It also loads the built-in Branin problem and evaluates it on normalized inputs. The Branin function is part of the Virtual Library of Simulation Experiments test-function collection [8].
What is computed¶
objective and constraint values at two input points.
objective-only and constraint-only output blocks.
a Branin contour plot on a grid.
Main objects¶
gpmpcontrib.ComputerExperimentgpmpcontrib.test_problems.branin
Outputs¶
Run python examples/example01_computer_experiment.py from the repository
root to execute the example. Regenerate the static figure with
cd docs && python make_example_results.py.
Branin contour values come from evaluating the built-in
ComputerExperiment on a regular grid. The two callable definitions used
earlier in the script print the same objective and constraint blocks at the
selected test points. This checks that the normalized input box and
objective evaluation agree with the problem metadata.¶
Source excerpt¶
def _pb_objective(x):
"""Objective function: calculates a specific mathematical operation."""
return (x[:, 0] - 10) ** 3 + (x[:, 1] - 20) ** 3
# Define two constraint functions
def _pb_constraints(x):
"""Constraint functions: calculates constraints based on input conditions."""
c1 = -((x[:, 0] - 5) ** 2) - (x[:, 1] - 5) ** 2 + 100
c2 = (x[:, 0] - 6) ** 2 + (x[:, 1] - 5) ** 2 - 82.81
return np.column_stack((c1, c2))
# Set up parameters for the ComputerExperiment class
_pb_dict = {
"input_dim": 2,
"input_box": [[13, 0], [100, 100]],
"single_objective": _pb_objective,
"single_constraint": {
"function": _pb_constraints,
"output_dim": 2,
"bounds": [[100.0, np.inf], [-np.inf, 82.81]],
},
}
# ------------------------------------------------------------
# Example 1: Using separate objective and constraint functions
print(" *** Example 1")
print(" =========")
# Initialize the ComputerExperiment object with separate objective and constraint
pb = gpc.ComputerExperiment(
_pb_dict["input_dim"],
_pb_dict["input_box"],
single_objective=_pb_dict["single_objective"],
single_constraint=_pb_dict["single_constraint"],
)
# Display the initialized ComputerExperiment object.
# This print statement shows the configuration of the experiment, including input
# dimensions, objectives, and constraints.
print(pb)
# Test the evaluation of the objective and constraint functions
x = np.array([[50.0, 50.0], [80.0, 80.0]])
print("\n * Input X:\n", x)
# Evaluate the function using the __call__ method and print the results
results = pb(x)
print("\n * Evaluated Results (one objective, two constraints):\n", results)