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 
40 // All the suff below is to avoid warnings on Windows about the use of unsafe
41 // functions. This should be only a temporary workaround, the solution is stop
42 // using C string and file functions...
43 #if defined(_MSC_VER)
44  #pragma warning(push)
45  #pragma warning(disable:4996)
46 #endif
47 
48 namespace farsa {
49 
51  : QObject(), ParameterSettableUI() {
52  this->evonet = evonet;
53  // Global connections
54  connect( evonet, SIGNAL(evonetUpdated()), this, SLOT(onEvonetUpdated()), Qt::QueuedConnection );
55 }
56 
58 {
59  // Nothing to do
60  // --- All objects are destroyed in others parts because none of them are owend by this object
61 }
62 
63 QList<ParameterSettableUIViewer> EvonetUI::getViewers( QWidget* parent, Qt::WindowFlags flags ) {
64  QList<ParameterSettableUIViewer> viewsList;
65  viewsList.append( networkView( parent, flags ) );
66  viewsList.append( neuroMonitorView( parent, flags ) );
67  viewsList.append( holisticView( parent, flags ) );
68  return viewsList;
69 }
70 
71 ParameterSettableUIViewer EvonetUI::networkView( QWidget* parent, Qt::WindowFlags flags )
72 {
73  networkDialog = new NetworkDialog(evonet,parent,flags);
74  networkDialog->pseudo_activate_net();
75  networkDialog->setWindowTitle( "Neural Network Editor" );
76  return ParameterSettableUIViewer( networkDialog, "Nervous System" );
77 }
78 
79 ParameterSettableUIViewer EvonetUI::neuroMonitorView( QWidget* parent, Qt::WindowFlags flags )
80 {
81  edv = new EvoDataViewer( evonet->getNoNeurons(), 1000, 0, parent, flags );
82 
83  //setting chunk properties
84  bool dn;
85  for (int i = 0; i < evonet->getNoNeurons(); i++) {
86  if (evonet->neurondisplay[i] == 1) {
87  dn = true;
88  } else {
89  dn = false;
90  }
91  if ( evonet->neurondcolor[i].isValid() ) {
92  edv->setChunkProperties(i, evonet->neuronrange[i][0], evonet->neuronrange[i][1], evonet->neuronl[i], evonet->neurondcolor[i], dn);
93  } else {
94  // if the color is not valid, will use the color red
95  edv->setChunkProperties(i, evonet->neuronrange[i][0], evonet->neuronrange[i][1], evonet->neuronl[i], QColor(255,0,0), dn);
96  }
97  }
98  edv->setWindowTitle( "Neurons Monitor" );
99  edv->setGeometry(50, 50, 600, 600);
100  return ParameterSettableUIViewer( edv, "Neurons Monitor" );
101 }
102 
103 ParameterSettableUIViewer EvonetUI::holisticView( QWidget* parent, Qt::WindowFlags flags )
104 {
105  hlv = new HolisticViewer(evonet, parent, flags);
106  hlv->resize(300, 300);
107  hlv->setWindowTitle("Holistic Viewer");
108  return ParameterSettableUIViewer( hlv, "Holistic View" );
109 }
110 
111 void EvonetUI::onEvonetUpdated() {
112  // updating EvoDataViewer
113  if ( edv ) {
114  float *acts;
115  do {
116  acts = evonet->getOldestStoredActivations();
117  if (acts != NULL) {
118  for (int ch = 0; ch < evonet->getNoNeurons(); ch++) {
119  edv->setChunkValue(ch, acts[ch]);
120  }
121  edv->setCurrentStep( evonet->updateCounts() );
122  }
123  } while (acts != NULL);
124  edv->update();
125  }
126  // updating holistic viewer
127  if ( hlv ) {
128  hlv->updateGrid();
129  hlv->updatePlot();
130  hlv->update();
131  }
132 }
133 
134 } //end namespace farsa
135 
136 // All the suff below is to restore the warning state on Windows
137 #if defined(_MSC_VER)
138  #pragma warning(pop)
139 #endif