コード例 #1
0
ファイル: derivada.cpp プロジェクト: txemagon/c-programs
int main(int argc, char *argv[]){
    double x;

    titulo();
    printf("\n\tx: ");
    scanf(" %lf", &x);
    pinta_entorno(x);
    printf("f'(x=%.2lf) = %.2lf\n", x, derivada(x));

    return EXIT_SUCCESS;
}
コード例 #2
0
ファイル: soODE.c プロジェクト: AndreaRozo/ODEs
void RungeKutta (float *x, float *y1, float *y2, int i, float h)
{
    float x_old = x[i-1];
    float y1_old = y1[i-1];
    float y2_old = y2[i-1];
    float y1_prima = derivada(y2_old,1);
    float y2_prima = derivada(y1_old,2);
    float x_middle = x_old + (h/2.0);
    float y1_middle = y1_old + (h/2.0) * y1_prima;
    float y2_middle = y2_old + (h/2.0) * y2_prima;
    float y1_middle_prima = derivada(y2_middle,1);
    float y2_middle_prima = derivada(y1_middle,2);
    float x_new = x_old + h;
    float y1_new = y1_old + h * y1_middle_prima;
    float y2_new = y2_old + h * y2_middle_prima;

    x[i] = x_new;
    y1[i] = y1_new;
    y2[i] = y2_new;
}
コード例 #3
0
void imprime (FILE *saida, double a, int k) {
	fprintf (saida, "%d & %.4f & %.4f & %.4f\\\\\n",
	k, a, funcao (a), derivada (a));
	fprintf (saida, "\\midrule\n");
}
コード例 #4
0
ファイル: mainwindow.cpp プロジェクト: alanFaccin/FinalIA2
/*
|--------------------------------------------------------------------------------
| 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);

}