CMAPlot.cpp
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Example for examining characteristics of the CMA with the help of
5  * the Probe framework.
6  *
7  *
8  *
9  * \author tvoss
10  * \date -
11  *
12  *
13  * \par Copyright 1995-2017 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://shark-ml.org/>
18  *
19  * Shark is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU Lesser General Public License as published
21  * by the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Shark is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 
36 
37 using namespace shark;
38 using namespace std;
39 
40 #include <boost/property_tree/json_parser.hpp>
41 
42 int main( int argc, char ** argv ) {
43 
44  // Results go here.
45  ofstream results( "results.txt" );
46  // Plotting commands (gnuplot) go here.
47  ofstream plot( "plot.txt" );
48  plot << "set key outside bottom center" << endl;
49  plot << "set size square" << endl;
50  plot << "set zeroaxis" << endl;
51  plot << "set border 0" << endl;
52  plot << "set xrange [-4:4]" << endl;
53  plot << "set yrange [-4:4]" << endl;
54  // Adjust the floating-point format to scientific and increase output precision.
55  results.setf( ios_base::scientific );
56  results.precision( 10 );
57  plot.setf( ios_base::scientific );
58  plot.precision( 10 );
59 
60  // Instantiate both the problem and the optimizer.
61  Himmelblau hb;
62  CMA cma;
63  hb.init();
64  cma.init( hb );
65 
66  // Iterate the optimizer until a solution of sufficient quality is found.
67  do{
68  // Print error ellipses for covariance matrices.
69  plot << "set object "
70  << hb.evaluationCounter() + 1
71  << " ellipse center "
72  << cma.mean()( 0 ) << ","
73  << cma.mean()( 1 ) << " size "
74  << cma.eigenValues()(0) * cma.sigma() * 2. << "," // times 2 because gnuplot takes diameters as arguments
75  << cma.eigenValues()(1) * cma.sigma() * 2. << " angle "
76  << ::atan( cma.eigenVectors()( 1, 0 ) / cma.eigenVectors()( 0, 0 ) ) / M_PI * 180 << " front fillstyle empty border 2" << endl;
77 
78  // Report information on the optimizer state and the current solution to the console.
79  results << hb.evaluationCounter() << " " // Column 1
80  << cma.condition() << " " // Column 2
81  << cma.sigma() << " " // Column 3
82  << cma.solution().value << " "; // Column 4
83  copy(
84  cma.solution().point.begin(),
85  cma.solution().point.end(), // Column 5 & 6
86  ostream_iterator< double >( results, " " )
87  );
88  copy(
89  cma.mean().begin(), // Column 7 & 8
90  cma.mean().end(),
91  ostream_iterator< double >( results, " " )
92  );
93  results << endl;
94 
95  // Do one CMA iteration/generation.
96  cma.step( hb );
97 
98  } while( cma.solution().value> 1E-20 );
99 
100  // Write final result.
101  // Print error ellipses for covariance matrices.
102  plot << "set object "
103  << hb.evaluationCounter() + 1
104  << " ellipse center "
105  << cma.mean()( 0 ) << ","
106  << cma.mean()( 1 ) << " size "
107  << cma.eigenValues()(0) * cma.sigma() * 2. << "," // times 2 because gunplot takes diameters as arguments
108  << cma.eigenValues()(1) * cma.sigma() * 2. << " angle "
109  << ::atan( cma.eigenVectors()( 1, 0 ) / cma.eigenVectors()( 0, 0 ) ) / M_PI * 180 << " front fillstyle empty border 2" << endl;
110 
111  // Report information on the optimizer state and the current solution to the console.
112  results << hb.evaluationCounter() << " " // Column 1
113  << cma.condition() << " " // Column 2
114  << cma.sigma() << " " // Column 3
115  << cma.solution().value << " "; // Column 4
116  copy(
117  cma.solution().point.begin(),
118  cma.solution().point.end(), // Column 5 & 6
119  ostream_iterator< double >( results, " " )
120  );
121  copy(
122  cma.mean().begin(), // Column 7 & 8
123  cma.mean().end(),
124  ostream_iterator< double >( results, " " )
125  );
126  results << endl;
127 
128  //plot << "plot 'results.txt' every ::2 using 7:8 with lp title 'Population mean'" << endl;
129  plot << "plot 'results.txt' using 7:8 with lp title 'Population mean'" << endl;
130 
131  return( EXIT_SUCCESS );
132 }