experiments/evorobot/src/holisticviewer.cpp

00001 /********************************************************************************
00002  *  FARSA Experiments Library                                                   *
00003  *  Copyright (C) 2007-2012                                                     *
00004  *  Stefano Nolfi <stefano.nolfi@istc.cnr.it>                                   *
00005  *  Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it>                           *
00006  *  Gianluca Massera <emmegian@yahoo.it>                                        *
00007  *  Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it>                         *
00008  *                                                                              *
00009  *  This program is free software; you can redistribute it and/or modify        *
00010  *  it under the terms of the GNU General Public License as published by        *
00011  *  the Free Software Foundation; either version 2 of the License, or           *
00012  *  (at your option) any later version.                                         *
00013  *                                                                              *
00014  *  This program is distributed in the hope that it will be useful,             *
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00017  *  GNU General Public License for more details.                                *
00018  *                                                                              *
00019  *  You should have received a copy of the GNU General Public License           *
00020  *  along with this program; if not, write to the Free Software                 *
00021  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00022  ********************************************************************************/
00023 
00024 #include "holisticviewer.h"
00025 
00026 namespace farsa {
00027 
00028 HolisticViewer::HolisticViewer(Evonet* network, QWidget* parent, Qt::WindowFlags flags)
00029     : QWidget(parent, flags),
00030       labels() {
00031     net = network;
00032 
00033     mainLayout = new QVBoxLayout(this);
00034 
00035     grid = new QGridLayout();
00036     grid->setSpacing(0);
00037     mainLayout->addLayout(grid);
00038 
00039     neuronChoice = new QHBoxLayout();
00040     neuronX = new QComboBox(this);
00041     neuronY = new QComboBox(this);
00042     neuronChoice->addWidget(neuronX);
00043     neuronChoice->addWidget(neuronY);
00044     mainLayout->addLayout(neuronChoice);
00045 
00046 }
00047 
00048 //return neuron activation scaled from 0 to 255
00049 int HolisticViewer::getNeuronAct(int n)
00050 {
00051     double act = net->getNeuron(n);
00052     double min = net->neuronrange[n][0];
00053     double max = net->neuronrange[n][1];
00054 
00055     act = linearMap(act,min,max,0,255);
00056     return (int)ceil(act);
00057 }
00058 
00059 void HolisticViewer::updateGrid()
00060 {
00061     int neuronsPerRow = ceil(sqrt((double)net->getNoNeurons()));
00062 
00063     //first iteration only
00064     if(labels.size() == 0)
00065     {
00066         QFont font;
00067         QString name, color;
00068 
00069         for(int r=0; r<neuronsPerRow; r++)
00070         {
00071             for(int c=0; c<neuronsPerRow; c++)
00072             {
00073                 int index = r*neuronsPerRow+c;
00074                 
00075                 if(index < net->getNoNeurons())
00076                 {
00077                     name = net->neuronl[index];
00078                     
00079                     //add items to the combo boxes
00080                     neuronX->addItem(name);
00081                     neuronY->addItem(name);
00082 
00083                     //insert labels into the grid
00084                     color = "yellow";
00085                     if(index < net->getNoInputs())
00086                         color = "red";
00087                     if(index > net->getNoInputs()+net->getNoHiddens())
00088                         color = "blue";
00089 
00090                     labels.append( new QLabel(this) );
00091                     labels[index]->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
00092                     labels[index]->setFont(font);
00093                     labels[index]->setText("<font color='"+ color + "';>" + name + "</font>");
00094                     
00095                     grid->addWidget(labels[index], r, c);
00096                 }
00097             }
00098         }
00099     }
00100 
00101     //every step
00102     for(int r=0; r<neuronsPerRow; r++)
00103     {
00104         for(int c=0; c<neuronsPerRow; c++)
00105         {
00106             int index = r*neuronsPerRow+c;
00107             if(index < net->getNoNeurons())
00108             {
00109                 labels[index]->setAutoFillBackground(true);
00110                 int act256 = getNeuronAct(index);
00111                 labels[index]->setPalette(QPalette(QColor(act256,act256,act256, 255)));
00112                 
00113                 int fontSize = (int)(width()+height())/70;
00114                 if(fontSize > 30)
00115                     fontSize = 30;
00116                 QFont font = labels[index]->font();
00117                 font.setPointSize(fontSize);
00118                 labels[index]->setFont(font);
00119             }
00120         }
00121     }
00122 }
00123 
00124 void HolisticViewer::updatePlot()
00125 {
00126     //take selected items from combos
00127     //load their buffer
00128     //plot everything
00129 }
00130 
00131 void HolisticViewer::paintEvent(QPaintEvent* /*evt*/)
00132 {
00133     QPainter painter(this);
00134 
00135     QPen blackPen(Qt::black);
00136     QPen bluePen(Qt::blue);
00137     QPen greenPen(Qt::green);
00138     QPen redPen(Qt::red);
00139 
00140     painter.drawRect(0,0,100,100);
00141     
00142     painter.fillRect(0,0,width(),height(),Qt::white);
00143     painter.setPen(blackPen);
00144     painter.setRenderHint(QPainter::Antialiasing, false);
00145 }
00146 
00147 } //end namespace farsa