//----------------------------------------------------------------------------//
//    module: Tune.cpp                                                        //
//                                                                            //
//    Tune parameters, i.e. learn and test model on several different data    //
//    sets that are build randomly from a given TData object. Load and save   //
//    functionality.                                                          //
//                                                                            //                                                                            //
//    copyright (c) 2003 by Lars Haendel                                      //
//    home: www.newty.de                                                      //
//                                                                            //
//    This program is free software; you can redistribute it and/or modify    //
//    it under the terms of the GNU General Public License as published by    //
//    the Free Software Foundation as version 2 of the License.               //
//                                                                            //
//    This program 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 General Public License for more details.                            //
//                                                                            //
//    You should have received a copy of the GNU General Public License       //
//    along with this program; if not, write to the Free Software             //
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.               //
//                                                                            //
//----------------------------------------------------------------------------//

#ifndef TUNE_H
#define TUNE_H

#define N_TUNE_RES 5       // # model characteristics evaluated/stored during parameter tuning

#include "Syncobjs.hpp"    // TCriticalSection


#include "ParaSet.h"       // due to:  TParaSetList
#include "ProjectG.h"      //          TProjectG
#include "data.h"          //          TData


typedef float TuneResult[N_TUNE_RES];

//---------------------------------------------------------------------------------------------------------------------------------------
// tune parameters
class TTune
{
private:

   TParaSetList* sets;              // parameter set list

   const TProjectG* prj;            // project settings
   const TData*    data;            // (learn-)data

   TPnc* pnc;                       // (temporary) learning object
   TParaSet para;                   // actually processed parameters

   TuneResult* results;             // tuning results

   enum TResultType {IdCub=0, IdCubRed=1, IdAvrVar=2, IdHitrate=3, Err_T=4};

   int nResultsOk, t;
   bool f_Loaded, f_ForLoading;

   TCriticalSection* sec;

public:

   // constructor/destructor
   TTune(TParaSetList* /*ca*/ _sets, const TProjectG*const& _prj, const TData*const& _data);
   TTune(const TProjectG*const& _prj, const TData*const& _data);
   ~TTune();

   void DoIt(const bool*const& f_Stop);   // do it: simulate to get the results for each parameter set


   int GetProgress();                     // get current progress
   const char* GetStatusText();           // get status text

   void Save(ofstream& file);             // save tuning results to file 
   void Load(ifstream& file, int& line);  // load runing results from file


   const int& GetResultsOk() const { return nResultsOk; };
   const float* GetResults(const int& id) const { return results[id]; };
   const TParaSet GetParameter(const int& id) const { sec->Enter(); TParaSet p=sets->Get(id); sec->Leave(); return p;}
};
#endif