-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyGPModel.m
49 lines (38 loc) · 1.64 KB
/
MyGPModel.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
classdef MyGPModel < GPModel
properties
baseline
end
methods
function model = MyGPModel()
model = model@GPModel(@GPMember);
end
function beforeRun(model, varargin)
baselineRoot = getBuyAndHoldGP();
model.baseline = backtest(baselineRoot, ...
varargin{1});
fprintf("Buy-and-hold strategy on training set: %f\n", model.baseline);
end
function fitnesses = evaluateFitness(model, varargin)
% Localize variables to avoid unnessary communication overhead.
% Skip this step if you do not use parallel computing toolbox.
n = model.options.populationSize;
% Retrieve the vairables initially provided when calling the
% run method.
data = varargin{1};
% Spread out the variable to avoid communication overhead when
% using parallel computing toolbox
population = model.population;
fitnesses = zeros(n, 1);
% Get status information from the model's status struct
gen = model.status.generation;
% Execute using MATLAB's parallel computing toolbox. If you do
% not have the license for it, replace `parfor` with `for`.
parfor s=1:n
r = backtest(population{s}, data);
fprintf("[Generation %d] Strategy %d return: %s\n", ...
gen, s, string(r));
fitnesses(s) = r;
end
end
end
end