Why it gives error if we set lb=[0 0 0 0], ub=[10 10 pi pi] instead of lb=0 and ub=pi?

1 view (last 30 days)
I download an algorithm from Mathworks site called "Student Psycology Based Optimization (SPBO) Algorithm" located on URL: https://www.mathworks.com/matlabcentral/fileexchange/80991-student-psycology-based-optimization-spbo-algorithm?s_tid=FX_rc1_behav
My fitness function is as:
function out=myfitness(b)
u = [1 2 3 4];
out1 = u-b; %needs R2016b or later
out = sum(out1.^2,2);
end
When I type the following on command window, it runs
[Best_fitness,Best_student,Convergence_curve]=SPBO(20,100,pi,0,4,@myfitness)
But when I type the following on command window:
[Best_fitness,Best_student,Convergence_curve]=SPBO(20,100,[10 10 pi pi],[0 0 0 0],4,@myfitness)
it gives the following error:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in SPBO (line 119)
par1(dw,do)=mini+(rand*(maxi-mini)); % Equation (4)
How to handle this?

Answers (2)

Walter Roberson
Walter Roberson on 30 Dec 2020
It is a bug in the code.
The code also regularly uses ans as a variable name to be stored into and recalled from, which makes the code fragile, since whether any particular construct sets ans or not depends upon the MATLAB release, and people reading the code (and repairing it) should not be expected to know all of the rules.
  1 Comment
Walter Roberson
Walter Roberson on 30 Dec 2020
par1(dw,do)=mini+(rand*(maxi(do)-mini(do))); % Equation (4)
But first you need to insert something near the top,
%expand mini and maxi out to length same as number of variables
mini(end:variable) = mini(end);
maxi(end:variable) = maxi(end);

Sign in to comment.


Cris LaPierre
Cris LaPierre on 30 Dec 2020
Edited: Cris LaPierre on 30 Dec 2020
% variable = number of your variables
...
% mini=[mini1,mini2,...,minin] where mini is the lower bound of variable n
% maxi=[maxi1,maxi2,...,maxin] where maxi is the upper bound of variable n
The syntax for calling SPBO is
% To run SPBO: [Best_fitness,Best_student,Convergence_curve]=SPBO(student,Max_iteration,mini,maxi,variable,fobj)
You appear to have flipped your mini and maxi. You should first input the mini values, then the maxi. I would expect mini should be less than maxi. Try this calling syntax.
[Best_fitness,Best_student,Convergence_curve]=SPBO(20,100,[0 0 0 0],[10 10 pi pi],4,@myfitness)
Just a note that I don't use this toolbox, and my answer is untested. Just seems likely to cause an error is it probably violates an unstated/untested assumption the code author made.
  4 Comments

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices 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!