[Data clustering] matlab source code based on genetic algorithm clustering design

1. Introduction

1 Introduction to
genetic algorithm Genetic algorithm is an optimized search algorithm that simulates natural evolution. Since it can search for the optimal solution only by relying on the fitness function, it does not require knowledge about the solution space of the problem, and the fitness function is not constrained by conditions such as continuous differentiability, so it is used in solving multi-dimensional, highly nonlinear complex optimization problems Has been widely used and in-depth research.
Genetic algorithms are used in pattern recognition, neural networks, machine learning, industrial optimization control, adaptive control, biological sciences, and social sciences.
2 Principles of genetic algorithm
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Second, the source code

clc
tic
%%参数初始化
maxgen=100; %进化代数,即迭代次数,初始预定值选为100
sizepop=200; %种群规模,初始预定值选为100
pcross=0.9; %交叉概率选择,0和1之间,一般取0.9
pmutation=0.01; %变异概率选择,0和1之间,一般取0.01
individuals=struct('fitness',zeros(1,sizepop),'chrom',[]);
%种群,种群由sizepop条染色体(chrom)及每条染色体的适应度(fitness)组成
avgfitness=[];
%记录每一代种群的平均适应度,首先赋给一个空数组
bestfitness=[];
%记录每一代种群的最佳适应度,首先赋给一个空数组
bestchrom=[];
%记录适应度最好的染色体,首先赋给一个空数组
%初始化种群
for i=1:sizepop
%随机产生一个种群
individuals.chrom(i,:)=4000*rand(1,12);
%把12个0~4000的随机数赋给种群中的一条染色体,代表K=4个聚类中心
x=individuals.chrom(i,:);
%计算每条染色体的适应度
individuals.fitness(i)=fitness(x);
end
%%找最好的染色体
[bestfitness bestindex]=max(individuals.fitness);
%找出适应度最大的染色体,并记录其适应度的值(bestfitness)和染色体所在的位置(bestindex)
bestchrom=individuals.chrom(bestindex,:);
%把最好的染色体赋给变量bestchrom
avgfitness=sum(individuals.fitness)/sizepop;
%计算群体中染色体的平均适应度

trace=[avgfitness bestfitness];
%记录每一代进化中最好的适应度和平均适应度

for i=1:maxgen
i
%输出进化代数
individuals=Select(individuals,sizepop);
avgfitness=sum(individuals.fitness)/sizepop;
%对种群进行选择操作,并计算出种群的平均适应度
individuals.chrom=Cross(pcross,individuals.chrom,sizepop);
%对种群中的染色体进行交叉操作
individuals.chrom=Mutation(pmutation,individuals.chrom,sizepop);
%对种群中的染色体进行变异操作
for j=1:sizepop
x=individuals.chrom(j,:);%解码
[individuals.fitness(j)]=fitness(x);
end
%计算进化种群中每条染色体的适应度
[newbestfitness,newbestindex]=max(individuals.fitness);
[worestfitness,worestindex]=min(individuals.fitness);
%找到最小和最大适应度的染色体及它们在种群中的位置
if bestfitness<newbestfitness
bestfitness=newbestfitness;
bestchrom=individuals.chrom(newbestindex,:);
end
%代替上一次进化中最好的染色体
individuals.chrom(worestindex,:)=bestchrom;
individuals.fitness(worestindex)=bestfitness;
%淘汰适应度最差的个体
avgfitness=sum(individuals.fitness)/sizepop;
trace=[trace;avgfitness bestfitness];
%记录每一代进化中最好的适应度和平均适应度
end
   
    % 交叉概率决定是否进行交叉
    pick=rand;
    while pick==0
        pick=rand;
    end
    if pick>pcross
        continue;
    end
    pick=rand;
    while pick==0    
        pick=rand;        
    end
    for i=1:sizepop    
        pick=pick-sumf(i);        
        if pick<0        
            index=[index i];            
            break;  
        end
    end
end
individuals.chrom=individuals.chrom(index,:);
individuals.fitness=individuals.fitness(index);
ret=individuals; 

Three, running results

Insert picture description here
Insert picture description here

Four, remarks

Complete code or write on behalf of adding QQ1575304183

Past review>>>>>>

[Data analysis] Time-varying parameter stochastic volatility vector autoregressive model (TVP-VAR)

[Signal processing] Matlab source code based on ICA algorithm signal separation

[Data analysis] fuzzy binary decision tree matlab source code

Guess you like

Origin blog.csdn.net/qq_34763204/article/details/113617588