Wing Profile OptimizationΒΆ
You want to design an optimal wing profile to maximize the lift and minimize the drag. In this case, your black-box function would be based on fluid flow simulation that returns the lift-drag ratio of a given wing profile setting.
from datetime import timedelta
import numpy as np
from amplify import FixstarsClient
from amplify_bbopt import (
DatasetGenerator,
KernelQAOptimizer,
RealVariable,
blackbox,
plot_history,
)
from utils.pseudo_simulators import pseudo_wing_simulator as wing_simulator
np.set_printoptions(legacy="1.25")
# Black-box function with decision variables
@blackbox
def my_blackbox_func(
wing_width: float = RealVariable(bounds=(1, 20), nbins=100),
wing_height: float = RealVariable(bounds=(1, 5), nbins=20),
wing_angle: float = RealVariable(bounds=(0, 45), nbins=20),
) -> float:
"""This black-box function executes wing_simulator() and returns
the negative lift-drag ratio for a given wing's width, height, and angle.
"""
lift, drag = wing_simulator(wing_width, wing_height, wing_angle)
print(f"{lift=:.2f}, {drag=:.2f}, {lift/drag=:.2f}")
return -lift / drag # value to minimize
# Generate initial training data set
num_init_data = 10
data = DatasetGenerator(objective=my_blackbox_func).generate(
num_samples=num_init_data
)
# Set up solver client
client = FixstarsClient()
client.parameters.timeout = timedelta(milliseconds=2000) # 2 seconds
# client.token = "xxxxxxxxxxx" # Enter your Amplify API token.
# Instantiate the kernel-QA optimizer
optimizer = KernelQAOptimizer(
data=data, client=client, objective=my_blackbox_func
)
print(optimizer)
# Perform kernel-QA optimization for [num_cycles] cycles
optimizer.optimize(num_cycles=5)
# Print results
print(f"{optimizer.best_solution=}") # Solution (optimal input)
print(f"{optimizer.best_objective=:.3e}") # Objective function value
Show code cell output
amplify-bbopt | 2024/10/04 05:43:59 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:43:59 | INFO | #0/10 initial data for my_blackbox_func
amplify-bbopt | 2024/10/04 05:43:59 | INFO | - [obj]: lift=622.00, drag=160.09, lift/drag=3.89
amplify-bbopt | 2024/10/04 05:43:59 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:43:59 | INFO | #1/10 initial data for my_blackbox_func
amplify-bbopt | 2024/10/04 05:43:59 | INFO | - [obj]: lift=135.56, drag=58.00, lift/drag=2.34
amplify-bbopt | 2024/10/04 05:43:59 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:43:59 | INFO | #2/10 initial data for my_blackbox_func
amplify-bbopt | 2024/10/04 05:43:59 | INFO | - [obj]: lift=24.15, drag=11.19, lift/drag=2.16
amplify-bbopt | 2024/10/04 05:43:59 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:43:59 | INFO | #3/10 initial data for my_blackbox_func
amplify-bbopt | 2024/10/04 05:43:59 | INFO | - [obj]: lift=586.23, drag=169.67, lift/drag=3.46
amplify-bbopt | 2024/10/04 05:43:59 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:43:59 | INFO | #4/10 initial data for my_blackbox_func
amplify-bbopt | 2024/10/04 05:43:59 | INFO | - [obj]: lift=375.11, drag=154.56, lift/drag=2.43
amplify-bbopt | 2024/10/04 05:43:59 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:43:59 | INFO | #5/10 initial data for my_blackbox_func
amplify-bbopt | 2024/10/04 05:43:59 | INFO | - [obj]: lift=532.36, drag=154.94, lift/drag=3.44
amplify-bbopt | 2024/10/04 05:43:59 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:43:59 | INFO | #6/10 initial data for my_blackbox_func
amplify-bbopt | 2024/10/04 05:43:59 | INFO | - [obj]: lift=570.83, drag=258.25, lift/drag=2.21
amplify-bbopt | 2024/10/04 05:43:59 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:43:59 | INFO | #7/10 initial data for my_blackbox_func
amplify-bbopt | 2024/10/04 05:43:59 | INFO | - [obj]: lift=618.28, drag=170.55, lift/drag=3.63
amplify-bbopt | 2024/10/04 05:43:59 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:43:59 | INFO | #8/10 initial data for my_blackbox_func
amplify-bbopt | 2024/10/04 05:43:59 | INFO | - [obj]: lift=394.82, drag=233.11, lift/drag=1.69
amplify-bbopt | 2024/10/04 05:43:59 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:43:59 | INFO | #9/10 initial data for my_blackbox_func
amplify-bbopt | 2024/10/04 05:43:59 | INFO | - [obj]: lift=66.17, drag=177.25, lift/drag=0.37
num variables: 3
num elemental variables: 3
num amplify variables: 137
optimizer client: FixstarsClient
objective weight: 1.0
--------------------
trainer class: ModelKernelTrainer
model class: ModelKernel
model params: {beta: 0.0, gamma: 0.0}
reg_param: 1
amplify-bbopt | 2024/10/04 05:43:59 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:43:59 | INFO | #1/5 optimization cycle, constraint wt: 7.77e+00
amplify-bbopt | 2024/10/04 05:43:59 | INFO | model corrcoef: 1.000, beta: 0.0
amplify-bbopt | 2024/10/04 05:44:02 | INFO | num_iterations: 20
amplify-bbopt | 2024/10/04 05:44:02 | INFO | - [obj]: lift=417.16, drag=71.77, lift/drag=5.81
amplify-bbopt | 2024/10/04 05:44:02 | INFO | y_hat=-5.812e+00, best objective=-5.812e+00
amplify-bbopt | 2024/10/04 05:44:02 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:44:02 | INFO | #2/5 optimization cycle, constraint wt: 1.16e+01
amplify-bbopt | 2024/10/04 05:44:02 | INFO | model corrcoef: 1.000, beta: 0.0
amplify-bbopt | 2024/10/04 05:44:05 | INFO | num_iterations: 19
amplify-bbopt | 2024/10/04 05:44:05 | INFO | - [obj]: lift=452.96, drag=73.54, lift/drag=6.16
amplify-bbopt | 2024/10/04 05:44:05 | INFO | y_hat=-6.160e+00, best objective=-6.160e+00
amplify-bbopt | 2024/10/04 05:44:05 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:44:05 | INFO | #3/5 optimization cycle, constraint wt: 1.23e+01
amplify-bbopt | 2024/10/04 05:44:05 | INFO | model corrcoef: 1.000, beta: 0.0
amplify-bbopt | 2024/10/04 05:44:08 | INFO | num_iterations: 19
amplify-bbopt | 2024/10/04 05:44:08 | INFO | - [obj]: lift=461.91, drag=73.98, lift/drag=6.24
amplify-bbopt | 2024/10/04 05:44:08 | INFO | y_hat=-6.244e+00, best objective=-6.244e+00
amplify-bbopt | 2024/10/04 05:44:08 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:44:08 | INFO | #4/5 optimization cycle, constraint wt: 1.25e+01
amplify-bbopt | 2024/10/04 05:44:08 | INFO | model corrcoef: 1.000, beta: 0.0
amplify-bbopt | 2024/10/04 05:44:11 | INFO | num_iterations: 18
amplify-bbopt | 2024/10/04 05:44:11 | INFO | modifying solution (11, is_frequent=False), {'wing_width': 19.808080808080806, 'wing_height': 2.263157894736842, 'wing_angle': 7.105263157894736} --> {'wing_width': 17.12121212121212, 'wing_height': 1.631578947368421, 'wing_angle': 2.3684210526315788}.
amplify-bbopt | 2024/10/04 05:44:11 | INFO | - [obj]: lift=283.90, drag=40.61, lift/drag=6.99
amplify-bbopt | 2024/10/04 05:44:11 | INFO | y_hat=-6.991e+00, best objective=-6.991e+00
amplify-bbopt | 2024/10/04 05:44:11 | INFO | ----------------------------------------
amplify-bbopt | 2024/10/04 05:44:11 | INFO | #5/5 optimization cycle, constraint wt: 1.40e+01
amplify-bbopt | 2024/10/04 05:44:11 | INFO | model corrcoef: 1.000, beta: 0.0
amplify-bbopt | 2024/10/04 05:44:14 | INFO | num_iterations: 19
amplify-bbopt | 2024/10/04 05:44:14 | INFO | - [obj]: lift=287.84, drag=40.80, lift/drag=7.05
amplify-bbopt | 2024/10/04 05:44:14 | INFO | y_hat=-7.054e+00, best objective=-7.054e+00
optimizer.best_solution={'wing_width': 17.12121212121212, 'wing_height': 1.631578947368421, 'wing_angle': 7.105263157894736}
optimizer.best_objective=-7.054e+00
fig = plot_history(optimizer.fetch_history()) # Plot the optimization history
fig.show()