ExperienceFile.hh
Go to the documentation of this file.
00001 #ifndef _EXPFILE_HH_
00002 #define _EXPFILE_HH_
00003 
00004 #include "Random.h"
00005 #include "core.hh"
00006 
00007 #include <vector>
00008 #include <algorithm>
00009 
00010 #include <sys/time.h>
00011 
00012 class ExperienceFile {
00013 public:
00017   ofstream vectorFile;
00018   int expNum;
00019 
00020   ExperienceFile(){
00021     expNum = 0;
00022   }
00023 
00024   ~ExperienceFile(){
00025     if (vectorFile.is_open())
00026       vectorFile.close();
00027   }
00028 
00029   void initFile(const char* filename, int nfeats){
00030     vectorFile.open(filename, ios::out | ios::binary);
00031 
00032     // first part, save the vector size
00033     vectorFile.write((char*)&nfeats, sizeof(int));
00034   }
00035 
00036   void saveExperience(experience e){
00037     if (!vectorFile.is_open())
00038       return;
00039 
00040     /*
00041       if (expNum == 50){
00042       vectorFile.close();
00043       return;
00044       }
00045     */
00046 
00047     vectorFile.write((char*)&(e.s[0]), e.s.size()*sizeof(float));
00048     vectorFile.write((char*)&(e.next[0]), e.next.size()*sizeof(float));
00049     vectorFile.write((char*)&e.act, sizeof(int));
00050     vectorFile.write((char*)&e.reward, sizeof(float));
00051     vectorFile.write((char*)&e.terminal, sizeof(bool));
00052 
00053     //cout << "Experience " << expNum << endl;
00054     expNum++;
00055     //printExperience(e);
00056   }
00057 
00058   void printExperience(experience e){
00059 
00060     cout << "State s: ";
00061     for(unsigned i = 0; i < e.s.size(); i++){
00062       cout << e.s[i] << ", ";
00063     }
00064     cout << endl << " Next: ";
00065     for(unsigned i = 0; i < e.next.size(); i++){
00066       cout << e.next[i] << ", ";
00067     }
00068     cout << endl;
00069     cout << "action: " << e.act << " reward: " << e.reward << endl;
00070 
00071   }
00072 
00073 
00074 
00075   std::vector<experience> loadExperiences(const char* filename){
00076     ifstream inFile (filename, ios::in | ios::binary);
00077 
00078     int numFeats;
00079     inFile.read((char*)&numFeats, sizeof(int));
00080 
00081     std::vector<experience> seeds;
00082 
00083     // while file is not empty
00084     while(!inFile.eof()){
00085       experience e;
00086       e.s.resize(numFeats);
00087       e.next.resize(numFeats);
00088 
00089       inFile.read((char*)&(e.s[0]), e.s.size()*sizeof(float));
00090       if (inFile.eof()) break;
00091       inFile.read((char*)&(e.next[0]), e.next.size()*sizeof(float));
00092       if (inFile.eof()) break;
00093       inFile.read((char*)&e.act, sizeof(int));
00094       inFile.read((char*)&e.reward, sizeof(float));
00095       inFile.read((char*)&e.terminal, sizeof(bool));
00096 
00097       //cout << "Experience " << seeds.size() << endl;
00098       //printExperience(e);
00099 
00100       seeds.push_back(e);
00101     }
00102 
00103     inFile.close();
00104 
00105     return seeds;
00106   }
00107 
00108   void closeFile(){
00109     if (vectorFile.is_open())
00110       vectorFile.close();
00111   }
00112 
00113 };
00114 
00115 #endif


rl_common
Author(s):
autogenerated on Thu Jun 6 2019 22:00:08