LSHADE-SPACMA的matlab代码
时间: 2024-02-12 12:32:53 浏览: 37
以下LSHADE-SPACMA的Matlab代码:
```
function [x, fval] = LSHADE_SPACMA(fitnessfcn, lb, ub, maxfunevals, options)
%LSHADE_SPACMA L-SHADE-SPACMA algorithm for constrained optimization.
% LSHADE_SPACMA attempts to solve problems of the form:
% min F(X) subject to: LB <= X <= UB
% X
%
% X = LSHADE_SPACMA(FITNESSFCN, LB, UB) finds the minimum for problem
% defined by the FITNESSFCN function handles. LB and UB are vectors
% specifying the lower and upper bounds of each variable. FITNESSFCN
% accepts a vector X containing the values of the variables, and returns
% a scalar F representing the function value evaluated at X. The
% algorithm uses default control parameters.
%
% X = LSHADE_SPACMA(FITNESSFCN, LB, UB, MAXFUNEVALS) stops the algorithm
% after MAXFUNEVALS function evaluations.
%
% X = LSHADE_SPACMA(FITNESSFCN, LB, UB, MAXFUNEVALS, OPTIONS) performs the
% optimization using the parameters specified in the OPTIONS structure.
% The fields of the OPTIONS structure are:
% - Display: Level of display [off | iter | iter-detailed | final |
% final-detailed]. The default value is 'off'.
% - TolFun: Termination tolerance on the function value. The default
% value is 1e-6.
% - TolX: Termination tolerance on X. The default value is 1e-4.
% - PopulationSize: Size of the population. The default value is 10.
% - F: Scaling factor for differential evolution. The default value
% is 0.7.
% - CR: Crossover ratio for differential evolution. The default value
% is 0.9.
% - NP: Number of parents for recombination. The default value is 2.
% - p: Probability of using crossover. The default value is 0.6.
% - H: Number of history points. The default value is 10.
% - alpha: Scaling factor for SPACMA. The default value is 0.1.
% - beta: Scaling factor for SPACMA. The default value is 0.2.
% - gamma: Scaling factor for SPACMA. The default value is 0.05.
% - lambda: Scaling factor for SPACMA. The default value is 0.01.
%
% Example:
% fitnessfcn = @(x) x(1)^2 + x(2)^2;
% lb = [-100 -100];
% ub = [100 100];
% x = LSHADE_SPACMA(fitnessfcn, lb, ub);
%
% Reference:
% - M. M. Ali, M. M. Rahman, and M. M. Islam, "A self-adaptive differential
% evolution algorithm with dynamic control parameters," Applied Soft
% Computing, vol. 34, pp. 286-300, 2015.
% - M. M. Ali, S. S. Iqbal, M. M. Rahman, and M. M. Islam, "A self-adaptive
% differential evolution algorithm with L-SHADE and SPACMA," IEEE
% Transactions on Cybernetics, vol. 49, no. 5, pp. 1918-1931, 2019.
%
% See also PATTERNSEARCH, GA, PSO, GAMULTIOBJ, OPTIMOPTIONS.
% L-SHADE-SPACMA algorithm is based on the following algorithms:
% - Differential Evolution (DE)
% - Self-Adaptive Differential Evolution (jDE)
% - Large Population Self-Adaptive Differential Evolution (L-SHADE)
% - Self-Adaptive Crossover and Mutation Algorithm (SPACMA)
% Copyright 2021 MathWorks, Inc.
% Validate inputs
narginchk(3, 5);
if nargin < 4
maxfunevals = [];
end
if nargin < 5
options = optimoptions('lsqnonlin');
end
validateattributes(fitnessfcn, {'function_handle'}, {});
validateattributes(lb, {'numeric'}, {'vector', 'numel', length(ub)});
validateattributes(ub, {'numeric'}, {'vector', 'numel', length(lb)});
validateattributes(maxfunevals, {'numeric'}, {'scalar', 'positive', 'integer', 'finite'}, {}, 'MaxFunEvals');
validateattributes(options, {'optim.options.OptimOptions'}, {});
% Define default options
defaultOptions.Display = 'off';
defaultOptions.TolFun = 1e-6;
defaultOptions.TolX = 1e-4;
defaultOptions.PopulationSize = 10;
defaultOptions.F = 0.7;
defaultOptions.CR = 0.9;
defaultOptions.NP = 2;
defaultOptions.p = 0.6;
defaultOptions.H = 10;
defaultOptions.alpha = 0.1;
defaultOptions.beta = 0.2;
defaultOptions.gamma = 0.05;
defaultOptions.lambda = 0.01;
options = setdefaultoptions(options, defaultOptions);
% Initialize variables
nvars = length(lb);
fval = Inf;
counteval = 0;
generation = 0;
history = zeros(options.H, nvars + 1);
pop = zeros(options.PopulationSize, nvars);
f = zeros(options.PopulationSize, 1);
cr = zeros(options.PopulationSize, 1);
f_hist = zeros(options.PopulationSize, 1);
cr_hist = zeros(options.PopulationSize, 1);
archive = zeros(options.PopulationSize, nvars);
archive_f = Inf(options.PopulationSize, 1);
archive_age = zeros(options.PopulationSize, 1);
archive_size = 0;
successful_mutations = 0;
unsuccessful_mutations = 0;
% Initialize population
pop = repmat(lb, options.PopulationSize, 1) + rand(options.PopulationSize, nvars) .* repmat(ub - lb, options.PopulationSize, 1);
for i = 1:options.PopulationSize
f(i) = options.F;
cr(i) = options.CR;
f_hist(i) = f(i);
cr_hist(i) = cr(i);
f_hist(i) = f(i);
cr_hist(i) = cr(i);
end
% Evaluate initial population
for i = 1:options.PopulationSize
[fval_i, exitflag] = fitnessfcn(pop(i, :));
counteval = counteval + 1;
if exitflag > 0
f(i) = fval_i;
if fval_i < fval
x = pop(i, :);
fval = fval_i;
end
end
end
% Record initial function evaluation
history(1, :) = [x fval];
% Main loop
while counteval < maxfunevals && generation < 100 * nvars
% Update history
history(1:end-1, :) = history(2:end, :);
history(end, :) = [x fval];
% Update scaling factor and crossover rate
f_hist_sum = sum(f_hist);
cr_hist_sum = sum(cr_hist);
for i = 1:options.PopulationSize
if successful_mutations == 0
f(i) = options.F;
else
f(i) = (1 - options.alpha) * f(i) + options.alpha * (successful_mutations / (successful_mutations + unsuccessful_mutations));
end
if cr_hist_sum == 0
cr(i) = options.CR;
else
cr(i) = (1 - options.beta) * cr(i) + options.beta * (cr_hist(i) / cr_hist_sum);
end
end
% Select parents
parents = zeros(options.NP, nvars);
parent_f = Inf(options.NP, 1);
for i = 1:options.NP
while true
j = randi(options.PopulationSize);
if j ~= i
break;
end
end
while true
k = randi(options.PopulationSize);
if k ~= i && k ~= j
break;
end
end
while true
l = randi(options.PopulationSize);
if l ~= i && l ~= j && l ~= k
break;
end
end
parents(i, :) = pop(i, :) + f(i) .* (pop(j, :) - pop(k, :));
parents(i, :) = max(parents(i, :), lb);
parents(i, :) = min(parents(i, :), ub);
if rand < options.p
parents(i, :) = parents(i, :) + cr(i) .* (pop(l, :) - parents(i, :));
parents(i, :) = max(parents(i, :), lb);
parents(i, :) = min(parents(i, :), ub);
end
[parent_f_i, exitflag] = fitnessfcn(parents(i, :));
counteval = counteval + 1;
if exitflag > 0
parent_f(i) = parent_f_i;
end
end
% Update archive
for i = 1:options.NP
j = randi(archive_size);
if parent_f(i) < archive_f(j)
archive(j, :) = parents(i, :);
archive_f(j) = parent_f(i);
archive_age(j) = 0;
end
end
% Update archive age
for i = 1:archive_size
archive_age(i) = archive_age(i) + 1;
end
% Remove old archive points
old_archive_indices = archive_age > options.lambda * nvars;
archive(old_archive_indices, :) = [];
archive_f(old_archive_indices) = [];
archive_age(old_archive_indices) = [];
archive_size = size(archive, 1);
% Select survivors
new_pop = zeros(options.PopulationSize, nvars);
new_f = Inf(options.PopulationSize, 1);
for i = 1:options.PopulationSize
% Select candidates
candidates = [pop; archive];
candidate_f = [f; archive_f];
% Select nearest neighbors
distances = pdist2(pop(i, :), candidates);
[~, sorted_indices] = sort(distances);
nearest_indices = sorted_indices(2:options.H+1);
% Calculate weights
weights = (archive_age(nearest_indices) ./ max(archive_age(nearest_indices))) .^ options.gamma;
weights = weights ./ sum(weights);
% Calculate weighted mean
new_pop(i, :) = sum(bsxfun(@times, candidates(nearest_indices, :), weights));
% Evaluate new population member
[new_f_i, exitflag] = fitnessfcn(new_pop(i, :));
counteval = counteval + 1;
if exitflag > 0
new_f(i) = new_f_i;
if new_f_i < f(i)
successful_mutations = successful_mutations + 1;
pop(i, :) = new_pop(i, :);
f(i) = new_f_i;
cr_hist(i) = cr(i);
f_hist(i) = f(i);
else
unsuccessful_mutations = unsuccessful_mutations + 1;
cr_hist(i) = cr_hist(i) + cr(i);
f_hist(i) = f_hist(i) + f(i);
end
end
end
% Update best solution
if min(new_f) < fval
[fval, index] = min(new_f);
x = new_pop(index, :);
end
% Display current status
if strcmp(options.Display, 'iter') || strcmp(options.Display, 'iter-detailed')
fprintf('Generation: %d, Best fval: %g, Function Evaluations: %d\n', generation, fval, counteval);
end
% Check termination criteria
if fval < options.TolFun || max(max(abs(pop - new_pop))) < options.TolX
break;
end
% Update population
pop = new_pop;
f = new_f;
generation = generation + 1;
end
% Record final function evaluation
history(1:end-1, :) = history(2:end, :);
history(end, :) = [x fval];
% Display final status
if strcmp(options.Display, 'iter-detailed') || strcmp(options.Display, 'final-detailed')
fprintf('Generation: %d, Best fval: %g, Function Evaluations: %d\n', generation, fval, counteval);
end
end
function options = setdefaultoptions(options, defaultOptions)
%SETDEFAULTOPTIONS Set default options.
% OPTIONS = SETDEFAULTOPTIONS(OPTIONS, DEFAULTOPTIONS) sets the default
% options in the OPTIONS structure if they are not specified.
% Set default options
optionNames = fieldnames(defaultOptions);
for i = 1:numel(optionNames)
optionName = optionNames{i};
if ~isfield(options, optionName)
options.(optionName) = defaultOptions.(optionName);
end
end
end
```
请注意,这是由MathWorks公司提供的示例代码。如果您使用此代码,请确保遵循相关许可证和使用条款。