geneticalgoui.cpp
1 /********************************************************************************
2  * FARSA GeneticAlgo UI *
3  * Copyright (C) 2012-2013 *
4  * Gianluca Massera <emmegian@yahoo.it> *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, write to the Free Software *
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19  ********************************************************************************/
20 
21 #include "core/geneticalgoui.h"
22 #include "core/geneticalgo.h"
23 #include "logger.h"
24 #include "datastreamswidget.h"
25 #include <QLabel>
26 #include <QGridLayout>
27 #include <QScrollArea>
28 #include <QMetaObject>
29 
30 namespace farsa {
31 
33  : QObject(), ParameterSettableUI() {
34  ga = component;
35  ga->addObserver( this );
36 }
37 
39 {
40  // Nothing to do
41  // --- All objects are destroyed in others parts because none of them are owend by this object
42 }
43 
44 QList<ParameterSettableUIViewer> GeneticAlgoUI::getViewers( QWidget* parent, Qt::WindowFlags flags ) {
45  QList<ParameterSettableUIViewer> viewsList;
46  viewsList.append( fitnessView( parent, flags ) );
47  return viewsList;
48 }
49 
51  QVector<double> bests = ga->bestFits().last();
52  QVector<double> averages = ga->averageFits().last();
53 
54  if ( fitnessPlot->getNumPlots()<bests.size() ) {
55  QMetaObject::invokeMethod(this, "fitPlotsToGenotype", Qt::BlockingQueuedConnection);
56  }
57 
58  QVector<float> values;
59  values.resize(2);
60  for( int i=0; i<bests.size(); i++ ) {
61  values[0] = bests[i];
62  values[1] = averages[i];
63  fitnessPlot->appendDatas( i, values );
64  }
65 }
66 
67 ParameterSettableUIViewer GeneticAlgoUI::fitnessView( QWidget* parent, Qt::WindowFlags flags ) {
68  QWidget* widget = new QWidget( parent, flags );
69  QGridLayout* lay = new QGridLayout( widget );
70 
71  QScrollArea* scroll = new QScrollArea( widget );
72  lay->addWidget( scroll, 0, 0 );
73  // creating the plot
74  fitnessPlot = new DataStreamsWidget( scroll );
75 
76  scroll->setWidget( fitnessPlot );
77  scroll->setWidgetResizable( true );
78  return ParameterSettableUIViewer( widget, "Fitness Components over Generations" );
79 }
80 
81 void GeneticAlgoUI::fitPlotsToGenotype() {
82  QVector<double> bests = ga->bestFits().last();
83  // adding datastream to plot if needed
84  for( int i=fitnessPlot->getNumPlots(); i<bests.size(); i++ ) {
85  int id = fitnessPlot->addPlot( QString("Component %1").arg(i) );
86  DataStreamPlot* plot = fitnessPlot->getPlot(id);
87  plot->addDataStream( "Max Fitness" );
88  plot->addDataStream( "Average Fitness" );
89  plot->setDataStreamColor( 0, Qt::red );
90  plot->setDataStreamColor( 1, Qt::blue );
91  }
92 }
93 
94 } // end namespace farsa