I have recently started reading about PID tuning methods and algorithms, and I encountered the particle swarm optimization algorithm and genetic algorithm.

The problem is, that I don't understand how each particle/chromosome determines his fitness. On real physical system, each particle/chromosome checks his fitness on the system? Wouldn't it take a really long time? I think that I am missing something here... Can those algorithms be implemented on an actual physical system? If so, then how?


2 Answers 2


GA and PSO methods are generally and more simply executed on a model of the plant you want to tune your PID for, not on the physical system. This way, you can of course converge much faster toward the solution and also you don't apply potentially disruptive gains to your PID controller.

The very first step is thus to come up with a good model of your process through identification.


Talking especifically of PSO:

PSO is a good method for PID tuning, as you state. This algo is based particles that help find the absolute minimum. Let's think that you tune to get the no overshot.

If you tune a PID you must have a 3 dimension PSO algo, if you tune a PD just two. And for each dimension you can have any amount of particles as you want. In my case, in Scilab 5.4 I tune PD with PSO with 10 or 15 particles and converge quite fast, in a pair of minutes.

A particle represents a Kp value, for instance. If you have 10 particles for each dimension, you will be able to test your Kp with 10 different values at once. Then, the best particle will reveal, and will tell the other nine particles to adapt their value towards that local best particle, and Kp values will readapt till you find the local minimum Kp value that gets the lowest overshot.

Before tuning a PID, I suggest you to try to find the lowest point in a U letter with PSO. My code with Scilab is next:

   weight=1; //weight of the PSO algorithm 
   const_c1=2; //constant of the velocity algorithm. 
   const_c2=2; //constant of the velocity algorithm. 
   Number_Iter=300; //number of iterations
   dim=1; // dimension

  //Initialize the parameter
  R1=rand(dim,N); //Random numbers [0 1]
  R2=rand(dim,N); //Random numbers [0 1]
  current_fitness=zeros(N,1); /

  //initializing swarm and velocities and position
  local_best_position = current_position;

 //evaluate initial population



 local_best_fitness = current_fitness;

for i=1:N

//velocity update
velocity = weight*velocity + const_c1*(R1.*(local_best_position-current_position)) + const_c2*(R2.*(global_best_position - current_position));

//swarm update
 current_position = current_position + velocity;

 //evaluate a new swarm

 while(iter < = Number_Iter)
iter = iter + 1;

    y= x1^2;

    disp(iter); disp(i);

 for var = 1 : N
if current_fitness(var) < local_best_fitness(var) then
  local_best_fitness(var) = current_fitness(var);
  local_best_position(var) = current_position(var);  

current_glob_bestfitness = min(local_best_fitness);

if (current_glob_bestfitness < global_best_fitness)
      global_best_fitness = current_glob_bestfitness;

  for var2=1:N


velocity = weight * velocity + const_c1*(R1.*(local_best_position - current_position)) + const_c2*(R2.*(global_best_position - current_position));

current_position = current_position + velocity;
  • $\begingroup$ I don't understand how can I test 10 different Kp values at once, can you please explain it to me again? thanks for your answer $\endgroup$
    – Alon
    Commented Mar 13, 2016 at 19:35
  • $\begingroup$ Implement the dynamic model of your robot, and then, for choosing Kp values PSO algorithm can be chosen. Each particle represents a different Kp value, and are tested on the dynamic model you have previously implemented, according to the minimum cost function that you choose (overshot, energy consumption, speed, whatever). $\endgroup$
    – galtor
    Commented Mar 13, 2016 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.