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