experiments/include/evonet.h

00001 /********************************************************************************
00002  *  FARSA Experiments Library                                                   *
00003  *  Copyright (C) 2007-2012                                                     *
00004  *  Stefano Nolfi <stefano.nolfi@istc.cnr.it>                                   *
00005  *  Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it>                           *
00006  *  Gianluca Massera <emmegian@yahoo.it>                                        *
00007  *  Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it>                         *
00008  *                                                                              *
00009  *  This program is free software; you can redistribute it and/or modify        *
00010  *  it under the terms of the GNU General Public License as published by        *
00011  *  the Free Software Foundation; either version 2 of the License, or           *
00012  *  (at your option) any later version.                                         *
00013  *                                                                              *
00014  *  This program is distributed in the hope that it will be useful,             *
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00017  *  GNU General Public License for more details.                                *
00018  *                                                                              *
00019  *  You should have received a copy of the GNU General Public License           *
00020  *  along with this program; if not, write to the Free Software                 *
00021  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00022  ********************************************************************************/
00023 
00024 #ifndef EVONET_H
00025 #define EVONET_H
00026 
00027 #include "experimentsconfig.h"
00028 #include "parametersettable.h"
00029 #include <stdio.h>
00030 #include <math.h>
00031 #include <string.h>
00032 #include <iostream>
00033 #include <stdlib.h>
00034 #include <QString>
00035 #include <QStringList>
00036 #include <QColor>
00037 #include <QDebug>
00038 #include <QObject>
00039 
00040 namespace farsa {
00041 
00048 class FARSA_EXPERIMENTS_API Evonet : public QObject, public ParameterSettableWithConfigureFunction {
00049     Q_OBJECT
00050 
00051     friend class NetworkDialog;
00052     friend class RendNetwork;
00053 signals:
00057     void evonetUpdated();
00058 public:
00060     static const int MAXSTOREDACTIVATIONS = 100;
00062     static const int MAXN = 1000;
00064     static const float DEFAULT_VALUE;
00075     void configure(ConfigurationParameters& params, QString prefix);
00085     void save(ConfigurationParameters& params, QString prefix);
00089     static void describe( QString type );
00095     void create_net_block( int inputNeuronType, int hiddenNeuronType, int outputNeuronType, bool recurrentHiddens, bool inputOutputConnections, bool recurrentOutputs, bool biasOnHidden, bool biasOnOutput );
00101     int load_net_blocks(const char *filename, int mode);
00102     void save_net_blocks(const char *filename, int mode);
00103     void readNewPheLine(QStringList, float*, float*);
00104     void readOldPheLine(QStringList, float*, float*);
00105     void updateNet();
00106     void computeParameters();
00107     int setInput(int inp, float value); //
00108     float getOutput(int out);
00109     float getInput(int in);
00110     float getHidden(int h);
00111     void resetNet();
00112     int freeParameters(); //return number of free parameters
00113     void getParameters(const int* dt);
00114     void getMutations(float* mut);
00115     void copyPheParameters(int* pheGene);
00116     void printIO();//to print inputs and outputs
00117     int getParamBias(int nbias);
00118     float getWrange();
00119     void injectHidden(int nh, float val);
00120     float logistic(float f);
00121     void printBlocks();
00122     int getNoInputs();
00123     int getNoHiddens();
00124     int getNoOutputs();
00125     int getNoNeurons();
00126     float getNeuron(int in);
00127     bool pheFileLoaded();
00128     Evonet();
00129     char neuronl[MAXN][10];
00130     int neurondisplay[MAXN];   // whether neurons should be displayed or not
00131     double neuronrange[MAXN][2];  // the range of variation of the neuron
00132     QColor neurondcolor[MAXN];    // the color used by the neuron monitor
00133     bool neuronlesion[MAXN];    // if >0 the n neurons will be silented
00134     double neuronlesionVal[MAXN]; //value to be assigned to the lesioned neuron
00135     void setRanges(double weight, double bias, double gain); // set ranges of weights, biases and gains
00146     float* getOldestStoredActivations();
00147 private:
00148     int ninputs;                // n. input units                          
00149     int nhiddens;               // n. hidden units                    
00150     int noutputs;               // n. output units
00151     int nneurons;               // total n. of neurons
00152     int net_nblocks;            // network number of connection blocks
00153     int net_block[MAXN][5];     // blocks: 0=type (w,b,d), from 1 per 2 receive from 3 per 4
00154     int neuronbias[MAXN];       // whether neurons have bias
00155     int neurontype[MAXN];       // the type of neuron (0=logistic,1=dynamic with timeconstant, 2=binary, 3=logistic netinput*0.2
00156     int neurongain[MAXN];       // whether neurons have gain parameters
00157     int neuronxy[MAXN][2];      // neurons xy position in display space
00158     float wrange;               // weights range
00159     float grange;               // gain range
00160     float brange;               // bias range
00161     int nparameters;            // usefull to set GA parameters number of free parameters
00162     int nparambias;             // number of parametric bias to add to the number of genes need for the nnet
00163         // neurons label by default Input In0 In1.. Hidden: Hid0 Hid1 Output: Out0
00164     int neuronlesions;          // whether some of the neurons has been lesioned or manually set
00165 
00166     //variable to compute activation
00167     float act[MAXN];            //activation
00168     float storedActivations[MAXSTOREDACTIVATIONS][MAXN]; // Stored activations
00169     int nextStoredActivation;   // The index where the next activation will be stored
00170     int firstStoredActivation;  // The index of the first activation that was stored (since the stored activations were reset)
00171     float input[MAXN];          //input vector
00172     float netinput[MAXN];
00173     float *freep;               //dynamically resized to contain needed parameters
00174     float *phep;                //dynamically resized to contain manually set values specified in .phe files
00175     bool  pheloaded;            //whether a .phe file has been loaded 
00176     float *muts;                //mutation parameters
00177     int geneMaxValue;           //how parameters are codified into the GA (es. 255, 511, 1023 <0 is included!>)
00178     float **selectedp;          //pointer to a list of selected parameters
00179     int   nselected;            //number of selected parameters
00180 
00181     int p;                  //usato come puntatore per data
00182     int ndata;              //number of free parameters
00183     //variable for rendering
00184     int drawnymax;
00185     int drawnxmax;
00186     
00187     // ConfigurationParameters variables
00188     QString netFile;
00189 };
00190 
00191 } // end namespace farsa
00192 
00193 #endif