### 仿生学算法在 MATLAB 中的实现
#### 遗传算法 (Genetic Algorithm)
遗传算法是一种基于自然选择和遗传机制的优化搜索方法。该算法通过模拟生物进化过程来解决问题。
```matlab
function [bestIndividual, bestFitness] = geneticAlgorithm(popSize, numVars, fitnessFcn, maxGenerations)
% 初始化种群
population = rand(popSize, numVars);
% 计算适应度值
fitnessValues = arrayfun(fitnessFcn, population);
for generation = 1:maxGenerations
% 选择操作
parentsIndices = randsample(numel(fitnessValues), popSize/2, true, fitnessValues / sum(fitnessValues));
% 交叉操作
children = zeros(popSize, numVars);
for i=1:popSize/2
parentA = population(parentsIndices(i), :);
parentB = population(parentsIndices(mod(i-1,popSize/2)+1), :);
crossoverPoint = ceil(rand * numVars);
childA = [parentA(1:crossoverPoint), parentB(crossoverPoint+1:end)];
childB = [parentB(1:crossoverPoint), parentA(crossoverPoint+1:end)];
children((i*2)-1,:) = childA;
children(i*2,:) = childB;
end
% 变异操作
mutationMask = rand(size(children)) < 0.05;
mutatedChildren = children .* ~mutationMask + randn(size(children)) .* mutationMask;
% 更新种群
newPopulation = [population; mutatedChildren];
[~, idx] = sort([fitnessValues; arrayfun(fitnessFcn,mutatedChildren)], 'descend');
population = newPopulation(idx(1:popSize), :);
fitnessValues = [fitnessValues; arrayfun(fitnessFcn,mutatedChildren)](idx(1:popSize));
% 找到当前最佳个体及其适应度
[~, bestIdx] = max(fitnessValues);
currentBestFitness = fitnessValues(bestIdx);
currentBestIndividual = population(bestIdx,:);
if mod(generation, 10) == 0 || generation == maxGenerations
fprintf('Generation %d Best Fitness Value %.4f\n', generation, currentBestFitness);
end
end
bestIndividual = currentBestIndividual;
bestFitness = currentBestFitness;
end
```
此段代码展示了如何利用MATLAB编写一个简单的遗传算法框架[^1]。
#### 蚁群算法 (Ant Colony Optimization)
蚁群算法模仿蚂蚁觅食行为,用于解决组合优化问题。
```matlab
function path = antColonyOptimization(distMatrix, alpha, beta, evaporationRate, pheromoneInitValue, antsCount, iterations)
nCities = size(distMatrix, 1);
tau = ones(nCities,nCities)*pheromoneInitValue;
for iter = 1:iterations
paths = cell(antsCount, 1);
for k = 1:antsCount
visited = false(1, nCities);
curCity = randi(nCities);
visited(curCity) = true;
tour = curCity;
while any(~visited)
nextProbabilities = ((tau(curCity, :) .^ alpha) .* ...
(1 ./ distMatrix(curCity, :) .^ beta))./(sum(tau(curCity, :) .^ alpha) .* ...
(1./distMatrix(curCity, :) .^ beta));
nextProbabilities(visited)=0;
nextCity = find(randsrc(1,1,[nextProbabilities(:)', (1:nCities).']), 1);
tour(end+1) = nextCity;
visited(nextCity) = true;
curCity = nextCity;
end
paths{k} = tour;
deltaTau = zeros(nCities);
for j = 1:length(tour)-1
fromCity = tour(j);
toCity = tour(j+1);
deltaTau(fromCity,toCity) = deltaTau(fromCity,toCity) + 1/distMatrix(fromCity,toCity);
end
tau = (1-evaporationRate).*tau + deltaTau';
end
% Update global pheromones based on the shortest found route this iteration.
minPathLength = inf;
for p = 1:numel(paths)
plen = calculateTourLength(distMatrix,paths{p});
if(plen<minPathLength)
minPathLength = plen;
bestPathIter = paths{p};
end
end
for j = 1:length(bestPathIter)-1
fromCity = bestPathIter(j);
toCity = bestPathIter(j+1);
tau(fromCity,toCity) = tau(fromCity,toCity)+(1/minPathLength);
end
end
function length = calculateTourLength(DM,tour)
length = DM(tour,end)+DM(tour(1));
for i = 2:length(tour)
length = length + DM(tour(i-1),tour(i));
end
end
path = bestPathIter;
end
```
上述实现了基本形式下的蚁群算法,在路径规划等问题上具有广泛应用价值[^2].