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  return (int)ceil(act);
57 }
58 
59 void HolisticViewer::updateGrid()
60 {
61  int neuronsPerRow = ceil(sqrt((double)net->getNoNeurons()));
62 
63  //first iteration only
64  if(labels.size() == 0)
65  {
66  QFont font;
67  QString name, color;
68 
69  for(int r=0; r<neuronsPerRow; r++)
70  {
71  for(int c=0; c<neuronsPerRow; c++)
72  {
73  int index = r*neuronsPerRow+c;
74 
75  if(index < net->getNoNeurons())
76  {
77  name = net->neuronl[index];
78 
79  //add items to the combo boxes
80  neuronX->addItem(name);
81  neuronY->addItem(name);
82 
83  //insert labels into the grid
84  color = "yellow";
85  if(index < net->getNoInputs())
86  color = "red";
87  if(index > net->getNoInputs()+net->getNoHiddens())
88  color = "blue";
89 
90  labels.append( new QLabel(this) );
91  labels[index]->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
92  labels[index]->setFont(font);
93  labels[index]->setText("<font color='"+ color + "';>" + name + "</font>");
94 
95  grid->addWidget(labels[index], r, c);
96  }
97  }
98  }
99  }
100 
101  //every step
102  for(int r=0; r<neuronsPerRow; r++)
103  {
104  for(int c=0; c<neuronsPerRow; c++)
105  {
106  int index = r*neuronsPerRow+c;
107  if(index < net->getNoNeurons())
108  {
109  labels[index]->setAutoFillBackground(true);
110  int act256 = getNeuronAct(index);
111  labels[index]->setPalette(QPalette(QColor(act256,act256,act256, 255)));
112 
113  int fontSize = (int)(width()+height())/70;
114  if(fontSize > 30)
115  fontSize = 30;
116  QFont font = labels[index]->font();
117  font.setPointSize(fontSize);
118  labels[index]->setFont(font);
119  }
120  }
121  }
122 }
123 
124 void HolisticViewer::updatePlot()
125 {
126  //take selected items from combos
127  //load their buffer
128  //plot everything
129 }
130 
131 void HolisticViewer::paintEvent(QPaintEvent* /*evt*/)
132 {
133  QPainter painter(this);
134 
135  QPen blackPen(Qt::black);
136  QPen bluePen(Qt::blue);
137  QPen greenPen(Qt::green);
138  QPen redPen(Qt::red);
139 
140  painter.drawRect(0,0,100,100);
141 
142  painter.fillRect(0,0,width(),height(),Qt::white);
143  painter.setPen(blackPen);
144  painter.setRenderHint(QPainter::Antialiasing, false);
145 }
146 
147 } //end namespace farsa