function W¼FITNESS(x,As,Bs) % Function to evaluate fitness given
Alpha
Af¼2; Bf¼2; Ca¼1/0.7; Cb¼5; % Invariant parameter values
W ¼ mean((AfþBf*x)*min(max((AsBs*x),0),1)*Cb*Ca); % Fitness
vector
W ¼W; % Return negative of fitness
Main Program:
clear all; % Clear the workspace
Amin ¼ 0.3; Amax ¼ 1; % Min and max values of As
Bmin ¼ 0; Bmax ¼ 0.2; % Min and max values of Bs
% Calculate n parameter combinations
n ¼ 10000; % Number of values of As and Bs to generate
% We are assuming a uniform distribution of values
% Make several runs. Here we use 10
REP ¼ zeros(10,1); % Create matrix to hold replicate
% We are assuming a uniform distribution of values
rand(’twister’, 100); % Set the random number seed
for i ¼ 1:10 % Iterate over replicates
Bs ¼ Bminþ(Bmax-Bmin)*rand(n,1); % Vector of values of Bf
As ¼ Aminþ(Amax-Amin)*rand(n,1); % Vector of values of As
REP(i)¼ fminsearch(@(x) FITNESS(x,As,Bs), 2); % Pass As,Bs as
well as x
end
[mean(REP) std(REP)] % Print mean and standard deviation
OUTPUT:
ans ¼ 3.3786 0.0516
2.18.16 Scenario 7: Plotting the fitness function
Differs slightly from R code in that W rather than log W is returned.
function W¼FITNESS(x) % Function to evaluate fitness
Af ¼ 2; Bf ¼ 2;As¼ 0.6; % Parameter values
pBs ¼ [0.1,0.3,0.4,0.2]; % Vector of probabilities for Bs
Bs ¼ [0.1,0.12,0.14,0.2]; % Vector of Bs values
W_ind ¼ (AfþBf*x)*(AsBs*x); % Fitness values for each Bs value
% log Fitness. Note use of “.” to denote element by element multiply
log_W ¼ -sum(pBs.*log(W_ind));
W ¼ exp(-log_W); % Send back fitness
Main Program:
clear all; % Clear the workspace
fplot(@FITNESS, [0,2.999]) % Plot. Note W ¼INF at x¼3
xlabel(’SIZE’); ylabel(’FITNESS’);
148 MODELING EVOLUTION