00001 #ifndef OPENTISSUE_KINEMATICS_INVERSE_INVERSE_WRITE_BENCHMARKS_H
00002 #define OPENTISSUE_KINEMATICS_INVERSE_INVERSE_WRITE_BENCHMARKS_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/big/io/big_matlab_write.h>
00013
00014 #include <cassert>
00015 #include <iostream>
00016 #include <fstream>
00017 #include <sstream>
00018
00019 namespace OpenTissue
00020 {
00021 namespace kinematics
00022 {
00023 namespace inverse
00024 {
00025 namespace detail
00026 {
00027
00058 template<typename output_iterator>
00059 inline void write_benchmarks(
00060 std::string const & prefix
00061 , std::string const & matlab_filename
00062 , std::string const & latex_filename
00063 , output_iterator begin
00064 , output_iterator end
00065 )
00066 {
00067 using namespace OpenTissue::math::big;
00068 using std::min;
00069 using std::max;
00070
00071 std::ofstream matlab_file( matlab_filename.c_str(), std::ios::out);
00072 if (!matlab_file)
00073 {
00074 std::cerr << "Error unable to create file: "<< matlab_filename << std::endl;
00075 return;
00076 }
00077
00078 std::ofstream latex_file( latex_filename.c_str(), std::ios::out);
00079 if (!latex_file)
00080 {
00081 std::cerr << "Error unable to create file: "<< latex_filename << std::endl;
00082 return;
00083 }
00084
00085
00086 matlab_file << "close all;" << std::endl;
00087
00088
00089
00090
00091
00092 {
00093 size_t benchmark = 0u;
00094 matlab_file << "I = [";
00095 for(output_iterator output=begin;output!=end;++output,++benchmark)
00096 {
00097 matlab_file << " " << output->iterations();
00098 }
00099 matlab_file << " ];" << std::endl;
00100 }
00101
00102
00103 {
00104 size_t benchmark = 0u;
00105 matlab_file << "T = [";
00106 for(output_iterator output=begin;output!=end;++output,++benchmark)
00107 {
00108 matlab_file << " " << output->wall_time();
00109 }
00110 matlab_file << " ];" << std::endl;
00111 }
00112
00113
00114 {
00115 size_t benchmark = 0u;
00116 matlab_file << "V = [";
00117 for(output_iterator output=begin;output!=end;++output,++benchmark)
00118 {
00119 matlab_file << " " << output->value();
00120 }
00121 matlab_file << " ];" << std::endl;
00122 }
00123
00124
00125 {
00126 size_t benchmark = 0u;
00127 matlab_file << "G = [";
00128 for(output_iterator output=begin;output!=end;++output,++benchmark)
00129 {
00130 matlab_file << " " << output->gradient_norm();
00131 }
00132 matlab_file << " ];" << std::endl;
00133 }
00134
00135
00136 {
00137 size_t benchmark = 0u;
00138 matlab_file << "S = [";
00139 for(output_iterator output=begin;output!=end;++output,++benchmark)
00140 {
00141 matlab_file << " " << output->status();
00142 }
00143 matlab_file << " ];" << std::endl;
00144 }
00145
00146 {
00147 size_t benchmark = 0u;
00148 for(output_iterator output=begin;output!=end;++output,++benchmark)
00149 {
00150 matlab_file << "P" << benchmark << " = " << *(output->profiling()) << ";" << std::endl;
00151 }
00152 }
00153 std::string error_legends = "";
00154
00155 {
00156 size_t cnt_status[7];
00157 cnt_status[0] = 0u;
00158 cnt_status[1] = 0u;
00159 cnt_status[2] = 0u;
00160 cnt_status[3] = 0u;
00161 cnt_status[4] = 0u;
00162 cnt_status[5] = 0u;
00163 cnt_status[6] = 0u;
00164 for(output_iterator output=begin;output!=end;++output)
00165 {
00166 cnt_status[output->status()] = cnt_status[output->status()] + 1;
00167 }
00168 for(size_t i=0;i<7u;++i)
00169 {
00170 std::cout << cnt_status[i] << "\t:\t"<< OpenTissue::math::optimization::get_error_message( i ) << std::endl;
00171 latex_file << cnt_status[i] << "&"<< OpenTissue::math::optimization::get_error_message( i ) << std::endl;
00172 }
00173 std::cout << std::endl;
00174 latex_file << std::endl;
00175
00176
00177
00178 matlab_file << "errors = [ ";
00179 for(size_t i=0;i<7u;++i)
00180 {
00181
00182 matlab_file << cnt_status[i] << " " ;
00183 }
00184 matlab_file << "];" << std::endl;
00185
00186
00187 for(size_t i=0;i<7u;++i)
00188 {
00189 if(cnt_status[i] > 0){
00190 error_legends += "'";
00191 error_legends += OpenTissue::math::optimization::get_error_message( i ) ;
00192 error_legends += "', ";
00193 }
00194 }
00195
00196 }
00197
00198 {
00199 double min_time = 10000.0;
00200 double max_time = 0.0;
00201 double avg_time = 0.0;
00202 size_t min_iterations = 10000;
00203 size_t max_iterations = 0;
00204 double avg_iterations = 0.0;
00205 size_t cnt_absolute_converged = 0;
00206 size_t benchmark = 0u;
00207 for(output_iterator output=begin;output!=end;++output,++benchmark)
00208 {
00209 if(output->status() == OpenTissue::math::optimization::ABSOLUTE_CONVERGENCE)
00210 {
00211 ++cnt_absolute_converged;
00212 min_time = min( output->wall_time(), min_time );
00213 max_time = max( output->wall_time(), max_time );
00214 avg_time += output->wall_time();
00215 min_iterations = min( output->iterations(), min_iterations );
00216 max_iterations = max( output->iterations(), max_iterations );
00217 avg_iterations += output->iterations();
00218 }
00219 }
00220 if(cnt_absolute_converged>0)
00221 {
00222 avg_time /= cnt_absolute_converged;
00223 avg_iterations /= cnt_absolute_converged;
00224
00225 latex_file << "Min (secs)" << " & " << "Avg (secs)" << " & " << "Max (secs)" << "\\\\" << std::endl;
00226 latex_file << min_time << " & " << avg_time << " & " << max_time << "\\\\" << std::endl;
00227 latex_file << "Min (\\#)" << " & " << "Avg (\\#)" << " & " << "Max (\\#)" << "\\\\" << std::endl;
00228 latex_file << min_iterations << " & " << avg_iterations << " & " << max_iterations << "\\\\" << std::endl;
00229
00230 std::cout << "time (min,avg,max) : " << min_time << " : " << avg_time << " : " << max_time << std::endl;
00231 std::cout << "iter (min,avg,max) : " << min_iterations << " : " << avg_iterations << " : " << max_iterations << std::endl;
00232 std::cout << std::endl;
00233 }
00234 }
00235
00236
00237 {
00238 matlab_file << "filename1 = '" << prefix << "_convergence';" << std::endl;
00239 matlab_file << "figure(1);" << std::endl;
00240 matlab_file << "clf" << std::endl;
00241 matlab_file << "set(gca,'fontsize',18);" << std::endl;
00242 matlab_file << "hold on;" << std::endl;
00243 size_t benchmark = 0u;
00244 for(output_iterator output=begin;output!=end;++output,++benchmark)
00245 {
00246 if(output->iterations()>0u)
00247 {
00248 size_t color_choice = (benchmark%6);
00249 std::string color = "";
00250 switch(color_choice)
00251 {
00252 case 0: color = ",'b'"; break;
00253 case 1: color = ",'g'"; break;
00254 case 2: color = ",'r'"; break;
00255 case 3: color = ",'c'"; break;
00256 case 4: color = ",'m'"; break;
00257 case 5: color = ",'y'"; break;
00258 };
00259 matlab_file << "plot(P"<<benchmark<<"(1:"<< output->iterations() <<")"<< color <<");" << std::endl;
00260 }
00261 }
00262 matlab_file << "axis tight;" << std::endl;
00263 matlab_file << "xlabel('Iterations','fontsize',18);" << std::endl;
00264 matlab_file << "ylabel('\\theta','fontsize',18);" << std::endl;
00265 matlab_file << "hold off;" << std::endl;
00266 matlab_file << "print('-f1','-depsc2', filename1);" << std::endl;
00267 matlab_file << "print('-f1','-dpng', filename1);" << std::endl;
00268 }
00269
00270 {
00271 matlab_file << "filename7 = '" << prefix << "_convergence_logarithmic';" << std::endl;
00272 matlab_file << "figure(7);" << std::endl;
00273 matlab_file << "clf" << std::endl;
00274
00275 matlab_file << "hold on;" << std::endl;
00276 size_t benchmark = 0u;
00277 for(output_iterator output=begin;output!=end;++output,++benchmark)
00278 {
00279 if(output->iterations()>0u)
00280 {
00281 size_t color_choice = (benchmark%6);
00282 std::string color = "";
00283 switch(color_choice)
00284 {
00285 case 0: color = ",'b'"; break;
00286 case 1: color = ",'g'"; break;
00287 case 2: color = ",'r'"; break;
00288 case 3: color = ",'c'"; break;
00289 case 4: color = ",'m'"; break;
00290 case 5: color = ",'y'"; break;
00291 };
00292 matlab_file << "loglog(P"<<benchmark<<"(1:"<< output->iterations() <<")"<< color <<");" << std::endl;
00293 }
00294 }
00295
00296 matlab_file << "xlabel('Iterations','fontsize',18);" << std::endl;
00297 matlab_file << "ylabel('\\theta','fontsize',18);" << std::endl;
00298 matlab_file << "hold off;" << std::endl;
00299 matlab_file << "print('-f7','-depsc2', filename7);" << std::endl;
00300 matlab_file << "print('-f7','-dpng', filename7);" << std::endl;
00301 }
00302
00303
00304
00305
00306 double bucket = 1.0;
00307 double max_I = 100;
00308
00309
00310
00311 {
00312 matlab_file << "filename2 = '" << prefix << "iterations';" << std::endl;
00313 matlab_file << "figure(2);" << std::endl;
00314 matlab_file << "clf" << std::endl;
00315 matlab_file << "set(gca,'fontsize',18);" << std::endl;
00316 matlab_file << "hold on;" << std::endl;
00317 matlab_file << "hist(I," << max_I << ");" << std::endl;
00318 matlab_file << "xlabel('Iterations','fontsize',18);" << std::endl;
00319 matlab_file << "ylabel('Count','fontsize',18);" << std::endl;
00320 matlab_file << "hold off;" << std::endl;
00321 matlab_file << "print('-f2','-depsc2', filename2);" << std::endl;
00322 matlab_file << "print('-f2','-dpng', filename2);" << std::endl;
00323 }
00324
00325 bucket = 0.01;
00326 max_I = 0.5;
00327
00328 {
00329 matlab_file << "filename3 = '" << prefix << "objective values';" << std::endl;
00330 matlab_file << "figure(3);" << std::endl;
00331 matlab_file << "clf" << std::endl;
00332 matlab_file << "set(gca,'fontsize',18);" << std::endl;
00333 matlab_file << "hold on;" << std::endl;
00334 matlab_file << "hist(V, 100)" << std::endl;
00335
00336 matlab_file << "xlabel('f(\\theta)','fontsize',18);" << std::endl;
00337 matlab_file << "ylabel('Count','fontsize',18);" << std::endl;
00338 matlab_file << "hold off;" << std::endl;
00339 matlab_file << "print('-f3','-depsc2', filename3);" << std::endl;
00340 matlab_file << "print('-f3','-dpng', filename3);" << std::endl;
00341 }
00342
00343
00344 bucket = 0.001;
00345 max_I = 0.2;
00346 {
00347 matlab_file << "filename4 = '" << prefix << "time';" << std::endl;
00348 matlab_file << "figure(4);" << std::endl;
00349 matlab_file << "clf" << std::endl;
00350 matlab_file << "set(gca,'fontsize',18);" << std::endl;
00351 matlab_file << "hold on;" << std::endl;
00352 matlab_file << "hist(T, 100)" << std::endl;
00353 matlab_file << "xlabel('Wall time (in seconds)','fontsize',18);" << std::endl;
00354 matlab_file << "ylabel('Count','fontsize',18);" << std::endl;
00355 matlab_file << "hold off;" << std::endl;
00356 matlab_file << "print('-f4','-depsc2', filename4);" << std::endl;
00357 matlab_file << "print('-f4','-dpng', filename4);" << std::endl;
00358 }
00359
00360
00361 {
00362 matlab_file << "filename5 = '" << prefix << "status';" << std::endl;
00363 matlab_file << "figure(5);" << std::endl;
00364 matlab_file << "clf" << std::endl;
00365
00366 matlab_file << "pie(errors);" << std::endl;
00367 matlab_file << "legend( " << error_legends << "'Location','BestOutside'); ";
00368
00369 matlab_file << "print('-f5','-depsc2', filename5);" << std::endl;
00370 matlab_file << "print('-f5','-dpng', filename5);" << std::endl;
00371 }
00372
00373 matlab_file.flush();
00374 matlab_file.close();
00375
00376 latex_file.flush();
00377 latex_file.close();
00378 std::cout << "done writting benchmarks..." << std::endl;
00379 }
00380
00381
00382 }
00383 }
00384 }
00385 }
00386
00387
00388 #endif