evonetui.cpp
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 #include "evonetui.h"
25 #include "displaycontroller.h"
26 #include "evodataviewer.h"
27 #include "holisticviewer.h"
28 #include "total99resources.h"
29 #include <QGridLayout>
30 #include <QPushButton>
31 #include <QCheckBox>
32 #include <QFileDialog>
33 #include <QVBoxLayout>
34 #include <QDir>
35 #include <QFileInfo>
36 #include <QFile>
37 #include <QEvent>
38 #include <QCoreApplication>
39 #include <QTimer>
40 
41 // All the suff below is to avoid warnings on Windows about the use of unsafe
42 // functions. This should be only a temporary workaround, the solution is stop
43 // using C string and file functions...
44 #if defined(_MSC_VER)
45  #pragma warning(push)
46  #pragma warning(disable:4996)
47 #endif
48 
49 namespace farsa {
50 
52  : QObject(), ParameterSettableUI() {
53  this->evonet = evonet;
54 
55  // Global connections
56  // !! DO NOT CONNECT TO THE evonetUpdated SIGNAL to update the network becuase that signals may be
57  // emitted so fast that the GUI will freeze !!
58  //connect( evonet, SIGNAL(evonetUpdated()), this, SLOT(onEvonetUpdated()), Qt::QueuedConnection );
59  QTimer* timer = new QTimer(this);
60  timer->setInterval( 40 );
61  timer->setSingleShot( false );
62  timer->start();
63  connect( timer, SIGNAL(timeout()), this, SLOT(onEvonetUpdated()) );
64 }
65 
67 {
68  // Nothing to do
69  // --- All objects are destroyed in others parts because none of them are owend by this object
70 }
71 
72 QList<ParameterSettableUIViewer> EvonetUI::getViewers( QWidget* parent, Qt::WindowFlags flags ) {
73  QList<ParameterSettableUIViewer> viewsList;
74  viewsList.append( networkView( parent, flags ) );
75  viewsList.append( neuroMonitorView( parent, flags ) );
76  viewsList.append( holisticView( parent, flags ) );
77  return viewsList;
78 }
79 
80 ParameterSettableUIViewer EvonetUI::networkView( QWidget* parent, Qt::WindowFlags flags )
81 {
82  networkDialog = new NetworkDialog(evonet,parent,flags);
83  networkDialog->pseudo_activate_net();
84  networkDialog->setWindowTitle( "Neural Network Editor" );
85  return ParameterSettableUIViewer( networkDialog, "Nervous System" );
86 }
87 
88 ParameterSettableUIViewer EvonetUI::neuroMonitorView( QWidget* parent, Qt::WindowFlags flags )
89 {
90  edv = new EvoDataViewer( evonet->getNoNeurons(), 1000, 0, parent, flags );
91 
92  //setting chunk properties
93  bool dn;
94  for (int i = 0; i < evonet->getNoNeurons(); i++) {
95  if (evonet->neurondisplay[i] == 1) {
96  dn = true;
97  } else {
98  dn = false;
99  }
100  if ( evonet->neurondcolor[i].isValid() ) {
101  edv->setChunkProperties(i, evonet->neuronrange[i][0], evonet->neuronrange[i][1], evonet->neuronl[i], evonet->neurondcolor[i], dn);
102  } else {
103  // if the color is not valid, will use the color red
104  edv->setChunkProperties(i, evonet->neuronrange[i][0], evonet->neuronrange[i][1], evonet->neuronl[i], QColor(255,0,0), dn);
105  }
106  }
107  edv->setWindowTitle( "Neurons Monitor" );
108  edv->setGeometry(50, 50, 600, 600);
109  return ParameterSettableUIViewer( edv, "Neurons Monitor" );
110 }
111 
112 ParameterSettableUIViewer EvonetUI::holisticView( QWidget* parent, Qt::WindowFlags flags )
113 {
114  hlv = new HolisticViewer(evonet, parent, flags);
115  hlv->resize(300, 300);
116  hlv->setWindowTitle("Holistic Viewer");
117  return ParameterSettableUIViewer( hlv, "Holistic View" );
118 }
119 
120 void EvonetUI::onEvonetUpdated() {
121  // updating the Newtork Dialog
122  if ( networkDialog ) {
123  networkDialog->update();
124  }
125  // updating EvoDataViewer
126  if ( edv ) {
127  float *acts;
128 
129  // FIXME: the getOldestStoredActivations return only one value of a circual buffer
130  // of stored activations that it's continously filled by Evonet
131  // !! In the case on which the Evonet is updated too fast respect to the velocity
132  // of consuming the values on the buffer, the GUI will cycle forever trying to
133  // consume all oldest activations. !!
134  //
135  // The best solution would be to use the ResourceLocker to lock on the Evonet but
136  // at the moment of comment writing this is not possible because to use ResourceLocker
137  // we should know who is the ResourceUsers with shareResourcesWith in order to use the
138  // lock and to use the getResource method.
139  // But because we planned to put into each ParameterSettable an attribute storing the
140  // ConfigurationParameter used for configuring it, and to also put inside the ConfigurationParameter
141  // an attribute for storing all Resources declared during the configuration,
142  // the complete solution of this problem is postponed to above planned modification.
143  // In fact, after that modification, it would be possible to shareResourceWith the ConfigurationParameter
144  // used for configure the Evonet and then use the ResourceLocker.
145  //
146  // For now, we simple add a safe break to avoid to cycle forever, but it means that the data
147  // might be displayed wrongly.
148  int counter = 0;
149  do {
150  acts = evonet->getOldestStoredActivations();
151  if (acts != NULL) {
152  counter++;
153  for (int ch = 0; ch < evonet->getNoNeurons(); ch++) {
154  edv->setChunkValue(ch, acts[ch]);
155  }
156  }
157  } while (acts != NULL && counter<Evonet::MAXSTOREDACTIVATIONS);
158  edv->setCurrentStep( evonet->updateCounts() );
159  edv->update();
160  }
161  // updating holistic viewer
162  if ( hlv ) {
163  hlv->updateGrid();
164  hlv->updatePlot();
165  hlv->update();
166  }
167 }
168 
169 } //end namespace farsa
170 
171 // All the suff below is to restore the warning state on Windows
172 #if defined(_MSC_VER)
173  #pragma warning(pop)
174 #endif