matrixlinker.cpp
1 /********************************************************************************
2  * Neural Network Framework. *
3  * Copyright (C) 2005-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #include "matrixlinker.h"
21 #include "randomgenerator.h"
22 
23 using namespace Eigen;
24 
25 namespace farsa {
26 
27 MatrixLinker::MatrixLinker( Cluster* from, Cluster* to, QString name )
28  : Linker(from, to, name),
29  nrows(fromVector().size()),
30  ncols(toVector().size()),
31  w(MatrixXd::Zero(nrows, ncols)) {
32 }
33 
35  : Linker( params, prefix ),
36  nrows(fromVector().size()),
37  ncols(toVector().size()),
38  w(MatrixXd::Zero(nrows, ncols)) {
39  // the weights are read from a single vector of data supposing they are row-major arranged
40  QString str = params.getValue(prefix + "weights");
41  if (!str.isEmpty()) {
42  QStringList list = str.split(QRegExp("\\s+"), QString::SkipEmptyParts);
43  int cycleLength = nrows*ncols;
44  if (list.size() != cycleLength) {
45 #ifdef FARSA_DEBUG
46  qWarning() << "The number of elements of the weights in configuration file (" << list.size()
47  << ") is different from the total number of weights (" << cycleLength << ").";
48 #endif
49  cycleLength = std::min(list.size(), cycleLength);
50  }
51  for( int i=0; i<cycleLength; i++) {
52  bool ok;
53  unsigned int r = i/ncols; //--- division may be expensive
54  unsigned int c = i%ncols; //--- module may be expensive
55  w(r,c) = list[i].toDouble(&ok);
56  if (!ok) {
57  w(r,c) = 0.0;
58  }
59  }
60  }
61 }
62 
64 }
65 
66 unsigned int MatrixLinker::size() const {
67  return nrows*ncols;
68 }
69 
70 void MatrixLinker::randomize( double min, double max ) {
71  for ( unsigned int i = 0; i<nrows; i++ ) {
72  for ( unsigned int j = 0; j<ncols; j++ ) {
73  w(i,j) = globalRNG->getDouble( min, max );
74  }
75  }
76 }
77 
78 void MatrixLinker::setWeight( unsigned int from, unsigned int to, double weight ) {
79  w(from,to) = weight;
80 }
81 
82 double MatrixLinker::weight( unsigned int from, unsigned int to ) {
83  return w(from,to);
84 }
85 
86 void MatrixLinker::save(ConfigurationParameters& params, QString prefix) {
87  Linker::save( params, prefix );
88  params.startObjectParameters( prefix, "MatrixLinker", this );
89  // First creating a string list, then transforming to a single string
90  QStringList list;
91  for( unsigned int r=0; r<rows(); r++ ) {
92  for( unsigned int c=0; c<cols(); c++ ) {
93  list.push_back(QString::number(w(r,c)));
94  }
95  }
96  params.createParameter(prefix, "weights", list.join(" "));
97 }
98 
99 }
FARSA_UTIL_API RandomGenerator * globalRNG
unsigned int rows()
Get the number of rows.
Definition: matrixlinker.h:51
unsigned int size() const
Returns the total number of the links: rows*cols.
FARSA_UTIL_TEMPLATE const T max(const T &t1, const U &t2)
unsigned int cols()
Get the number of cols.
Definition: matrixlinker.h:55
virtual void save(ConfigurationParameters &params, QString prefix)
Save the actual status of parameters into the ConfigurationParameters object passed.
Definition: linker.cpp:58
virtual double weight(unsigned int from, unsigned int to)
Get the weight of the connection specified.
Define the common interface among Clusters.
Definition: cluster.h:73
bool startObjectParameters(QString groupPath, QString typeName, ParameterSettable *object)
QString getValue(QString path, bool alsoMatchParents=false) const
virtual ~MatrixLinker()
Destructor.
Abstract Linker Class.
Definition: linker.h:38
virtual void randomize(double min, double max)
Randomize the weights of the MatrixLinker.
FARSA_UTIL_TEMPLATE const T min(const T &t1, const U &t2)
virtual void setWeight(unsigned int from, unsigned int to, double weight)
Set the weight of the connection specified.
MatrixLinker(Cluster *from, Cluster *to, QString name="unnamed")
Connect clusters with a complete connections By default it create a fully-connected matrix...
double getDouble(double min, double max)
virtual void save(ConfigurationParameters &params, QString prefix)
Save the actual status of parameters into the ConfigurationParameters object passed.
void createParameter(QString groupPath, QString parameter)