⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diffevolve.m

📁 一系列好用的用户友好的启发式优化算法
💻 M
📖 第 1 页 / 共 2 页
字号:
                  'gave the following error:\n %s'], lasterr);
        end
        if (numel(fitnew) ~= popsize)
            error('diffevolve:function_not_vectorized_or_incorrect_dimensions', ...
                 ['The user-supplied cost function does not appear to get enough arguments,\n',...
                  'or is not vectorized properly. Make sure the dimensions of the limits [Lb]\n',...
                  'and [Ub] are the same as the required input vector for the function, or that\n',...
                  'the function is vectorized properly.'])            
        end
        prevpop = newpop;
        pop(fitnew < fitold, :) = newpop(fitnew < fitold, :);
        
        % increase number of function evaluations
        evals = evals + numel(fitnew);
        
        % improving solutions
        [bestindfit, ind] = min(fitnew);
        bestind = newpop(ind, :);        
        if (bestindfit < oldbestfit)
            
            % new best individual
            oldbestfit  = bestindfit;
            oldbestind  = bestind;
            improvement = maxiterinpop;
            
            % assign also the globals
            DIFFEVOLVE_bestind  = bestind;
            DIFFEVOLVE_bestfval = bestindfit;
            
            % display improving solution
            if display                
                if converging1
                   fprintf(1, converge1bck);
                   pause(0.05)
                end
                if converging2
                   fprintf(1, converge2bck);
                   pause(0.05)
                end  
                if (oldbestfit < 0)
                    fprintf('\b')
                end
                fprintf(1, fitbackspace);
                fprintf(1, '%1.8e', oldbestfit);
                converging1 = false;
                firsttime   = true;
                pause(0.05)
            end 
            
        end
                
        % Check for convergence
        if (evals > maxfevals)
            
            % maximum allowed function evaluations has been superceded
            fprintf(overfevals1);
            fprintf(overfevals2);            
            break
            
        end
        switch convergence
            
            % exhaustive 
            case 1
                if (oldbestfit <= bestindfit) && (oldbestfit ~= inf)
                    if (improvement <= 100) && display
                        if ~converging1
                            fprintf(1, convergestr);
                            fprintf(1, '%3.0f', improvement-1);
                            converging1 = true;
                            pause(0.05)
                        else
                            fprintf(1, '\b\b\b%3.0f', improvement-1);
                            pause(0.05)
                        end    
                    end           
                    improvement = improvement - 1;
                end
                
            % max iterations 
            case 2                   
                if display && (maxiters-iters < 100)            
                    if firsttime                
                        fprintf(1, itersstr);
                        fprintf(1, '%3.0f', maxiters-iters);                
                        pause(0.05)
                    else
                        fprintf(1, '\b\b\b%3.0f', maxiters-iters);
                        pause(0.05)
                    end
                    firsttime   = false;
                    converging2 = true;
                end  
                iters = iters + 1;
                
            % goal-attain
            case 3
                if (oldbestfit <= convvalue)
                    break;
                end
        end 

        % Transversal Differential Evolution 
        for i = 1:popsize
            for j = 1:n
                base = 1;  d1 = 1;  d2 = 1;
                while base == d1 || base == d2 || d1 == d2
                    base = round(rand*(popsize-1))+1;
                    d1   = round(rand*(popsize-1))+1;
                    d2   = round(rand*(popsize-1))+1;
                end
                replace = rand < crossconst | rand == i;
                if replace
                    F            = (Fub-Flb)*rand + Flb;
                    newpop(i, :) = pop(base, :) +  F*(pop(d1, :) - pop(d2, :));
                else
                    newpop(i, :) = pop(i, :);
                end                
            end
        end        
        
        % enforce boundaries
        outsiders1 = (newpop < mins);
        outsiders2 = (newpop > maxs);
        if any(outsiders1(:)) || any(outsiders2(:))
            reinit = rand(sizepop).*range + mins; 
            newpop(outsiders1) = reinit(outsiders1);
            newpop(outsiders2) = reinit(outsiders2);
        end
        
    end
    
%%  (pre-) end values

    % if solution has been found
    if isfinite(oldbestfit)
        
        % when called normally
        if (~skippop)
            fval = oldbestfit;
            sol  = oldbestind;
            
        % when called from GODLIKE
        else
            
            fval = fitnew;
            sol  = prevpop;
        end
        
        % display final message
        if display
            fprintf(1, '\nDifferential Evolution optimization algorithm has converged.\n');
            pause(0.05)
        end
        
    % all trials might be infeasible 
    else
        fprintf(1,'\n');
        warning('diffevolve:no_solution',...
              'DIFFEVOLVE was unable to find any solution that gave finite values.')
        fval = oldbestfit;
        sol  = NaN;
    end
    
%%  Grace function evaluations

    if (grace > 0)
        
        % display progress
        if display
            fprintf(1, 'Performing direct-search...');
            pause(0.05)
        end
        
        % perform direct search
        options = optimset('TolX', eps, 'MaxFunEvals', grace, 'TolFun', ...
                           eps, 'MaxIter', 1e4, 'Display', 'off');
        [soltry, fvaltry] = fminsearch(func, sol, options);
        
        % enforce boundaries
        if ~any(soltry >= ub | soltry <= lb)
           sol  = soltry;
           fval = fvaltry;
        end    
        evals = evals + grace;        
    end
    
%%  finalize
    
    % display progress
    if display
        fprintf(1, 'All done.\n\n');
        pause(0.05)
    end  
    
    % clear the temp globals
    clear global DIFFEVOLVE_bestind DIFFEVOLVE_bestfval

end

% parser function to easily parse the input arguments
function [pop, func, popsize, lb, ub, grace, display, maxfevals, convmethod, ...
        convvalue, crossconst, Flb, Fub, n] = parseprob(problem)

    func       = problem.costfun;
    popsize    = problem.popsize; 
    lb         = problem.lb;
    ub         = problem.ub; 
    grace      = problem.grace;
    display    = problem.display; 
    maxfevals  = problem.maxfevals;
    convmethod = problem.conv.method;
    convvalue  = problem.conv.value;  
    pop        = problem.DE.pop;
    crossconst = problem.DE.crossconst;   
    Flb        = problem.DE.Flb;
    Fub        = problem.DE.Fub;
    n          = problem.DE.n;
    
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -