Deleted comments can be found in the Google cache (the archived version will require modification of the attributes, as the show/hide comments toggle doesn't execute properly).
Thank you very much dear Ameer Hamza. But now it gives another error. Can you help me to run the following algorithm for my fitness function given below?
function [xmin,fmin,histout] = GQPSO(fun,D,nPop,lb,ub,maxit,maxeval)
% INPUT:
% fun : function handle for optimization
% D : problem dimension (number of variables)
% nPop : number of particles in the swarm
% lb : lower bound constrain
% ub : upper bound constrain
% maxit : max number of iterations
% maxeval : max number of function evaluations
% OUTPUT:
% xmin : best solution found
% fmin : function value at the best solution, f(xmin)
% histout : record of function evaluations and fitness value by iteration
Your fitness function is expected to receive a 2D array in which the number of rows corresponds to the population size, and the columns correspond to variables, and it is expected to return a vector which is the same size as the population size. Instead your fitness function expects a vector of four elements as input and returns a scalar.
With regard to your new fitness function obj : As I posted above:
Your fitness function is expected to receive a 2D array in which the number of rows corresponds to the population size, and the columns correspond to variables, and it is expected to return a vector which is the same size as the population size.
Now let us examine your code:
u=[2 7 50*th 85*th]; % angles of all four vectors
Okay, that is a vector of 4 elements.
%%%%%%%%%%%%%%%
% Swapping vector b. Taken help from Mathworks site
%%%%%%%%%%%%%%%
[~, ix] = sort(u);
That is going to return the vector [3 4 1 2] into ix.
[~, ix1(ix)] = sort(b);
There you are asking to sort a 2D array with 30 rows and 4 columns. The sort information for that is going to require 30 x 4 output locations, but ix1(ix) would be a vector of 4 locations. Also, sort() by default operates along the first non-singleton dimension, which would be the first dimension for a 30 x 4 array, but it seems more likely that you want to sort along the rows, which would be the second dimension, seeing as you expect an array of length 4.
For this step I would suggest instead
[~, ix1] = sort(b, 2);
ix1 = ix1(:,ix);
which would give you a 2D array, in which each row talks about the sort order of b for its corresponding row
Then
b = b(ix1);
is a problem as it does not do per-row reordering.
Now.. what really is that code trying to do??
Let us experiment:
th=pi/180
u=[2 7 50*th 85*th]
[~, ix] = sort(u)
b = [4 1 3 2]
[~, ix1] = sort(b)
ix1 = ix1(:,ix)
new_b = b(ix1)
b_2 = [0 17 -11 -6]
[~, ix1_2] = sort(b_2)
ix1_2 = ix1_2(:,ix)
new_b_2 = b_2(ix1)
ix comes out as [3 4 1 2]
new_b comes out as [3 4 1 2]
new_b_2 comes out as [0 17 -11 -6]
t = sort([b;b_2],2)
t(:,ix)
comes out as
3 4 1 2
0 17 -11 -6
so it looks to me as if it wants to sort b per-row and then re-order according to the sorting of u. In that case I would suggest
Your fitness function is expected to receive a 2D array in which the number of rows corresponds to the population size, and the columns correspond to variables, and it is expected to returna vector which is the same size as thepopulation size.
Your obj is returning a scalar, not a vector of length 30.
It is not possible to change the algorithm to account for the objective function returning the wrong results.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
1 Comment
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/702837-why-unifrnd-lb-ub-npop-d-gives-error#comment_1241738
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/702837-why-unifrnd-lb-ub-npop-d-gives-error#comment_1241738
Sign in to comment.