-
Notifications
You must be signed in to change notification settings - Fork 10
/
gpdemo1.m
91 lines (82 loc) · 3.19 KB
/
gpdemo1.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
%GPDEMO1 GPTIPS 2 demo of simple symbolic regression on Koza's quartic polynomial.
%
% Demonstrates simple (naive) symbolic regression and some post run
% analysis functions such as SUMMARY, RUNTREE and GPPRETTY to simplify
% expressions if the Symbolic Math Toolbox is present. Also shows the use
% of DRAWTREES to visualise the tree structure of GPTIPS individuals.
%
% (c) Dominic Searson 2009-2015
%
% GPTIPS 2
%
% See also GPDEMO1_CONFIG, QUARTIC_FITFUN, GPDEMO2, GPDEMO3, GPDEMO4,
% SUMMARY, RUNTREE, GPPRETTY
clc;
disp('GPTIPS 2 Demo 1: naive symbolic regression');
disp('------------------------------------------');
disp('Naive symbolic regression on 20 data points genenerated by the');
disp('quartic polynomial y=x+x^2+x^3+x^4 in the range -1 < x < 1');
disp('The GP run configuration file is gpdemo1_config.m');
disp(' ');
disp('In this example, the direct output of a single evolved GP tree is used to');
disp('model the data generated by the quartic polynomial.');
disp('The function nodes used are TIMES, MINUS, PLUS and RDIVIDE.');
disp('The only terminal node used is the input x and trees are constrained');
disp('to have a maximum depth of 12.');
disp(' ');
disp('A population size of 50 is run for 100 generations.');
disp('The ''fitness'' is sum of absolute differences between the actual and');
disp('the predicted y values and GPTIPS tries to minimise this.');
disp(' ');
disp('First, call GPTIPS using the configuration in gpdemo1_config.m');
disp('using:');
disp('>>gp=rungp(@gpdemo1_config);');
disp('Press a key to continue');
disp(' ');
pause;
gp=rungp(@gpdemo1_config);
disp('Next, plot summary information of run using:');
disp('>>summary(gp)');
disp('Press a key to continue');
disp(' ');
pause;
summary(gp,false);
disp('Run the best model of the run on the fitness function using:');
disp('>>runtree(gp,''best'');');
disp('Press a key to continue');
disp(' ');
pause;
runtree(gp,'best');
disp(' ');
disp('The best model of the run is stored in the field:');
disp('gp.results.best.eval_individual{1} :');
disp(' ');
disp( gp.results.best.eval_individual{1});
disp(' ');
disp(['This model has a tree depth of ' int2str( getdepth(gp.results.best.individual{1}))]);
disp(['It was found at generation ' int2str(gp.results.best.foundatgen)]);
disp(['and has fitness ' num2str(gp.results.best.fitness)]);
%If Symbolic Math toolbox is present
if gp.info.toolbox.symbolic
disp(' ');
disp('Using the symbolic math toolbox simplified versions of this');
disp('expression can be found: ')
disp('E.g. using the the GPPRETTY command on the best model: ');
disp('>>gppretty(gp,''best'') ');
disp('Press a key to continue');
disp(' ');
pause;
gppretty(gp,'best');
disp(' ');
disp('If you are lucky then this is the quartic polynomial used to');
disp('generate the data.');
disp('NOTE: In general, it is unusual for GP to evolve the exact form');
disp('of the generative function.');
end
disp(' ');
disp('Next, visualise the tree structure of the best model of the run:');
disp('>>drawtrees(gp,''best'');');
disp('Press a key to continue');
disp(' ');
pause;
drawtrees(gp,'best');