clustering based on a custom function

4 views (last 30 days)
I have set of nodes of which some are randomly selected to form a solution (or particle), and are known as 'clusterheads'. For example, I generate 20 random coordinates for nodes and pick 5 of these nodes as clusterheads. I form 6 Solutions so there will be 6 random selections of the original 20 nodes.
For each solution, I want the remaining nodes to cluster with a clusterhead based on a specific weight function. However when I run my code instead of getting the remaining nodes to cluster with the clusterhead to form an array for each clusterhead, I get arrays which are 5-10x the size of the original set of nodes. I find that there are many repeated values from these arrays.
For example, instead of getting 15 nodes connecting to various clusterheads, i get 75-150(number not consistent) 'nodes' connecting to the clusterheads. Each cluster has many repeated values. Any help would be appreciated.
clc;
clear;
close all;
%paramenters
nodes = 20; % number of nodes
chead = 5; % number of clusterheads for each solution
numparticles = 6; % number of solutions/particles
maxrange = 50
%initialization
x = [rand(nodes,2)*100;50 50]; % set of random co-ordinates (or nodes) on 100x100 grid and a base station at (50,50)
ResEn = 20*rand(nodes,1); % energy of each node (0-20J)
k = randperm(nodes);
emptyparticle.position = []; % position of 5 clusterheads that i want all others to cluster to based on a function
emptyparticle.velocity = []; % velocity of each solution partice
emptyparticle.energy = []; %energy of each solution particle
emptyparticle.cluster = cell(chead,1); %empty arrays that i want nodes to go to based on function
emptyparticle.cost = []; %cost function based on clusterhead position and clusters, ignore for now
emptyparticle.best.position = []; % best position of each particle,ignore for now
emptyparticle.best.cost = []; %best cost of each particle, ignore for now
emptyparticle.clustervalues = []; %an array that i created that stores the values of the cluster function
particle = repmat(emptyparticle,20,1);
for i = 1:numparticles %10 particles(or solution), so 10 solutions with 5 clusterheads each and each clusterhead has its own cluster of nodes
k = randperm(nodes);
emptyparticle.position.cluster = x(k(1:chead),:); %each solution is assigned a random set of nodes as cluster heads, for example particle(1).position would be [x1 y1;x2 y2;x3 y3;x4 y4;x5 y5]
particle(i).position = x(k(1:chead),:);
particle(i).velocity = 0;
particle(i).energy = ResEn(k(1:chead),:);
end
%clustering process. For node j and clusterhead k,
%a weighting function =
%Energy(clusterhead(k))/(dist(node(j),clusterhead(k))*dist(basestation,clusterhead(k))*nodedegree(clusterhead(k)))
%nodedegree is number of nodes already connected to clusterhead
for i = 1:numparticles
for k = 1:chead
for j = 1:nodes
if pdist2(particle(i).position(k),x(j))>50
% if distance between a ch and node is above range, weight value is 0
particle(i).clustervalues(k,j) = 0;
elseif pdist2(particle(i).position(k),x(j))<50
if size(particle(i).cluster{k},1) == 0
%if cluster for clusterhead of a clusterhead is empty
particle(i).clustervalues(k,j) = ResEn(j)/(pdist2(particle(i).position(k),x(j)*pdist2(x(nodes+1),particle(i).position(k))));
elseif size(particle(i).cluster{k},1) > 0
%if cluster for clusterhead has a node/nodes in it
particle(i).clustervalues(k,j) = ResEn(j)/(pdist2(particle(i).position(k),x(j)*pdist2(x(nodes+1),particle(i).position(k))*size(particle(i).cluster{k},1)));
end
[m, ind] = max(particle(i).clustervalues(:,j));
%from the array created i want to find the maximum weightfunction for each node and cluster the node with the corresponding clusterhead
particle(i).cluster{ind} = [particle(i).cluster{ind};x(j,:)];
end
end
end
end
  2 Comments
Image Analyst
Image Analyst on 28 Mar 2021
Are you doing your own clustering? Why not use a built-in one like kmeans, knnsearch, dbscan, fitctree, etc.?

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 28 Mar 2021
I can't say that I have spent enough time with your code to fully understand it.
But, I notice the following. Inside the loop over k, you assign the clustervalues:
particle(i).clustervalues(k,j)
Then, with this line of code
[m, ind] = max(particle(i).clustervalues(:,j));
you are looking for the maximum clustervalue from the 5 clusterheads. However, that line of code is still inside the loop over k. So, I think you might inadvertently be taking that "maximum" multiple times:
  • k=1: max of value 1
  • k=2: max of values 1:2
  • k=3: max of values 1:3
  • etc
I think you want to pull that line of code outside the loop over k.
  1 Comment
kamran waheed
kamran waheed on 28 Mar 2021
Thanks for your advice and time, I really appreciate it and yes it works as intended now.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!