How to constrain genetic algorithm input u?

2 views (last 30 days)
JAMES KEEN
JAMES KEEN on 23 Feb 2021
Commented: Walter Roberson on 11 Mar 2021
My real system (buck converter) can only take an input of 0 to 1 (duty ratio) and I need to constrain the system so the controller action does not go past this. How can I do this?
The code for the project is below, it is steve bruntons genetic algorithm coding. https://youtu.be/S5C_z1nVaSg?t=470
I assume the u variable is the duty ratio but I am not sure.
---------------------------------------------------- First live script with Ga cmd in it-------------------------------------
clear all, close all, clc
dt = 0.000001; % this is 10^-6
PopSize = 30 % was 500
MaxGenerations = 10; %was 1000
s = tf('s');
G = (1439928003.68621)/(s*s+5333.33333333333*s+95995200.2457475) % got this by doing feedback cmd
options = optimoptions(@ga,'PopulationSize',PopSize,'MaxGenerations',MaxGenerations,'OutputFcn',@myfun);
[x,fval] = ga(@(K)pidtest(G,dt,K),3,-eye(3),zeros(3,1),[],[],[],[],[],options);
-----------------------------------------------in adjacent live script------------------------------------------------
function J = pidtest(G,dt,parms)
s = tf('s');
K = parms(1)+ parms(2)/s + parms(3)*s/(1+0.000001*s)% this is 10^-6
Loop = series(K,G);
ClosedLoop = feedback(Loop,1);
t = 0:dt:0.05; % this indicates length of time to show
[y,t] = step(ClosedLoop,t);
CTRLtf = K/(1+K*G);
u = lsim(K,1-y,t); % not sure if this is what I should change
Q = [1]; % Q weighting (couldn't get it so it looked at inductor current also) effecfts response of system?
R = 0.005; % R weighting effects controller action
J = dt*sum(Q*(1-y(:)).^2 + R*u(:).^2) % LQR is my cost function
step(5*ClosedLoop,t)
h = findobj(gcf,'type','line');
set(h,'linewidth',2);
drawnow
---------------------------------------------------------------------------------------------------------------------------------
Thanks in advance, if anymore information is desired please ask away.
The PID controller h
  7 Comments
JAMES KEEN
JAMES KEEN on 24 Feb 2021
Edited: JAMES KEEN on 24 Feb 2021
The image below explains the system and how i want the controller K to be limited to an output of 0 to 1. The numbers that make up K can be any value they want but I don't want the controller K output to be allowed to exceed the bounds of 0 to 1. The reason I want to make sure the controller K does not exceed the constraint of 0 to 1 is because the duty ratio (a percentage) can only go in the range of 0 to 1 without problems occuring.
This second image shows what the code is producing (it produces varying values of P, I, D values to find most optimal controller which are gathered together under the name (K)). I have also shown why I need the PID controller (K) to be within the limits of 0 to 1, it is because the real system cannot handle a duty ratio (output of PID) that is below 0 or above 1.
If there are any other questions please feel free to ask as I am quite new to this sector also.
Walter Roberson
Walter Roberson on 11 Mar 2021
Sorry, I have no idea how the output desired is intended to connect to your code. You do have a K in your code but it is
K = parms(1)+ parms(2)/s + parms(3)*s/(1+0.000001*s)% this is 10^-6
and it is not returned. It is a transfer function, but transfer functions are not any particular value: transfer functions describe a relationship between inputs and outputs. You can talk about the value of a transfer function evaluated at a particular frequency, or the maximum value over a range of frequencies or such things, but not about a transfer function having a definite value.
If the point is just that the transfer function must be between 0 and 1 over a particular range of frequencies, then if we assume that the parameters are non-negative, you get
K = ((P1 + 1000000*P3)*s^2 + (1000000*P1 + P2)*s + 1000000*P2)/(s^2 + 1000000*s)
and for non-negative P1, P2, P3, then the numerator is always positive, and restricting it to the range 0 to 1 can be done by requring that the numerator <= denominator. Solving for equality we get
((P1 + 1000000*P3)*s^2 + (1000000*P1 + P2)*s + 1000000*P2) == (s^2 + 1000000*s)
((P1 + 1000000*P3)*s^2 + (1000000*P1 + P2)*s + 1000000*P2) - (s^2 + 1000000*s)
(P1 + 1000000*P3 - 1)*s^2 + (1000000*P1 + P2 - 1000000)*s + 1000000*P2
Really this is <= 0 rather than a == 0 .
Maximize:
syms P1 P2 P3 s
eqn = (P1 + 1000000*P3 - 1)*s^2 + (1000000*P1 + P2 - 1000000)*s + 1000000*P2
scrit = solve(diff(eqn,s), s)
scurl = subs(diff(eqn,s,s),s,scrit)
which gives
2*P1 + 2000000*P3 - 2
and that needs to be negative for scrit to be discussing a maxima, and the boundary for that is P3 = (1-P1)/1000000. This pretty much tells you that the s^2 coefficient here needs to be negative for the K to be guaranteed bounded to the proper range for all s.
However... if you are only operating over a particular range of s values then you might be able to achieve the desired limits even if the limit would not hold for a wider range of s values.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!