Info

This question is closed. Reopen it to edit or answer.

could anyone help me how to have the same values for all the three clusters with respect to the code shown below.

1 view (last 30 days)
code:
clear all;
clc;
PPP=[2.6917 0.3600;
1.6045 0.5565;
-0.8608 0.8706;
1.3589 0.4763]
particles =1;
iterations = 3;
dimensions=2;
swarm_fitness(1:particles)=-Inf;
clusters=zeros(1,particles);
swarm_pos = cell(1,particles);
swarm_vel = cell(1,particles);
swarm_best = cell(1,particles);
for particle=1:particles
PARTICLE=particle;
clusters(particle)= 3;
swarm_pos{particle} [ 0.9625 0.9028;
0.9824 0.1940;
0.7983 0.3804]
initial_clusters=size(swarm_pos{particle},1);
end
for T=1:iterations
for particle=1:particles
[mm,nn] = size(swarm_pos{particle});
distance=[];
for user=1:4
for cluster=1:mm
distance(user,cluster)=norm(swarm_pos{particle}(cluster,:)-PPP(user,:));
end
end
distances{particle}=distance;
end
c=zeros(4,particles);
for particle=1:particles
[value, index] = max(distances{particle},[],2);
c(:,particle) = index;
end
average_fitness = zeros(particles,1);
for particle=1:particles
[mm,nn] = size(swarm_pos{particle});
for cluster = 1 : mm
if any(c(:,particle) == cluster)
CC_CC=c(:,particle)==cluster;cluster;particle;
CCC_CCC=distances{particle};
cc1=CCC_CCC(:,cluster);
ccc1=CC_CC';
cc2=sum(ccc1(:)==1);
cc3= ccc1*cc1;
local_fitness=cc3/cc2;%minimum value/average
average_fitness(particle,1) = average_fitness(particle,1) + local_fitness;% adding the fitness of cluster together
end
end
average_fitness(particle,1) = average_fitness(particle,1) / mm;
if (average_fitness(particle,1) > swarm_fitness(particle));
ss=(average_fitness(particle,1) > swarm_fitness(particle));
swarm_fitness(particle) = average_fitness(particle,1);
swarm_best(:,particle) = swarm_pos(:,particle); %LOCAL BEST FITNESS
end
end
r1=0.2
w=0.8;
c1=2.7455;
for particle=1:particles
P2=particle;
C2C2=swarm_pos{particle}
cognitive =c1*r1*(swarm_best{particle}-swarm_pos{particle});
end
end
When i run the code it executes and gives me the result where
C2C2 =[ 0.9625 0.9028;
0.9824 0.1940;
0.7983 0.3804]
But i want to have C2C2 such that all the three rows should contain the same values in the following manner
C2C2 =[ 0.9625 0.9028;
0.9625 0.9028;
0.9625 0.9028]
or
C2C2=[0.9824 0.1940;
0.9824 0.1940;
0.9824 0.1940]
or
C2C2=[0.7983 0.3804;
0.7983 0.3804;
0.7983 0.3804]
Could anyone please help me on this.
  1 Comment
Adam
Adam on 4 Oct 2019
You haven't given any description of what you are trying to do. From the numbers you give your example seems to just boil down to
C2C2=swarm_pos{particle}
and given what you want then I guess
C2C2 = repmat( swarm_pos{particle}(1,:), [3 1] );
would work, which completely ignores almost every line of the code you included!

Answers (0)

Community Treasure Hunt

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

Start Hunting!