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

📄 genetic.m

📁 一系列好用的用户友好的启发式优化算法
💻 M
📖 第 1 页 / 共 2 页
字号:
            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', fitproper(inds));
                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

        %==================================================================
        % reproduce
        %==================================================================
        rands      = rand(1, popsize);        
        rands      = rands(poprep, :);
        newpopinds = fitcum(:, poprep);        
        newpopinds = newpopinds > rands;
        newpopinds = sum(newpopinds) + poprep;
        newpopinds(newpopinds > popsize) = popsize;
        pop        = pop(newpopinds', :); 

        %==================================================================
        % crossover
        %==================================================================
   
        % generate parents indices              
        parentsinds = 1;
        while (rem(sum(parentsinds), 2) > 0 || (sum(parentsinds) == 0))  
            parentsinds = rand(popsize, 1) < crossprob;                 
        end        
        parents     = pop(parentsinds, :);
        parentsinds = popvec(parentsinds);

        % randomize order of parents
        [dummy, inds] = sort(rand(size(parents, 1), 1), 1);
        parents       = parents(inds, :);
        
        % separate sexes
        numparents = size(parents, 1);
        fathers    = parents(1:2:numparents, :);
        mothers    = parents(2:2:numparents, :);

        % determine crossoverpoints
        numcrosses = numparents / 2;
        crosspos   = round( (dims-1)*rand( numcrosses, 1 ) + 1 ); 
        crosspos   = numcrosses * (crosspos - 1) + (1:numcrosses)';
        tempmatrix = zeros(size(mothers));
        tempmatrix(crosspos) = true;
        crosspos   = cumsum(tempmatrix, 2);

        % spawn children      
        daughters  = ~crosspos .* mothers;                    
        daughters  = daughters + fathers .* crosspos;       
        sons       = ~crosspos .* fathers;
        sons       = sons + mothers .* crosspos;
        children   = [daughters; sons];       
        
        %==================================================================
        %  mutation
        %==================================================================
        mutations   = rand(popsize, dims).*range + mins; 
        mutind      = rand(popsize, dims) < mutationprob;
        pop(mutind) = mutations(mutind);
        
        %==================================================================
        % selection 
        %==================================================================
        
        % get fitnesses
        parentsfit  = fitnesses(parentsinds);
        childrenfit = feval(func, children);
        childrenfit = childrenfit / sum(childrenfit(:));
        
        % increase function evaluations
        evals = evals + numel(childrenfit);
        
        % select the good kids
        goodkids = childrenfit < parentsfit;
       
        % select the bad kids
        badkids    = ~goodkids;
        numbadkids = sum(badkids);
        
        % kill the bad kids, and replace them with immigrants 
        if (numbadkids > 0)
            immigrants = rand(numbadkids, dims) .* ...
                         range(1:numbadkids, :) + mins(1:numbadkids, :);  
        else
            immigrants = [];
        end            
        
        % swap bad parents with immigrants, good parents with their kids
        pop(parentsinds(goodkids), :) = children(goodkids, :); 
        pop(parentsinds(badkids), :)  = immigrants;   
        
    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 = fitproper;
            sol  = pop;
        end
        
        % display final message
        if display
            fprintf(1, '\nGenetic Algorithm has converged.\n');
            pause(0.05)
        end
        
    % all trials might be infeasible 
    else
        fprintf(1,'\n');
        warning('genetic:no_solution',...
              'GENETIC 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 temp globals
    clear global GENETIC_bestfval GENETIC_bestind
    
end

% parser function to easily parse the input arguments
function [pop, func, popsize, lb, ub, grace, display, maxfevals, convmethod,...
          convvalue, mutationprob, crossprob] = 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.GA.pop;
        mutationprob = problem.GA.mutprob;
        crossprob    = problem.GA.crossprob;       
              
end

⌨️ 快捷键说明

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