RBFInputData.cpp

Go to the documentation of this file.
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 "RBFInputData.h"
00025 #include "Observation.h"
00026 
00027 namespace verve
00028 {
00029         RBFInputData::RBFInputData()
00030         {
00031                 numDiscInputs = 0;
00032                 discNumOptionsData = NULL;
00033                 discInputData = NULL;
00034                 numContInputs = 0;
00035                 contResolution = 0;
00036                 contCircularData = NULL;
00037                 contInputData = NULL;
00038         }
00039 
00040         RBFInputData::~RBFInputData()
00041         {
00042                 if (discNumOptionsData)
00043                 {
00044                         delete [] discNumOptionsData;
00045                 }
00046 
00047                 if (discInputData)
00048                 {
00049                         delete [] discInputData;
00050                 }
00051 
00052                 if (contCircularData)
00053                 {
00054                         delete [] contCircularData;
00055                 }
00056 
00057                 if (contInputData)
00058                 {
00059                         delete [] contInputData;
00060                 }
00061         }
00062 
00063         void RBFInputData::init(unsigned int numDiscreteInputs, 
00064                 const unsigned int* discreteNumOptionsData, 
00065                 const unsigned int* discreteInputData, 
00066                 unsigned int numContinuousInputs, 
00067                 unsigned int continuousResolution, 
00068                 const bool* continuousCircularData, 
00069                 const real* continuousInputData)
00070         {
00071                 if (discNumOptionsData)
00072                 {
00073                         delete [] discNumOptionsData;
00074                 }
00075 
00076                 if (discInputData)
00077                 {
00078                         delete [] discInputData;
00079                 }
00080 
00081                 if (contCircularData)
00082                 {
00083                         delete [] contCircularData;
00084                 }
00085 
00086                 if (contInputData)
00087                 {
00088                         delete [] contInputData;
00089                 }
00090 
00091                 numDiscInputs = numDiscreteInputs;
00092                 discNumOptionsData = new unsigned int[numDiscreteInputs];
00093                 discInputData = new unsigned int[numDiscreteInputs];
00094                 for (unsigned int i = 0; i < numDiscreteInputs; ++i)
00095                 {
00096                         discNumOptionsData[i] = discreteNumOptionsData[i];
00097                         discInputData[i] = discreteInputData[i];
00098                 }
00099 
00100                 numContInputs = numContinuousInputs;
00101                 contResolution = continuousResolution;
00102                 contCircularData = new bool[numContinuousInputs];
00103                 contInputData = new real[numContinuousInputs];
00104                 for (unsigned int i = 0; i < numContinuousInputs; ++i)
00105                 {
00106                         contCircularData[i] = continuousCircularData[i];
00107                         contInputData[i] = continuousInputData[i];
00108                 }
00109         }
00110 
00111         void RBFInputData::init(const Observation& obs)
00112         {
00113                 if (discNumOptionsData)
00114                 {
00115                         delete [] discNumOptionsData;
00116                 }
00117 
00118                 if (discInputData)
00119                 {
00120                         delete [] discInputData;
00121                 }
00122 
00123                 if (contCircularData)
00124                 {
00125                         delete [] contCircularData;
00126                 }
00127 
00128                 if (contInputData)
00129                 {
00130                         delete [] contInputData;
00131                 }
00132 
00133                 numDiscInputs = obs.getNumDiscreteInputs();
00134                 discNumOptionsData = new unsigned int[numDiscInputs];
00135                 discInputData = new unsigned int[numDiscInputs];
00136                 for (unsigned int i = 0; i < numDiscInputs; ++i)
00137                 {
00138                         discNumOptionsData[i] = obs.getDiscreteInputNumOptions(i);
00139                         discInputData[i] = obs.getDiscreteValue(i);
00140                 }
00141 
00142                 numContInputs = obs.getNumContinuousInputs();
00143                 contResolution = obs.getContinuousResolution();
00144                 contCircularData = new bool[numContInputs];
00145                 contInputData = new real[numContInputs];
00146                 for (unsigned int i = 0; i < numContInputs; ++i)
00147                 {
00148                         contCircularData[i] = obs.getContinuousInputIsCircular(i);
00149                         contInputData[i] = obs.getContinuousValue(i);
00150                 }
00151         }
00152 
00153         void RBFInputData::zeroInputData()
00154         {
00155                 for (unsigned int i = 0; i < numDiscInputs; ++i)
00156                 {
00157                         discInputData[i] = 0;
00158                 }
00159 
00160                 for (unsigned int i = 0; i < numContInputs; ++i)
00161                 {
00162                         contInputData[i] = 0;
00163                 }
00164         }
00165 
00166         void RBFInputData::copyInputData(const unsigned int* discreteInputData, 
00167                 const real* continuousInputData)
00168         {
00169                 for (unsigned int i = 0; i < numDiscInputs; ++i)
00170                 {
00171                         discInputData[i] = discreteInputData[i];
00172                 }
00173 
00174                 for (unsigned int i = 0; i < numContInputs; ++i)
00175                 {
00176                         contInputData[i] = continuousInputData[i];
00177                 }
00178         }
00179 
00180         unsigned int RBFInputData::computeNumUniqueStates(
00181                 unsigned int continuousResolution)const
00182         {
00183                 // We use the given resolution here, not the internal one.
00184 
00185                 unsigned int numStates = (unsigned int)globals::pow(
00186                         (real)continuousResolution, (real)numContInputs);
00187 
00188                 if (0 == continuousResolution)
00189                 {
00190                         // This value should always be at least 1.
00191                         numStates = 1;
00192                 }
00193 
00194                 for (unsigned int i = 0; i < numDiscInputs; ++i)
00195                 {
00196                         numStates *= discNumOptionsData[i];
00197                 }
00198 
00199                 return numStates;
00200         }
00201 
00202         void RBFInputData::setToUniqueState(unsigned int index, 
00203                 unsigned int numStates, unsigned int continuousResolution)
00204         {
00205                 // We use the given resolution here, not the internal one.
00206 
00207                 if (0 == numDiscInputs && 0 == numContInputs 
00208                         || numContInputs > 0 && 0 == continuousResolution)
00209                 {
00210                         return;
00211                 }
00212 
00213                 // Here we are basically converting a single number (the 
00214                 // state index) to an n-digit number, where n is the number 
00215                 // of dimensions (including discrete and continuous), and each 
00216                 // dimension can have a different base.
00217 
00218                 // Loop over all input dimensions.
00219                 for (unsigned int i = 0; i < numDiscInputs + numContInputs; ++i)
00220                 {
00221                         unsigned int value = 0;
00222 
00223                         // Compute the number of "sub-possibilities" (i.e. the number 
00224                         // of possible values for all "lower" dimensions in the 
00225                         // recursive descent).
00226                         if (i < numDiscInputs)
00227                         {
00228                                 // We are still handling discrete input dimensions.
00229                                 numStates = numStates / discNumOptionsData[i];
00230                         }
00231                         else
00232                         {
00233                                 // We have started handling continuous input dimensions.
00234                                 numStates = numStates / continuousResolution;
00235                         }
00236 
00237                         while (index >= numStates)
00238                         {
00239                                 value += 1;
00240                                 index -= numStates;
00241                         }
00242 
00243                         // Set the value for this dimension.
00244                         if (i < numDiscInputs)
00245                         {
00246                                 // We are still handling discrete input dimensions.
00247                                 discInputData[i] = value;
00248                         }
00249                         else
00250                         {
00251                                 unsigned int continuousIndex = i - numDiscInputs;
00252                                 // We have started handling continuous input dimensions.  
00253                                 // Handle the special case where the resolution is 1.
00254                                 if (1 == continuousResolution)
00255                                 {
00256                                         contInputData[continuousIndex] = 0;
00257                                 }
00258                                 else
00259                                 {
00260                                         // Compute the distance between successive values for 
00261                                         // the continuous dimensions, assuming there are 
00262                                         // 'continuousResolution' positions for each one.  For 
00263                                         // circular inputs, we don't place an RBF at the 
00264                                         // extreme ends but 1/2 inter-RBF distance away from 
00265                                         // the ends.  For normal continuous inputs we put one 
00266                                         // at each end.
00267 
00268                                         if (contCircularData[continuousIndex])
00269                                         {
00270                                                 real continuousIncrement = 
00271                                                         2 / (real)continuousResolution;
00272                                                 contInputData[continuousIndex] = -1 + 
00273                                                         (verve::real)0.5 * continuousIncrement + 
00274                                                         value * continuousIncrement;
00275                                         }
00276                                         else
00277                                         {
00278                                                 real continuousIncrement = 
00279                                                         2 / (real)(continuousResolution - 1);
00280                                                 contInputData[continuousIndex] = -1 + 
00281                                                         + value * continuousIncrement;
00282                                         }
00283                                 }
00284                         }
00285                 }
00286         }
00287 }

Generated on Tue Jan 24 21:46:37 2006 for Verve by  doxygen 1.4.6-NO