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-2015 Shark Development Team
10  *
11  * <BR><HR>
12  * This file is part of Shark.
13  * <http://image.diku.dk/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 
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  return hypervolume(PointExtractor(),solution,referencePoint);
53 }
54 
55 
56 int main( int argc, char ** argv ) {
57 
58  std::size_t frontSize = 10; //number of points that approximate the front
59  std::size_t numDimensions = 10; //dimensions of the objective functions
60  std::size_t numTrials = 10; // how often the optimization is repeated
61  std::size_t recordingInterval = 20; //we want to record after some multiple of this
62  std::size_t numIterations = 20*recordingInterval; //number of iterations to perform
63 
64  //assortment of test functions
65  typedef boost::shared_ptr<MultiObjectiveFunction> Function;
66  std::vector<Function > functions;
67  functions.push_back(Function(new ZDT1(numDimensions)));
68  functions.push_back(Function(new ZDT2(numDimensions)));
69  functions.push_back(Function(new ZDT3(numDimensions)));
70  functions.push_back(Function(new ZDT6(numDimensions)));
71 
72  RealMatrix meanVolumes(functions.size(), numIterations/recordingInterval+1,0.0);
73  for(std::size_t f = 0; f != functions.size(); ++f){
74  for(std::size_t trial = 0; trial != numTrials; ++trial){
75  //print progress
76  std::cout<<"\r" <<functions[f]->name() <<": "<<trial<<"/"<<numTrials<<std::flush;
77  //create and initialize the optimizer
78  MOCMA mocma;
79  mocma.mu() = frontSize;
80  mocma.init( *functions[f] );
81 
82  //record and hypervolume of initial solution
83  meanVolumes(f,0) += hypervolume(mocma.solution());
84 
85  //optimize
86  for(std::size_t i = 1; i <= numIterations; ++i){
87  mocma.step(*functions[f]);
88  if(i % recordingInterval == 0){
89  meanVolumes(f,i / recordingInterval) += hypervolume(mocma.solution());
90  }
91  }
92  }
93  }
94  meanVolumes /= numTrials;
95 
96  std::cout<<"\r# Iteration ";
97  for(std::size_t f = 0; f != functions.size(); ++f)
98  std::cout<<functions[f]->name()<<" ";
99  std::cout<<"\n";
100 
101  std::cout.precision( 7 );
102  for(std::size_t i = 0; i != meanVolumes.size2();++i){
103  std::cout<< i*recordingInterval<<" ";
104  for(std::size_t f = 0; f != functions.size(); ++f){
105  std::cout<<meanVolumes(f,i)<<" ";
106  }
107  std::cout<<"\n";
108  }
109 }