Problem with minimizing a LQR problem with Genetic Algorithm
Show older comments
I am trying to solve a LQR problem using Genetic Algorithm.
The values of the LQR matrices are not importent to me, I am just trying to understand the way that it is work.
First I solve the system using the Matlab's LQR algorithm and them I try to solve using the Genetic Algorithm and compare the results.
In the code attach I am trying to compare between P to P2, but I dont get a match.
Also when I run the code, I get the following Error: Optimization terminated: average change in the fitness value less than options
%% State Space
A = [1 2; 3 4];
B = [1; 1];
Q = [1 0;0 1];
R = 1;
%% Slove the LQR
[K,P1] = lqr(A,B,Q,R);
P2 = norm(P1);
%% Solve the Genetic Algorithm
P = ga(@cost_fun,2);
function P = cost_fun(K1)
A = [1 2; 3 4];
B = [1; 0];
Q = [1 0;0 1];
R = 1;
A_i = A - B*K1;
Q_i = - Q - K1'*R*K1;
P_i = lyap(A_i,Q_i);
P = norm(P_i);
end
Accepted Answer
More Answers (1)
Hi @gilad shaul
To find
through
, you need to expand the Continuous-time Algebraic Riccati Equation (CARE) in terms of
, and then use gamultiobj() to solve the root-finding problem.
If you like the alternative solution, please consider voting 👍 the Answer as a little token of appreciation.
%% ----- Expanding the CARE -----
syms p11 p12 p22
A = sym('A', [2 2]); % state matrix
B = sym('B', [2 1]); % input matrix
P = sym('P', [2 2]); % positive definite matrix
A = [sym('0') sym('1');
sym('0') sym('-1')];
B = [sym('0'); sym('1')];
P = [p11 p12;
p12 p22];
Q = sym(eye(2));
R = sym('1');
CARE = A.'*P + P*A - P*B/R*B.'*P + Q
%% ----- Solving using gamultiobj -----
fitfun = @multiobjfcn;
PopSize = 15;
MaxGen = 30;
nvars = 3;
A = -eye(nvars);
b = zeros(nvars, 1);
Aeq = [];
beq = [];
lb = [0 0 0];
ub = [3 3 3];
nonlcon = [];
options = optimoptions('gamultiobj', 'ConstraintTolerance', 1e-6, 'FunctionTolerance', 1e-6, 'HybridFcn', @fgoalattain);
[p, fval] = gamultiobj(fitfun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options);
popt = p(end, :);
R = 1;
B = [0; 1];
P = [popt(1) popt(2); popt(2) popt(3)]
Kopt = R\(B'*P)
%% ----- Multi-objective Functions -----
function y = multiobjfcn(p)
y = zeros(3, 1);
y(1) = (1 - p(2)^2)^2; % Remember to square the CARE
y(2) = (- p(3)^2 - 2*p(3) + 2*p(2) + 1)^2;
y(3) = (p(1) - p(2) - p(2)*p(3))^2;
end
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!