📄 ecga.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% File: eCGA.m%% Description: The main loop of eCGA. %% Author: Kumara Sastry%% Date: March 2007%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%clear;% Get the user defined parameter values;parameters = userDefinedParameters();% Number of bitsell = parameters.NumVariables;% Population sizen = parameters.PopulationSize;% Alphabet cardinalitiesranges(1:ell) = parameters.Cardinalities;% Selection Methodselection = parameters.Selection;selectionMethod = selection{1};if(strcmpi(selectionMethod,'TRUNCATION')) truncParam = selection{2};else tournSize = selection{2};end% Replacement stragegy: Default is generational replacementreplacement = parameters.Replacement;replacementMethod = replacement{1};if(strcmpi(replacementMethod, 'RTR')) windowSize = replacement{2};end% Convergence critieria parameters convergenceMethods = parameters.Convergence{1};convergenceParameters = parameters.Convergence{2};% See if any of the convergence criteria are set:% 1. Variance of fitness is below a threshold (might not be useful if% the fitness is noisy)% 2. If the population has converged to a single solution% 3. If the best fitness is greater than the maximum fitness desired% 4. If the generation no. is greater than the maximum number of% generations.convergenceCriteria = strcmpi(convergenceMethods, {'MAXGEN', 'MAXFIT', 'FITVAR', 'POPVAR'});% Maximum number of generationsif(convergenceCriteria(1)) maxGen = convergenceParameters{1}; else maxGen = 10*ell;end% Maximum fitness value desired (e.g., fitness of the optimal solution.if(convergenceCriteria(2)) maxFit = convergenceParameters{2}; else maxFit = inf;end% Threshold for fitness variance below which the population is assumed to% be convergedif(convergenceCriteria(3)) maxFitVar = convergenceParameters{3}; else maxFitVar = 0.0;end% Threshold for the unique number of individuals in the converged% population.if(convergenceCriteria(4)) maxPopVar = convergenceParameters{4}; %else maxPopVar = 1;end%--------------------------------------------------------------------------% Start with eCGA%--------------------------------------------------------------------------% Initialize the population randomlypopulation = (rand(n,ell) > 0.5);% Evaluate the fitness of the initial populationfitness = evaluateIndividuals(ranges, population);% Set the convergence flag to falseisConverged = false;gen = 0;[bestFit, bestID] = max(fitness);bestSolution = population(bestID,:);% While the convergence criteria is not metwhile ~isConverged, % Perform tournament selection and determine the mating pool if(strcmpi(selectionMethod,'TRUNCATION')) winners = truncationSelection(truncParam, fitness); else winners = tournamentSelection(tournSize, fitness); end % Build the marginal product model mpm = buildModel(ranges, population(winners,:)); % Sample the new population using the MPM newPopulation = sampleModel(mpm, n); % Evaluate the fitness of the new individuals newFitness = evaluateIndividuals(ranges, newPopulation); % Determine the current best solution and fitness [currentBestFit, bestID] = max(fitness); currentBestSolution = population(bestID, :); % If the current best is better than the best-so-far replace the % best-so-far solution with the current best if(currentBestFit > bestFit) bestFit = currentBestFit; bestSolution = currentBestSolution; end % Replacement Strategies: % 1. Restricted tournament replacement % 2. Elitist Lambda+Mu Replacement % 3. Generational replacement if(strcmpi(replacementMethod,'RTR')) [population, fitness] = restrictedTournamentReplacement(population, fitness, newPopulation, newFitness, windowSize); elseif (strcmpi(replacementMethod,'ELITIST')) [population, fitness] = elitistReplacement(population, fitness, newPopulation, newFitness); else % DEFAULT: Generational replacement strategy population = newPopulation; fitness = newFitness; end % See if any of the convergence criteria are met: if((gen > maxGen) || (bestFit >= maxFit) || (var(fitness) < maxFitVar) || (size(unique(population,'rows'),1) <= maxPopVar)) isConverged = true; end gen = gen+1; % Display the model, best-so-far solution and its fitness displayResults(gen, mpm, bestSolution, bestFit);end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -