-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathexampleexperiment.m
143 lines (128 loc) · 5.59 KB
/
exampleexperiment.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
%
% This script runs random search for BUDGET_MULTIPLIER*DIM function
% evaluations on a COCO suite and can serve also as a timing experiment.
%
% This example experiment allows also for easy implementation of independent
% restarts by simply increasing NUM_OF_INDEPENDENT_RESTARTS. To make this
% effective, the algorithm should have at least one more stopping criterion
% than just a maximal budget.
%
more off; % to get immediate output in Octave
%%%%%%%%%%%%%%%%%%%%%%%%%
% Experiment Parameters %
%%%%%%%%%%%%%%%%%%%%%%%%%
BUDGET_MULTIPLIER = 2; % algorithm runs for BUDGET_MULTIPLIER*dimension funevals
NUM_OF_INDEPENDENT_RESTARTS = 1e9; % max. number of independent algorithm
% restarts; if >0, make sure that the
% algorithm is not always doing the same thing
% in each run (which is typically trivial for
% randomized algorithms)
%%%%%%%%%%%%%%%%%%%%%%%%%
% Prepare Experiment %
%%%%%%%%%%%%%%%%%%%%%%%%%
% choose a test suite and a matching logger, for
% example one of the following:
%
% bbob 24 unconstrained noiseless single-objective functions
% bbob-biobj 55 unconstrained noiseless bi-objective functions
% [bbob-biobj-ext* 92 unconstrained noiseless bi-objective functions]
% bbob-largescale 24 unconstrained noiseless single-objective functions in large dimensions
% bbob-constrained 48 constrained noiseless single-objective functions
% bbob-mixint 24 unconstrained noiseless single-objective functions with mixed-integer variables
% bbob-biobj-mixint 92 unconstrained noiseless bi-objective functions with mixed-integer variables
% sbox-cost* 24 bound-constrained noiseless single-objective functions
%
% Suites with a star are partly implemented but not yet fully supported.
%
suite_name = 'bbob';
observer_name = 'bbob';
observer_options = strcat('result_folder: RS_on_', ...
suite_name, ...
[' algorithm_name: RS '...
' algorithm_info: A_simple_random_search ']);
% initialize suite and observer with default options,
% to change the default, see
% http://numbbo.github.io/coco-doc/C/#suite-parameters and
% http://numbbo.github.io/coco-doc/C/#observer-parameters
% for details.
suite = cocoSuite(suite_name, '', '');
observer = cocoObserver(observer_name, observer_options);
% set log level depending on how much output you want to see, e.g. 'warning'
% for fewer output than 'info'.
cocoSetLogLevel('info');
% keep track of problem dimension and #funevals to print timing information:
printeddim = 1;
doneEvalsAfter = 0; % summed function evaluations for a single problem
doneEvalsTotal = 0; % summed function evaluations per dimension
printstring = '\n'; % store strings to be printed until experiment is finished
%%%%%%%%%%%%%%%%%%%%%%%%%
% Run Experiment %
%%%%%%%%%%%%%%%%%%%%%%%%%
while true
% get next problem and dimension from the chosen suite:
problem = cocoSuiteGetNextProblem(suite, observer);
if ~cocoProblemIsValid(problem)
break;
end
dimension = cocoProblemGetDimension(problem);
% printing
if printeddim < dimension
if printeddim > 1
elapsedtime = toc;
printstring = strcat(printstring, ...
sprintf(' COCO TIMING: dimension %d finished in %e seconds/evaluation\n', ...
printeddim, elapsedtime/double(doneEvalsTotal)));
tic;
end
doneEvalsTotal = 0;
printeddim = dimension;
tic;
end
% restart functionality: do at most NUM_OF_INDEPENDENT_RESTARTS+1
% independent runs until budget is used:
i = -1; % count number of independent restarts
while (BUDGET_MULTIPLIER*dimension > (cocoProblemGetEvaluations(problem) + ...
cocoProblemGetEvaluationsConstraints(problem)))
% signal that a restart took place
cocoObserverSignalRestart(observer, problem)
i = i+1;
if (i > 0)
fprintf('INFO: algorithm restarted\n');
end
doneEvalsBefore = cocoProblemGetEvaluations(problem) + ...
cocoProblemGetEvaluationsConstraints(problem);
% start algorithm with remaining number of function evaluations:
my_optimizer(problem,...
cocoProblemGetSmallestValuesOfInterest(problem),...
cocoProblemGetLargestValuesOfInterest(problem),...
cocoProblemGetNumberOfIntegerVariables(problem),...
cocoProblemGetNumberOfConstraints(problem),...
BUDGET_MULTIPLIER*dimension - doneEvalsBefore);
% check whether things went wrong or whether experiment is over:
doneEvalsAfter = cocoProblemGetEvaluations(problem) + ...
cocoProblemGetEvaluationsConstraints(problem);
if cocoProblemFinalTargetHit(problem) == 1 ||...
doneEvalsAfter >= BUDGET_MULTIPLIER * dimension
break;
end
if (doneEvalsAfter == doneEvalsBefore)
fprintf('WARNING: Budget has not been exhausted (%d/%d evaluations done)!\n', ....
doneEvalsBefore, BUDGET_MULTIPLIER * dimension);
break;
end
if (doneEvalsAfter < doneEvalsBefore)
fprintf('ERROR: Something weird happened here which should not happen: f-evaluations decreased');
end
if (i >= NUM_OF_INDEPENDENT_RESTARTS)
break;
end
end
doneEvalsTotal = doneEvalsTotal + doneEvalsAfter;
end
elapsedtime = toc;
printstring = strcat(printstring, ...
sprintf(' COCO TIMING: dimension %d finished in %e seconds/evaluation\n', ...
printeddim, elapsedtime/double(doneEvalsTotal)));
fprintf(printstring);
cocoObserverFree(observer);
cocoSuiteFree(suite);