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-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
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 
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;
53  ArchiveType wrapper(&sphere);
54 
55  // Initialize the optimizer for the objective function instance.
56  CMA cma;
57  wrapper.init();
58  cma.init( wrapper );
59 
60  // Iterate the optimizer until a solution of sufficient quality is found.
61  const double target = 1e-4;
62  cout << "Optimize the sphere benchmark problem to target accuracy " << target << endl;
63  do {
64  // Note the use of the wrapper instead of the fitness function:
65  cma.step( wrapper );
66 
67  // Report information on the optimizer state and the current solution to the console.
68  cout << sphere.evaluationCounter() << " "
69  << cma.solution().value << " "
70  << cma.solution().point << " "
71  << cma.sigma() << endl;
72  } while (cma.solution().value > target);
73 
74  // output archive contents (all visited search points)
75  size_t N = wrapper.size();
76  cout << endl;
77  cout << "The archive contains " << N << " evaluated search points:" << endl;
78  for (ArchiveIteratorType it=wrapper.begin(); it != wrapper.end(); ++it)
79  {
80  RealVector const& x = it->point;
81  double fx = it->result;
82  cout << " f( " << x << " ) = " << fx << endl;
83  }
84 }