displaycontroller.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 RENDNETWORK_H
25 #define RENDNETWORK_H
26 
27 #include <QBrush> // for rendnetwork
28 #include <QPen>
29 #include <QPixmap>
30 #include <QWidget>
31 #include <QShortcut>
32 
33 #include <QWidget> // for NetworkDialog
34 #include <QGridLayout>
35 #include <QToolBar>
36 #include <QBoxLayout>
37 #include <QSlider>
38 #include <QLabel>
39 #include <QComboBox>
40 #include "evonet.h"
41 
42 namespace farsa {
43 
44 class MixerDialog;
45 class RendNetwork;
46 
47 /*
48  * The dialog used to show the network widget and the associated menu bar
49  */
50 class FARSA_EXPERIMENTS_API NetworkDialog : public QWidget
51 {
52  Q_OBJECT
53 
54 public:
55  // the network rendering widget
56  RendNetwork *rendNetwork;
57  // the mixer widget that allow the user to change parameters values
58  MixerDialog *mixerDialog;
59  // the network
60  Evonet *enet;
61  // status bar of the dialog
62  QLabel *statusb;
63  // user's selected neurons
64  int cneuron[4];
65  // n. of user's selected cneurons
66  int cneuronn;
67  // the current cycle
68  int rnrun;
69  // mouse left-clicked x position
70  int rnmousex;
71  // mouse left-clicked y position
72  int rnmousey;
73  // vector of parameter displayed through the graphic interface (max length = nneurons)
74  float **graphicp;
75  // number of currently extracted parameters
76  int ngp;
77  // x scale for the controller rendering
78  float cscalex;
79  // y scale for the controller rendering
80  float cscaley;
81  // the display modality (0=label 1=delta, 2=weight, 3=biases, 4=gain)
82  int pseudomode;
83  // return the current selected block
84  int getcblock();
85  // extract labels and/or values to be displayed/modified
86  void pseudo_activate_net();
87  // pointer to free parameters
88  float *freep;
89  // number of free parameters
90  int nfreep;
91  // n. input units
92  int ninputs;
93  // n. hidden units
94  int nhiddens;
95  // n. output units
96  int noutputs;
97  // total n. of neurons
98  int nneurons;
99  // neurons current labels (updated by pseudo_activate_net())
100  char neuroncl[Evonet::MAXN][8];
101  // the step of the grid (0 = no grid)
102  int grid;
103  // whether some of the neurons has been lesioned or manually set
104  int neuronlesions;
105  // biases value
106  float biases[Evonet::MAXN];
107  // display an error message
108  void error(const char *emessage);
109  // display a warning message
110  void warning(const char *emessage);
111 
112  // Constructor
113  NetworkDialog(Evonet* net, QWidget* parent=0, Qt::WindowFlags flags=0);
114 
115  // destructor
116  virtual ~NetworkDialog();
117 
118 signals:
119  void selectedblockChanged(); // emitted when the selected block is modified
120 
121 private:
122 
123 
127  QWidget *m_widget;
128 
133  QBoxLayout *m_mainLayout;
134 
140  QToolBar *m_toolBar;
141 
142  // create the toolbar
143  void createToolBars();
144  // creates actions associated to menu commands
145  void createActions();
146  // generate a string containing the name of a block
147  void block_name(int i, char *st);
148  // set the neurons to be displayed in the neurons' monitor
149  QAction *set_neurondisplayAct;
150  // set whether neurons have gains
151  QAction *set_neurongainAct;
152  //set whether neurons have biases
153  QAction *set_neuronbiasAct;
154  //set whether neurons are lesioned
155  QAction *set_lesionAct;
156  //display neurons labels
157  QAction *display_labelAct;
158  //display and allow to modify weights
159  QAction *display_weightAct;
160  //display and allow to modify time constants
161  QAction *display_deltaAct;
162  //display and allow to modify biases
163  QAction *display_biasAct;
164  //display and allow to modify gain
165  QAction *display_gainAct;
166  //erase blocks
167  QAction *erase_Act;
168  //add an update block
169  QAction *add_ublockAct;
170  //add a connection block
171  QAction *add_cblockAct;
172  //add a gain block
173  QAction *add_gblockAct;
174  //increase the value of selected parameters
175  QShortcut *increasevAct;
176  //decrease the value of selected parameters
177  QShortcut *decreasevAct;
178  //open a file
179  QAction *openAct;
180  //save a file
181  QAction *saveAct;
182  //modify the neuron type
183  QComboBox *neurontypeAct;
184  //select a block
185  QComboBox *cblockAct;
186  //change the block type
187  QComboBox *blocktypeAct;
188 
189  private slots:
190  // update the combo box of the neuron type
191  void updatetoolb();
192  // update the combo box of the block type
193  void updateblocktype();
194  //change the type of a block
195  void changeblocktype(int t);
196  // set whether neurons have to be displayed by the monitor
197  void set_neurondisplay();
198  // set whether neurons have biases
199  void set_neuronbias();
200  // set the neuron type
201  void set_neurontype(int i);
202  // update the combo box when the selected block has been modified
203  void selblock_changed(int i);
204  // set whether neurons have gains
205  void set_neurongain();
206  // lesion/unlesion neurons
207  void set_lesion();
208  // display neurons labels
209  void display_label();
210  // display/modify weights
211  void display_weight();
212  // display/modify timeconstants
213  void display_delta();
214  //display/modify biases
215  void display_bias();
216  //display/modify gains
217  void display_gain();
218  //erase blocks
219  void erase();
220  //add an update block
221  void add_ublock();
222  //add a connection block
223  void add_cblock();
224  //add a gain block
225  void add_gblock();
226  //increase the value of selected parameters
227  void increasev();
228  //decrease the value of selected parameters
229  void decreasev();
230  //open a file
231  void open();
232  //save a file
233  void save();
234  void openMixer();
235 
236 };
237 
238 
239 /*
240  * The network widget use to show the network architecture and parameters
241  */
242 class FARSA_EXPERIMENTS_API RendNetwork : public QWidget
243 {
244  Q_OBJECT
245 
246 public:
247  enum Shape { Line, Points, Polyline, Polygon, Rect, RoundRect, Ellipse, Arc,
248  Chord, Pie, Path, Text, Pixmap };
249 
250  RendNetwork( NetworkDialog* networkDialog, QWidget *parent = 0);
251 
252  QSize minimumSizeHint() const;
253  QSize sizeHint() const;
254  // mouse left-clicked x position
255  int rnmousex;
256  // mouse left-clicked y position
257  int rnmousey;
258  // distance between two points
259  double mdist(float x, float y, float x1, float y1);
260  //distance beween a point and a segment
261  double segmentdist(float px,float py,float ax,float ay,float bx,float by);
262 
263 signals:
264  //signal when the first selected neuron has been modified
265  void selectedneuronsChanged();
266 
267 public slots:
268  void setShape(Shape shape);
269  void setPen(const QPen &pen);
270  void setBrush(const QBrush &brush);
271  void setAntialiased(bool antialiased);
272  void setTransformed(bool transformed);
273 
274 protected:
275  // display the network (neurons and connection weights
276  void paintEvent(QPaintEvent *event);
277  void mousePressEvent(QMouseEvent *event);
278  void mouseMoveEvent(QMouseEvent *event);
279  void mouseReleaseEvent(QMouseEvent *event);
280  void mouseDoubleClickEvent(QMouseEvent *event);
281 
282 private:
283  Shape shape;
284  QPen pen;
285  QBrush brush;
286  bool antialiased;
287  bool transformed;
288  QPixmap pixmap;
289  NetworkDialog* networkDialog;
290 };
291 
292 /*
293  * Slider use to visualize/modify parameters
294  */
295 class FARSA_EXPERIMENTS_API EvoSlider : public QSlider
296 {
297  Q_OBJECT
298 public:
299  // slider creator
300  EvoSlider(float *ref, float rangemin=0, float rangemax=1, QWidget *parent=0);
301 signals:
302  void networkChanged();
303 private:
304  // slider value
305  float *vref;
306  RendNetwork *rendnet;
307 private slots:
308  void updateValue(int ival);
309 
310 };
311 
312 /*
313  * Mixer containing sliders used to visualize/modify selected parameters of the neural network
314  */
315 class FARSA_EXPERIMENTS_API MixerDialog : public QWidget
316 {
317  Q_OBJECT
318 
319 public:
320  MixerDialog(Evonet *enet, NetworkDialog* netd);
321  void setUpMixer();
322 private:
323  // the network
324  Evonet *evonet;
325  // the dialog box containing the network widget and the associated menu bar
326  NetworkDialog* netdl;
327  // the Layout of the mixer widget
328  QGridLayout* layout;
329  // the status bar of the mixer
330  QLabel *statusmixer; // status bar of the mixer widget
331  double trialRef;
332 };
333 
334 } //end namespace farsa
335 
336 #endif