Change past observation history while using bayesopt.m

조회 수: 6 (최근 30일)
GilHwan
GilHwan 2024년 10월 3일
댓글: GilHwan 2024년 10월 10일
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?

채택된 답변

Aneela
Aneela 2024년 10월 9일
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
GilHwan
GilHwan 2024년 10월 10일
Hi Aneela,
Thank you for your help.
I may try to modify fitrgp function to update past observations.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Gaussian Process Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by