CMAExperiment.cpp
Go to the documentation of this file.
1/*!
2 *
3 *
4 * \brief Example of an experiment using the CMA-ES on several benchmark functions
5 *
6 * \author O.Krause
7 * \date 2014
8 *
9 * \par Copyright 1995-2017 Shark Development Team
10 *
11 * <BR><HR>
12 * This file is part of Shark.
13 * <https://shark-ml.github.io/Shark/>
14 *
15 * Shark is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as published
17 * by the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * Shark is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with Shark. If not, see <http://www.gnu.org/licenses/>.
27 *
28 */
29// Implementation of the CMA-ES
31// Access to benchmark functions
36
37using namespace shark;
38
39
40int main( int argc, char ** argv ) {
41
42 std::size_t numDimensions = 10; //dimensions of the objective functions
43 std::size_t numTrials = 100; // how often the optimization is repeated
44 std::size_t recordingInterval = 20; //we want to record after some multiple of this
45 std::size_t numIterations = 20*recordingInterval; //number of iterations to perform
46
47 //assortment of test functions
48 typedef boost::shared_ptr<SingleObjectiveFunction > Function;
49 std::vector<Function > functions;
50 functions.push_back(Function(new benchmarks::Rosenbrock(numDimensions)));
51 functions.push_back(Function(new benchmarks::Cigar(numDimensions)));
52 functions.push_back(Function(new benchmarks::Discus(numDimensions)));
53 functions.push_back(Function(new benchmarks::Ellipsoid(numDimensions)));
54
55 RealMatrix meanPerformance(functions.size(), numIterations/recordingInterval+1,0.0);
56 for(std::size_t f = 0; f != functions.size(); ++f){
57 for(std::size_t trial = 0; trial != numTrials; ++trial){
58 //print progress
59 std::cout<<"\r" <<functions[f]->name() <<": "<<trial<<"/"<<numTrials<<std::flush;
60 //create and initialize the optimizer
61 CMA cma;
62 functions[f]->init();
63 cma.init( *functions[f] );
64
65 //record value
66 meanPerformance(f,0) += cma.solution().value;
67
68 //optimize
69 for(std::size_t i = 1; i <= numIterations; ++i){
70 cma.step(*functions[f]);
71 if(i % recordingInterval == 0){
72 meanPerformance(f,i / recordingInterval) += cma.solution().value;
73 }
74 }
75 }
76 }
77 meanPerformance /= numTrials;
78
79 std::cout<<"\r# Iteration ";
80 for(std::size_t f = 0; f != functions.size(); ++f)
81 std::cout<<functions[f]->name()<<" ";
82 std::cout<<"\n";
83
84 std::cout.precision( 7 );
85 for(std::size_t i = 0; i != meanPerformance.size2();++i){
86 std::cout<< i*recordingInterval<<" ";
87 for(std::size_t f = 0; f != functions.size(); ++f){
88 std::cout<<meanPerformance(f,i)<<" ";
89 }
90 std::cout<<"\n";
91 }
92}