learningalgorithm.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 "neuralnet.h"
21 #include "learningalgorithm.h"
22 #include "factory.h"
23 
24 namespace farsa {
25 
26 void Pattern::setInputsOf( Cluster* cl, const DoubleVector& ins ) {
27  pinfo[cl].inputs.resize( cl->numNeurons() );
28  pinfo[cl].inputs = ins;
29 };
30 
31 void Pattern::setOutputsOf( Cluster* cl, const DoubleVector& ous ) {
32  pinfo[cl].outputs.resize( cl->numNeurons() );
33  pinfo[cl].outputs = ous;
34 };
35 
36 void Pattern::setInputsOutputsOf( Cluster* cl, const DoubleVector& ins, const DoubleVector& ous ) {
37  pinfo[cl].inputs.resize( cl->numNeurons() );
38  pinfo[cl].inputs = ins;
39  pinfo[cl].outputs.resize( cl->numNeurons() );
40  pinfo[cl].outputs = ous;
41 };
42 
43 const DoubleVector& Pattern::inputsOf( Cluster* cl ) const {
44  if ( pinfo.count(cl) != 0 ) {
45  return pinfo[cl].inputs;
46  } else {
47  // FIXME: maybe we should raise an exception
48  static DoubleVector vecNull;
49  return vecNull;
50  }
51 };
52 
53 const DoubleVector& Pattern::outputsOf( Cluster* cl ) const {
54  if ( pinfo.count(cl) != 0 ) {
55  return pinfo[cl].outputs;
56  } else {
57  // FIXME: maybe we should raise an exception
58  static DoubleVector vecNull;
59  return vecNull;
60  }
61 };
62 
64  return pinfo[cl];
65 };
66 
67 void Pattern::configure(ConfigurationParameters& params, QString prefix) {
68  //--- get all parameters with the prefix 'cluster:'
69  QStringList clusterList = params.getParametersWithPrefixList( prefix, "cluster:" );
70  foreach( QString cluster, clusterList ) {
71  QString id = cluster.split(':')[1];
72  if ( id.isEmpty() ) continue;
73  //--- now, it check if there is a inputs and outputs parameter and load it
74  QString str = params.getValue( prefix + "inputs:" + id );
75  DoubleVector inputs;
76  if (!str.isEmpty()) {
77  QStringList list = str.split(QRegExp("\\s+"), QString::SkipEmptyParts);
78  inputs.resize( list.size() );
79  for( int i=0; i<list.size(); i++) {
80  inputs[i] = list[i].toDouble();
81  }
82  }
83  str = params.getValue( prefix + "outputs:" + id );
84  DoubleVector outputs;
85  if (!str.isEmpty()) {
86  QStringList list = str.split(QRegExp("\\s+"), QString::SkipEmptyParts);
87  outputs.resize( list.size() );
88  for( int i=0; i<list.size(); i++) {
89  outputs[i] = list[i].toDouble();
90  }
91  }
92  if ( inputs.size() == 0 && outputs.size() == 0 ) continue;
93  Cluster* cl = params.getObjectFromParameter<Cluster>( prefix+cluster, false, true );
94  if ( inputs.size() > 0 ) {
95  setInputsOf( cl, inputs );
96  }
97  if ( outputs.size() > 0 ) {
98  setOutputsOf( cl, outputs );
99  }
100  }
101 }
102 
103 void Pattern::save(ConfigurationParameters& params, QString prefix) {
104  params.startObjectParameters(prefix, "Pattern", this);
105  QString tmpl = "%1:%2";
106  QList<Cluster*> cls = pinfo.keys();
107  for( int i=0; i<cls.size(); i++ ) {
108  PatternInfo& info = pinfo[ cls[i] ];
109  params.createParameter(prefix, tmpl.arg("cluster").arg(i), cls[i]);
110  if ( info.inputs.size() > 0 ) {
111  QStringList list;
112  for( int j=0; j<info.inputs.size(); j++ ) {
113  list.push_back(QString::number(info.inputs[j]));
114  }
115  params.createParameter(prefix, tmpl.arg("inputs").arg(i), list.join(" "));
116  }
117  if ( info.outputs.size() > 0 ) {
118  QStringList list;
119  for( int j=0; j<info.outputs.size(); j++ ) {
120  list.push_back(QString::number(info.outputs[j]));
121  }
122  params.createParameter(prefix, tmpl.arg("outputs").arg(i), list.join(" "));
123  }
124  }
125 }
126 
127 void Pattern::describe( QString type ) {
128  Descriptor d = addTypeDescription( type, "Represent a pattern of inputs/outputs for Clusters", "A Pattern is specified by groups of three parameters: cluster, inputs and outputs. The inputs and outputs parameters specify the values to set on the neurons of the cluster specified by the corresponding cluster parameter. The inputs and outputs parameter are not mandatory but specify a cluster without setting inputs or outputs has no effect" );
129  d.describeObject( "cluster" ).type( "Cluster" ).props( IsMandatory | AllowMultiple ).help( "The Cluster on which the inputs and outputs parameters referes" );
130  d.describeReal( "inputs" ).props( IsList | AllowMultiple ).help( "The values to set on the cluster's input neurons" );
131  d.describeReal( "outputs" ).props( IsList | AllowMultiple ).help( "The values to set on the cluster's output neurons" );
132 }
133 
136  this->netp = net;
137 }
138 
141  this->netp = NULL;
142 }
143 
145 }
146 
147 PatternSet LearningAlgorithm::loadPatternSet( ConfigurationParameters& params, QString path, QString prefix ) {
149  //--- convert to PatternSet
150  PatternSet patternSet;
151  foreach( QString group, params.getGroupsWithPrefixList(path, prefix) ) {
152  patternSet << *(params.getObjectFromGroup<Pattern>( path + "/" + group ));
153  }
154 #if defined(__GNUC__) && defined(DEVELOPER_WARNINGS)
155  #warning Se patternSet copia il Pattern creato all interno, allora quelli creati qui creano un leak perche non vengono mai distrutti !!
156 #endif
158  return patternSet;
159 }
160 
161 void LearningAlgorithm::savePatternSet( PatternSet& set, ConfigurationParameters& params, QString prefix ) {
162  QString tmpl = prefix+":%1";
163  for( int i=0; i<set.size(); i++ ) {
164  QString group = tmpl.arg(i);
165  params.createGroup( group );
166  set[i].save( params, group );
167  }
168 }
169 
170 }
void setOutputsOf(Cluster *, const DoubleVector &)
set the outputs associated with Cluster passed
TypeToCreate * getObjectFromParameter(QString param, bool alsoMatchParents=false, bool configure=true, bool forceObjectCreation=false)
PatternSet loadPatternSet(ConfigurationParameters &params, QString path, QString prefix)
Utility function for loading a PatternSet from a ConfigurationParameters.
const DoubleVector & outputsOf(Cluster *) const
return stored information if exists, otherwise it return a zero vector
void setInputsOutputsOf(Cluster *, const DoubleVector &inputs, const DoubleVector &outputs)
set the both inputs and outputs associated with Cluster passed
static void describe(QString type)
Add to Factory::typeDescriptions() the descriptions of all parameters and subgroups.
QStringList getParametersWithPrefixList(QString group, QString prefix) const
const DoubleVector & inputsOf(Cluster *) const
return stored information if exists, otherwise it return a zero vector
void createGroup(QString groupPath)
void setInputsOf(Cluster *, const DoubleVector &)
set the inputs associated with Cluster passed
Define the common interface among Clusters.
Definition: cluster.h:73
bool startObjectParameters(QString groupPath, QString typeName, ParameterSettable *object)
The Neural Network Class.
Definition: neuralnet.h:221
static Descriptor addTypeDescription(QString type, QString shortHelp, QString longHelp=QString(""))
QString getValue(QString path, bool alsoMatchParents=false) const
virtual void save(ConfigurationParameters &params, QString prefix)
Save the actual status of parameters into the ConfigurationParameters object passed.
TypeToCreate * getObjectFromGroup(QString group, bool configure=true, bool forceObjectCreation=false)
PatternInfo & operator[](Cluster *)
return the stored information
Pattern object.
QStringList getGroupsWithPrefixList(QString group, QString prefix) const
virtual ~LearningAlgorithm()
Destructor.
void savePatternSet(PatternSet &set, ConfigurationParameters &params, QString prefix)
Utility function for saving a PatternSet to a ConfigurationParameters.
virtual void configure(ConfigurationParameters &params, QString prefix)
Configures the object using a ConfigurationParameters object.
unsigned int numNeurons() const
Return the number of neurons (the length of input and output arrays)
Definition: cluster.h:82
void createParameter(QString groupPath, QString parameter)
This file contains the declaration of Neural Network Class.