123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- /*
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Library General Public
- License version 2 as published by the Free Software Foundation.
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Library General Public License for more details.
- You should have received a copy of the GNU Library General Public License
- along with this library; see the file COPYING.LIB. If not, write to
- the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA.
- ---
- Copyright (C) 2009, Esther-Sabrina Platzer <esther.platzer@uni-jena.de>
- Matthias Wacker <Matthias.Wacker@mti.uni-jena.de>
- */
- #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)
- {
- //avoid self-copy
- if(this != &plotter)
- {
- m_pCostfunc= plotter.m_pCostfunc;
- m_absoluteBounds= plotter.m_absoluteBounds;
- }
- return *this;
- }
- void Plotter::setOptProblem(const SimpleOptProblem& optprob)
- {
- m_pOptprob= &optprob;
- // Note: not nice, but a CostFunction cannot be const, as evaluate() is not const and cannot be made const
- m_pCostfunc= const_cast<SimpleOptProblem&>(optprob).getOriginalCostFunction();
- }
- void Plotter::plot1D(int paramnum, const float from, const float to, const float stepwidth, const char* path)
- {
- // if the parameter number is < 0 or has a higher dimension then the optimization problem, throw an assertion
- assert(paramnum >= 0 && paramnum < m_pOptprob->getNumParams());
-
- // open plot file for writing
- std::ofstream plotfile;
- plotfile.open(path);
-
- // take actual parameter values from the optimization problem
- matrix_type tmpParams= m_pOptprob->getAllCurrentParams();
-
- // start and end values for costfunction evaluation
- float start,end;
-
- // if absolute bounds are used
- if(m_absoluteBounds)
- {
- start= from;
- end= to;
- }
- // else compute relative bounds
- else
- {
- start= tmpParams(paramnum,0) + from;
- end= tmpParams(paramnum,0) + to;
- }
-
- // setting relevant parameter to start value
- tmpParams(paramnum,0)= start;
-
- while(tmpParams(paramnum,0) <= end)
- {
- // evaluate costfunction and write to plotfile
- plotfile << tmpParams(paramnum,0) << " " << m_pCostfunc->evaluate(tmpParams) << std::endl;
-
- // increment evaluation value
- 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)
- {
- // if the parameter number is < 0 or has a higher dimension then the optimization problem, throw an assertion
- assert(paramnum1 >= 0 && paramnum1 < m_pOptprob->getNumParams() && paramnum2 >= 0 && paramnum2 < m_pOptprob->getNumParams() );
-
- // open plot file for writing
- std::ofstream plotfile;
- plotfile.open(path);
-
- // take actual parameter values from the optimization problem
- matrix_type tmpParams= m_pOptprob->getAllCurrentParams();
-
- // start and end values for costfunction evaluation
- float start1,end1,start2,end2;
-
- // if absolute bounds are used
- if(m_absoluteBounds)
- {
- start1= from1;
- end1= to1;
- start2= from2;
- end2= to2;
- }
- // else compute relative bounds
- else
- {
- start1= tmpParams(paramnum1,0) + from1;
- end1= tmpParams(paramnum1,0) + to1;
- start2= tmpParams(paramnum2,0) + from2;
- end2= tmpParams(paramnum2,0) + to2;
- }
-
- // setting relevant parameter to start value
- tmpParams(paramnum1,0)= start1;
- tmpParams(paramnum2,0)= start2;
-
- while(tmpParams(paramnum1,0) <= end1)
- {
- while(tmpParams(paramnum2,0) <= end2)
- {
- // evaluate costfunction and write to plotfile
- plotfile << tmpParams(paramnum1,0) << " " << tmpParams(paramnum2,0) << " " << m_pCostfunc->evaluate(tmpParams) << std::endl;
-
- // increment evaluation value of parameter 2
- tmpParams(paramnum2,0)+= stepwidth2;
- }
- plotfile<<std::endl;
- // reset inner loop
- tmpParams(paramnum2,0)= start2;
-
- // increment evaluation value of parameter 1
- 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)
- {
- // if the parameter number is < 0 or has a higher dimension then the optimization problem, throw an assertion
- assert(paramnum1 >= 0 && paramnum1 < m_pOptprob->getNumParams() && paramnum2 >= 0 && paramnum2 < m_pOptprob->getNumParams() && paramnum3 >= 0 && paramnum3 < m_pOptprob->getNumParams());
-
- // take actual parameter values from the optimization problem
- matrix_type tmpParams= m_pOptprob->getAllCurrentParams();
-
- // start and end values for costfunction evaluation
- float start1,end1;
-
- // if absolute bounds are used
- if(m_absoluteBounds)
- {
- start1= from1;
- end1= to1;
- }
- // else compute relative bounds
- else
- {
- start1= tmpParams(paramnum1,0) + from1;
- end1= tmpParams(paramnum1,0) + to1;
- }
-
- // iterating over the first parameter
- tmpParams(paramnum1,0)= start1;
- std::string filename(const_cast<char*>(path));
-
- // cutting suffix ".txt", if there
- if(filename.find(".txt") != std::string::npos)
- {
- filename.erase(filename.find(".txt"),4);
- }
-
- while(tmpParams(paramnum1,0) <= end1)
- {
- // generate name of plotfile
- char number[5];
- sprintf(number,"%f",tmpParams(paramnum1,0));
- std::string numfilename= filename + "_" + number + ".txt";
-
- // open plot file for writing
- std::ofstream plotfile;
- plotfile.open(numfilename.c_str());
-
- // start and end values for costfunction evaluation
- float start2,end2,start3,end3;
-
- if(m_absoluteBounds)
- {
- start2= from2;
- end2= to2;
- start3= from3;
- end3= to3;
- }
- // else compute relative bounds
- else
- {
- start2= tmpParams(paramnum2,0) + from2;
- end2= tmpParams(paramnum2,0) + to2;
- start3= tmpParams(paramnum3,0) + from3;
- end3= tmpParams(paramnum3,0) + to3;
- }
-
- // setting relevant parameter to start value
- tmpParams(paramnum2,0)= start2;
- tmpParams(paramnum3,0)= start3;
-
- while(tmpParams(paramnum2,0) <= end2)
- {
- while(tmpParams(paramnum3,0) <= end3)
- {
- // evaluate costfunction and write to plotfile
- plotfile << tmpParams(paramnum2,0) << " " << tmpParams(paramnum3,0) << " " << m_pCostfunc->evaluate(tmpParams) << std::endl;
-
- // increment evaluation value of parameter 3
- tmpParams(paramnum3,0)+= stepwidth3;
- }
- plotfile<<std::endl;
- // reset inner loop
- tmpParams(paramnum3,0)= start3;
-
- // increment evaluation value of parameter 2
- tmpParams(paramnum2,0)+= stepwidth2;
- }
- // close plotfile
- plotfile.close();
-
- // increment parameter 1
- tmpParams(paramnum1,0)+= stepwidth1;
- }
- }
- void Plotter::setBoundInterpretationStatus(bool absolute)
- {
- m_absoluteBounds= absolute;
- }
- bool Plotter::getBoundInterpretationStatus()
- {
- return m_absoluteBounds;
- }
|