00001 #include <qfiledialog.h>
00002 #include <qapplication.h>
00003 #include <qlabel.h>
00004 #include <qmessagebox.h>
00005
00006 #include "SIGEL_MasterGUI/SIG_Experiment.h"
00007
00008 #include "SIGEL_GP/SIG_GUIGPManager.h"
00009
00010 #include "SIGEL_RobotIO/SIG_RobotBuilder.h"
00011 #include "SIGEL_Robot/SIG_LanguageParameters.h"
00012
00013 #include "SIGEL_Tools/SIG_IO.h"
00014
00015
00016 namespace SIGEL_MasterGUI
00017 {
00018
00019 SIG_Experiment::SIG_Experiment( QString name, QWidgetStack *theWidgetStack, SIG_ExperimentItem *theExperimentItem ) : experimentName(name), widgetStack( theWidgetStack ), experimentItem(theExperimentItem), gpExperiment(), gpManager(0)
00020 {
00021
00022 menuGPParameter = new QPopupMenu( this );
00023 menuGPParameter->insertItem( "GPParameter" );
00024
00025
00026 menuSimulationParameter = new QPopupMenu( this );
00027 menuSimulationParameter->insertItem( "Import", this, SLOT( slotSimulationParameterImport() ) );
00028 menuSimulationParameter->insertItem( "Export", this, SLOT( slotSimulationParameterExport() ) );
00029
00030
00031 menuEnvironmentView = new QPopupMenu( this );
00032 menuEnvironmentView->insertItem( "Import", this, SLOT( slotEnvironmentImport() ) );
00033 menuEnvironmentView->insertItem( "Export", this, SLOT( slotEnvironmentExport() ) );
00034
00035
00036 menuRobotView = new QPopupMenu( this );
00037 menuRobotView->insertItem( "RobotView" );
00038
00039
00040 menuExperimentView = new QPopupMenu( this );
00041 menuExperimentView->insertItem( "Start", this, SLOT( slotStartEvolution() ) );
00042 menuExperimentView->insertItem( "Stop", this, SLOT( slotStopEvolution() ) );
00043
00044 gpParameter = new SIG_GPParameter( this , "GPParameter", 0, gpExperiment );
00045 simulationParameter = new SIG_SimulationParameter( this, "SimulationParameter", 0, gpExperiment);
00046 environmentView = new SIG_EnvironmentView( this, "EnvironmentView", 0, gpExperiment );
00047 robotView = new SIG_RobotView( this, "RobotView",0 , gpExperiment );
00048 experimentView = new SIG_ExperimentView( this, "ExperimentView", 0, gpExperiment );
00049 allIndividualsView = new SIG_AllIndividualsView( this, "AllIndividualsView", gpExperiment );
00050 languageParameters = new SIG_LanguageParameters( this, "LanguageParametersView", 0, gpExperiment );
00051
00052
00053 widgetStack->addWidget( gpParameter, 0 );
00054 widgetStack->addWidget( simulationParameter, 0 );
00055 widgetStack->addWidget( environmentView, 0);
00056 widgetStack->addWidget( robotView,0 );
00057 widgetStack->addWidget( experimentView, 0 );
00058 widgetStack->addWidget( allIndividualsView, 0 );
00059 widgetStack->addWidget( languageParameters, 0 );
00060
00061
00062 widgetDict.insert( "GP-Parameters" , gpParameter );
00063 widgetDict.insert( "Simulation-Parameters", simulationParameter );
00064 widgetDict.insert( "Language-Parameters", languageParameters );
00065 widgetDict.insert( "Environment", environmentView );
00066 widgetDict.insert( "Robot", robotView );
00067 widgetDict.insert( "Individuals", allIndividualsView );
00068 widgetDict.insert( experimentName, experimentView );
00069
00070
00071 menuDict.insert( "GP-Parameters" , menuGPParameter );
00072 menuDict.insert( "Simulation-Parameters", menuSimulationParameter );
00073 menuDict.insert( "Environment" , menuEnvironmentView );
00074 menuDict.insert( "Robot" , menuRobotView );
00075 menuDict.insert( experimentName, menuExperimentView );
00076
00077 QObject::connect( experimentView->pushbuttonStart,
00078 SIGNAL( clicked() ),
00079 this,
00080 SLOT( slotStartEvolution() ) );
00081
00082 QObject::connect( experimentView->pushbuttonStop,
00083 SIGNAL( clicked() ),
00084 this,
00085 SLOT( slotStopEvolution() ) );
00086
00087 QObject::connect( gpParameter->pushbuttonImport,
00088 SIGNAL( clicked() ),
00089 this,
00090 SLOT( slotGPParameterImport() ) );
00091
00092 QObject::connect( gpParameter->pushbuttonExport,
00093 SIGNAL( clicked() ),
00094 this,
00095 SLOT( slotGPParameterExport() ) );
00096
00097 QObject::connect( languageParameters->pushbuttonImport,
00098 SIGNAL( clicked() ),
00099 this,
00100 SLOT( slotLanguageParameterImport() ) );
00101
00102 QObject::connect( languageParameters->pushbuttonExport,
00103 SIGNAL( clicked() ),
00104 this,
00105 SLOT( slotLanguageParameterExport() ) );
00106
00107 QObject::connect( simulationParameter->pushbuttonImport,
00108 SIGNAL( clicked() ),
00109 this,
00110 SLOT( slotSimulationParameterImport() ) );
00111
00112 QObject::connect( simulationParameter->pushbuttonExport,
00113 SIGNAL( clicked() ),
00114 this,
00115 SLOT( slotSimulationParameterExport() ) );
00116
00117 QObject::connect( environmentView->pushbuttonImport,
00118 SIGNAL( clicked() ),
00119 this,
00120 SLOT( slotEnvironmentImport() ) );
00121
00122 QObject::connect( environmentView->pushbuttonExport,
00123 SIGNAL( clicked() ),
00124 this,
00125 SLOT( slotEnvironmentExport() ) );
00126
00127 QObject::connect( robotView->pushbuttonImport,
00128 SIGNAL( clicked() ),
00129 this,
00130 SLOT( slotRobotImport() ) );
00131
00132 QObject::connect( robotView->pushbuttonLoad,
00133 SIGNAL( clicked() ),
00134 this,
00135 SLOT( slotRobotLoad() ) );
00136
00137 QObject::connect( robotView->pushbuttonSave,
00138 SIGNAL( clicked() ),
00139 this,
00140 SLOT( slotRobotSave() ) );
00141
00142 QObject::connect( allIndividualsView,
00143 SIGNAL( signalDataRefreshNeeded() ),
00144 this,
00145 SLOT( putAllIntoExperiment() ) );
00146
00147 QObject::connect( this,
00148 SIGNAL( signalEvolutionNotRunning( bool ) ),
00149 allIndividualsView,
00150 SLOT( slotEvolutionNotRunning( bool ) ) );
00151 widgetDict.setAutoDelete( true );
00152
00153 getAllOutOfExperiment();
00154 };
00155
00156 SIG_Experiment::~SIG_Experiment()
00157 {
00158
00159 QDictIterator<QWidget> it( widgetDict );
00160 while ( it.current() )
00161 {
00162 widgetStack->removeWidget( it.current() );
00163 ++it;
00164 }
00165
00166 if( gpManager )
00167 {
00168 delete gpManager;
00169 }
00170 };
00171
00172 QString SIG_Experiment::getName() const
00173 {
00174 return experimentName;
00175 };
00176
00177 void SIG_Experiment::setName( QString newName )
00178 {
00179 widgetDict.setAutoDelete( false );
00180 QWidget *theExperimentView = widgetDict[ experimentName ];
00181 widgetDict.remove( experimentName );
00182 widgetDict.insert( newName, theExperimentView );
00183 widgetDict.setAutoDelete( true );
00184
00185 experimentName = newName;
00186 experimentItem->setText( 0, newName );
00187 };
00188
00189 void SIG_Experiment::putAllIntoExperiment()
00190 {
00191 experimentView->putIntoExperiment();
00192 gpParameter->putIntoExperiment();
00193 simulationParameter->putIntoExperiment();
00194 languageParameters->putIntoExperiment();
00195 environmentView->putIntoExperiment();
00196 };
00197
00198 void SIG_Experiment::getAllOutOfExperiment()
00199 {
00200 experimentView->getOutOfExperiment();
00201 gpParameter->getOutOfExperiment();
00202 simulationParameter->getOutOfExperiment();
00203 robotView->getOutOfExperiment();
00204 languageParameters->getOutOfExperiment();
00205 environmentView->getOutOfExperiment();
00206 allIndividualsView->slotCompleteRefreshList();
00207 };
00208
00209 QString SIG_Experiment::checkEnding( QString fileName, QString ending )
00210 {
00211 QString endWithPoint = "." + ending;
00212 if( fileName.right( endWithPoint.length() ) == endWithPoint )
00213 return fileName;
00214 else
00215 return fileName.append( "." + ending);
00216 };
00217
00218 void SIG_Experiment::slotRightClick( QString option, const QPoint & thePoint )
00219 {
00220 QPopupMenu *showMenu = menuDict[option];
00221 QWidget *showWidget = widgetDict[option];
00222 if( showMenu && showWidget )
00223 {
00224 showMenu->popup( thePoint );
00225 widgetStack->raiseWidget( showWidget );
00226 }
00227 };
00228
00229 void SIG_Experiment::slotSelectionChanged( QString option )
00230 {
00231 widgetStack->raiseWidget( widgetDict[option] );
00232 };
00233
00234 void SIG_Experiment::slotStartEvolution()
00235 {
00236 if( (gpExperiment.robot.getBodyIter().count() != 0) && (gpExperiment.population.getSize() >= 4) && (gpExperiment.gpParameter.getFitnessName() != QString::null) )
00237 {
00238 delete gpManager;
00239
00240 gpManager = new SIGEL_GP::SIG_GUIGPManager( *this );
00241 #ifdef SIG_DEBUG
00242 SIGEL_Tools::SIG_IO::cout << "Starting Evolution (Haha)\n";
00243 #endif
00244 putAllIntoExperiment();
00245
00246 emit signalEvolutionNotRunning( false );
00247 experimentView->pushbuttonStart->setEnabled( false );
00248 experimentView->pushbuttonStop->setEnabled( true );
00249
00250
00251 gpParameter->setEnabled( false );
00252 simulationParameter->setEnabled( false );
00253 robotView->setEnabled( false );
00254 languageParameters->setEnabled( false );
00255 environmentView->setEnabled( false );
00256
00257
00258 gpManager->start();
00259
00260 slotEvolutionStopped();
00261 }
00262 else
00263 {
00264 QMessageBox::warning( this, "Can't start evolution...", "The evolution cannot be started. There may be several reasons:<ul><li>There is no robot loaded.</li><li>There are less than four individuals in the population</li><li>No fitness function name was specified.</li></ul>");
00265 }
00266 };
00267
00268 void SIG_Experiment::slotStopEvolution()
00269 {
00270 emit signalEvolutionNotRunning( true );
00271 #ifdef SIG_DEBUG
00272 SIGEL_Tools::SIG_IO::cout << "Stopping Evolution (Haha)\n";
00273 #endif
00274
00275 experimentView->pushbuttonStop->setEnabled( false );
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 gpManager->userTerminated = true;
00286 };
00287
00288 void SIG_Experiment::slotSimulationParameterImport()
00289 {
00290 QString fileName = QFileDialog::getOpenFileName( QString::null, "Simulation Parameter Files (*.sip);;All Files (*)", 0, "FileOpenSimulationParameter", "Import Simulation Parameters...");
00291 if ( !fileName.isEmpty() )
00292 {
00293 QFile file( fileName );
00294 if( file.open(IO_ReadOnly) )
00295 {
00296 QTextStream theStream( &file );
00297 gpExperiment.simulationParameter.readFromFile( theStream );
00298 }
00299 file.close();
00300 simulationParameter->getOutOfExperiment();
00301 }
00302 };
00303
00304 void SIG_Experiment::slotSimulationParameterExport()
00305 {
00306 simulationParameter->putIntoExperiment();
00307 QString fileName = QFileDialog::getSaveFileName( QString::null, "Simulation Parameter Files (*.sip);;All Files (*)", 0, "FileOpenSimulationParameter", "Export Simulation Parameters...");
00308 if( !fileName.isEmpty() )
00309 {
00310 fileName = checkEnding( fileName, "sip" );
00311 QFile file( fileName );
00312 if( file.exists() )
00313 switch( QMessageBox::warning( 0, "File exists...", "The file " + file.name() + " exists!\nDo you want to overwrite?", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape ) )
00314 {
00315 case QMessageBox::Yes:
00316 if( file.open(IO_WriteOnly) )
00317 {
00318 QTextStream theStream( &file );
00319 gpExperiment.simulationParameter.writeToFile( theStream );
00320 }
00321 file.close();
00322 break;
00323 }
00324 if( file.open(IO_WriteOnly) )
00325 {
00326 QTextStream theStream( &file );
00327 gpExperiment.simulationParameter.writeToFile( theStream );
00328 }
00329 file.close();
00330 }
00331 };
00332
00333 void SIG_Experiment::slotEnvironmentImport()
00334 {
00335 QString fileName = QFileDialog::getOpenFileName( QString::null, "Environment Files (*.env);;All Files (*)", 0, "FileImportEnvironment", "Import Environment...");
00336 if ( !fileName.isEmpty() )
00337 {
00338 QFile file( fileName );
00339 if( file.open(IO_ReadOnly) )
00340 {
00341 QTextStream theStream( &file );
00342 gpExperiment.environment.readFromFile( theStream );
00343 }
00344 file.close();
00345 environmentView->getOutOfExperiment();
00346 }
00347 };
00348
00349 void SIG_Experiment::slotEnvironmentExport()
00350 {
00351 environmentView->putIntoExperiment();
00352 QString fileName = QFileDialog::getSaveFileName( QString::null, "Environment Files (*.env);;All Files (*)", 0, "EnvironmentExportDialog", "Export Environment...");
00353 if( !fileName.isEmpty() )
00354 {
00355 fileName = checkEnding( fileName, "env" );
00356 QFile file( fileName );
00357 if( file.exists() )
00358 switch( QMessageBox::warning( 0, "File exists...", "The file " + file.name() + " exists!\nDo you want to overwrite?", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape ) )
00359 {
00360 case QMessageBox::Yes:
00361 if( file.open(IO_WriteOnly) )
00362 {
00363 QTextStream theStream( &file );
00364 gpExperiment.environment.writeToFile( theStream );
00365 }
00366 file.close();
00367 break;
00368 }
00369 if( file.open(IO_WriteOnly) )
00370 {
00371 QTextStream theStream( &file );
00372 gpExperiment.environment.writeToFile( theStream );
00373 }
00374 file.close();
00375 }
00376 };
00377
00378 void SIG_Experiment::slotGPParameterImport()
00379 {
00380 QString fileName = QFileDialog::getOpenFileName( QString::null, "GP Parameter Files (*.gpp);;All Files (*)", 0, "FileImportGPParameter", "Import GP Parameter...");
00381 if ( !fileName.isEmpty() )
00382 {
00383 QFile file( fileName );
00384 if( file.open(IO_ReadOnly) )
00385 {
00386 QTextStream theStream( &file );
00387 gpExperiment.gpParameter.readFromFile( theStream );
00388 }
00389 file.close();
00390 gpParameter->getOutOfExperiment();
00391 }
00392 };
00393
00394 void SIG_Experiment::slotGPParameterExport()
00395 {
00396 gpParameter->putIntoExperiment();
00397 QString fileName = QFileDialog::getSaveFileName( QString::null, "GP Parameter Files (*.gpp);;All Files (*)", 0, "GPParameterExportDialog", "Export GP Parameter...");
00398 if( !fileName.isEmpty() )
00399 {
00400 fileName = checkEnding( fileName, "gpp" );
00401 QFile file( fileName );
00402 if( file.exists() )
00403 switch( QMessageBox::warning( 0, "File exists...", "The file " + file.name() + " exists!\nDo you want to overwrite?", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape ) )
00404 {
00405 case QMessageBox::Yes:
00406 if( file.open(IO_WriteOnly) )
00407 {
00408 QTextStream theStream( &file );
00409 gpExperiment.gpParameter.writeToFile( theStream );
00410 }
00411 file.close();
00412 break;
00413 }
00414 if( file.open(IO_WriteOnly) )
00415 {
00416 QTextStream theStream( &file );
00417 gpExperiment.gpParameter.writeToFile( theStream );
00418 }
00419 file.close();
00420 }
00421 };
00422
00423 void SIG_Experiment::slotLanguageParameterImport()
00424 {
00425 QString fileName = QFileDialog::getOpenFileName( QString::null, "Language Parameter Files (*.lap);;All Files (*)", 0, "FileImportLanguageParameter", "Import Language Parameter...");
00426 if ( !fileName.isEmpty() )
00427 {
00428 QFile file( fileName );
00429 if( file.open(IO_ReadOnly) )
00430 {
00431 QTextStream theStream( &file );
00432 SIGEL_Robot::SIG_LanguageParameters *newLanguageParameters = new SIGEL_Robot::SIG_LanguageParameters( theStream, true );
00433 gpExperiment.robot.setLangParam( newLanguageParameters );
00434 }
00435 file.close();
00436 languageParameters->getOutOfExperiment();
00437 }
00438 };
00439
00440 void SIG_Experiment::slotLanguageParameterExport()
00441 {
00442 languageParameters->putIntoExperiment();
00443 QString fileName = QFileDialog::getSaveFileName( QString::null, "Language Parameter Files (*.lap);;All Files (*)", 0, "FileExportLanguageParameter", "Export Language Parameters...");
00444 if( !fileName.isEmpty() )
00445 {
00446 fileName = checkEnding( fileName, "lap" );
00447 QFile file( fileName );
00448 if( file.exists() )
00449 switch( QMessageBox::warning( 0, "File exists...", "The file " + file.name() + " exists!\nDo you want to overwrite?", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape ) )
00450 {
00451 case QMessageBox::Yes:
00452 if( file.open(IO_WriteOnly) )
00453 {
00454 QTextStream theStream( &file );
00455 gpExperiment.robot.getLangParam()->writeToFileTransfer( theStream );
00456 }
00457 file.close();
00458 break;
00459 }
00460 if( file.open(IO_WriteOnly) )
00461 {
00462 QTextStream theStream( &file );
00463 gpExperiment.robot.getLangParam()->writeToFileTransfer( theStream );
00464 }
00465 file.close();
00466 }
00467 };
00468
00469 void SIG_Experiment::slotPopulationImport()
00470 {
00471 QString fileName = QFileDialog::getOpenFileName( QString::null, "Population Files (*.pop);;All Files (*)", 0, "FileImportPopulation", "Import Population...");
00472 if ( !fileName.isEmpty() )
00473 {
00474 QFile file( fileName );
00475 if( file.open(IO_ReadOnly) )
00476 {
00477 QTextStream theStream( &file );
00478 gpExperiment.population.readFromFile( theStream );
00479 }
00480 file.close();
00481 allIndividualsView->slotCompleteRefreshList();
00482 }
00483 };
00484
00485 void SIG_Experiment::slotPopulationExport()
00486 {
00487 QString fileName = QFileDialog::getSaveFileName( QString::null, "Population files (*.pop);;All Files (*)", 0, "FileExportPopulation", "Export Population...");
00488 if( !fileName.isEmpty() )
00489 {
00490 fileName = checkEnding( fileName, "pop" );
00491 QFile file( fileName );
00492 if( file.exists() )
00493 switch( QMessageBox::warning( 0, "File exists...", "The file " + file.name() + " exists!\nDo you want to overwrite?", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape ) )
00494 {
00495 case QMessageBox::Yes:
00496 if( file.open(IO_WriteOnly) )
00497 {
00498 QTextStream theStream( &file );
00499 gpExperiment.population.writeToFile( theStream );
00500 }
00501 file.close();
00502 break;
00503 }
00504 if( file.open(IO_WriteOnly) )
00505 {
00506 QTextStream theStream( &file );
00507 gpExperiment.population.writeToFile( theStream );
00508 }
00509 file.close();
00510 }
00511 };
00512
00513 void SIG_Experiment::slotRobotImport()
00514 {
00515 QString fileName = QFileDialog::getOpenFileName( QString::null, "Raw Robot Files (*.rrb);;All Files (*)", 0, "FileImportRobot", "Import Robot...");
00516 if( !fileName.isEmpty() )
00517 {
00518 try
00519 {
00520 SIGEL_RobotIO::SIG_RobotBuilder builder( fileName );
00521 builder.buildInto( gpExperiment.robot );
00522 robotView->textlabelUsedRobot->setText( "Used robot: " + fileName );
00523 }
00524 catch( SIGEL_Tools::SIG_Exception e )
00525 {
00526 QMessageBox::warning( this, "Robot import error!", e.getMessage() );
00527 gpExperiment.robot.clear();
00528 }
00529
00530 this->getAllOutOfExperiment();
00531 }
00532 };
00533
00534 void SIG_Experiment::slotGNUPlotExport()
00535 {
00536 QString fileName = QFileDialog::getSaveFileName( QString::null, "GNU plot data file (*.dat);;All Files (*)", 0, "FileExportGNUPlot", "Export to GNU plot...");
00537 if( !fileName.isEmpty() )
00538 {
00539 fileName = checkEnding( fileName, "dat" );
00540 gpExperiment.exportExperimentHistoryToGNUPlot( fileName );
00541 }
00542 };
00543
00544 void SIG_Experiment::slotRobotLoad()
00545 {
00546 QString fileName = QFileDialog::getOpenFileName( QString::null, "Compiled Robot Files (*.crb);;All Files (*)", 0, "FileLoadRobot", "Load Robot...");
00547 if( !fileName.isEmpty() )
00548 {
00549 QFile file( fileName );
00550 if( file.open(IO_ReadOnly) )
00551 {
00552 QTextStream theStream( &file );
00553 try
00554 {
00555 gpExperiment.robot.readFromFileTransfer( theStream );
00556 robotView->textlabelUsedRobot->setText( "Used robot: " + fileName );
00557 }
00558 catch( SIGEL_Tools::SIG_Exception e )
00559 {
00560 QMessageBox::warning( this, "Robot import error!", e.getMessage() );
00561 gpExperiment.robot.clear();
00562 }
00563 }
00564 file.close();
00565
00566 this->getAllOutOfExperiment();
00567 }
00568 };
00569
00570 void SIG_Experiment::slotRobotSave()
00571 {
00572 QString fileName = QFileDialog::getSaveFileName( QString::null, "Cooked Robot Files (*.crb);;All Files (*)", 0, "FileSaveRobot", "Save Robot...");
00573 if( !fileName.isEmpty() )
00574 {
00575 fileName = checkEnding( fileName, "crb" );
00576 QFile file( fileName );
00577 if( file.exists() )
00578 switch( QMessageBox::warning( 0, "File exists...", "The file " + file.name() + " exists!\nDo you want to overwrite?", QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape ) )
00579 {
00580 case QMessageBox::Yes:
00581 if( file.open(IO_WriteOnly) )
00582 {
00583 QTextStream theStream( &file );
00584 gpExperiment.robot.writeToFileTransfer( theStream );
00585 }
00586 file.close();
00587 break;
00588 }
00589 if( file.open(IO_WriteOnly) )
00590 {
00591 QTextStream theStream( &file );
00592 gpExperiment.robot.writeToFileTransfer( theStream );
00593 }
00594 file.close();
00595 }
00596 };
00597
00598 void SIG_Experiment::slotEvolutionStopped()
00599 {
00600 emit signalEvolutionNotRunning( true );
00601 experimentView->pushbuttonStart->setEnabled( true );
00602 experimentView->pushbuttonStop->setEnabled( false );
00603
00604
00605 gpParameter->setEnabled( true );
00606 simulationParameter->setEnabled( true );
00607 robotView->setEnabled( true );
00608 languageParameters->setEnabled( true );
00609 environmentView->setEnabled( true );
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619 };
00620
00621 }
00622