-
Notifications
You must be signed in to change notification settings - Fork 20
/
binaryExample.m
108 lines (83 loc) · 2.03 KB
/
binaryExample.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
echo on
% This script shows how to use the ga using a float representation.
% You should see the demos for
% more information as well. gademo1, gademo2, gademo3
global bounds
% Setting the seed back to the beginning for comparison sake
rand('seed',0)
% Crossover Operators
xFns = 'simpleXover';
xOpts = [.4];
% Mutation Operators
mFns = 'binaryMutation';
mOpts = [0.005];
% Termination Operators
termFns = 'maxGenTerm';
termOps = [200]; % 200 Generations
% Selection Function
selectFn = 'roulette'
selectOps = [];
% Evaluation Function
evalFn = 'gaMichEval';
evalOps = [];
type gaMichEval
% Bounds on the variables
bounds = [-3 12.1; 4.1 5.8];
% GA Options [epsilon float/binar display]
gaOpts=[1e-6 0 1];
% Generate an intialize population of size 20
startPop = initializega(20,bounds,'gaMichEval',[],[1e-6 0]);
% 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
% 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
plot(trace(:,1),trace(:,3));
% Hit a return to continue
pause
% Lets increase the population size by running the defaults
%
rand('seed',0)
termOps=[100];
[x endPop bestPop trace]=ga(bounds,evalFn,evalOps,[],gaOpts,termFns,termOps,...
selectFn,selectOps);
% 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
% 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
plot(trace(:,1),trace(:,3));
echo off