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  void setNet(Evonet* n);
120 
121 signals:
122  void selectedblockChanged(); // emitted when the selected block is modified
123 
124 private:
125 
126 
130  QWidget *m_widget;
131 
136  QBoxLayout *m_mainLayout;
137 
143  QToolBar *m_toolBar;
144 
145  // create the toolbar
146  void createToolBars();
147  // creates actions associated to menu commands
148  void createActions();
149  // generate a string containing the name of a block
150  void block_name(int i, char *st);
151  // set the neurons to be displayed in the neurons' monitor
152  QAction *set_neurondisplayAct;
153  // set whether neurons have gains
154  QAction *set_neurongainAct;
155  //set whether neurons have biases
156  QAction *set_neuronbiasAct;
157  //set whether neurons are lesioned
158  QAction *set_lesionAct;
159  //display neurons labels
160  QAction *display_labelAct;
161  //display and allow to modify weights
162  QAction *display_weightAct;
163  //display and allow to modify time constants
164  QAction *display_deltaAct;
165  //display and allow to modify biases
166  QAction *display_biasAct;
167  //display and allow to modify gain
168  QAction *display_gainAct;
169  //erase blocks
170  QAction *erase_Act;
171  //add an update block
172  QAction *add_ublockAct;
173  //add a connection block
174  QAction *add_cblockAct;
175  //add a gain block
176  QAction *add_gblockAct;
177  //increase the value of selected parameters
178  QShortcut *increasevAct;
179  //decrease the value of selected parameters
180  QShortcut *decreasevAct;
181  //open a file
182  QAction *openAct;
183  //save a file
184  QAction *saveAct;
185  //modify the neuron type
186  QComboBox *neurontypeAct;
187  //select a block
188  QComboBox *cblockAct;
189  //change the block type
190  QComboBox *blocktypeAct;
191 
192  private slots:
193  // update the combo box of the neuron type
194  void updatetoolb();
195  // update the combo box of the block type
196  void updateblocktype();
197  //change the type of a block
198  void changeblocktype(int t);
199  // set whether neurons have to be displayed by the monitor
200  void set_neurondisplay();
201  // set whether neurons have biases
202  void set_neuronbias();
203  // set the neuron type
204  void set_neurontype(int i);
205  // update the combo box when the selected block has been modified
206  void selblock_changed(int i);
207  // set whether neurons have gains
208  void set_neurongain();
209  // lesion/unlesion neurons
210  void set_lesion();
211  // display neurons labels
212  void display_label();
213  // display/modify weights
214  void display_weight();
215  // display/modify timeconstants
216  void display_delta();
217  //display/modify biases
218  void display_bias();
219  //display/modify gains
220  void display_gain();
221  //erase blocks
222  void erase();
223  //add an update block
224  void add_ublock();
225  //add a connection block
226  void add_cblock();
227  //add a gain block
228  void add_gblock();
229  //increase the value of selected parameters
230  void increasev();
231  //decrease the value of selected parameters
232  void decreasev();
233  //open a file
234  void open();
235  //save a file
236  void save();
237  void openMixer();
238 
239 };
240 
241 
242 /*
243  * The network widget use to show the network architecture and parameters
244  */
245 class FARSA_EXPERIMENTS_API RendNetwork : public QWidget
246 {
247  Q_OBJECT
248 
249 public:
250  enum Shape { Line, Points, Polyline, Polygon, Rect, RoundRect, Ellipse, Arc,
251  Chord, Pie, Path, Text, Pixmap };
252 
253  RendNetwork( NetworkDialog* networkDialog, QWidget *parent = 0);
254 
255  QSize minimumSizeHint() const;
256  QSize sizeHint() const;
257  // mouse left-clicked x position
258  int rnmousex;
259  // mouse left-clicked y position
260  int rnmousey;
261  // distance between two points
262  double mdist(float x, float y, float x1, float y1);
263  //distance beween a point and a segment
264  double segmentdist(float px,float py,float ax,float ay,float bx,float by);
265 
266 signals:
267  //signal when the first selected neuron has been modified
268  void selectedneuronsChanged();
269 
270 public slots:
271  void setShape(Shape shape);
272  void setPen(const QPen &pen);
273  void setBrush(const QBrush &brush);
274  void setAntialiased(bool antialiased);
275  void setTransformed(bool transformed);
276 
277 protected:
278  // display the network (neurons and connection weights
279  void paintEvent(QPaintEvent *event);
280  void mousePressEvent(QMouseEvent *event);
281  void mouseMoveEvent(QMouseEvent *event);
282  void mouseReleaseEvent(QMouseEvent *event);
283  void mouseDoubleClickEvent(QMouseEvent *event);
284 
285 private:
286  Shape shape;
287  QPen pen;
288  QBrush brush;
289  bool antialiased;
290  bool transformed;
291  QPixmap pixmap;
292  NetworkDialog* networkDialog;
293 };
294 
295 /*
296  * Slider use to visualize/modify parameters
297  */
298 class FARSA_EXPERIMENTS_API EvoSlider : public QDoubleSpinBox
299 {
300  Q_OBJECT
301 public:
302  // slider creator
303  EvoSlider(float *ref, float rangemin=0, float rangemax=1, QWidget *parent=0);
304 signals:
305  void networkChanged();
306 private:
307  // slider value
308  float *vref;
309  RendNetwork *rendnet;
310 private slots:
311  void updateValue(double ival);
312 
313 };
314 
315 /*
316  * Mixer containing sliders used to visualize/modify selected parameters of the neural network
317  */
318 class FARSA_EXPERIMENTS_API MixerDialog : public QWidget
319 {
320  Q_OBJECT
321 
322 public:
323  MixerDialog(Evonet *enet, NetworkDialog* netd);
324  void setUpMixer();
325  void setNet(Evonet* n);
326 private:
327  // the network
328  Evonet *evonet;
329  // the dialog box containing the network widget and the associated menu bar
330  NetworkDialog* netdl;
331  // the Layout of the mixer widget
332  QGridLayout* layout;
333  // the status bar of the mixer
334  QLabel *statusmixer; // status bar of the mixer widget
335  double trialRef;
336 };
337 
338 } //end namespace farsa
339 
340 #endif
static const int MAXN
Maximum number of neurons for a neural network of this type.
Definition: evonet.h:135
Evonet is the neural network taken from the EvoRobot.
Definition: evonet.h:121