CCA
RandomNumberGenerator.h
Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of CCA project
00004  *
00005  * Copyright (C) 2011 by Arne Nordmann <anordman at cor-lab dot uni-bielefeld dot de>
00006  *
00007  * This file may be licensed under the terms of the
00008  * GNU Lesser General Public License Version 3 (the ``LGPL''),
00009  * or (at your option) any later version.
00010  *
00011  * Software distributed under the License is distributed
00012  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
00013  * express or implied. See the LGPL for the specific language
00014  * governing rights and limitations.
00015  *
00016  * You should have received a copy of the LGPL along with this
00017  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
00018  * or write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  * The development of this software was supported by:
00022  *   CoR-Lab, Research Institute for Cognition and Robotics
00023  *     Bielefeld University
00024  *
00025  * ============================================================ */
00026 
00027 #pragma once
00028 
00029 #include <string>
00030 #include <iostream>
00031 
00032 #include <nemo/Vector.h>
00033 #include <nemo/Random.h>
00034 
00035 #include "cca/Node.h"
00036 
00037 #include "cca/dto/debug/DebugDTO.h"
00038 
00039 namespace cca {
00040 
00046 template<class DATATYPE>
00047 class RandomNumberGenerator: public Node {
00048 public:
00053     RandomNumberGenerator(std::string nodename) :
00054             Node(nodename), outputPort(), _lower(0), _upper(1), _dim(1),
00055                     _rng(_lower, _upper) {
00056         outputPort = OutputPort<DATATYPE>::create();
00057     }
00058 
00059     ~RandomNumberGenerator() {
00060     }
00061 
00067     void setRange(double lower, double upper) {
00068         this->_upper = upper;
00069         this->_lower = lower;
00070         this->_rng = nemo::uniform<double>(this->_lower, this->_upper);
00071     }
00072 
00076     void setDimension(unsigned int dimension) {
00077         this->_dim = dimension;
00078     }
00079 
00083     std::string print() const {
00084         std::ostringstream outstream(std::ostringstream::out);
00085         outstream.precision(3); // Precision when printing double values
00086         outstream << "<RandomNumberGenerator \"" << this->getName() << "\"";
00087         outstream << " (dimension: " << this->_dim << ")";
00088         outstream << ", range is [" << this->_lower << ", " << this->_upper
00089                 << "]";
00090         outstream << ">" << std::endl;
00091         return outstream.str();
00092     }
00093 
00097     static NodePtr create(std::string name = "RNG") {
00098         NodePtr rng(new RandomNumberGenerator(name));
00099         return rng;
00100     }
00101 
00102 protected:
00107     virtual void onProcess() {
00108         std::cout << "[RandomNumberGenerator] Processing..." << std::endl;
00109 
00110         nemo::RealVector values(nemo::dim(this->_dim), this->_rng);
00111 
00112         typename OutputPort<DATATYPE>::DataPtr out(
00113                 new DATATYPE(values));
00114         outputPort->publish(out);
00115     }
00116 
00117     typename OutputPort<DATATYPE>::Ptr outputPort;
00118 
00119 private:
00123     double _lower, _upper;
00124 
00128     unsigned int _dim;
00129 
00133     mutable nemo::uniform<double> _rng;
00134 };
00135 
00136 }