How to make black-box optimization work with time samples

2 views (last 30 days)
I got a black box optimization which I want to change in a way that it could work with time samples. I mean now I got a black box optimization which runs my main control system for 30 times for example (number of iterations of black box optimizer). I want to put this black-box and its function (@theproblem) inside my main code and make it work with time samples. I mean each time sample gives an output to "fminsearch" and it finds the minimum of output. here the output is L2 norm defined as "f". how can I do this?
clc;
clear;
close all;
%% Find LQ tunng to achive desired response
nx = 2;
nu = 2;
Q0 = eye(nx);
q0 = Q0(find(triu(ones(nx))));
R0 = eye(nu);
r0 = R0(find(triu(ones(nu))));
x0 = [q0;r0];
options = optimset('Display','iter-detailed','MaxIter',80);
[x,fval,exitflag,output] = fminsearch(@theproblem,x0,options);
function f = theproblem(x)
nx = 2;
nu = 2;
q = x(1:nx*(nx+1)/2);
r = x(nx*(nx+1)/2+1:end);
pattern = find(triu(ones(nx)));
Q(pattern) = q;
Q = reshape(Q,nx,nx);
Q = Q+Q'-diag(diag(Q));
pattern = find(triu(ones(nu)));
R(pattern) = r;
R = reshape(R,nu,nu);
R = R+R'-diag(diag(R));
% parameterizing roots of weight to ensure the objects always are psd
% to avoid problems in LQR, hence Q is really a root factor of the weight
f = computegoodness(Q'*Q,R'*R);
% Does not work well as solver makes too long steps into negative definite
%f = computegoodness(Q,R);
end
function f = computegoodness(Q,R)
% Create LQ controller with this Q and R
A = [1 2;4 5];
B = [1 0;0 1];
C = [1 0;0 1];
L = lqr(A,B,Q,R);
L0 = pinv(-C*inv(A-B*L)*B);
Gc = ss(A-B*L,B*L0,C,0);
y = step(Gc,0:0.01:10);
clf
subplot(2,2,1);plot(y(:,1,1),'linewidth',2);hold on
subplot(2,2,2);plot(y(:,2,1),'linewidth',2);hold on;
subplot(2,2,3);plot(y(:,1,2),'linewidth',2);hold on;
subplot(2,2,4);plot(y(:,2,2),'linewidth',2);hold on;
% Dream step response
Gdream = ss(-.5*eye(2),.5*eye(2),eye(2),zeros(2));
ydream = step(Gdream,0:0.01:10);
subplot(2,2,1);plot(ydream(:,1,1),'--','linewidth',2);hold on
subplot(2,2,2);plot(ydream(:,2,1),'--','linewidth',2);hold on;
subplot(2,2,3);plot(ydream(:,1,2),'--','linewidth',2);hold on;
subplot(2,2,4);plot(ydream(:,2,2),'--','linewidth',2);hold on;
drawnow
f = norm((y(:)-ydream(:)))^2;
end

Answers (0)

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!