experiments/evorobot/include/displaycontroller.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 RENDNETWORK_H
00025 #define RENDNETWORK_H
00026 
00027 #include <QBrush>     // for rendnetwork
00028 #include <QPen>     
00029 #include <QPixmap>
00030 #include <QWidget>
00031 #include <QShortcut>
00032 
00033 #include <QWidget>    // for NetworkDialog
00034 #include <QGridLayout>
00035 #include <QToolBar>
00036 #include <QBoxLayout>
00037 #include <QSlider>
00038 #include "evonet.h"
00039 
00040 namespace farsa {
00041 
00042 class MixerDialog;
00043 class RendNetwork;
00044 
00045 /*
00046  * The dialog to show a widget within a window
00047  */
00048 class FARSA_EXPERIMENTS_API NetworkDialog : public QWidget
00049 {
00050     Q_OBJECT
00051 
00052 public:
00053 
00054     RendNetwork *rendNetwork;      // The network rendering widget
00055     MixerDialog *mixerDialog;
00056     Evonet  *enet;
00057     int     drawblocks;            // whether we display or not the list of block  
00058     int     cneuron[4];            // selected neurons
00059     int     cneuronn;              // n. of selected cneurons
00060     int     rnrun;                 // the current cycle
00061     int     rnmousex;              // mouse left-clicked x position
00062     int     rnmousey;              // mouse left-clicked y position
00063     float   **graphicp;            // vector of parameter displayed through the graphic interface (max length = nneurons)
00064     int     ngp;                   // number of cirrently extracted parameters
00065     float   cscalex;               // x scale for the controller rendering
00066     float   cscaley;               // y scale for the controller rendering
00067     int     pseudomode;            // the display modality (0=label 1=delta, 2=weight, 3=biases, 4=gain)
00068 
00069     void    pseudo_activate_net(int dmode); // extract labels or values to be displayed
00070 
00071     //int load_net_blocks(char *filename, int mode); // load parameters from a configuration file
00072     //void save_net_blocks(char *filename);          // save the network description 
00073 
00074     float *freep;                  // pointer to free parameters             
00075     int nfreep;                    // number of free parameters 
00076     int ninputs;                   // n. input units                          
00077     int nhiddens;                  // n. hidden units                    
00078     int noutputs;                  // n. output units
00079     int nneurons;                  // total n. of neurons
00080     int net_nblocks;               // network number of connection blocks
00081     int net_block[Evonet::MAXN][5];        // blocks: 0=type (w,b,d), from 1 per 2 receive from 3 per 4
00082     int neuronbias[Evonet::MAXN];          // whether neurons have bias
00083     int neurontype[Evonet::MAXN];         // whether neurons have time constants
00084     int neurongain[Evonet::MAXN];          // whether neurons have gain parameters
00085     int neuronxy[Evonet::MAXN][2];         // neurons xy position in display space
00086     int neurondisplay[Evonet::MAXN];       // whether neurons should be displayed or not
00087     char neuronl[Evonet::MAXN][8];         // neurons labels (created in the robot_env module and used by the rend_network module)
00088     char neuroncl[Evonet::MAXN][8];        // neurons current labels (updated by pseudo_activate_net())
00089     float wrange;                  // weights range
00090     float grange;                  // gain range
00091     int  grid;                     // the step of the grid (0 = no grid)
00092     int nparameters;               // usefull to set GA parameters number of free parameters
00093     int nparambias;                // number of parametric bias to add to the number of genes need for the nnet
00094     int neuronlesions;             // whether some of the neurons has been lesioned or manually set
00095     float biases[Evonet::MAXN];            // biases value
00096     int   updated[Evonet::MAXN];           // the number of times a neuron is updated 
00097 
00098 
00099     void error(const char *emessage);    // display an error message
00100     void warning(const char *emessage);  // display a warning message
00101 
00102 
00103     // Constructor
00104     NetworkDialog(Evonet* net, QWidget* parent=0, Qt::WindowFlags flags=0);
00105 
00106     // destructor
00107     virtual ~NetworkDialog();
00108 
00109 private:
00110 
00116     //void keyReleaseEvent(QKeyEvent* event);
00117 
00122     void shotWidget();
00123 
00127     QWidget *m_widget;
00128 
00133     QBoxLayout *m_mainLayout;
00134 
00140     QToolBar *m_toolBar;
00141 
00142     // create the toolbar
00143     void createToolBars();
00144     void createActions();
00145 
00146     QAction *set_neurondisplayAct;
00147     QAction *set_neurontypeAct;
00148     QAction *set_neurongainAct;
00149     QAction *set_neuronbiasAct;
00150     QAction *set_lesionAct;
00151     QAction *display_labelAct;
00152     QAction *display_weightAct;
00153     QAction *display_deltaAct;
00154     QAction *display_biasAct;
00155     QAction *display_gainAct;
00156     QAction *display_blocksAct;
00157     QAction *erase_netAct;
00158     QAction *add_ublockAct;
00159     QAction *add_cblockAct;
00160     QAction *add_gblockAct;
00161     QShortcut *increasevAct;
00162     QShortcut *decreasevAct;
00163     QAction *openAct;
00164     QAction *saveAct;
00165     QAction *randomfreepAct;
00166     QAction *mixerAct;
00167 
00168     private slots:
00169 
00170     void set_neurondisplay();
00171     void set_neuronbias();
00172     void set_neurontype();
00173     void set_neurongain();
00174     void set_lesion();
00175     void display_label();
00176     void display_weight();
00177     void display_delta();
00178     void display_bias();
00179     void display_gain();
00180     void display_blocks();
00181     void erase_net();
00182     void add_ublock(); 
00183     void add_cblock();
00184     void add_gblock();
00185     void increasev();
00186     void decreasev();
00187     void open(); 
00188     void save();
00189     void randomfreep();
00190     void openMixer();
00191 
00192 
00193 
00194 };
00195 
00196 
00197 /*
00198  * The network widget
00199  */
00200 class FARSA_EXPERIMENTS_API RendNetwork : public QWidget
00201 {
00202     Q_OBJECT
00203 
00204 public:
00205     enum Shape { Line, Points, Polyline, Polygon, Rect, RoundRect, Ellipse, Arc,
00206                  Chord, Pie, Path, Text, Pixmap };
00207 
00208     RendNetwork( NetworkDialog* networkDialog, QWidget *parent = 0);
00209 
00210     QSize minimumSizeHint() const;
00211     QSize sizeHint() const;
00212     int     rnmousex;              // mouse left-clicked x position
00213     int     rnmousey;              // mouse left-clicked y position
00214     double mdist(float x, float y, float x1, float y1);  // distance between two points
00215     double segmentdist(float px,float py,float ax,float ay,float bx,float by); //distance beween a point and a segment 
00216     //NetworkDialog *networkDialog;
00217 
00218 
00219 public slots:
00220     void setShape(Shape shape);
00221     void setPen(const QPen &pen);
00222     void setBrush(const QBrush &brush);
00223     void setAntialiased(bool antialiased);
00224     void setTransformed(bool transformed);
00225 
00226 protected:
00227     void paintEvent(QPaintEvent *event);
00228     void mousePressEvent(QMouseEvent *event);
00229     void mouseMoveEvent(QMouseEvent *event);
00230     void mouseReleaseEvent(QMouseEvent *event);
00231     void mouseDoubleClickEvent(QMouseEvent *event);
00232 
00233 private:
00234     Shape shape;
00235     QPen pen;
00236     QBrush brush;
00237     bool antialiased;
00238     bool transformed;
00239     QPixmap pixmap;
00240     NetworkDialog* networkDialog;
00241 };
00242 
00243 class FARSA_EXPERIMENTS_API EvoSlider : public QSlider
00244 {
00245     Q_OBJECT
00246 public:
00247     EvoSlider(double *ref, double rangemin=0, double rangemax=1, QWidget *parent=0);
00248 
00249 private:
00250     double *vref;
00251 private slots:
00252     void updateValue(int ival);
00253 
00254 };
00255 
00256 class FARSA_EXPERIMENTS_API MixerDialog : public QWidget
00257 {
00258     Q_OBJECT
00259 
00260 public:
00261     MixerDialog(Evonet *enet);
00262 private:
00263     Evonet *evonet;
00264     void setUpMixer();
00265     double trialRef;
00266 };
00267 
00268 } //end namespace farsa
00269 
00270 #endif