/* 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 Matthias Wacker */ #include "optimization/Plotter.h" #include "core/optimization/blackbox/Definitions_core_opt.h" #include #include 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(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<= 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(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<