holisticviewer.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 "holisticviewer.h"
25 
26 namespace farsa {
27 
28 HolisticViewer::HolisticViewer(Evonet* network, QWidget* parent, Qt::WindowFlags flags)
29  : QWidget(parent, flags),
30  labels() {
31  net = network;
32 
33  mainLayout = new QVBoxLayout(this);
34 
35  grid = new QGridLayout();
36  grid->setSpacing(0);
37  mainLayout->addLayout(grid);
38 
39  neuronChoice = new QHBoxLayout();
40  neuronX = new QComboBox(this);
41  neuronY = new QComboBox(this);
42  neuronChoice->addWidget(neuronX);
43  neuronChoice->addWidget(neuronY);
44  mainLayout->addLayout(neuronChoice);
45 
46 }
47 
48 //return neuron activation scaled from 0 to 255
49 int HolisticViewer::getNeuronAct(int n)
50 {
51  double act = net->getNeuron(n);
52  double min = net->neuronrange[n][0];
53  double max = net->neuronrange[n][1];
54 
55  act = linearMap(act,min,max,0,255);
56 
57  // Clamping between 0 and 255
58  int intAct = (int)ceil(act);
59  intAct = (intAct < 0) ? 0 : ((intAct > 255) ? 255 : intAct);
60  return intAct;
61 }
62 
63 void HolisticViewer::updateGrid()
64 {
65  int neuronsPerRow = ceil(sqrt((double)net->getNoNeurons()));
66 
67  //first iteration only
68  if(labels.size() == 0)
69  {
70  QFont font;
71  QString name, color;
72 
73  for(int r=0; r<neuronsPerRow; r++)
74  {
75  for(int c=0; c<neuronsPerRow; c++)
76  {
77  int index = r*neuronsPerRow+c;
78 
79  if(index < net->getNoNeurons())
80  {
81  name = net->neuronl[index];
82 
83  //add items to the combo boxes
84  neuronX->addItem(name);
85  neuronY->addItem(name);
86 
87  //insert labels into the grid
88  color = "yellow";
89  if(index < net->getNoInputs())
90  color = "red";
91  if(index > net->getNoInputs()+net->getNoHiddens())
92  color = "blue";
93 
94  labels.append( new QLabel(this) );
95  labels[index]->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
96  labels[index]->setFont(font);
97  labels[index]->setText("<font color='"+ color + "';>" + name + "</font>");
98 
99  grid->addWidget(labels[index], r, c);
100  }
101  }
102  }
103  }
104 
105  //every step
106  for(int r=0; r<neuronsPerRow; r++)
107  {
108  for(int c=0; c<neuronsPerRow; c++)
109  {
110  int index = r*neuronsPerRow+c;
111  if(index < net->getNoNeurons())
112  {
113  labels[index]->setAutoFillBackground(true);
114  int act256 = getNeuronAct(index);
115  labels[index]->setPalette(QPalette(QColor(act256,act256,act256, 255)));
116 
117  int fontSize = (int)(width()+height())/70;
118  if(fontSize > 30)
119  fontSize = 30;
120  QFont font = labels[index]->font();
121  font.setPointSize(fontSize);
122  labels[index]->setFont(font);
123  }
124  }
125  }
126 }
127 
128 void HolisticViewer::updatePlot()
129 {
130  //take selected items from combos
131  //load their buffer
132  //plot everything
133 }
134 
135 void HolisticViewer::paintEvent(QPaintEvent* /*evt*/)
136 {
137  QPainter painter(this);
138 
139  QPen blackPen(Qt::black);
140  QPen bluePen(Qt::blue);
141  QPen greenPen(Qt::green);
142  QPen redPen(Qt::red);
143 
144  painter.drawRect(0,0,100,100);
145 
146  painter.fillRect(0,0,width(),height(),Qt::white);
147  painter.setPen(blackPen);
148  painter.setRenderHint(QPainter::Antialiasing, false);
149 }
150 
151 void HolisticViewer::setNet(Evonet* n)
152 {
153  net = n;
154  updateGrid();
155 }
156 
157 } //end namespace farsa
FARSA_UTIL_TEMPLATE float linearMap(float x, float min=-10, float max=10, float outMin=-1, float outMax=1)
int getNoInputs()
return the number of sensory neurons
Definition: evonet.cpp:1196
int getNoNeurons()
return the total number of neurons
Definition: evonet.cpp:1211
FARSA_UTIL_TEMPLATE const T max(const T &t1, const U &t2)
float getNeuron(int in)
return the activation of a neuron
Definition: evonet.cpp:1038
int getNoHiddens()
return the number of internal neurons
Definition: evonet.cpp:1201
FARSA_UTIL_TEMPLATE const T min(const T &t1, const U &t2)
double neuronrange[MAXN][2]
the matrix that contain the variation range of neurons used by the neuron monitor graphic widget ...
Definition: evonet.h:415