tagging release
[dasher.git] / Src / Test / LanguageModelling / lib_expt.h
blob0fb8f236307e45761897dc97f6c253ea5f4cb7ff
1 #ifndef __LIB_EXPT_H__
2 #define __LIB_EXPT_H__
4 /// A class for managing computational experiments
5 ///
6 /// (c) 2005 P. J. Cowans
8 // TODO - implement a way of sharing data between related runs
10 #include <iostream>
11 #include <fstream>
12 #include <string>
13 #include <map>
15 class cExperiment {
16 public:
17 cExperiment(const std::string & oPrefix) {
19 std::string oParameterLogFilename(oPrefix + ".params");
20 std::string oDataLogFilename(oPrefix + ".data");
22 oParameterLog.open(oParameterLogFilename.c_str());
23 oDataLog.open(oDataLogFilename.c_str());
27 ~cExperiment() {
28 oParameterLog.close();
29 oDataLog.close();
32 /// Set a parameter
34 // TODO - can we do anything sensible with templates?
36 inline void SetParameterInt(const std::string & sName, int iValue) {
37 oParameterMapInt[sName] = iValue;
40 inline void SetParameterString(const std::string & sName, const std::string & sValue) {
41 oParameterMapString[sName] = sValue;
44 /// Get a parameter
46 inline int GetParameterInt(const std::string & sName) const {
47 // TODO - handle case when parameter is not set (throw an exception)
49 std::map < std::string, int >::const_iterator it(oParameterMapInt.find(sName));
51 if(it == oParameterMapInt.end()) {
52 std::cerr << "Error - asked for non-initialised parameter " << sName << std::endl;
53 exit(1);
55 else {
56 std::cerr << sName << " " << it->second << std::endl;
59 return it->second;
62 inline std::string GetParameterString(const std::string & sName)const {
63 // TODO - handle case when parameter is not set (throw an exception)
65 std::map < std::string, std::string >::const_iterator it(oParameterMapString.find(sName));
67 if(it == oParameterMapString.end()) {
68 std::cerr << "Error - asked for non-initialised parameter " << sName << std::endl;
69 exit(1);
71 else {
72 std::cerr << sName << " " << it->second << std::endl;
75 return it->second;
78 /// Dump a list of the parameters
80 void DumpParameters() {
81 for(std::map < std::string, int >::iterator it(oParameterMapInt.begin()); it != oParameterMapInt.end(); ++it)
82 oParameterLog << it->first << ": " << it->second << std::endl;
84 for(std::map < std::string, std::string >::iterator it(oParameterMapString.begin()); it != oParameterMapString.end(); ++it)
85 oParameterLog << it->first << ": " << it->second << std::endl;
88 /// Records a string in the log file
90 void RecordData(const std::string & oData) {
91 oDataLog << oData;
94 /// Actually do the experiment
96 double Run() {
97 DumpParameters();
98 return Execute();
101 /// This method needs to be implemented by the child class - the actual work of doing the experiment happens here.
103 virtual double Execute() = 0;
105 private:
106 std::map < std::string, int >oParameterMapInt;
107 std::map < std::string, std::string > oParameterMapString;
108 std::ofstream oParameterLog;
109 std::ofstream oDataLog;
112 #endif