StateSpace.c
Go to the documentation of this file.00001 #include "StateSpace.h"
00002
00003
00005 StateSpace::StateSpace(Environment* env) {
00006
00007
00008 this->useInternalRAM = true;
00009
00010
00011 ram1 = new Ram();
00012
00013
00014 ENV_POS d, pos;
00015 env->getDiscretization(&d);
00016 internStateSpace = new unsigned char[d.x * d.y];
00017
00018
00019 this->env = env;
00020
00021
00022 STATE tmpState;
00023
00024 for (int _row=0; _row < d.y; _row++) {
00025 for (int _column=0; _column<d.x; _column++) {
00026
00027 pos.x = _column;
00028 pos.y = _row;
00029
00030 tmpState.value = 0.0;
00031
00032 tmpState.feedback_up = 0;
00033 tmpState.feedback_down = 0;
00034 tmpState.feedback_left = 0;
00035 tmpState.feedback_right = 0;
00036
00037 setState (&pos, &tmpState);
00038 }
00039 }
00040 }
00041
00043 StateSpace::~StateSpace() {
00044 delete ram1;
00045 delete[] internStateSpace;
00046 }
00047
00048
00050 void StateSpace::getState (ENV_POS* pos, STATE *_state) {
00051
00052 unsigned int _addr;
00053 unsigned char i;
00054 unsigned char* _bytes = (unsigned char*)_state;
00055
00056 ENV_POS d;
00057 env->getDiscretization(&d);
00058
00059
00060 _addr = ((pos->y * d.x) + pos->x)*STATESIZE;
00061
00062
00063 if (this->useInternalRAM) {
00064 for (i=0; i<STATESIZE; i++) {
00065 _bytes[i] = internStateSpace[(_addr + i)];
00066 }
00067
00068
00069 } else {
00070 ram1->readBytes(_addr, (unsigned char*)&_state[0], STATESIZE);
00071 }
00072 }
00073
00075 void StateSpace::setState (ENV_POS* pos, STATE *_state) {
00076
00077 unsigned int _addr;
00078 unsigned char i;
00079 unsigned char* _bytes = (unsigned char*)_state;
00080
00081 ENV_POS d;
00082 env->getDiscretization(&d);
00083
00084
00085 _addr = ((pos->y * d.x) + pos->x)*STATESIZE;
00086
00087
00088 if (this->useInternalRAM) {
00089 for (i=0; i<STATESIZE; i++) {
00090 internStateSpace[_addr + i] = _bytes[i];
00091 }
00092
00093
00094 } else {
00095 ram1->writeBytes(_addr, (unsigned char*)&_state[0], STATESIZE);
00096 }
00097 }
00098
00100 void StateSpace::setValue(ENV_POS* pos, float _value) {
00101
00102 STATE _s;
00103 this->getState ( pos, &_s );
00104 _s.value = _value;
00105 this->setState ( pos, &_s );
00106 }
00107
00109 void StateSpace::setFeedback(ENV_POS* pos, unsigned char _action, char _fb) {
00110
00111 STATE _s;
00112 this->getState (pos, &_s);
00113
00114 if (_action == UP) {
00115 _s.feedback_up = _fb;
00116 } else if (_action == DOWN) {
00117 _s.feedback_down = _fb;
00118 } else if (_action == LEFT) {
00119 _s.feedback_left = _fb;
00120 } else if (_action == RIGHT) {
00121 _s.feedback_right = _fb;
00122 }
00123
00124 this->setState (pos, &_s);
00125 }
00126
00128 double StateSpace::getValue(ENV_POS* pos) {
00129 STATE _s;
00130 this->getState (pos, &_s);
00131 return _s.value;
00132 }
00133
00135 char StateSpace::getFeedback (ENV_POS* pos, unsigned char _action) {
00136
00137 STATE _s;
00138 this->getState (pos, &_s);
00139
00140 if (_action == UP) {
00141 return _s.feedback_up;
00142 } else if (_action == DOWN) {
00143 return _s.feedback_down;
00144 } else if (_action == LEFT) {
00145 return _s.feedback_left;
00146 } else if (_action == RIGHT) {
00147 return _s.feedback_right;
00148 }
00149 }
00150
00151
00153 void StateSpace::ramtestSaveBytes(unsigned int _addr, unsigned char *data, unsigned char _length) {
00154
00155 unsigned char* statesPtr = (unsigned char*)&internStateSpace[0];
00156 unsigned char i;
00157
00158 if (this->useInternalRAM) {
00159 for (i=0; i<_length; i++) {
00160 statesPtr[_addr + i] = data[i];
00161 }
00162
00163 } else {
00164 ram1->writeBytes(_addr, data, _length);
00165 }
00166 }
00167
00169 void StateSpace::ramtestReadBytes(unsigned int _addr, unsigned char *data, unsigned char _length) {
00170
00171 unsigned char* statesPtr = (unsigned char*)&internStateSpace[0];
00172 unsigned char i;
00173
00174 if (this->useInternalRAM) {
00175 for (i=0; i<_length; i++) {
00176 data[i] = statesPtr[_addr + i];
00177 }
00178
00179 } else {
00180 ram1->readBytes(_addr, data, _length);
00181 }
00182 }
00183
00185 unsigned int StateSpace::getMemorySize() {
00186
00187
00188 if (this->useInternalRAM) {
00189 return sizeof(internStateSpace);
00190
00191
00192 } else {
00193 return 65535;
00194 }
00195 }
00196