% Invasive Weed Optimization (IWO) Clustering
% Created By Seyed Muhammad Hossein Mousavi - 2022
% Comparison with K-means and GMM
clc;
clear;
close all;
warning('off');
%% Basics
% Loading
data = load('dat');
X = data.XX;
%
k = 3; % Number of Clusters
%
CostFunction=@(m) ClusterCost(m, X); % Cost Function
VarSize=[k size(X,2)]; % Decision Variables Matrix Size
nVar=prod(VarSize); % Number of Decision Variables
VarMin= repmat(min(X),k,1); % Lower Bound of Variables
VarMax= repmat(max(X),k,1); % Upper Bound of Variables
%% IWO Params
MaxIt = 25; % Maximum Number of Iterations
nPop0 = 2; % Initial Population Size
nPop = 5; % Maximum Population Size
Smin = 2; % Minimum Number of Seeds
Smax = 5; % Maximum Number of Seeds
Exponent = 1.5; % Variance Reduction Exponent
sigma_initial = 0.2; % Initial Value of Standard Deviation
sigma_final = 0.001; % Final Value of Standard Deviation
%% Intro
% Empty Plant Structure
empty_plant.Position = [];
empty_plant.Cost = [];
empty_plant.Out = [];
pop = repmat(empty_plant, nPop0, 1); % Initial Population Array
for i = 1:numel(pop)
% Initialize Position
pop(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Evaluation
[pop(i).Cost, pop(i).Out]= CostFunction(pop(i).Position);
end
% Best Solution Ever Found
BestSol = pop(1);
% Initialize Best Cost History
BestCosts = zeros(MaxIt, 1);
%% IWO Main Body
for it = 1:MaxIt
% Update Standard Deviation
sigma = ((MaxIt - it)/(MaxIt - 1))^Exponent * (sigma_initial - sigma_final) + sigma_final;
% Get Best and Worst Cost Values
Costs = [pop.Cost];
BestCost = min(Costs);
WorstCost = max(Costs);
% Initialize Offsprings Population
newpop = [];
% Reproduction
for i = 1:numel(pop)
ratio = (pop(i).Cost - WorstCost)/(BestCost - WorstCost);
S = floor(Smin + (Smax - Smin)*ratio);
for j = 1:S
% Initialize Offspring
newsol = empty_plant;
% Generate Random Location
newsol.Position = pop(i).Position + sigma * randn(VarSize);
% Apply Lower/Upper Bounds
newsol.Position = max(newsol.Position, VarMin);
newsol.Position = min(newsol.Position, VarMax);
% Evaluate Offsring
[newsol.Cost, newsol.Out] = CostFunction(newsol.Position);
% Add Offpsring to the Population
newpop = [newpop
newsol];
end
end
% Merge Populations
pop = [pop
newpop];
% Sort Population
[~, SortOrder] = sort([pop.Cost]);
pop = pop(SortOrder);
% Competitive Exclusion (Delete Extra Members)
if numel(pop)>nPop
pop = pop(1:nPop);
end
% Store Best Solution Ever Found
BestSol = pop(1);
% Store Best Cost History
BestCosts(it) = BestSol.Cost;
% Display Iteration Information
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCosts(it))]);
% Plot
DECenters=PlotRes(X, BestSol);
pause(0.01);
end
%% Plot IWO Train
figure;
semilogy(BestCosts, 'LineWidth', 2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;
DElbl=BestSol.Out.ind;
%% K-Means Clustering for Comparison
[kidx,KCenters] = kmeans(X,k);
figure
set(gcf, 'Position', [150, 50, 700, 400])
subplot(2,3,1)
gscatter(X(:,1),X(:,2),kidx);title('K-Means')
hold on;
plot(KCenters(:,1),KCenters(:,2),'ok','LineWidth',2,'MarkerSize',6);
subplot(2,3,2)
gscatter(X(:,1),X(:,3),kidx);title('K-Means')
hold on;
plot(KCenters(:,1),KCenters(:,3),'ok','LineWidth',2,'MarkerSize',6);
subplot(2,3,3)
gscatter(X(:,1),X(:,4),kidx);title('K-Means')
hold on;
plot(KCenters(:,1),KCenters(:,4),'ok','LineWidth',2,'MarkerSize',6);
subplot(2,3,4)
gscatter(X(:,2),X(:,3),kidx);title('K-Means')
hold on;
plot(KCenters(:,2),KCenters(:,3),'ok','LineWidth',2,'MarkerSize',6);
subplot(2,3,5)
gscatter(X(:,2),X(:,4),kidx);title('K-Means')
hold on;
plot(KCenters(:,2),KCenters(:,4),'ok','LineWidth',2,'MarkerSize',6);
subplot(2,3,6)
gscatter(X(:,3),X(:,4),kidx);title('K-Means')
hold on;
plot(KCenters(:,3),KCenters(:,4),'ok','LineWidth',2,'MarkerSize',6);
%
KMeanslbl=kidx;
%% Gaussian Mixture Model Clustering for Comparison
options = statset('Display','final');
gm = fitgmdist(X,k,'Options',options)
idx = cluster(gm,X);
figure
set(gcf, 'Position', [50, 300, 700, 400])
subplot(2,3,1)
gscatter(X(:,1),X(:,2),idx);title('GMM')
hold on;
subplot(2,3,2)
gscatter(X(:,1),X(:,3),idx);title('GMM')
hold on;
subplot(2,3,3)
gscatter(X(:,1),X(:,4),idx);title('GMM')
hold on;
subplot(2,3,4)
gscatter(X(:,2),X(:,3),idx);title('GMM')
hold on;
subplot(2,3,5)
gscatter(X(:,2),X(:,4),idx);title('GMM')
hold on;
subplot(2,3,6)
gscatter(X(:,3),X(:,4),idx);title('GMM')
hold on;
%
GMMlbl=idx;
%% MAE and MSE Errors
IWO_GMM_MAE=mae(DElbl,GMMlbl);
IWO_KMeans_MAE=mae(DElbl,KMeanslbl);
GMM_KMeans_MAE=mae(GMMlbl,KMeanslbl);
IWO_GMM_MSE=mse(DElbl,GMMlbl);
IWO_KMeans_MSE=mse(DElbl,KMeanslbl);
GMM_KMeans_MSE=mse(GMMlbl,KMeanslbl);
fprintf('IWO vs GMM MAE = %0.4f.\n',IWO_GMM_MAE)
fprintf('IWO vs K-Means MAE = %0.4f.\n',IWO_KMeans_MAE)
fprintf('GMM vs K-Means MAE = %0.4f.\n',GMM_KMeans_MAE)
fprintf('IWO vs GMM MSE = %0.4f.\n',IWO_GMM_MSE)
fprintf('IWO vs K-Means MSE = %0.4f.\n',IWO_KMeans_MSE)
fprintf('GMM vs K-Means MSE = %0.4f.\n',GMM_KMeans_MSE)
没有合适的资源?快使用搜索试试~ 我知道了~
【数据聚类】杂草算法优化K-means算法数据聚类【含Matlab源码 2168期】.zip
共9个文件
jpg:5个
m:3个
mat:1个
需积分: 5 1 下载量 174 浏览量
2024-06-19
19:25:25
上传
评论
收藏 524KB ZIP 举报
温馨提示
【数据聚类】杂草算法优化K-means算法数据聚类【含Matlab源码 2168期】
资源推荐
资源详情
资源评论
收起资源包目录
【数据聚类】杂草算法优化K-means算法数据聚类【含Matlab源码 2168期】.zip (9个子文件)
【数据聚类】基于matlab杂草算法优化K-means算法数据聚类【含Matlab源码 2168期】
运行结果1.jpg 37KB
PlotRes.m 1KB
dat.mat 28KB
Invasive Weed Optimization (IWO) Clustering.jpg 460KB
main.m 5KB
运行结果2.jpg 42KB
ClusterCost.m 312B
运行结果4.jpg 43KB
运行结果3.jpg 49KB
共 9 条
- 1
资源评论
Matlab领域
- 粉丝: 2w+
- 资源: 2476
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功