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-2015 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://image.diku.dk/shark/>
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  cma.init( hb );
64 
65  // Iterate the optimizer until a solution of sufficient quality is found.
66  do{
67  // Print error ellipses for covariance matrices.
68  plot << "set object "
69  << hb.evaluationCounter() + 1
70  << " ellipse center "
71  << cma.mean()( 0 ) << ","
72  << cma.mean()( 1 ) << " size "
73  << cma.eigenValues()(0) * cma.sigma() * 2. << "," // times 2 because gunplot takes diameters as arguments
74  << cma.eigenValues()(1) * cma.sigma() * 2. << " angle "
75  << ::atan( cma.eigenVectors()( 1, 0 ) / cma.eigenVectors()( 0, 0 ) ) / M_PI * 180 << " front fillstyle empty border 2" << endl;
76 
77  // Report information on the optimizer state and the current solution to the console.
78  results << hb.evaluationCounter() << " " // Column 1
79  << cma.condition() << " " // Column 2
80  << cma.sigma() << " " // Column 3
81  << cma.solution().value << " "; // Column 4
82  copy(
83  cma.solution().point.begin(),
84  cma.solution().point.end(), // Column 5 & 6
85  ostream_iterator< double >( results, " " )
86  );
87  copy(
88  cma.mean().begin(), // Column 7 & 8
89  cma.mean().end(),
90  ostream_iterator< double >( results, " " )
91  );
92  results << endl;
93 
94  // Do one CMA iteration/generation.
95  cma.step( hb );
96 
97  } while( cma.solution().value> 1E-20 );
98 
99  // Write final result.
100  // Print error ellipses for covariance matrices.
101  plot << "set object "
102  << hb.evaluationCounter() + 1
103  << " ellipse center "
104  << cma.mean()( 0 ) << ","
105  << cma.mean()( 1 ) << " size "
106  << cma.eigenValues()(0) * cma.sigma() * 2. << "," // times 2 because gunplot takes diameters as arguments
107  << cma.eigenValues()(1) * cma.sigma() * 2. << " angle "
108  << ::atan( cma.eigenVectors()( 1, 0 ) / cma.eigenVectors()( 0, 0 ) ) / M_PI * 180 << " front fillstyle empty border 2" << endl;
109 
110  // Report information on the optimizer state and the current solution to the console.
111  results << hb.evaluationCounter() << " " // Column 1
112  << cma.condition() << " " // Column 2
113  << cma.sigma() << " " // Column 3
114  << cma.solution().value << " "; // Column 4
115  copy(
116  cma.solution().point.begin(),
117  cma.solution().point.end(), // Column 5 & 6
118  ostream_iterator< double >( results, " " )
119  );
120  copy(
121  cma.mean().begin(), // Column 7 & 8
122  cma.mean().end(),
123  ostream_iterator< double >( results, " " )
124  );
125  results << endl;
126 
127  //plot << "plot 'results.txt' every ::2 using 7:8 with lp title 'Population mean'" << endl;
128  plot << "plot 'results.txt' using 7:8 with lp title 'Population mean'" << endl;
129 
130  return( EXIT_SUCCESS );
131 }