gui_feedback.cpp

gehe zur Dokumentation dieser Datei
00001 #include "gui.h"
00002 //#include "valueIteration.h"
00003 //#include "gui_simu_simuthread.h"
00004 
00005 
00013 void Simulation::setReward (int _row, int _column, int _action, double _reward) {
00014 
00015    this->feedbackMemory[_row][_column][_action] = _reward;
00016 }
00024 double Simulation::getReward (int _row, int _column, int _action) {
00025    return this->feedbackMemory[_row][_column][_action];
00026 }
00027 
00037 void Simulation::updateFeedbackTable(int _row, int _column, double _up, double _down, double _left, double _right) {
00038 
00039    QTableItem *feedback_item;
00040    static int updateCall = 0;
00041 
00042    this->dad->lock();
00043 
00044 
00045    // Feedback-Memory aktualisieren
00046    setReward(_row, _column, UP, _up);
00047    setReward(_row, _column, DOWN, _down);
00048    setReward(_row, _column, LEFT, _left);
00049    setReward(_row, _column, RIGHT, _right);
00050 
00051    // Feedback-Tabellen Item
00052    feedback_item = lg->envRewardTable->item (_row, _column);
00053    feedback_item->setText ("   " + QString::number ((float)getReward(_row, _column, UP)) + "\n" + 
00054                QString::number ((float)getReward(_row, _column, LEFT)) + 
00055             "    " + QString::number ((float)getReward(_row, _column, RIGHT)) + "\n" +
00056             "   " + QString::number ((float)getReward(_row, _column, DOWN)));
00057 
00058    updateCall++;
00059    if (updateCall == i_cell ) {
00060       for (int i=0; i < lg->numRows->text().toInt(); i++) {
00061          lg->envRewardTable->adjustRow (i);
00062       }
00063       for (int i=0; i < lg->numCols->text().toInt(); i++) {
00064          lg->envRewardTable->adjustColumn (i);
00065       }
00066       updateCall = 0;
00067    }
00068 
00069    this->pkt->unlockMutex();
00070    
00071    // GUI-Mutex wieder freigeben
00072    this->dad->unlock();
00073 }
00074 
00075 
00081 void Simulation::feedbackClicked(int row, int col) {
00082 
00083    this->lg->envUpRewardInput->setValue( (int)getReward(row, col, UP) );
00084    this->lg->envDownRewardInput->setValue( (int)getReward(row, col, DOWN) );
00085    this->lg->envLeftRewardInput->setValue( (int)getReward(row, col, LEFT) );
00086    this->lg->envRightRewardInput->setValue( (int)getReward(row, col, RIGHT) );
00087    
00088    this->lg->envRowLabel->setText(QString::number(row+1));
00089    this->lg->envColLabel->setText(QString::number(col+1));
00090 }
00091 
00092 
00093 
00097 void Simulation::save_feedback_clicked() {
00098    int _row, _col;
00099    
00100    int _up = this->lg->envUpRewardInput->value();
00101    int _down = this->lg->envDownRewardInput->value();
00102    int _left = this->lg->envLeftRewardInput->value();
00103    int _right = this->lg->envRightRewardInput->value();
00104 
00105 
00106    // Alle Zellen checken, ob sie jeweils selektiert sind
00107    for (int i=0; i < this->lg->envRewardTable->numRows(); i++) {
00108       for (int j=0; j < this->lg->envRewardTable->numCols(); j++) {
00109          if (this->lg->envRewardTable->isSelected(i,j)) {
00110 
00111             _row = i;
00112             _col = j;
00113 
00114             // Feedback-Memory aktualisieren
00115             this->feedbackMemory[_row][_col][UP] = _up;
00116             this->feedbackMemory[_row][_col][DOWN] = _down;
00117             this->feedbackMemory[_row][_col][LEFT] = _left;
00118             this->feedbackMemory[_row][_col][RIGHT] = _right;
00119    
00120             updateFeedbackTable ( _row, _col, _up, _down, _left, _right);
00121 
00122             this->pkt->setFeedback( _row, _col, _up, _down, _left, _right);
00123          }
00124       }
00125    }
00126 
00127    // Breite und H�he der Zellen anpassen
00128    for (int i=0; i < this->lg->envRewardTable->numRows(); i++) {
00129       lg->envRewardTable->adjustRow (i);
00130    }
00131    
00132    for (int j=0; j < this->lg->envRewardTable->numCols(); j++) {
00133       lg->envRewardTable->adjustColumn (j);
00134    }
00135 }
00136 
00140 void Simulation::save_button_clicked() {
00141 
00142       QString s = QFileDialog::getSaveFileName(
00143                     ".",
00144                     "Feedback (*.feedback);;All files(*.*)",
00145                     this,
00146                     "open file dialog",
00147                     "Choose a file" );
00148 
00149    this->lg->debugLabel->setText("save button: " + s);
00150 
00151    QFile qf(s);
00152    if ( qf.open(IO_WriteOnly) ) {
00153 
00154       QDataStream stream( &qf ); // we will serialize the data into the file
00155 
00156       //Header schreiben mit Anzahl von Zeilen und Spalten
00157       stream   << (Q_INT32)this->lg->numRows->text().toInt()
00158          << (Q_INT32)this->lg->numCols->text().toInt();
00159       
00160       for (int i=0; i < this->lg->numRows->text().toInt(); i++) {
00161          for (int j=0; j < this->lg->numCols->text().toInt(); j++) {
00162             stream   << (Q_INT32)getReward(i, j, UP)
00163                << (Q_INT32)getReward(i, j, DOWN)
00164                << (Q_INT32)getReward(i, j, LEFT)
00165                << (Q_INT32)getReward(i, j, RIGHT);
00166          }
00167       }
00168       qf.close();
00169    } else {
00170       QMessageBox::critical(  this, "Laufroboter Dialog", "ERROR: Could not write to file: " + s);
00171    }
00172    
00173 }
00174 
00178 void Simulation::save_matlab_button_clicked() {
00179 
00180    char zeile[256];
00181 
00182       QString s = QFileDialog::getSaveFileName(
00183                     ".",
00184                     "Matlab/Octave (*.m);;All files(*.*)",
00185                     this,
00186                     "open file dialog",
00187                     "Choose a file" );
00188 
00189    this->lg->debugLabel->setText("save matlab button: " + s);
00190 
00191    QFile qf(s);
00192    if ( qf.open(IO_WriteOnly) ) {
00193 
00194       //Header schreiben mit Anzahl von Zeilen und Spalten
00195       sprintf (zeile, "envRows=%d;\nenvCols=%d;\n", this->lg->numRows->text().toInt(), this->lg->numCols->text().toInt());
00196       qf.writeBlock(zeile, strlen(zeile));
00197 
00198       for (int i=0; i < this->lg->numRows->text().toInt(); i++) {
00199          for (int j=0; j < this->lg->numCols->text().toInt(); j++) {
00200             sprintf (zeile, "env{%d} = [%f, %f, %f, %f];\n", 
00201                ((i*this->lg->numCols->text().toInt()) + j + 1),
00202                getReward(i, j, UP),
00203                getReward(i, j, DOWN),
00204                getReward(i, j, LEFT),
00205                getReward(i, j, RIGHT));
00206             qf.writeBlock(zeile, strlen(zeile));
00207          }
00208       }
00209       qf.close();
00210    } else {
00211       QMessageBox::critical(  this, "Laufroboter Dialog", "ERROR: Could not write to file: " + s);
00212    }
00213    
00214 }
00215 
00216 
00220 void Simulation::load_button_clicked() {
00221 
00222       QString s = QFileDialog::getOpenFileName(
00223                     ".",
00224                     "Feedback (*.feedback);;All files(*.*)",
00225                     this,
00226                     "open file dialog",
00227                     "Choose a file" );
00228 
00229    this->lg->debugLabel->setText("load button: " + s);
00230 
00231    QFile qf(s);
00232    Q_INT32 _up, _down, _left, _right, _rows, _cols;
00233 
00234    if ( qf.open(IO_ReadOnly) ) {
00235       QDataStream stream( &qf ); // we will serialize the data into the file
00236 
00237       //Header lesen mit Anzahl von Zeilen und Spalten
00238       stream >> _rows >> _cols;
00239 
00240       if (_rows != this->lg->numRows->text().toInt() ||
00241           _cols != this->lg->numCols->text().toInt()) {
00242          QMessageBox::critical(  this, "Laufroboter Dialog", 
00243                   "Your " + this->lg->numRows->text() + "x" +
00244                   this->lg->numCols->text() + " matrix, doesn't fit with a " +
00245                   QString::number(_rows) + "x" + QString::number(_cols) + 
00246                   " matrix, \nwhich is hold inside your selected file. \n\n" + s);
00247       }  else {
00248 
00249          for (int row=0; row < this->lg->numRows->text().toInt(); row++) {
00250             for (int col=0; col < this->lg->numCols->text().toInt(); col++) {
00251                stream >> _up >> _down >> _left >> _right;
00252                this->feedbackMemory[row][col][UP] = _up;
00253                this->feedbackMemory[row][col][DOWN] = _down;
00254                this->feedbackMemory[row][col][LEFT] = _left;
00255                this->feedbackMemory[row][col][RIGHT] = _right;
00256                updateFeedbackTable(row, col, _up, _down, _left, _right);
00257                this->pkt->setFeedback(row, col, _up, _down, _left, _right);
00258             }
00259          }
00260       }
00261       qf.close();
00262    } else {
00263       this->lg->debugLabel->setText("ERROR: Could not read from file: " + s);
00264    }
00265 }
00266 
00267 

Erzeugt am Mon Nov 24 15:30:59 2008 für Walking Robot Simulation GUI - API Documentation von  doxygen 1.5.5