00001 /************************************************************************ 00002 * Verve * 00003 * Copyright (C) 2004-2006 * 00004 * Tyler Streeter tylerstreeter@gmail.com * 00005 * All rights reserved. * 00006 * Web: http://verve-agents.sourceforge.net * 00007 * * 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of EITHER: * 00010 * (1) The GNU Lesser General Public License as published by the Free * 00011 * Software Foundation; either version 2.1 of the License, or (at * 00012 * your option) any later version. The text of the GNU Lesser * 00013 * General Public License is included with this library in the * 00014 * file license-LGPL.txt. * 00015 * (2) The BSD-style license that is included with this library in * 00016 * the file license-BSD.txt. * 00017 * * 00018 * This library is distributed in the hope that it will be useful, * 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files * 00021 * license-LGPL.txt and license-BSD.txt for more details. * 00022 ************************************************************************/ 00023 00024 #include "Observation.h" 00025 #include "Agent.h" 00026 00027 namespace verve 00028 { 00029 Observation::Observation() 00030 { 00031 mNumDiscreteDimensions = 0; 00032 mNumContinuousDimensions = 0; 00033 mDiscreteInputData = NULL; 00034 mDiscreteNumOptionsData = NULL; 00035 mContinuousInputData = NULL; 00036 mContinuousCircularData = NULL; 00037 mContinuousResolution = 0; 00038 } 00039 00040 Observation::~Observation() 00041 { 00042 if (mDiscreteInputData) 00043 { 00044 delete [] mDiscreteInputData; 00045 } 00046 00047 if (mDiscreteNumOptionsData) 00048 { 00049 delete [] mDiscreteNumOptionsData; 00050 } 00051 00052 if (mContinuousInputData) 00053 { 00054 delete [] mContinuousInputData; 00055 } 00056 00057 if (mContinuousCircularData) 00058 { 00059 delete [] mContinuousCircularData; 00060 } 00061 } 00062 00063 void Observation::init(const Agent& a) 00064 { 00065 const AgentDescriptor* desc = a.getDescriptor(); 00066 00067 mNumDiscreteDimensions = desc->getNumDiscreteSensors(); 00068 mNumContinuousDimensions = desc->getNumContinuousSensors(); 00069 00070 if (0 == mNumDiscreteDimensions) 00071 { 00072 mDiscreteInputData = NULL; 00073 mDiscreteNumOptionsData = NULL; 00074 } 00075 else 00076 { 00077 mDiscreteInputData = new unsigned int[mNumDiscreteDimensions]; 00078 for (unsigned int i = 0; i < mNumDiscreteDimensions; ++i) 00079 { 00080 mDiscreteInputData[i] = 0; 00081 } 00082 00083 mDiscreteNumOptionsData = 00084 new unsigned int[mNumDiscreteDimensions]; 00085 for (unsigned int i = 0; i < mNumDiscreteDimensions; ++i) 00086 { 00087 mDiscreteNumOptionsData[i] = 00088 desc->getDiscreteSensorNumOptions(i); 00089 } 00090 } 00091 00092 if (0 == mNumContinuousDimensions) 00093 { 00094 mContinuousInputData = NULL; 00095 mContinuousCircularData = NULL; 00096 } 00097 else 00098 { 00099 mContinuousInputData = new real[mNumContinuousDimensions]; 00100 for (unsigned int i = 0; i < mNumContinuousDimensions; ++i) 00101 { 00102 mContinuousInputData[i] = 0; 00103 } 00104 00105 mContinuousCircularData = new bool[mNumContinuousDimensions]; 00106 for (unsigned int i = 0; i < mNumContinuousDimensions; ++i) 00107 { 00108 mContinuousCircularData[i] = 00109 desc->getContinuousSensorIsCircular(i); 00110 } 00111 } 00112 00113 mContinuousResolution = desc->getContinuousSensorResolution(); 00114 } 00115 00116 void Observation::init(const Observation& obs) 00117 { 00118 mNumDiscreteDimensions = obs.mNumDiscreteDimensions; 00119 mNumContinuousDimensions = obs.mNumContinuousDimensions; 00120 00121 if (0 == mNumDiscreteDimensions) 00122 { 00123 mDiscreteInputData = NULL; 00124 mDiscreteNumOptionsData = NULL; 00125 } 00126 else 00127 { 00128 mDiscreteInputData = new unsigned int[mNumDiscreteDimensions]; 00129 for (unsigned int i = 0; i < mNumDiscreteDimensions; ++i) 00130 { 00131 mDiscreteInputData[i] = 0; 00132 } 00133 00134 mDiscreteNumOptionsData = new unsigned int[mNumDiscreteDimensions]; 00135 for (unsigned int i = 0; i < mNumDiscreteDimensions; ++i) 00136 { 00137 mDiscreteNumOptionsData[i] = 00138 obs.mDiscreteNumOptionsData[i]; 00139 } 00140 } 00141 00142 if (0 == mNumContinuousDimensions) 00143 { 00144 mContinuousInputData = NULL; 00145 mContinuousCircularData = NULL; 00146 } 00147 else 00148 { 00149 mContinuousInputData = new real[mNumContinuousDimensions]; 00150 for (unsigned int i = 0; i < mNumContinuousDimensions; ++i) 00151 { 00152 mContinuousInputData[i] = 0; 00153 } 00154 00155 mContinuousCircularData = new bool[mNumContinuousDimensions]; 00156 for (unsigned int i = 0; i < mNumContinuousDimensions; ++i) 00157 { 00158 mContinuousCircularData[i] = obs.mContinuousCircularData[i]; 00159 } 00160 } 00161 00162 mContinuousResolution = obs.mContinuousResolution; 00163 00164 copyInputData(obs); 00165 } 00166 00167 void Observation::zeroInputData() 00168 { 00169 for (unsigned int i = 0; i < mNumDiscreteDimensions; ++i) 00170 { 00171 mDiscreteInputData[i] = 0; 00172 } 00173 00174 for (unsigned int i = 0; i < mNumContinuousDimensions; ++i) 00175 { 00176 mContinuousInputData[i] = 0; 00177 } 00178 } 00179 00180 void Observation::copyInputData(const Observation& obs) 00181 { 00182 for (unsigned int i = 0; i < mNumDiscreteDimensions; ++i) 00183 { 00184 mDiscreteInputData[i] = obs.mDiscreteInputData[i]; 00185 } 00186 00187 for (unsigned int i = 0; i < mNumContinuousDimensions; ++i) 00188 { 00189 mContinuousInputData[i] = obs.mContinuousInputData[i]; 00190 } 00191 } 00192 00193 unsigned int Observation::getNumDiscreteInputs()const 00194 { 00195 return mNumDiscreteDimensions; 00196 } 00197 00198 unsigned int Observation::getNumContinuousInputs()const 00199 { 00200 return mNumContinuousDimensions; 00201 } 00202 00203 void Observation::setDiscreteValue(unsigned int index, 00204 unsigned int value) 00205 { 00206 assert(value >= 0 && value < mDiscreteNumOptionsData[index]); 00207 mDiscreteInputData[index] = value; 00208 } 00209 00210 unsigned int Observation::getDiscreteValue(unsigned int index)const 00211 { 00212 return mDiscreteInputData[index]; 00213 } 00214 00215 const unsigned int* Observation::getDiscreteInputData()const 00216 { 00217 return mDiscreteInputData; 00218 } 00219 00220 const unsigned int* Observation::getDiscreteNumOptionsData()const 00221 { 00222 return mDiscreteNumOptionsData; 00223 } 00224 00225 void Observation::setContinuousValue(unsigned int index, real value) 00226 { 00227 assert(value >= -1 && value <= 1); 00228 mContinuousInputData[index] = value; 00229 } 00230 00231 real Observation::getContinuousValue(unsigned int index)const 00232 { 00233 return mContinuousInputData[index]; 00234 } 00235 00236 const real* Observation::getContinuousInputData()const 00237 { 00238 return mContinuousInputData; 00239 } 00240 00241 const bool* Observation::getContinuousCircularData()const 00242 { 00243 return mContinuousCircularData; 00244 } 00245 00246 unsigned int Observation::getDiscreteInputNumOptions(unsigned int i)const 00247 { 00248 return mDiscreteNumOptionsData[i]; 00249 } 00250 00251 bool Observation::getContinuousInputIsCircular(unsigned int i)const 00252 { 00253 return mContinuousCircularData[i]; 00254 } 00255 00256 unsigned int Observation::getContinuousResolution()const 00257 { 00258 return mContinuousResolution; 00259 } 00260 }