Change past observation history while using bayesopt.m

5 views (last 30 days)
Hi,
I am working on using bayesopt (Bayesian optimization) function in a real-time setting.
My goal is to modify past observation data while the optimization is running.
For example, at n-th iteration, the optimizer determines next input (u(n+1)) based on past histories (inputs: u(1:n-1) and observations: y(1:n-1)) and currnet input and observation (u(n) and y(n)). However, before deciding next input (u(n+1)), I want to modifiy previous observation value (y(n-1)) to different value.
I attemped to use 'OutputFcn' option in bayesopt, but the previous observations that I can access using this options are read-only variables.
Is there any possible way that I can change previous observation while running bayesopt?

Accepted Answer

Aneela
Aneela on 9 Oct 2024
bayesopttreats past observations as immutable. It is not possible to modify them while running “bayesopt”.
However, to access and modify past observation variables you can consider Custom Bayesian Optimization Loop.
  • Use MATLAB's Gaussian Process functions (fitrgp, etc.) to model the objective function and predict to determine the next point to evaluate.
  • In each iteration, you can manually modify any past observations before updating the model.
Refer to the below reference custom code for Bayesian Optimization loop:
objectiveFcn = @(x) (x - 2).^2 + sin(5 * x);
maxIterations = 5;
numInitialPoints = 5;
bounds = [0, 5];
u_history = linspace(bounds(1), bounds(2), numInitialPoints)';
y_history = objectiveFcn(u_history);
% Bayesian optimization loop
for n = 1:maxIterations
gpModel = fitrgp(u_history, y_history, 'KernelFunction', 'matern52', 'Standardize', true);
u_next = determineNextInput(gpModel, bounds);
y_next = objectiveFcn(u_next);
% Modify previous observation if needed
if n > 1
y_history(end) = modifyObservation(y_history(end));
end
u_history = [u_history; u_next];
y_history = [y_history; y_next];
fprintf('Iteration %d: u = %.4f, y = %.4f\n', n, u_next, y_next);
end
% determine next input using Expected Improvement
function u_next = determineNextInput(gpModel, bounds)
acquisitionFcn = @(x) -expectedImprovement(x, gpModel);
options = optimoptions('fmincon', 'Display', 'off');
u_next = fmincon(acquisitionFcn, mean(bounds), [], [], [], [], bounds(1), bounds(2), [], options);
end
% Expected Improvement acquisition function
function ei = expectedImprovement(x, gpModel)
[mu, sigma] = predict(gpModel, x);
y_best = min(gpModel.Y);
z = (y_best - mu) ./ sigma;
ei = (y_best - mu) .* normcdf(z) + sigma .* normpdf(z);
ei(sigma == 0) = 0;
end
% Modify the observation
function y_modified = modifyObservation(y)
y_modified = y + randn * 0.01; % Example modification
end
Hope this helps!
  1 Comment
GilHwan
GilHwan on 10 Oct 2024
Hi Aneela,
Thank you for your help.
I may try to modify fitrgp function to update past observations.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!