evonet.h
1 /********************************************************************************
2  * FARSA Experiments Library *
3  * Copyright (C) 2007-2012 *
4  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
5  * Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it> *
6  * Gianluca Massera <emmegian@yahoo.it> *
7  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the Free Software *
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
22  ********************************************************************************/
23 
24 #ifndef EVONET_H
25 #define EVONET_H
26 
27 #include "experimentsconfig.h"
28 #include "parametersettable.h"
29 #include <stdio.h>
30 #include <math.h>
31 #include <string.h>
32 #include <iostream>
33 #include <stdlib.h>
34 #include <QString>
35 #include <QStringList>
36 #include <QColor>
37 #include <QDebug>
38 #include <QObject>
39 
40 namespace farsa {
41 
48 class FARSA_EXPERIMENTS_API Evonet : public QObject, public ParameterSettableWithConfigureFunction {
49  Q_OBJECT
50 
51  friend class NetworkDialog;
52  friend class RendNetwork;
53 signals:
57  void evonetUpdated();
58 public:
60  static const int MAXSTOREDACTIVATIONS = 100;
62  static const int MAXN = 1000;
64  static const float DEFAULT_VALUE;
75  void configure(ConfigurationParameters& params, QString prefix);
85  void save(ConfigurationParameters& params, QString prefix);
89  static void describe( QString type );
92  ParameterSettableUI* getUIManager();
98  void create_net_block( int inputNeuronType, int hiddenNeuronType, int outputNeuronType, bool recurrentHiddens, bool inputOutputConnections, bool recurrentOutputs, bool biasOnHidden, bool biasOnOutput );
104  int load_net_blocks(const char *filename, int mode);
105  void save_net_blocks(const char *filename, int mode);
106  void readNewPheLine(QStringList, float*, float*);
107  void readOldPheLine(QStringList, float*, float*);
108  void updateNet();
109  void computeParameters();
110  int setInput(int inp, float value); //
111  float getOutput(int out);
112  float getInput(int in);
113  float getHidden(int h);
114  void resetNet();
115  int freeParameters(); //return number of free parameters
116  void getParameters(const int* dt);
117  void getMutations(float* mut);
118  void copyPheParameters(int* pheGene);
119  void printIO();//to print inputs and outputs
120  int getParamBias(int nbias);
121  float getWrange();
122  void injectHidden(int nh, float val);
123  float logistic(float f);
124  void printBlocks();
125  int getNoInputs();
126  int getNoHiddens();
127  int getNoOutputs();
128  int getNoNeurons();
129  float getNeuron(int in);
130  bool pheFileLoaded();
131  Evonet();
132  char neuronl[MAXN][10];
133  int neurondisplay[MAXN]; // whether neurons should be displayed or not
134  double neuronrange[MAXN][2]; // the range of variation of the neuron
135  QColor neurondcolor[MAXN]; // the color used by the neuron monitor
136  bool neuronlesion[MAXN]; // if >0 the n neurons will be silented
137  float neuronlesionVal[MAXN]; //value to be assigned to the lesioned neuron
138  void setRanges(double weight, double bias, double gain); // set ranges of weights, biases and gains
149  float* getOldestStoredActivations();
155  int updateCounts();
156  float **selectedp; //pointer to a list of selected parameters
157  int nselected; //number of selected parameters
158  int neuronlesions; // whether some of the neurons has been lesioned or manually set
159 
160 private:
161  int ninputs; // n. input units
162  int nhiddens; // n. hidden units
163  int noutputs; // n. output units
164  int nneurons; // total n. of neurons
165  int net_nblocks; // network number of connection blocks
166  int net_block[MAXN][6]; // blocks: 0=(connection,update,gain), 12=(id first neuron and n.neurons), 34=(id second neuron and n.neurons, optional),5=(genetic, fixed, backprop)
167  int neuronbias[MAXN]; // whether neurons have bias
168  int neurontype[MAXN]; // the type of neuron (0=logistic,1=dynamic with timeconstant, 2=binary, 3=logistic netinput*0.2
169  int neurongain[MAXN]; // whether neurons have gain parameters
170  int neuronxy[MAXN][2]; // neurons xy position in display space
171  float wrange; // weights range
172  float grange; // gain range
173  float brange; // bias range
174  int nparameters; // usefull to set GA parameters number of free parameters
175  int nparambias; // number of parametric bias to add to the number of genes need for the nnet
176  // neurons label by default Input In0 In1.. Hidden: Hid0 Hid1 Output: Out0
177 
178  //variable to compute activation
179  float act[MAXN]; //activation
180  float storedActivations[MAXSTOREDACTIVATIONS][MAXN]; // Stored activations
181  int nextStoredActivation; // The index where the next activation will be stored
182  int firstStoredActivation; // The index of the first activation that was stored (since the stored activations were reset)
183  float input[MAXN]; //input vector
184  float netinput[MAXN];
185  float *freep; //dynamically resized to contain needed parameters
186  float *phep; //dynamically resized to contain manually set values specified in .phe files
187  bool pheloaded; //whether a .phe file has been loaded
188  float *muts; //mutation parameters
189  int geneMaxValue; //how parameters are codified into the GA (es. 255, 511, 1023 <0 is included!>)
190 
191  // counter of updates of the network; resetted to zero by resetNet, incremented by updateNet
192  int updatescounter;
193 
194  int p; //usato come puntatore per data
195  int ndata; //number of free parameters
196  //variable for rendering
197  int drawnymax;
198  int drawnxmax;
199 
200  // ConfigurationParameters variables
201  QString netFile;
202 };
203 
204 } // end namespace farsa
205 
206 #endif