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