ElitistCMASimple.cpp
Go to the documentation of this file.
1/*!
2 *
3 *
4 * \brief Example for running CMA-ES on an exemplary benchmark function.
5
6 *
7 *
8 * \author tvoss
9 * \date -
10 *
11 *
12 * \par Copyright 1995-2017 Shark Development Team
13 *
14 * <BR><HR>
15 * This file is part of Shark.
16 * <https://shark-ml.github.io/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 */
34
35int main( int argc, char ** argv ) {
36
37 // Adjust the floating-point format to scientific and increase output precision.
38 std::cout.setf( std::ios_base::scientific );
39 std::cout.precision( 10 );
40
41 // Instantiate both the problem and the optimizer.
42 shark::benchmarks::Sphere sphere( 2 );
43 sphere.setNumberOfVariables( 2 );
45
46 // Initialize the optimizer for the objective function instance.
47 sphere.init();
48 cma.init( sphere );
49
50 // Iterate the optimizer until a solution of sufficient quality is found.
51 do {
52
53 cma.step( sphere );
54
55 // Report information on the optimizer state and the current solution to the console.
56 std::cout << sphere.evaluationCounter() << " "
57 << cma.solution().value << " "
58 << cma.solution().point << " "
59 << cma.sigma() << std::endl;
60 }while(cma.solution().value > 1E-20 );
61}