-
Notifications
You must be signed in to change notification settings - Fork 2
/
UpdateFitnesses.m
121 lines (103 loc) · 6.65 KB
/
UpdateFitnesses.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
function [fitnessMatrix, preyPopulation, preyFitnesses, predatorPopulation, predatorFitnesses] = UpdateFitnesses(fitnessMatrix, ...
preyPopulation, nPreyAgents, maxPreyTurningAngle, preyStepLength, ...
nPreyNNInputs, nPreyNNHidden, nPreyNNOutputs, ...
predatorPopulation, nPredatorAgents, maxPredatorTurningAngle, predatorStepLength, ...
nPredatorNNInputs, nPredatorNNHidden, nPredatorNNOutputs, ...
nAgentNeighbors, deltaT, maxTime, fieldSize, captureDistance, nCompetitions, gen)
% run a number of simulations to fill in the missing fitnesses in our
% fitnessMatrix, then record a competition between
fprintf("\nGeneration %d\n----\n", gen);
[rows, cols] = find(~fitnessMatrix); % selects all (i,j) where fitnessMatrix(i,j)==0
preyPopulationParfor = preyPopulation(rows,:);
predatorPopulationParfor = predatorPopulation(cols,:);
fitnessParfor = zeros(1, length(rows));
parfor i = 1:length(rows)
fprintf("Evaluating prey %2d vs predator %2d ...\n", rows(i), cols(i));
fitness = 0;
for n = 1:nCompetitions
thisFitness = Compete(preyPopulationParfor(i,:), ...
nPreyAgents, maxPreyTurningAngle, preyStepLength, ...
nPreyNNInputs, nPreyNNHidden, nPreyNNOutputs, ...
predatorPopulationParfor(i,:), ...
nPredatorAgents, maxPredatorTurningAngle, predatorStepLength, ...
nPredatorNNInputs, nPredatorNNHidden, nPredatorNNOutputs, ...
nAgentNeighbors, deltaT, maxTime, fieldSize, captureDistance, gen, false);
fitness = fitness + thisFitness;
end
fitnessParfor(i) = fitness/nCompetitions;
end
for i = 1:length(rows)
row = rows(i);
col = cols(i);
fitnessMatrix(row,col) = fitnessParfor(i);
end
preyFitnesses = mean(fitnessMatrix, 2)';
predatorFitnesses = -mean(fitnessMatrix, 1);
[fitnessMatrix, preyPopulation, preyFitnesses, predatorPopulation, predatorFitnesses] = SortPopulation(fitnessMatrix, preyPopulation, preyFitnesses, predatorPopulation, predatorFitnesses);
% this run competes the best prey v best predator and records the competition
[~, simStats, preyStats, predatorStats]= Compete(preyPopulation(1,:), ...
nPreyAgents, maxPreyTurningAngle, preyStepLength, ...
nPreyNNInputs, nPreyNNHidden, nPreyNNOutputs, ...
predatorPopulation(1,:), ...
nPredatorAgents, maxPredatorTurningAngle, predatorStepLength, ...
nPredatorNNInputs, nPredatorNNHidden, nPredatorNNOutputs, ...
nAgentNeighbors, deltaT, maxTime, fieldSize, captureDistance, gen, true);
grandparentFolderName = 'Simulation Data 2';
parentFolderName = sprintf('%04d-%03d-%02d-%.2f--%.2f-%d', ...
preyStats.nAgents, predatorStats.nAgents, ...
preyStats.nFriendlyNeighbors, predatorStats.speed, ...
simStats.deltaT, simStats.maxTime);
folderName = sprintf('Generation %d', simStats.generation);
if ispc
filePath = [pwd, '\', grandparentFolderName, '\', parentFolderName, '\', folderName, '\'];
elseif isunix
filePath = [pwd, '/', grandparentFolderName, '/', parentFolderName, '/', folderName, '/'];
end
mkdir(filePath);
fileName = sprintf('DataGeneration %d',simStats.generation)
save(strcat(filePath,fileName),'simStats', 'preyStats', 'predatorStats')
% preyPassiveStats = SimulatePassiveBM(simStats, preyStats);
% predatorPassiveStats = SimulatePassiveBM(simStats, predatorStats);
% preyActiveStats = SimulateActiveBM(simStats, preyStats);
% predatorActiveStats = SimulateActiveBM(simStats, predatorStats);
% [preyBoidStats, predatorBoidStats] = SimulateBoids(simStats, preyStats, predatorStats, fieldSize);
%
% tic;
% PlotSimulation(filePath, simStats, preyStats, predatorStats, preyBoidStats, predatorBoidStats, fieldSize);
% PlotDiffusion(filePath, 'Prey', simStats, preyPassiveStats, preyActiveStats, preyBoidStats, preyStats);
% PlotDiffusion(filePath, 'Predator', simStats, predatorPassiveStats, predatorActiveStats, predatorBoidStats, predatorStats);
% PlotMSD(filePath, 'Prey', simStats, preyPassiveStats, preyActiveStats, preyBoidStats, preyStats);
% PlotMSD(filePath, 'Predator', simStats, predatorPassiveStats, predatorActiveStats, predatorBoidStats, predatorStats);
% toc
if (gen < 1000 && mod(gen, 50)==0) || (mod(gen, 500)==0)
% this run competes the best prey v best predator and records the competition
[timeElapsed, simStats, preyStats, predatorStats]= Compete(preyPopulation(1,:), ...
nPreyAgents, maxPreyTurningAngle, preyStepLength, ...
nPreyNNInputs, nPreyNNHidden, nPreyNNOutputs, ...
predatorPopulation(1,:), ...
nPredatorAgents, maxPredatorTurningAngle, predatorStepLength, ...
nPredatorNNInputs, nPredatorNNHidden, nPredatorNNOutputs, ...
nAgentNeighbors, deltaT, maxTime, fieldSize, captureDistance, gen, true);
%preyPassiveStats = SimulatePassiveBM(simStats, preyStats);
%predatorPassiveStats = SimulatePassiveBM(simStats, predatorStats);
preyActiveStats = SimulateActiveBM(simStats, preyStats);
predatorActiveStats = SimulateActiveBM(simStats, predatorStats);
[preyBoidStats, predatorBoidStats] = SimulateBoids(simStats, preyStats, predatorStats, fieldSize);
grandparentFolderName = 'Simulation Data';
parentFolderName = sprintf('%04d-%03d-%02d-%.2f--%.2f-%d', ...
preyStats.nAgents, predatorStats.nAgents, ...
preyStats.nFriendlyNeighbors, predatorStats.speed, ...
simStats.deltaT, simStats.maxTime);
folderName = sprintf('Generation %d', simStats.generation);
if ispc
filePath = [pwd, '\', grandparentFolderName, '\', parentFolderName, '\', folderName, '\'];
elseif isunix
filePath = [pwd, '/', grandparentFolderName, '/', parentFolderName, '/', folderName, '/'];
end
mkdir(filePath);
PlotSimulation(filePath, simStats, preyStats, predatorStats, preyBoidStats, predatorBoidStats, fieldSize);
PlotMSD(filePath, 'Prey', simStats, preyActiveStats, preyBoidStats, preyStats);
PlotMSD(filePath, 'Predator', simStats, predatorActiveStats, predatorBoidStats, predatorStats);
%PlotDiffusion(filePath, 'Prey', simStats, preyPassiveStats, preyActiveStats, preyBoidStats, preyStats);
%PlotDiffusion(filePath, 'Predator', simStats, predatorPassiveStats, predatorActiveStats, predatorBoidStats, predatorStats);
end