Exemplo n.º 1
0
/*
|--------------------------------------------------------------------------------
| 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);

}
 void send_to_server(PDU && pdu, Args && ...args) {
     StaticOutStream<256> out_s;
     pdu.emit(out_s, std::move(args)...);
     InStream in_s(out_s.get_data(), out_s.get_offset());
     this->copy_paste.send_to_mod_channel(in_s, CHANNELS::CHANNEL_FLAG_FIRST | CHANNELS::CHANNEL_FLAG_LAST);
 }
Exemplo n.º 3
0
int main(int argc, char **argv) {
  TestScene *scene = new TestScene(argc, argv);

  typedef std::vector<carve::geom2d::P2> loop_t;
  std::vector<loop_t> poly;

  std::ifstream in(argv[1]);
  while (in.good()) {
    std::string s;
    std::getline(in, s);
    if (s == "BEGIN") {
      poly.push_back(loop_t());
    } else {
      std::istringstream in_s(s);
      double x,y;
      in_s >> x >> y;
      poly.back().push_back(carve::geom::VECTOR(x, y));
    }
  }

  std::vector<std::pair<size_t, size_t> > result;
  std::vector<carve::geom2d::P2> merged;
  std::vector<carve::triangulate::tri_idx> triangulated;

  try {
    result = carve::triangulate::incorporateHolesIntoPolygon(poly);
    merged.reserve(result.size());
    for (size_t i = 0; i < result.size(); ++i) {
      merged.push_back(poly[result[i].first][result[i].second]);
    }
    carve::triangulate::triangulate(merged, triangulated);
    carve::triangulate::improve(merged, triangulated);

  } catch (carve::exception exc) {
    std::cerr << "FAIL: " << exc.str() << std::endl;
    return -1;
  }

  carve::geom::aabb<2> aabb;
  aabb.fit(merged.begin(), merged.end());
  double scale = 20.0 / std::max(aabb.extent.x, aabb.extent.y);

  glNewList(scene->d_list, GL_COMPILE);

  glDisable(GL_LIGHTING);

  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glColor4f(0.2, 0.3, 0.4, 1.0);

  glBegin(GL_TRIANGLES);
  for (size_t i = 0; i != triangulated.size(); ++i) {
    double x, y;
    x = (merged[triangulated[i].a].x - aabb.pos.x) * scale;
    y = (merged[triangulated[i].a].y - aabb.pos.y) * scale;
    glVertex3f(x, y, 0.0);
    x = (merged[triangulated[i].b].x - aabb.pos.x) * scale;
    y = (merged[triangulated[i].b].y - aabb.pos.y) * scale;
    glVertex3f(x, y, 0.0);
    x = (merged[triangulated[i].c].x - aabb.pos.x) * scale;
    y = (merged[triangulated[i].c].y - aabb.pos.y) * scale;
    glVertex3f(x, y, 0.0);
  }
  glEnd();

  glColor4f(0.0, 0.0, 0.0, 0.1);

  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glDisable(GL_DEPTH_TEST);
  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  glBegin(GL_TRIANGLES);
  for (size_t i = 0; i != triangulated.size(); ++i) {
    double x, y;
    x = (merged[triangulated[i].a].x - aabb.pos.x) * scale;
    y = (merged[triangulated[i].a].y - aabb.pos.y) * scale;
    glVertex3f(x, y, 0.0);
    x = (merged[triangulated[i].b].x - aabb.pos.x) * scale;
    y = (merged[triangulated[i].b].y - aabb.pos.y) * scale;
    glVertex3f(x, y, 0.0);
    x = (merged[triangulated[i].c].x - aabb.pos.x) * scale;
    y = (merged[triangulated[i].c].y - aabb.pos.y) * scale;
    glVertex3f(x, y, 0.0);
  }
  glEnd();
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  glEnable(GL_DEPTH_TEST);

  glColor4f(1, 1, 1, 1);
  glBegin(GL_LINE_LOOP);
  for (int i = 0; i < merged.size(); ++i) {
    glVertex3f((merged[i].x - aabb.pos.x) * scale, (merged[i].y - aabb.pos.y) * scale, 2.0);
  }
  glEnd();

  glEndList();

  scene->run();

  delete scene;

  return 0;
}