stocks.cc
Go to the documentation of this file.
00001 #include <rl_env/stocks.hh>
00002 
00003 
00004 Stocks::Stocks(Random &rand, bool stochastic, int nsectors, int nstocks):
00005   nsectors(nsectors),
00006   nstocks(nstocks),
00007   noisy(stochastic),
00008   rng(rand),
00009   s(nsectors*nstocks + nsectors)
00010 {
00011   STOCK_DEBUG = false; //true;
00012 
00013   initStocks();
00014   reset();
00015 }
00016 
00017 
00018 Stocks::Stocks(Random &rand, bool stochastic):
00019   nsectors(3),
00020   nstocks(2),
00021   noisy(stochastic),
00022   rng(rand),
00023   s(nsectors*nstocks + nsectors)
00024 {
00025   STOCK_DEBUG = false; //true;
00026 
00027   initStocks();
00028   reset();
00029 }
00030 
00031 
00032 Stocks::~Stocks() {
00033   delete [] owners;
00034   for (int i = 0; i < nsectors; i++){
00035     delete [] rising[i];
00036   }
00037   delete [] rising;
00038 }
00039 
00040 const std::vector<float> &Stocks::sensation() const { return s; }
00041 
00042 float Stocks::apply(int action) {
00043 
00044   // figure out ownership of stocks with this action
00045   if (action < nsectors){
00046     // flip that bit of sensation array
00047     s[owners[action]] = !s[owners[action]];
00048   }
00049   else if (action == nsectors){
00050     // do nothing
00051   }
00052   else {
00053     cout << "Invalid action!" << endl;
00054   }
00055 
00056   if (STOCK_DEBUG){
00057     cout << "Action: " << action << " Ownership now: ";
00058     for (int i = 0; i < nsectors; i++){
00059       cout << s[owners[i]] << ", ";
00060     }
00061     cout << endl;
00062   }
00063 
00064   float r = reward();
00065 
00066   calcStockRising();
00067 
00068   return r;
00069 
00070 }
00071 
00072 void Stocks::calcStockRising() {
00073 
00074   // for each sector
00075   for (int i = 0; i < nsectors; i++){
00076     // get average of stocks
00077     float sum = 0;
00078     for (int j = 0; j < nstocks; j++){
00079       sum += s[rising[i][j]];
00080     }
00081     float riseProb = 0.1 + 0.8 * (sum / (float)nstocks);
00082 
00083     // set value for each stock
00084     for (int j = 0; j < nstocks; j++){
00085       if (noisy) {
00086         s[rising[i][j]] = rng.bernoulli(riseProb);
00087       } else {
00088         s[rising[i][j]] = (riseProb > 0.5);
00089       }
00090     }
00091 
00092     if (STOCK_DEBUG){
00093       cout << "S" << i << " Prob: " << riseProb << " stocks now: ";
00094       for (int j = 0; j < nstocks; j++){
00095         cout << s[rising[i][j]] << ", ";
00096       }
00097       cout << endl;
00098     }
00099 
00100   }
00101 
00102 }
00103 
00104 float Stocks::reward() {
00105 
00106   float sum = 0.0;
00107 
00108   // for each sector
00109   for (int i = 0; i < nsectors; i++){
00110     // skip if we don't own it
00111     if (!s[owners[i]])
00112       continue;
00113 
00114     if (STOCK_DEBUG) cout << "Own sector " << i << ": ";
00115 
00116 
00117     // add 1 for each rising, sub 1 for each not
00118     for (int j = 0; j < nstocks; j++){
00119       if (s[rising[i][j]]){
00120         sum++;
00121         if (STOCK_DEBUG) cout << "rising, ";
00122       } else {
00123         sum--;
00124         if (STOCK_DEBUG) cout << "falling, ";
00125       }
00126     }
00127 
00128     if (STOCK_DEBUG) cout << endl;
00129   }
00130 
00131   if (STOCK_DEBUG)
00132     cout << "Reward is " << sum << endl;
00133 
00134   return sum;
00135 
00136 }
00137 
00138 bool Stocks::terminal() const {
00139   return false;
00140 }
00141 
00142 void Stocks::initStocks(){
00143   // set owners and rising variables
00144   owners = new int[nsectors];
00145   rising = new int*[nsectors];
00146   for (int i = 0; i < nsectors; i++){
00147     rising[i] = new int[nstocks];
00148     owners[i] = i;
00149     if (STOCK_DEBUG)
00150       cout << "Owners[" << i << "] is " << owners[i] << endl;
00151     for (int j = 0; j < nstocks; j++){
00152       rising[i][j] = nsectors + (i*nstocks) + j;
00153       if (STOCK_DEBUG)
00154         cout << "Rising[" << i << "][" << j << "] is " << rising[i][j] << endl;
00155     }
00156   }
00157 }
00158 
00159 void Stocks::reset() {
00160 
00161   // random values for each variable
00162   for (unsigned i = 0; i < s.size(); i++){
00163     s[i] = rng.uniformDiscrete(0,1);
00164   }
00165 }
00166 
00167 
00168 int Stocks::getNumActions() {
00169   return nsectors+1;
00170 }
00171 
00172 void Stocks::setSensation(std::vector<float> sIn){
00173   for (unsigned i = 0; i < s.size(); i++){
00174     s[i] = sIn[i];
00175   }
00176 }
00177 
00178 
00179 void Stocks::getMinMaxFeatures(std::vector<float> *minFeat,
00180                                std::vector<float> *maxFeat){
00181 
00182   minFeat->resize(s.size(), 0.0);
00183   maxFeat->resize(s.size(), 1.0);
00184 
00185 }
00186 
00187 void Stocks::getMinMaxReward(float *minR,
00188                              float *maxR){
00189 
00190   *minR = -(nsectors*nstocks);
00191   *maxR = (nsectors*nstocks);
00192 }


rl_env
Author(s):
autogenerated on Thu Jun 6 2019 22:00:24