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 #include <QElapsedTimer>
41 
42 // All the suff below is to avoid warnings on Windows about the use of unsafe
43 // functions. This should be only a temporary workaround, the solution is stop
44 // using C string and file functions...
45 #if defined(_MSC_VER)
46  #pragma warning(push)
47  #pragma warning(disable:4996)
48 #endif
49 
50 namespace farsa {
51 
53  : QObject()
55  , edv(NULL)
56  , hlv(NULL)
57  , networkDialog(NULL)
58  , evonet(net)
59  , neuronsMonitorDownloader( DataDownloader<ActivationsToGui>::NoNotification )
60  , nNeurons(0)
61 {
62  // Associating uploaders and downloaders
63  GlobalUploaderDownloader::associate(neuronsMonitorUploader, &neuronsMonitorDownloader);
64 
65  // Global connections
66  // !! DO NOT CONNECT TO THE evonetUpdated SIGNAL to update the network becuase that signals may be
67  // emitted so fast that the GUI will freeze !!
68  //connect( evonet, SIGNAL(evonetUpdated()), this, SLOT(onEvonetUpdated()), Qt::QueuedConnection );
69  QTimer* timer = new QTimer(this);
70  timer->setInterval( 40 );
71  timer->setSingleShot( false );
72  timer->start();
73  connect( timer, SIGNAL(timeout()), this, SLOT(onEvonetUpdated()) );
74 }
75 
77 {
78  // Nothing to do
79  // --- All objects are destroyed in others parts because none of them are owend by this object
80 }
81 
82 QList<ParameterSettableUIViewer> EvonetUI::getViewers( QWidget* parent, Qt::WindowFlags flags ) {
83  QList<ParameterSettableUIViewer> viewsList;
84  viewsList.append( networkView( parent, flags ) );
85  viewsList.append( neuroMonitorView( parent, flags ) );
86  viewsList.append( holisticView( parent, flags ) );
87  return viewsList;
88 }
89 
91 {
92  GlobalUploaderDownloader::detach(&neuronsMonitorDownloader);
93  GlobalUploaderDownloader::associate(n->getNeuronsMonitorUploader(), &neuronsMonitorDownloader);
94 
95  networkDialog->setNet(n);
96  hlv->setNet(n);
97 
98  evonet = n;
99 }
100 
101 ParameterSettableUIViewer EvonetUI::networkView( QWidget* parent, Qt::WindowFlags flags )
102 {
103  networkDialog = new NetworkDialog(evonet,parent,flags);
104  networkDialog->pseudo_activate_net();
105  networkDialog->setWindowTitle( "Neural Network Editor" );
106  return ParameterSettableUIViewer( networkDialog, "Nervous System" );
107 }
108 
109 ParameterSettableUIViewer EvonetUI::neuroMonitorView( QWidget* parent, Qt::WindowFlags flags )
110 {
111  int addInfo = (evonet->showTeachingInput() ? evonet->getNoOutputs() + 1 : 0);
112  nNeurons = evonet->getNoNeurons();
113  edv = new EvoDataViewer( evonet->getNoNeurons() + addInfo, 1000, 0, parent, flags );
114 
115  //setting chunk properties
116  int i;
117  bool dn;
118  for (i = 0; i < evonet->getNoNeurons(); i++) {
119  if (evonet->neurondisplay[i] == 1) {
120  dn = true;
121  } else {
122  dn = false;
123  }
124  if ( evonet->neurondcolor[i].isValid() ) {
125  edv->setChunkProperties(i, evonet->neuronrange[i][0], evonet->neuronrange[i][1], evonet->neuronl[i], evonet->neurondcolor[i], dn);
126  } else {
127  // if the color is not valid, will use the color red
128  edv->setChunkProperties(i, evonet->neuronrange[i][0], evonet->neuronrange[i][1], evonet->neuronl[i], QColor(255,0,0), dn);
129  }
130  }
131  // Setting chunk properties for teaching input and backpropagation error (if the teaching input must be shown)
132  dn = true;
133  if (evonet->showTeachingInput())
134  {
135  for (i = 0; i < evonet->getNoOutputs(); i++)
136  {
137  edv->setChunkProperties(evonet->getNoNeurons() + i, 0.0, 1.0, "tInput[" + QString::number(i) + "]", QColor(255,0,0), dn);
138  }
139  edv->setChunkProperties(evonet->getNoNeurons() + evonet->getNoOutputs(), 0.0, 1.0, "error", QColor(255,0,0), dn);
140  }
141  //
142  edv->setWindowTitle( "Neurons Monitor" );
143  edv->setGeometry(50, 50, 600, 600);
144  return ParameterSettableUIViewer( edv, "Neurons Monitor" );
145 }
146 
147 ParameterSettableUIViewer EvonetUI::holisticView( QWidget* parent, Qt::WindowFlags flags )
148 {
149  hlv = new HolisticViewer(evonet, parent, flags);
150  hlv->resize(300, 300);
151  hlv->setWindowTitle("Holistic Viewer");
152  return ParameterSettableUIViewer( hlv, "Holistic View" );
153 }
154 
155 void EvonetUI::onEvonetUpdated() {
156  if ( edv ) {
157  QElapsedTimer timer;
158  timer.start();
159  while(true) {
160  // This call returns NULL if no new activation is available
161  const ActivationsToGui* d = neuronsMonitorDownloader.downloadDatum();
162 
163  if (d == NULL) {
164  break;
165  }
166 
167  if (d->activations) {
168  for (int ch = 0; ch < d->data.size(); ch++) {
169  edv->setChunkValue(ch, d->data[ch]);
170  }
171 
172  // Also setting the current step
173  edv->setCurrentStep(d->updatesCounter);
174  } else {
175  // Setting teaching inputs
176  for (int ch = 0; ch < (d->data.size() - 1); ch++) {
177  edv->setChunkValue(nNeurons + ch, d->data[ch]);
178  }
179  // The error is the last value in d->data
180  edv->setChunkValue(nNeurons + d->data.size() - 1, d->data.last());
181 
182 
183  // Also setting the current step
184  edv->setCurrentStep(d->updatesCounter);
185  }
186 
187  if (d->updateLabelAndColors) {
188  for (int i = 0; i < d->neuronl.size(); ++i) {
189  if (d->neurondcolor[i].isValid()) {
190  edv->setChunkColor(i, d->neurondcolor[i]);
191  } else {
192  edv->setChunkColor(i, QColor(255,0,0));
193  }
194  edv->setChunkLabel(i, d->neuronl[i]);
195  }
196  }
197 
198  // break the cycle if it takes too long to get data
199  if ( timer.elapsed() > 20 ) {
200  break;
201  }
202  }
203  edv->update();
204  }
205 
206  // updating the Newtork Dialog
207  if ( networkDialog ) {
208  networkDialog->update();
209  }
210  // updating holistic viewer
211  if ( hlv ) {
212  hlv->updateGrid();
213  hlv->updatePlot();
214  hlv->update();
215  }
216 }
217 
218 } //end namespace farsa
219 
220 // All the suff below is to restore the warning state on Windows
221 #if defined(_MSC_VER)
222  #pragma warning(pop)
223 #endif
static void detach(DataUploader< DataType > *uploader)
void setChunkValue(int ch, double value)
set chunk value by chunck id
bool showTeachingInput()
Checks whether the teaching input has to be shown.
Definition: evonet.cpp:2031
void setCurrentStep(int step)
it stores the current step for each value.
int getNoNeurons()
return the total number of neurons
Definition: evonet.cpp:1211
void setChunkLabel(int ch, const QString &label)
set the chunk label
void changeNet(Evonet *n)
add the "Tests" menu to the menu bar of Total99
Definition: evonetui.cpp:90
DataUploader< ActivationsToGui > * getNeuronsMonitorUploader()
Returns the uploader for activations.
Definition: evonet.h:181
QList< ParameterSettableUIViewer > getViewers(QWidget *parent, Qt::WindowFlags flags)
fill the menu "Actions" of Total99 with following actions:
Definition: evonetui.cpp:82
void setChunkProperties(int ch, double rangeMin, double rangeMax, const QString &label, QColor color, bool visible)
Set all the chunk properties in one step.
QColor neurondcolor[MAXN]
the color used to display the actiovation state of each neuron in the neuron monitor widget ...
Definition: evonet.h:419
int neurondisplay[MAXN]
the vectors that specify for each neuron whether it should be displayed or not by the neuron monitor ...
Definition: evonet.h:410
~EvonetUI()
Destructor.
Definition: evonetui.cpp:76
Evonet is the neural network taken from the EvoRobot.
Definition: evonet.h:121
EvonetUI(Evonet *net, DataUploader< ActivationsToGui > *neuronsMonitorUploader)
Construct the GUI manager for Evonet.
Definition: evonetui.cpp:52
The class with data exchanged with the GUI.
Definition: evonet.h:73
int getNoOutputs()
return the number of motor neurons
Definition: evonet.cpp:1206
static void associate(DataUploader< DataType > *uploader, DataDownloader< DataType > *downloader)
double neuronrange[MAXN][2]
the matrix that contain the variation range of neurons used by the neuron monitor graphic widget ...
Definition: evonet.h:415