[Contents] [TitleIndex] [WordIndex

Xcos Example: Optimization of a PID

This page presents an example of Xcos API use.

We will compute the optimal parameters of a PID controller.

All the necessary files are available in the archive PID.zip.

In particular, the 'automatic_test.zcos' file contains the schema used in this example:


First, we initialize Xcos by loading some important Xcos libraries. Then, we load our diagram using importXcosDiagram, and set some initial parameters:



w0 = 2*%pi*100;
m  = 0.5;
K0 = 0.1;

The goal here will be to find better values for w0, m, and K0 that give optimal results.

The goal here will be to find better values for w0, m, and K0 that give optimal results.

Let's define some initial parameters.

MaxEvalFunc = 10;
Pfact  = 1;
Ifact  = 1;
h_step = 1e-3;  // the delta used to compute finite difference derivative
x0     = [1;5]; // Initial parameters
                // P is proportionnal to Pfact
                // I is proportionnal to Ifact
Lower = [0.01;0.01];
Upper = [1000; 100];

global Iter;
Iter = 0;

Once this is done, we define two functions:

The objective function (we minimize the error between the reference signal (the square generator) and the output signal. We also try to minimize the derivative of the output signal (so as ta have some kind of smooth command).

function y = f_pid(x)
  global Iter;
  %scicos_context.w0 = w0;
  %scicos_context.m  = m;
  %scicos_context.K0 = K0;
  %scicos_context.P  = x(1)*Pfact;
  %scicos_context.I  = x(2)*Ifact;
  Iter = Iter + 1;
  Info = scicos_simulate(scs_m,list(),%scicos_context,flag='nw');
  y_error = mean(abs((block_output('values')(:,1) -  block_output('values')(:,2))));
  y_diff  = mean(abs(diff(block_output('values')(:,2))));
  y = 0.5*y_error + 0.5*1*y_diff; ...
  printf('Evaluation %d - P = %f I = %f y = %f (y_error = %f, y_diff = %f)\n',Iter,x(1),x(2),y,y_error,y_diff);

For the optim wrapper, we compute the value of the objective function and the gradient (using the Scilab derivative function).

function [f,df,ind] = my_optim_pid(x,ind)
  f  = f_pid(x);
  df = derivative(f_pid,x,h=h_step,order=4);

Once these functions are defined, we launch a first simulation to see the initial behavior.

%scicos_context = [];
%scicos_context.w0 = w0;
%scicos_context.m  = m;
%scicos_context.K0 = K0;
%scicos_context.P = Pfact*x0(1);
%scicos_context.I = Ifact*x0(2);

Info = scicos_simulate(scs_m,list(),%scicos_context);

The behavior shown by Xcos. PID_Init.png

Now, we launch the optimization

[f_opt, x_opt] = optim(my_optim_pid,'b',Lower,Upper,x0,algo='qn','ar',MaxEvalFunc,MaxEvalFunc,1e-3,1e-3,[1e-3;1e-3]);

Finally, we launch a simulation using the optimized parameters to see the behavior of the loop.

// We find a good solution, but it doesn't take into account the stability of the system.
%scicos_context.w0 = w0; ...
%scicos_context.m  = m; ...
%scicos_context.K0 = K0; ...
%scicos_context.P = x_opt(1)*Pfact;
%scicos_context.I = x_opt(2)*Ifact;

x_opt = [Pfact;Ifact].*x_opt;

Info = scicos_simulate(scs_m,Info,%scicos_context);

The behavior shown by Xcos. PID_Final.png


2022-09-08 09:27