24 #include "evorobotviewer.h"
25 #include "renderworldwrapperwidget.h"
26 #include "abstracttest.h"
28 #include "total99resources.h"
29 #include <QGridLayout>
30 #include <QPushButton>
32 #include <QFileDialog>
33 #include <QVBoxLayout>
38 #include <QCoreApplication>
43 using namespace qglviewer;
50 #pragma warning(disable:4996)
60 , ga(evorobot->getGA())
63 , renderworldwrapper(NULL)
65 , simulationThrottle(NULL)
66 , simulationSpeed(NULL)
74 timer =
new QTimer(
this);
75 timer->setInterval( 40 );
76 timer->setSingleShot(
false );
82 connect( timer, SIGNAL(timeout()),
this, SLOT(onWorldAdvance()) );
86 connect( evorobot, SIGNAL(actionFinished()),
this, SLOT(onActionFinished()) );
96 actionsMenu->addAction(
"Evolve", evorobot, SLOT(evolve()) );
97 actionsMenu->addAction(
"Stop", evorobot, SLOT(stop()) );
101 QList<ParameterSettableUIViewer> viewsList;
102 viewsList.append( evogaControls( parent, flags ) );
104 viewsList.append( fitview( parent, flags ) );
105 viewsList.append( statview( parent, flags ) );
106 viewsList.append( renderWorld( parent, flags ) );
109 for (
int i = 0; i < testsList.size(); i++) {
111 if ( testIndividual ) {
112 viewsList.append( testIndividualUI( testIndividual, parent, flags ) );
119 QMenu* testMenu = menuBar->addMenu(
"Tests" );
121 for (
int i = 0; i < testsList.size(); i++) {
123 QAction* action = testMenu->addAction( test->
menuText(), evorobot, SLOT(runTestFromQAction()) );
124 action->setData( QVariant(testsList[i]) );
125 action->setToolTip( test->
tooltip() );
139 ParameterSettableUIViewer EvoRobotViewer::evogaControls( QWidget* parent, Qt::WindowFlags flags ) {
140 QWidget* widget =
new QWidget( parent, flags );
141 QGridLayout* lay =
new QGridLayout( widget );
145 infoEvoga =
new QLabel(
"Information", widget );
146 infoEvoga->setStyleSheet(
"QLabel { font: bold normal large \"Courier\" }" );
147 lay->addWidget( infoEvoga, 0, 0, 1, 2 );
149 QPushButton* bt =
new QPushButton(
"Do Step", widget );
150 bt->setAutoRepeat(
true);
151 bt->setEnabled(
false );
152 connect( bt, SIGNAL(clicked(
bool)), ga, SLOT(doNextStep()) );
154 QCheckBox* cb =
new QCheckBox(
"Step by Step Mode", widget );
155 connect( cb, SIGNAL(clicked(
bool)), ga, SLOT(enableStepByStep(
bool)) );
156 connect( cb, SIGNAL(clicked(
bool)), bt, SLOT(setEnabled(
bool)) );
158 lay->addWidget( cb, 1, 0 );
159 lay->addWidget( bt, 1, 1 );
161 bt =
new QPushButton(
"Next Trial", widget );
162 connect( bt, SIGNAL(clicked()),
this, SLOT(evogaNextTrial()) );
163 lay->addWidget( bt, 2, 0, 1, 2 );
168 QLabel* lb =
new QLabel(
"Simulation Throttle - speed regulator", widget );
169 lay->addWidget( lb, 3, 0, 1, 2 );
170 simulationThrottle =
new QSlider( widget );
171 simulationThrottle->setMinimum( 0 );
172 simulationThrottle->setMaximum( 100 );
173 simulationThrottle->setMinimumHeight( 200 );
174 simulationThrottle->setOrientation( Qt::Vertical );
176 simulationThrottle->setValue( floor(13.0*std::log((
float)currDelay)) );
177 connect( simulationThrottle, SIGNAL(valueChanged(
int)),
this, SLOT(onSimulationThrottleChanges(
int)) );
178 lay->addWidget( simulationThrottle, 4, 0, 3, 1 );
179 lb =
new QLabel(
"slow", widget );
180 lay->addWidget( lb, 4, 1 );
182 QString str =
"Running as fast as possible";
183 if ( currDelay > 0 ) {
186 extra =
"Running approximately at real time";
188 str = QString(
"Speed: %1 frames/second [%2 ms]\n%3")
189 .arg(1000.0/currDelay)
193 simulationSpeed =
new QLabel( str, widget );
194 lay->addWidget( simulationSpeed, 5, 1 );
195 lb =
new QLabel(
"fast", widget );
196 lay->addWidget( lb, 6, 1 );
198 return ParameterSettableUIViewer( widget,
"Evoga Controls" );
201 void EvoRobotViewer::evogaNextTrial() {
205 void EvoRobotViewer::onSimulationThrottleChanges(
int newvalue ) {
207 int delay = ceil(std::exp( newvalue/13.0 )-1.0);
212 extra =
"Running approximately at real time";
214 simulationSpeed->setText( QString(
"Speed: %1 frames/second [%2 ms]\n%3")
219 simulationSpeed->setText(
"Running as fast as possible" );
223 ParameterSettableUIViewer EvoRobotViewer::fitview( QWidget* parent, Qt::WindowFlags flags )
226 ftv =
new FitViewer(3,4000,parent,flags);
227 ftv->setChunkProperties(0,
"MaxFit", Qt::red,
true);
228 ftv->setChunkProperties(1,
"AverageFit", Qt::green,
true);
229 ftv->setChunkProperties(2,
"MinFit", Qt::blue,
true);
230 ftv->setLabels(QString(
"EvoICub Fitness Monitor"), QString(
"Generations"), QString(
"Fitness"));
231 ftv->setGeometry(50, 50, 500, 500);
232 ftv->setWindowTitle(
"Fitness Curves" );
233 connect( ga, SIGNAL(startingReplication(
int)),
234 this, SLOT(onEvogaStartingReplication(
int)), Qt::QueuedConnection );
235 connect( ga, SIGNAL(recoveredInterruptedEvolution(QString)),
236 this, SLOT(onEvogaRecoveredInterruptedEvolution(QString)), Qt::QueuedConnection );
237 connect( ga, SIGNAL(endGeneration(
int,
double,
double,
double)),
238 this, SLOT(onEvogaEndGeneration(
int,
double,
double,
double)), Qt::QueuedConnection );
239 return ParameterSettableUIViewer( ftv,
"Fitness monitor" );
251 ParameterSettableUIViewer EvoRobotViewer::statview( QWidget* parent, Qt::WindowFlags flags )
253 statViewer =
new QWidget( parent, flags );
254 statViewer->setWindowTitle(
"Statistics Viewer" );
255 QGridLayout* lay =
new QGridLayout( statViewer );
256 QPushButton* but =
new QPushButton(
"Load a Stat File", statViewer );
257 connect( but, SIGNAL(clicked()),
this, SLOT(loadStat()) );
258 lay->addWidget( but, 0, 0 );
259 but =
new QPushButton(
"Load All Stat", statViewer );
260 connect( but, SIGNAL(clicked()),
this, SLOT(loadAllStat()) );
261 lay->addWidget( but, 0, 1 );
262 return ParameterSettableUIViewer( statViewer,
"Statistic Viewer" );
265 void EvoRobotViewer::loadStat() {
266 QString filename = QFileDialog::getOpenFileName(statViewer, tr(
"Open Stat File"),
".", tr(
"Files with statistics (*.fit *.ini)"));
267 if (filename.isEmpty()) {
270 FitViewer* fitViewer = statViewer->findChild<FitViewer*>(
"statFitViewer" );
274 fitViewer =
new FitViewer(3, 4000, statViewer);
275 fitViewer->setObjectName(
"statFitViewer" );
276 fitViewer->setLabels(QString(
"Stat monitor. File: ").append(filename), QString(
"Generation"), QString(
"Fitnes"));
277 fitViewer->setChunkProperties(0,
"MaxFit", Qt::red,
true);
278 fitViewer->setChunkProperties(1,
"Average", Qt::green,
true);
279 fitViewer->setChunkProperties(2,
"Minimum", Qt::blue,
true);
280 fitViewer->loadRawData(0, filename, 0);
281 fitViewer->loadRawData(1, filename, 1);
282 fitViewer->loadRawData(2, filename, 2);
283 QGridLayout* lay = qobject_cast<QGridLayout*>( statViewer->layout() );
284 lay->addWidget( fitViewer, 1, 0, 1, 2 );
285 lay->setRowStretch( 1, 2 );
289 void EvoRobotViewer::loadAllStat()
291 FitViewer* fitViewer = statViewer->findChild<FitViewer*>(
"statFitViewer" );
297 QFileInfoList statFiles = currentDir.entryInfoList( QStringList() <<
"statS*.fit", QDir::Files, QDir::Name );
300 fitViewer->setObjectName(
"statFitViewer" );
301 QString title = QString(
"Stat monitor");
302 fitViewer->setLabels(title, QString(
"Generation"), QString(
"Fitness"));
304 QColor colors[10] = { QColor(Qt::red), QColor(Qt::green), QColor(Qt::blue), QColor(Qt::cyan),
305 QColor(Qt::magenta), QColor(Qt::darkYellow), QColor(Qt::gray), QColor(255, 140, 0, 255),
306 QColor(153, 50, 204, 255), QColor(Qt::black) };
307 for(
int i=0; i<statFiles.size(); i++ ) {
308 QFileInfo statFile = statFiles[i];
309 QColor col = colors[i%10];
310 fitViewer->setChunkProperties( i, QString(
"Seed:").append(statFile.baseName().split(
"S").last()), col,
true );
311 fitViewer->loadRawData( i, statFile.fileName(), 0 );
313 fitViewer->sortchunks();
314 QGridLayout* lay = qobject_cast<QGridLayout*>( statViewer->layout() );
315 lay->addWidget( fitViewer, 1, 0, 1, 2 );
316 lay->setRowStretch( 1, 2 );
320 ParameterSettableUIViewer EvoRobotViewer::testIndividualUI( TestIndividual* test, QWidget* parent, Qt::WindowFlags flags ) {
321 TestIndividualGUI* testIndUI =
new TestIndividualGUI( test, parent, flags );
322 testIndUI->setWindowTitle(
"Select the Individual to Test" );
323 return ParameterSettableUIViewer( testIndUI,
"Individual to Test", QString(),
"From this view you can select an individual to test using the \"TestIndividual\" from the \"Tests\" menu" );
326 void EvoRobotViewer::onWorldAdvance() {
327 if (renderworldwrapper) {
334 EvoRobotExperiment* exp = getResource<EvoRobotExperiment>(
"experiment");
337 infoEvoga->setText( QString(
"Step %1 of %2 --- Trial %3 of %4" )
338 .arg( exp->getCurStep(), 5 ).arg( exp->getNSteps() )
339 .arg( exp->getCurTrial() + 1, 5 ).arg( exp->getNTrials() ) );
349 void EvoRobotViewer::onEvogaStartingReplication(
int ) {
353 void EvoRobotViewer::onEvogaRecoveredInterruptedEvolution( QString statfile ) {
354 ftv->loadRawData(0,statfile,0);
355 ftv->loadRawData(1,statfile,1);
356 ftv->loadRawData(2,statfile,2);
359 void EvoRobotViewer::onEvogaEndGeneration(
int generation,
double fmax,
double faverage,
double fmin ) {
360 ftv->setChunkValue(0,generation,fmax);
361 ftv->setChunkValue(1,generation,faverage);
362 ftv->setChunkValue(2,generation,fmin);
363 ftv->diplayUntilStep(generation);
367 void EvoRobotViewer::onActionFinished() {
368 infoEvoga->setText( evorobot->
status() );
505 QWidget(parent, flags)
510 QGridLayout* mainLay =
new QGridLayout(
this );
513 QPushButton* bt =
new QPushButton(
"Refresh",
this );
514 connect( bt, SIGNAL(clicked()),
this, SLOT(populateCombo()) );
515 mainLay->addWidget(bt, 0, 0, 1, 2);
517 combo =
new QComboBox(
this );
518 list =
new QListWidget(
this );
519 mainLay->addWidget(
new QLabel(
"Select File to load:"), 1, 0);
520 mainLay->addWidget(combo, 1, 1);
521 mainLay->addWidget(list, 2, 0, 1, 2);
527 connect(combo, SIGNAL(activated(QString)),
this, SLOT(seedWasChosen()));
528 connect(list, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
this, SLOT(agentClicked()));
529 connect(list, SIGNAL(itemClicked(QListWidgetItem*)),
this, SLOT(agentClicked()));
532 void TestIndividualGUI::populateCombo()
537 QString bestF = test->component()->getGA()->bestsFilename();
538 QString genF = test->component()->getGA()->generationFilename();
541 QDir* dir =
new QDir();
542 QStringList expression = (QStringList() << bestF << genF);
543 fileList = dir->entryList(expression);
546 combo->addItems(fileList);
549 void TestIndividualGUI::seedWasChosen()
552 test->setPopulationToTest( combo->currentText(), false );
555 int loadindi = test->component()->getGA()->numLoadedGenotypes();
556 for(
int i=1; i<=loadindi; i++)
558 list->addItem(QString::number(i));
562 void TestIndividualGUI::agentClicked()
565 test->setIndividualToTest( list->currentRow() );
571 #if defined(_MSC_VER)