DiscretizationAgent.cc
Go to the documentation of this file.
00001 #include <rl_agent/DiscretizationAgent.hh>
00002 #include <algorithm>
00003 
00004 DiscretizationAgent::DiscretizationAgent(int n, Agent* a, 
00005                                          std::vector<float> fmin,
00006                                          std::vector<float> fmax,
00007                                          bool d){
00008 
00009   statesPerDim.resize(fmin.size(), n);
00010   initEverything(a, fmin, fmax, d);
00011 
00012 }
00013 
00014 DiscretizationAgent::DiscretizationAgent(std::vector<int> n, Agent* a, 
00015                                          std::vector<float> fmin,
00016                                          std::vector<float> fmax,
00017                                          bool d){
00018 
00019   if (n.size() != fmin.size()){
00020     cout << "ERROR: discretition vector size is different than # features" << endl;
00021     exit(-1);
00022   }
00023 
00024   statesPerDim = n;
00025   initEverything(a, fmin, fmax, d);
00026 
00027 }
00028 
00029 void DiscretizationAgent::initEverything(Agent* a, 
00030                                          std::vector<float> fmin,
00031                                          std::vector<float> fmax,
00032                                          bool d){
00033   agent = a;
00034   featmin = fmin;
00035   featmax = fmax;
00036   DEBUG = d;
00037 
00038   // print number of features
00039   int totalFeatures = 1;
00040   for (unsigned i = 0; i < featmin.size(); i++){
00041     if (DEBUG) cout << "Dim " << i << " has " 
00042                     << (1+statesPerDim[i]) << " values." << endl;
00043     totalFeatures *= (1+statesPerDim[i]);
00044   }
00045   if (DEBUG) cout << "Total # States: " << totalFeatures << endl;
00046 
00047 }
00048 
00049 DiscretizationAgent::~DiscretizationAgent(){
00050   delete agent;
00051 }
00052 
00053 int DiscretizationAgent::first_action(const std::vector<float> &s) {
00054   std::vector<float> ds = discretizeState(s);
00055 
00056   return agent->first_action(ds);
00057 }
00058 
00059 int DiscretizationAgent::next_action(float r, const std::vector<float> &s) {
00060   std::vector<float> ds = discretizeState(s);
00061 
00062   return agent->next_action((int)r, ds);
00063 }
00064 
00065 void DiscretizationAgent::last_action(float r) {
00066   return agent->last_action((int)r);
00067 }
00068 
00069 
00070 void DiscretizationAgent::setDebug(bool b){}
00071 
00072 std::vector<float> DiscretizationAgent::discretizeState(const std::vector<float> &s){
00073   std::vector<float> ds;
00074   ds.resize(s.size());
00075   
00076   // since i'm sometimes doing this for discrete domains
00077   // want to center bins on 0, not edge on 0
00078   //cout << "feat " << i << " range: " << featmax[i] << " " << featmin[i] << " " << (featmax[i]-featmin[i]) << " n: " << (float)statesPerDim;
00079   for (unsigned i = 0; i < s.size(); i++){
00080     float factor = (featmax[i] - featmin[i]) / (float)statesPerDim[i];
00081     int bin = 0;
00082     if (s[i] > 0){
00083       bin = (int)((s[i]+factor/2) / factor);
00084     } else {
00085       bin = (int)((s[i]-factor/2) / factor);
00086     }
00087     
00088     ds[i] = factor*bin;
00089     //cout << "DA factor: " << factor << " bin: " << bin;
00090     //cout << " Original: " << s[i] << " Discrete: " << ds[i] << endl;
00091   }
00092 
00093   return ds;
00094 
00095 }
00096 
00097 
00098 void DiscretizationAgent::seedExp(std::vector<experience> seedings){
00099   // discretize each experience
00100   for (unsigned i = 0; i < seedings.size(); i++){
00101     experience* e = &(seedings[i]);
00102 
00103     e->next = discretizeState(e->next);
00104     e->s = discretizeState(e->s);
00105     e->reward = ((int)e->reward);
00106 
00107   }
00108 
00109   // and pass to internal agent
00110   agent->seedExp(seedings);
00111 }
00112 
00113 void DiscretizationAgent::savePolicy(const char* filename){
00114   agent->savePolicy(filename);
00115 }


rl_agent
Author(s): Todd Hester
autogenerated on Thu Jun 6 2019 22:00:13