123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- #include "optimization/Plotter.h"
- #include "core/optimization/blackbox/Definitions_core_opt.h"
- #include <iostream>
- #include <fstream>
- using namespace OPTIMIZATION;
- Plotter::Plotter() : m_absoluteBounds(true),
- m_pCostfunc(NULL)
- {
- }
- Plotter::Plotter(const Plotter& plotter)
- {
- m_pCostfunc= plotter.m_pCostfunc;
- m_absoluteBounds= plotter.m_absoluteBounds;
- }
- Plotter::~Plotter()
- {
- }
- Plotter& Plotter::operator=(const Plotter & plotter)
- {
-
- if(this != &plotter)
- {
- m_pCostfunc= plotter.m_pCostfunc;
- m_absoluteBounds= plotter.m_absoluteBounds;
- }
- return *this;
- }
- void Plotter::setOptProblem(const SimpleOptProblem& optprob)
- {
- m_pOptprob= &optprob;
-
- m_pCostfunc= const_cast<SimpleOptProblem&>(optprob).getOriginalCostFunction();
- }
- void Plotter::plot1D(int paramnum, const float from, const float to, const float stepwidth, const char* path)
- {
-
- assert(paramnum >= 0 && paramnum < m_pOptprob->getNumParams());
-
-
- std::ofstream plotfile;
- plotfile.open(path);
-
-
- matrix_type tmpParams= m_pOptprob->getAllCurrentParams();
-
-
- float start,end;
-
-
- if(m_absoluteBounds)
- {
- start= from;
- end= to;
- }
-
- else
- {
- start= tmpParams(paramnum,0) + from;
- end= tmpParams(paramnum,0) + to;
- }
-
-
- tmpParams(paramnum,0)= start;
-
- while(tmpParams(paramnum,0) <= end)
- {
-
- plotfile << tmpParams(paramnum,0) << " " << m_pCostfunc->evaluate(tmpParams) << std::endl;
-
-
- tmpParams(paramnum,0)+= stepwidth;
- }
-
- plotfile.close();
- }
- void Plotter::plot2D(int paramnum1, const float from1, const float to1, const float stepwidth1, int paramnum2, const float from2, const float to2, const float stepwidth2, const char* path)
- {
-
- assert(paramnum1 >= 0 && paramnum1 < m_pOptprob->getNumParams() && paramnum2 >= 0 && paramnum2 < m_pOptprob->getNumParams() );
-
-
- std::ofstream plotfile;
- plotfile.open(path);
-
-
- matrix_type tmpParams= m_pOptprob->getAllCurrentParams();
-
-
- float start1,end1,start2,end2;
-
-
- if(m_absoluteBounds)
- {
- start1= from1;
- end1= to1;
- start2= from2;
- end2= to2;
- }
-
- else
- {
- start1= tmpParams(paramnum1,0) + from1;
- end1= tmpParams(paramnum1,0) + to1;
- start2= tmpParams(paramnum2,0) + from2;
- end2= tmpParams(paramnum2,0) + to2;
- }
-
-
- tmpParams(paramnum1,0)= start1;
- tmpParams(paramnum2,0)= start2;
-
- while(tmpParams(paramnum1,0) <= end1)
- {
- while(tmpParams(paramnum2,0) <= end2)
- {
-
- plotfile << tmpParams(paramnum1,0) << " " << tmpParams(paramnum2,0) << " " << m_pCostfunc->evaluate(tmpParams) << std::endl;
-
-
- tmpParams(paramnum2,0)+= stepwidth2;
- }
- plotfile<<std::endl;
-
- tmpParams(paramnum2,0)= start2;
-
-
- tmpParams(paramnum1,0)+= stepwidth1;
- }
-
- plotfile.close();
- }
- void Plotter::plot3D(int paramnum1, const float from1, const float to1, const float stepwidth1, int paramnum2, const float from2, const float to2, const float stepwidth2, int paramnum3, const float from3, const float to3, const float stepwidth3, const char* path)
- {
-
- assert(paramnum1 >= 0 && paramnum1 < m_pOptprob->getNumParams() && paramnum2 >= 0 && paramnum2 < m_pOptprob->getNumParams() && paramnum3 >= 0 && paramnum3 < m_pOptprob->getNumParams());
-
-
- matrix_type tmpParams= m_pOptprob->getAllCurrentParams();
-
-
- float start1,end1;
-
-
- if(m_absoluteBounds)
- {
- start1= from1;
- end1= to1;
- }
-
- else
- {
- start1= tmpParams(paramnum1,0) + from1;
- end1= tmpParams(paramnum1,0) + to1;
- }
-
-
- tmpParams(paramnum1,0)= start1;
- std::string filename(const_cast<char*>(path));
-
-
- if(filename.find(".txt") != std::string::npos)
- {
- filename.erase(filename.find(".txt"),4);
- }
-
- while(tmpParams(paramnum1,0) <= end1)
- {
-
- char number[5];
- sprintf(number,"%f",tmpParams(paramnum1,0));
- std::string numfilename= filename + "_" + number + ".txt";
-
-
- std::ofstream plotfile;
- plotfile.open(numfilename.c_str());
-
-
- float start2,end2,start3,end3;
-
- if(m_absoluteBounds)
- {
- start2= from2;
- end2= to2;
- start3= from3;
- end3= to3;
- }
-
- else
- {
- start2= tmpParams(paramnum2,0) + from2;
- end2= tmpParams(paramnum2,0) + to2;
- start3= tmpParams(paramnum3,0) + from3;
- end3= tmpParams(paramnum3,0) + to3;
- }
-
-
- tmpParams(paramnum2,0)= start2;
- tmpParams(paramnum3,0)= start3;
-
- while(tmpParams(paramnum2,0) <= end2)
- {
- while(tmpParams(paramnum3,0) <= end3)
- {
-
- plotfile << tmpParams(paramnum2,0) << " " << tmpParams(paramnum3,0) << " " << m_pCostfunc->evaluate(tmpParams) << std::endl;
-
-
- tmpParams(paramnum3,0)+= stepwidth3;
- }
- plotfile<<std::endl;
-
- tmpParams(paramnum3,0)= start3;
-
-
- tmpParams(paramnum2,0)+= stepwidth2;
- }
-
- plotfile.close();
-
-
- tmpParams(paramnum1,0)+= stepwidth1;
- }
- }
- void Plotter::setBoundInterpretationStatus(bool absolute)
- {
- m_absoluteBounds= absolute;
- }
- bool Plotter::getBoundInterpretationStatus()
- {
- return m_absoluteBounds;
- }
|