Particle Swarm Optimization cost value is different from real value

2 views (last 30 days)
Hello,
I am trying to find optimal controller gain in Simulink file using particle swarm optimization.
The Entire structure follows:
  1. Generate a cost function. Input of this cost function is controller gain. If input is applied, this parameter is applied to simulink file and run by sim() function. After simulation is executed, the tracking error signal is saved in workspace and error is calculated. the output value of this cost function is this norm of tracking error vector.
  2. Execute particle swarm optimization with 1. cost function with parallel computing.
For that, I wrote below code
delete(gcp('nocreate')); %First delete processor if there is any.
parpool('AttachedFiles',{'Plant_test_version.slx'}); % Attach file to processor
spmd
load_system('Plant_test_version'); % Load simulink file to processors.
end
addAttachedFiles(gcp,'Plant_parallel.slx'); % I am not sure this line is needed, but to make sure, I wrote this line.
option1 = optimoptions('particleswarm','UseParallel',true,'UseVectorized',false,'Display','iter','PlotFcn','pswplotbestf');
[sol1,fval1,exitflag1,output1] = particleswarm(@cost_function,26,0.05*ones(26,1),15*ones(26,1),option1); % # of Input parameter is 26.
This code runs well.
The problem is, the cost value that particle swarm give is different when I actually put the solution in cost function.
To make sure what I mean, I will explain it again.
If pso results was,
sol1 = [1 2 3]
fval1 = 100
Then If I put sol1 value into @cost_function, then the output should be 100 but it isn't.
I suspect this is because of parallel computing, but I totally cannot figure out why this occurs.
Can anyone help me?
Thank you,
  2 Comments
Sam Chak
Sam Chak on 2 Jul 2023
This is strange. Could you confirm whether the model in Plant_test_version.slx is exactly the same as the one in Plant_parallel.slx? Also, did you run the test version for the same simulation time as the original model?
종원 이
종원 이 on 6 Jul 2023
Oh, I am sorry I made a mistake.
Yes, you are right, I editted the code like below
addAttachedFiles(gcp,'Plant_test_version.slx');
But result was the same.

Sign in to comment.

Answers (1)

Song-Hyun Ji
Song-Hyun Ji on 3 Jul 2023
I am not sure if you consider the solution as the global optimum like this example. If you think there is a product problem, please contact the Technical Support team with the reproducible sample files.
  2 Comments
종원 이
종원 이 on 6 Jul 2023
Yes, I tried some example function, and it worked well so I think It's not a product problem.
That's why I am suspecting the reason problem occurs is parallel computing process in Simulink file.
Song-Hyun Ji
Song-Hyun Ji on 7 Jul 2023
올려주신 코드만으로는 문제의 원인 파악 및 해결책을 찾을 수가 없습니다. 동일 문제 재현되는 샘플 코드와 함께 아래의 링크를 통해 문제 리포트하여 주시기 바랍니다.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!