MOCMAExperiment.cpp
Go to the documentation of this file.
1/*!
2 *
3 *
4 * \brief Example of an expriment using the MO-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 MO-CMA-ES
31// Access to benchmark functions
36
37using namespace shark;
38using namespace shark::benchmarks;
39
40template<class Solution>
41double hypervolume( Solution const& solution){
42 // the reference point (11,11).
43 RealVector referencePoint(2,11);
44 //instance of the hypervolume calculator
46 auto toPoints = [](typename Solution::const_reference point){return point.value;};
47 return hypervolume(boost::adaptors::transform(solution,toPoints),referencePoint);
48}
49
50
51int main( int argc, char ** argv ) {
52
53 std::size_t frontSize = 10; //number of points that approximate the front
54 std::size_t numDimensions = 10; //dimensions of the objective functions
55 std::size_t numTrials = 10; // how often the optimization is repeated
56 std::size_t recordingInterval = 20; //we want to record after some multiple of this
57 std::size_t numIterations = 20*recordingInterval; //number of iterations to perform
58
59 //assortment of test functions
60 typedef boost::shared_ptr<MultiObjectiveFunction> Function;
61 std::vector<Function > functions;
62 functions.push_back(Function(new ZDT1(numDimensions)));
63 functions.push_back(Function(new ZDT2(numDimensions)));
64 functions.push_back(Function(new ZDT3(numDimensions)));
65 functions.push_back(Function(new ZDT6(numDimensions)));
66
67 RealMatrix meanVolumes(functions.size(), numIterations/recordingInterval+1,0.0);
68 for(std::size_t f = 0; f != functions.size(); ++f){
69 for(std::size_t trial = 0; trial != numTrials; ++trial){
70 //print progress
71 std::cout<<"\r" <<functions[f]->name() <<": "<<trial<<"/"<<numTrials<<std::flush;
72 //create and initialize the optimizer
73 MOCMA mocma;
74 mocma.mu() = frontSize;
75 functions[f]->init();
76 mocma.init( *functions[f] );
77
78 //record and hypervolume of initial solution
79 meanVolumes(f,0) += hypervolume(mocma.solution());
80
81 //optimize
82 for(std::size_t i = 1; i <= numIterations; ++i){
83 mocma.step(*functions[f]);
84 if(i % recordingInterval == 0){
85 meanVolumes(f,i / recordingInterval) += hypervolume(mocma.solution());
86 }
87 }
88 }
89 }
90 meanVolumes /= numTrials;
91
92 std::cout<<"\r# Iteration ";
93 for(std::size_t f = 0; f != functions.size(); ++f)
94 std::cout<<functions[f]->name()<<" ";
95 std::cout<<"\n";
96
97 std::cout.precision( 7 );
98 for(std::size_t i = 0; i != meanVolumes.size2();++i){
99 std::cout<< i*recordingInterval<<" ";
100 for(std::size_t f = 0; f != functions.size(); ++f){
101 std::cout<<meanVolumes(f,i)<<" ";
102 }
103 std::cout<<"\n";
104 }
105}