Optimization of resonant system based on Gaussian fit

Driver:

ActiveLearning

Download script:

harmonic_oscillator_fit.py

The target of the study is to tune a resonant system. For simplicity, the resonant system is a harmonic oscillator with eigenfrequency \(\omega_0\) and damping \(\gamma\) driven with force \(F\) at frequency \(\omega\). The amplitude of the oscillator is

\[a_{\rm harm}(\omega) = \frac{F}{\sqrt{(2\omega\omega_0\gamma)^2 + (\omega_0^2 - \omega)^2}} .\]

We assume the system as a black box with unknown resonant behavior. To capture the resonant feature, it is fitted against a Gaussian plus a linear function

\[a_{\rm fit}(\omega) = A \exp\left(-\frac{1}{2}\frac{(\omega-\tau)^2}{\sigma^2}\right) + B\omega + C .\]

The target is to tune \(F, \omega_0, \gamma\) such that the fitted amplitude is \(A = 10\), the resonance frequency is \(\tau = 2\) and the resonance width \(\sigma\) is as small as possible.

 1import sys,os
 2import numpy as np
 3import time
 4
 5from jcmoptimizer import Server, Client, Study, Obseravtion
 6server = Server()
 7client = Client(host=server.host)
 8
 9
10#Amplitude of a driven harmonic oscillator. 
11def amplitude(omega: float, F: float, omega0: float, gamma: float) -> float:
12    return F/np.sqrt((2*omega*omega0*gamma)**2 + (omega0**2 - omega**2)**2)
13
14# Definition of the search domain to tune the oscillator
15design_space = [
16    {'name': 'F', 'type': 'continuous', 'domain': (0.1, 40.0)}, 
17    {'name': 'omega0', 'type': 'continuous', 'domain': (1.0, 4.0)},
18    {'name': 'gamma', 'type': 'continuous', 'domain': (0.03, 0.3)},
19]
20
21#The amplitude is scaned for 10 different driving frequencies
22omegas = np.linspace(1,3,10)
23
24# Creation of the study object with study_id 'harmonic_oscillator_fit'
25study = client.create_study(
26    design_space=design_space,
27    driver="ActiveLearning",
28    study_name="Optimization of resonant system based on Gaussian fit",
29    study_id="harmonic_oscillator_fit"
30)
31
32#configuration of study
33study.configure(
34    max_iter = 30,
35    surrogates = [
36        #A Gaussian process learns the dependence of the amplitudes for
37        #all wavelengths on the design parameters.
38        dict(type="GP", name="amplitudes", output_dim=len(omegas))
39    ],    
40    variables = [
41        #The amplitudes are fitted to a Gaussian + linear expression with
42        #amplitude A, resonance frequency tau, linear gradient B, and constant offset C.
43        #The output are the fitted values and the mean-squared error (MSE).
44        dict(type="Fit", name="fit", input="amplitudes", 
45             expression="A*exp(-0.5*(omega-tau)^2/sigma^2) + B*omega + C", 
46             output_names=["A", "B", "C", "tau", "sigma", "MSE"],
47             output_dim=6, 
48             model_variables=["omega"], variable_values=omegas[:,None].tolist(),
49             initial_parameters=[1.0, 0.0, 0.0, 1.0, 0.5],
50             prior_uncertainties=[10.0, 10.0, 10.0, 10.0, 10.0],
51            ),  
52        #The expression that defines the loss function:
53        dict(type="Expression", name="loss",
54             expression="(A-20)^2 + (tau - 2)^2 + sigma^2 + 0.001*MSE")
55    ],
56    objectives = [
57        #The only objective of the study is to minimize the loss.
58        dict(type="Minimizer", variable="loss"),
59    ],
60    acquisition_optimizer = dict(
61        #Sample computation based on fits is more expensive. In this case
62        #advanced sample computation is usually too laborious.
63        compute_suggestion_in_advance = False
64    ),    
65)
66
67# Evaluation of the black-box function for specified design parameters
68def evaluate(study: Study, F: float, omega0: float, gamma: float) -> Observation:
69    #The harmonic-oscillator amplitude is evaluated for all omega values and the
70    #observed values are used as training input for the Gaussian process "amplitudes"
71    observation = study.new_observation()
72    observation.add([amplitude(omega, F, omega0, gamma) for omega in omegas])
73    return observation
74
75# Run the minimization
76study.set_evaluator(evaluate)
77study.run()
78
79# Print result
80best = study.get_state("driver.best_sample")
81print(f"Best design parameters: F={best['F']:.3f}, omega0={best['omega0']:.3f}, "
82      f"gamma={best['gamma']:.3f}")
83print("Objective: (A-20)^2 + (tau - 2)^2 + sigma^2 + 0.001*MSE = "
84      f"{study.get_state('driver.min_objective'):.3f}")