Shark machine learning library
About Shark
News!
Contribute
Credits and copyright
Downloads
Getting Started
Installation
Using the docs
Documentation
Tutorials
Quick references
Class list
Global functions
FAQ
Showroom
obj-x86_64-linux-gnu
examples
EA
SOO
Archive.cpp
Go to the documentation of this file.
1
/*!
2
*
3
*
4
* \brief Demonstration of the archive fitness function wrapper.
5
*
6
*
7
*
8
* \author Tobias Glasmachers
9
* \date 2013
10
*
11
*
12
* \par Copyright 1995-2015 Shark Development Team
13
*
14
* <BR><HR>
15
* This file is part of Shark.
16
* <http://image.diku.dk/shark/>
17
*
18
* Shark is free software: you can redistribute it and/or modify
19
* it under the terms of the GNU Lesser General Public License as published
20
* by the Free Software Foundation, either version 3 of the License, or
21
* (at your option) any later version.
22
*
23
* Shark is distributed in the hope that it will be useful,
24
* but WITHOUT ANY WARRANTY; without even the implied warranty of
25
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
* GNU Lesser General Public License for more details.
27
*
28
* You should have received a copy of the GNU Lesser General Public License
29
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
30
*
31
*/
32
33
#include <
shark/Algorithms/DirectSearch/CMA.h
>
34
#include <
shark/ObjectiveFunctions/Benchmarks/Benchmarks.h
>
35
#include <
shark/ObjectiveFunctions/EvaluationArchive.h
>
36
37
38
using namespace
shark
;
39
using namespace
std
;
40
41
42
int
main
() {
43
cout.setf( ios_base::scientific );
44
cout.precision( 10 );
45
46
// Instantiate the problem
47
Sphere
sphere( 2 );
48
sphere.
setNumberOfVariables
( 2 );
49
50
// Create an archive object as a wrapper around the problem
51
typedef
EvaluationArchive< RealVector, double>
ArchiveType;
52
typedef
EvaluationArchive< RealVector, double>::PointResultPairConstIterator
ArchiveIteratorType;
53
ArchiveType wrapper(&sphere);
54
55
// Initialize the optimizer for the objective function instance.
56
CMA
cma;
57
cma.
init
( wrapper );
58
59
// Iterate the optimizer until a solution of sufficient quality is found.
60
const
double
target = 1e-4;
61
cout <<
"Optimize the sphere benchmark problem to target accuracy "
<< target << endl;
62
do
{
63
// Note the use of the wrapper instead of the fitness function:
64
cma.
step
( wrapper );
65
66
// Report information on the optimizer state and the current solution to the console.
67
cout << sphere.
evaluationCounter
() <<
" "
68
<< cma.
solution
().
value
<<
" "
69
<< cma.
solution
().
point
<<
" "
70
<< cma.
sigma
() << endl;
71
}
while
(cma.
solution
().
value
> target);
72
73
// output archive contents (all visited search points)
74
size_t
N = wrapper.size();
75
cout << endl;
76
cout <<
"The archive contains "
<< N <<
" evaluated search points:"
<< endl;
77
for
(ArchiveIteratorType it=wrapper.begin(); it != wrapper.end(); ++it)
78
{
79
RealVector
const
& x = it->point;
80
double
fx = it->result;
81
cout <<
" f( "
<< x <<
" ) = "
<< fx << endl;
82
}
83
}