void main() { while(1) { lcd("Switch: %d ",in_a(0)); sleep(10); } }
void main() { char mid; lcd("Press SW1"); sw1_press(); while(1) { mid = in_a(0); if(mid==0) { beep_b(4); backward(500); turn_left(400); } else { forward(10); } } }
void fsglft (int B, double*** f_real, double*** f_imag, double*** f_hat_real, double*** f_hat_imag, std::string path) { double*** f_fsft_real = new double**[2 * B]; double*** f_fsft_imag = new double**[2 * B]; for ( int i = 0; i < 2 * B; ++i ) { f_fsft_real[i] = new double*[B]; f_fsft_imag[i] = new double*[B]; for ( int l = 0; l < B; ++l ) { f_fsft_real[i][l] = new double[2 * B + 1]; f_fsft_imag[i][l] = new double[2 * B + 1]; } } for ( int i = 0; i < 2 * B; ++i ) { fsft (B, f_real[i], f_imag[i], f_fsft_real[i], f_fsft_imag[i]); } char* filename_r = new char[1000]; char* filename_a = new char[1000]; sprintf (filename_r, "%s/precomp/half_range_Hermite_quadrature/%d_100_digits_abcissae.txt", path.c_str (), 2 * B); sprintf (filename_a, "%s/precomp/half_range_Hermite_quadrature/%d_1000_digits_weights.txt", path.c_str (), 2 * B); long double* r = new long double[2 * B]; long double* a = new long double[2 * B]; std::ifstream in_r (filename_r); std::ifstream in_a (filename_a); std::string line_r; std::string line_a; for ( int i = 0; i < 2 * B; ++i ) { std::getline (in_r, line_r); std::getline (in_a, line_a); std::stringstream str_stream_r (line_r); std::stringstream str_stream_a (line_a); str_stream_r >> r[i]; str_stream_a >> a[i]; } delete[] filename_r; delete[] filename_a; long double* rdata_Clenshaw = new long double[2 * B]; long double* idata_Clenshaw = new long double[2 * B]; long double* data_trans_real; long double* data_trans_imag; for ( int l = 0; l < B; ++l ) { data_trans_real = new long double[B - l]; data_trans_imag = new long double[B - l]; for ( int m = - l; m <= l; ++m ) { for ( int i = 0; i < 2 * B; ++i ) { rdata_Clenshaw[i] = a[i] * r[i] * r[i] * f_fsft_real[i][l][m + B - 1]; idata_Clenshaw[i] = a[i] * r[i] * r[i] * f_fsft_imag[i][l][m + B - 1]; } DRT_Clenshaw (B, l, r, rdata_Clenshaw, idata_Clenshaw, data_trans_real, data_trans_imag); for ( int n = l + 1; n <= B; ++n ) { f_hat_real[n - 1][l][m + l] = data_trans_real[n - l - 1]; f_hat_imag[n - 1][l][m + l] = data_trans_imag[n - l - 1]; } } delete[] data_trans_real; delete[] data_trans_imag; } for ( int i = 0; i < 2 * B; ++i ) { for ( int l = 0; l < B; ++l ) { delete[] f_fsft_real[i][l]; delete[] f_fsft_imag[i][l]; } delete[] f_fsft_real[i]; delete[] f_fsft_imag[i]; } delete[] f_fsft_real; delete[] f_fsft_imag; delete[] rdata_Clenshaw; delete[] idata_Clenshaw; delete[] r; delete[] a; return; }
/* |-------------------------------------------------------------------------------- | Método responsável pelo slot "IniciarTreinamento" |-------------------------------------------------------------------------------- */ void MainWindow::slotIniciarTreinamento() { if(this->_lineEditNumeroEpocas->text() == ""){ QMessageBox m; m.setText("Campo número de épocas está vazio."); m.exec(); return; } if(this->_lineEditPrecisao->text() == ""){ QMessageBox m; m.setText("Campo precisão está vazio."); m.exec(); return; } if(this->_lineEditTaxaAprendizado->text() == ""){ QMessageBox m; m.setText("Campo taxa de aprendizado está vazio."); m.exec(); return; } if(this->_lineEditAmostraTreinamento->text() == ""){ QMessageBox m; m.setText("Campo amostras de treinamento está vazio."); m.exec(); return; } if(this->_lineEditSaidaDesejada->text() == ""){ QMessageBox m; m.setText("Campo saídas desejadas está vazio."); m.exec(); return; } if(this->_lineEditGuardarPesos->text() == ""){ QMessageBox m; m.setText("Campo guardar pesos está vazio."); m.exec(); return; } this->_lineEditNumeroEpocas->setEnabled(false); this->_lineEditPrecisao->setEnabled(false); this->_lineEditTaxaAprendizado->setEnabled(false); this->_lineEditAmostraTreinamento->setEnabled(false); this->_lineEditSaidaDesejada->setEnabled(false); this->_buttonSelecionarArquivoAmostas->setEnabled(false); this->_buttonSelecionarArquivoSaidas->setEnabled(false); this->_buttonIniciarTreinamento->setEnabled(false); this->_lineEditMomentum->setEnabled(false); /* |-------------------------------------------------------------------------------- | Carrega Arquivo de Amostras |-------------------------------------------------------------------------------- */ QString path_amostras = this->_lineEditAmostraTreinamento->text(); QFile amostras(path_amostras); if(!amostras.open(QIODevice::ReadOnly)) { qDebug() << amostras.errorString(); exit(0); } QTextStream in_a(&amostras); while(!in_a.atEnd()) { QString line_a = in_a.readLine(); QStringList fields_a = line_a.split(","); QVector<double> amostra; for(int i = 0; i < fields_a.size(); i++) { double value_a = fields_a.at(i).toDouble(); amostra.append(value_a); } amostras_treinamento.append(amostra); } amostras.close(); /* |-------------------------------------------------------------------------------- | Carrega Arquivo de Saidas |-------------------------------------------------------------------------------- */ QString path_saidas = this->_lineEditSaidaDesejada->text(); QFile saidas(path_saidas); if(!saidas.open(QIODevice::ReadOnly)) { qDebug() << saidas.errorString(); exit(0); } QTextStream in_s(&saidas); while(!in_s.atEnd()) { QString line_s = in_s.readLine(); QStringList fields_s = line_s.split(","); QVector<double> saida; for(int i = 0; i < fields_s.size(); i++) { double value_s = fields_s.at(i).toDouble(); saida.append(value_s); } saidas_desejada.append(saida); } saidas.close(); /* |-------------------------------------------------------------------------------- | Treinamento |-------------------------------------------------------------------------------- */ double precisao = this->_lineEditPrecisao->text().toDouble(); // pega precisao informada na interface double aprendizado = this->_lineEditTaxaAprendizado->text().toDouble(); // pega a taxa de aprendizado infomada da interface int numepocas = this->_lineEditNumeroEpocas->text().toInt(); // pega o numero de épocas informada na interaface double MOMENTUM = this->_lineEditMomentum->text().toDouble(); // Termo de momentum. QVector<double> gepocas(numepocas), gerro(numepocas);// vetores que guardam a epoca e seu respectivo erro int epocas = 0; double erro_medio_quadratico= 0; double erro_quadratico = 0; double somatorio = 0; int funcao = 0; if(this->_CB_funcaoL->isChecked()){ funcao = 0; this->_CB_funcaoTH->setChecked(false); } if(this->_CB_funcaoTH->isChecked()){ funcao = 1; this->_CB_funcaoL->setChecked(false); } zeraPesos(1); zeraPesos(2); zeraVetoresNeuronios(1); zeraVetoresNeuronios(2); randomizaPesos(1); randomizaPesos(2); // pesos randomizados para a camada 1. qDebug() <<" pesos randomizados para a camada 1"; for (int j = 0; j < (NUM_NEURONIOS_CAMADA_UM*NUM_ENTRADAS); j++) { qDebug() <<w1[j] ; } // pesos randomizados para a camada 2. qDebug() <<" pesos randomizados para a camada 2"; for (int j = 0; j < (NUM_NEURONIOS_CAMADA_DOIS*NUM_NEURONIOS_CAMADA_UM); j++) { qDebug() <<w2[j] ; } qDebug() <<"**************************** Inico Treinamento ****************************"; do{ // Propaga os k padrıes pela rede. for (int k = 0; k < amostras_treinamento.size(); k++) { qDebug() <<"calc c1"; //C·lculo para camada C1. int n = 0; for (int j = 0; j < NUM_NEURONIOS_CAMADA_UM; j++) { somatorio = 0; for (int i = 0; i < NUM_ENTRADAS; i++) { somatorio += w1[n] * amostras_treinamento.at(k).at(i);//amostras_treinamento.at(k); n += 1; } Entrada_I1[j] = somatorio; saida_y1[j] = FuncaoAtivacao(Entrada_I1[j],funcao,1.0); } qDebug() <<"calc c2"; //C·lculo para camada C2. n = 0; for (int j = 0; j < NUM_NEURONIOS_CAMADA_DOIS; j++) { somatorio = 0; for (int i = 0; i < NUM_NEURONIOS_CAMADA_UM; i++) { somatorio += w2[n] * saida_y1[i]; n += 1; } Entrada_I2[j] = somatorio; saida_y2[j] = FuncaoAtivacao(Entrada_I2[j],funcao,1.0); } //********************* C·lculo do Erro MÈdio Quadr·tico ************************ qDebug() <<"Calculo do Erro Quadratico"; //Calculo do Erro Quadratico. erro_quadratico = 0; for(int j = 0; j < NUM_NEURONIOS_CAMADA_DOIS; j++) { erro_quadratico += pow((saidas_desejada[k][j] - saida_y2[j]),2); } //Calculo do Erro Medio Quadratico (Criterio de Parada). erro_medio_quadratico += (0.5 * erro_quadratico); //**************************** Retropropagacao do Erro ************************** //Calculo do erro para camada 2. for (int i = 0; i < NUM_NEURONIOS_CAMADA_DOIS; i++) { erro_camada2[i] = (saidas_desejada[k][i] - saida_y2[i]) * derivada(Entrada_I2[i],funcao,1.0); } //Atualizacao dos pesos para camada 2. for (int i = 0; i < NUM_NEURONIOS_CAMADA_UM; i++) { n = 0; for (int j = 0; j < NUM_NEURONIOS_CAMADA_DOIS; j++) { dw2[n + i] = aprendizado * saida_y1[i] * erro_camada2[j] + (MOMENTUM * dw2[n + i]); w2[n + i] = w2[n + i] + dw2[n + i]; n += NUM_NEURONIOS_CAMADA_UM; } } //Calculo do erro para camada 1. for (int i = 0; i < NUM_NEURONIOS_CAMADA_UM; i++) { n = 0; somatorio = 0; for (int j = 0; j < NUM_NEURONIOS_CAMADA_DOIS; j++) { somatorio += (erro_camada2[j] * w2[n + i]); n += NUM_NEURONIOS_CAMADA_UM; } erro_camada1[i] = somatorio * derivada(Entrada_I1[i],funcao,1.0); } //Atualizacao dos pesos para camada 1. for (int i = 0; i < NUM_ENTRADAS; i++) { n = 0; for (int j = 0; j < NUM_NEURONIOS_CAMADA_UM; j++) { dw1[n + i] = aprendizado * amostras_treinamento.at(k).at(i) * erro_camada1[j] + (MOMENTUM * dw1[n + i]); w1[n + i] = w1[n + i] + dw1[n + i]; n += NUM_ENTRADAS; } } } // C·lculo do erro mÈdio quadr·tico da Època de treinamento. erro_medio_quadratico = (1.0 / amostras_treinamento.size()) * erro_medio_quadratico; gerro.append(erro_medio_quadratico); gepocas.append(epocas); epocas++; // qDebug() << erro_medio_quadratico; //qDebug() << epocas; erro_medio_quadratico = 0; } while(epocas < numepocas ||erro_medio_quadratico >= precisao); qDebug() <<"**************************** Fim Treinamento ****************************"; /* |-------------------------------------------------------------------------------- | Gráfico |-------------------------------------------------------------------------------- */ this->_chart->graph(0)->setData(gepocas, gerro); this->_chart->xAxis->setLabel("CONTAGEM DAS ÉPOCAS"); this->_chart->yAxis->setLabel("ERRO"); this->_chart->xAxis->setRange(0, numepocas); this->_chart->yAxis->setRange(0, 1.5); this->_chart->replot(); /* |-------------------------------------------------------------------------------- | Habilitar Botões |-------------------------------------------------------------------------------- */ this->_lineEditNumeroEpocas->setEnabled(true); this->_lineEditPrecisao->setEnabled(true); this->_lineEditTaxaAprendizado->setEnabled(true); this->_lineEditAmostraTreinamento->setEnabled(true); this->_lineEditSaidaDesejada->setEnabled(true); this->_buttonSelecionarArquivoAmostas->setEnabled(true); this->_buttonSelecionarArquivoSaidas->setEnabled(true); this->_buttonIniciarTreinamento->setEnabled(true); this->_buttonGuardarPesos->setEnabled(true); this->_lineEditMomentum->setEnabled(true); }