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

📄 floatexample.m

📁 实数编码的遗传算法
💻 M
字号:
echo on
% This script shows how to use the ga using a float representation. 
global bounds

% Setting the seed to the same for binary
rand('seed',156789)

% Crossover Operators
xFns = 'arithXover';
%xOpts = [4 40];
%xOpts = [5 100];
xOpts = [8 100];

% Mutation Operators
mFns = 'nonUnifMutation';
%mOpts = [2 40 3];
%mOpts = [3 100 3];
mOpts = [15 100 3];


% Termination Operators
termFns = 'maxGenTerm';
%termOps = [40]; % 40 Generations
termOps = [160];
% Selection Function
selectFn = 'normGeomSelect';
%selectOps = [0.08];
selectOps = [0.08];

% Evaluation Function
evalFn = 'exampleFn';
evalOps = [];

type exampleFn

% Bounds on the variables
%bounds = [-0.8 0];
bounds = [-0.08 1.2];

% GA Options [epsilon float/binar display]
gaOpts=[1e-6 1 1];

% Generate an intialize population of size 20
%startPop = initializega(20,bounds,'exampleFn',[1e-6 1])
%startPop = initializega(20,bounds,'exampleFn',evalOps,[1e-6 1])

% Lets run the GA
% Hit a return to continue
%pause


%[x endPop bestPop trace]=ga(bounds,evalFn,evalOps,startPop,gaOpts,...
%    termFns,termOps,selectFn,selectOps,xFns,xOpts,mFns,mOpts);


% x is the best solution found
%x
% Hit a return to continue
%pause


% endPop is the ending population
%endPop
% Hit a return to continue
%pause


% bestPop is the best solution tracked over generations
%bestPop
% Hit a return to continue
%pause


% trace is a trace of the best value and average value of generations
%trace
% Hit a return to continue
%pause


% Plot the best over time
%clf
%plot(trace(:,1),trace(:,2));
% Hit a return to continue
%pause


% Add the average to the graph
%hold on

%for k=1:160
%   trace(k,3)=1.0/trace(k,3);
%end


%plot(trace(:,1),trace(:,3));
% Hit a return to continue
%pause


% Lets increase the population size by running the defaults
% 

[x endPop bestPop trace]=ga(bounds,evalFn,evalOps,[],gaOpts,...
    termFns,termOps,selectFn,selectOps,xFns,xOpts,mFns,mOpts);

% x is the best solution found
x
numVar=size(x,2)-1
%for i=1:numVar
%save sxx.txt x(:,i) -ascii -double
save sxx.txt x -ascii -double

%end


% Hit a return to continue
pause


% endPop is the ending population
endPop
% Hit a return to continue
pause


% bestPop is the best solution tracked over generations
bestPop
% Hit a return to continue
pause


% trace is a trace of the best value and average value of generations
trace
% Hit a return to continue
pause


% Plot the best over time
%clf
%plot(trace(:,1),trace(:,2));
%plot(trace(:,1),1.0/trace(:,2));
% Hit a return to continue
pause


% Add the average to the graph
%hold on
%plot(trace(:,1),trace(:,3));
for k=1:160
   trace(k,3)=1.0/trace(k,3);
end

plot(trace(:,1),trace(:,3));



echo off

⌨️ 快捷键说明

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