#include "ann.h"
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
Ann::Ann(QWidget * parent):QWidget(parent)
{
V=NULL;
W=NULL;
x=NULL;
y=NULL;
error=NULL;
error=new D[6000];
F_label = new QLabel(QStringLiteral("设置泛化样本数:"));
F_line=new QLineEdit();
F_openBtn=new QPushButton(QStringLiteral("选择泛化样本文件"));
F_openline=new QLineEdit();
F_Btn=new QPushButton(QStringLiteral("泛化"));
F_Btn->setEnabled(false);
I_label = new QLabel(QStringLiteral("输入层神经元个数:"));
H_label = new QLabel(QStringLiteral("隐藏层神经元个数:"));
O_label = new QLabel(QStringLiteral("输出层神经元个数:"));
S_label = new QLabel(QStringLiteral("神经网络学习速率:"));
P_label = new QLabel(QStringLiteral("神经网络误差精度:"));
N_label = new QLabel(QStringLiteral("训练网络的样本数:"));
file_label=new QPushButton(QStringLiteral("打开文件"));
I_line = new QLineEdit();
I_line->setValidator(new QIntValidator(1, 100, this));
H_line = new QLineEdit();
H_line->setValidator(new QIntValidator(1, 100, this));
O_line = new QLineEdit();
O_line->setValidator(new QIntValidator(1, 100, this));
S_line = new QLineEdit();
P_line = new QLineEdit();
N_line = new QLineEdit();
file_line = new QLineEdit();
setBtn = new QPushButton(QStringLiteral("设置"));
openBtn = new QPushButton(QStringLiteral("开始训练"));
openBtn->setEnabled(false);
saveBtn = new QPushButton(QStringLiteral("保存权值并回想"));
list = new QListWidget(this);
plotter=new Plotter(this);
QHBoxLayout * HLayout10 = new QHBoxLayout;
HLayout10->addWidget(F_label);
HLayout10->addWidget(F_line);
HLayout10->addWidget(F_openBtn);
HLayout10->addWidget(F_openline);
HLayout10->addWidget(F_Btn);
QHBoxLayout * HLayout8 = new QHBoxLayout;
HLayout8->addWidget(file_label);
HLayout8->addWidget(file_line);
HLayout8->addWidget(openBtn);
QHBoxLayout * HLayout9 = new QHBoxLayout;
HLayout9->addWidget(setBtn);
HLayout9->addWidget(saveBtn);
QHBoxLayout * HLayout7 = new QHBoxLayout;
HLayout7->addWidget(N_label);
HLayout7->addWidget(N_line);
QHBoxLayout * HLayout6 = new QHBoxLayout;
HLayout6->addWidget(P_label);
HLayout6->addWidget(P_line);
QHBoxLayout * HLayout1 = new QHBoxLayout;
HLayout1->addWidget(I_label);
HLayout1->addWidget(I_line);
QHBoxLayout * HLayout2 = new QHBoxLayout;
HLayout2->addWidget(H_label);
HLayout2->addWidget(H_line);
QHBoxLayout * HLayout3 = new QHBoxLayout;
HLayout3->addWidget(O_label);
HLayout3->addWidget(O_line);
QHBoxLayout * HLayout4 = new QHBoxLayout;
HLayout4->addWidget(S_label);
HLayout4->addWidget(S_line);
QVBoxLayout * VLayout1 = new QVBoxLayout;
VLayout1->addLayout(HLayout8);
VLayout1->addLayout(HLayout1);
VLayout1->addLayout(HLayout2);
VLayout1->addLayout(HLayout3);
VLayout1->addLayout(HLayout4);
VLayout1->addLayout(HLayout6);
VLayout1->addLayout(HLayout7);
VLayout1->addLayout(HLayout9);
QHBoxLayout * HLayout5 = new QHBoxLayout;
HLayout5->addLayout(VLayout1);
HLayout5->addWidget(list);
QVBoxLayout * VLayout2 = new QVBoxLayout;
VLayout2->addLayout(HLayout5);
VLayout2->addWidget(plotter);
VLayout2->addLayout(HLayout10);
setLayout(VLayout2);
connect(setBtn,SIGNAL(clicked()),this,SLOT(setup()));
connect(file_label,SIGNAL(clicked()),this,SLOT(openfiledlg()));
connect(openBtn,SIGNAL(clicked()),this,SLOT(R_data()));
connect(saveBtn,SIGNAL(clicked()),this,SLOT(Save_Q()));
connect(this,SIGNAL(send(D *,int )),plotter,SLOT(curve(D *,int )));
connect(this,SIGNAL(send1(D *,D *,int )),plotter,SLOT(curve1(D *,D *,int )));
connect(F_openBtn,SIGNAL(clicked()),this,SLOT(openfiledlg1()));
connect(F_Btn,SIGNAL(clicked()),this,SLOT(F_test()));
setWindowTitle(QStringLiteral("陈琦-神经网络用具"));
}
void Ann::openfiledlg()
{
QString s = QFileDialog::getOpenFileName(this,QStringLiteral("文件对话框"),QDir::currentPath()+"/data","text files(*.txt)");
//file_line -> setText(s.toAscii());
file_line -> setText(s);
}
void Ann::openfiledlg1()
{
if(F_line->text().isEmpty())
{
QMessageBox::warning(this,QStringLiteral("警告"),QStringLiteral("请先设置泛化样本数!"),QMessageBox::Ok);
}
else
{
F_Btn->setEnabled(true);
QString s = QFileDialog::getOpenFileName(this,QStringLiteral("文件对话框"),QDir::currentPath()+"/data","text files(*.txt)");
F_openline -> setText(s);
}
}
void Ann::setup()
{
openBtn->setEnabled(true);
if(I_line->text().trimmed().isEmpty()|H_line->text().trimmed().isEmpty()
|O_line->text().trimmed().isEmpty()|S_line->text().trimmed().isEmpty()
|P_line->text().trimmed().isEmpty()|N_line->text().trimmed().isEmpty())
{
QMessageBox::warning(this,QStringLiteral("警告"),QStringLiteral("设置错误,请重新设置!"),QMessageBox::Ok);
}
else
{
IN=I_line->text().trimmed().toInt();
HN=H_line->text().trimmed().toInt();
ON=O_line->text().trimmed().toInt();
S=S_line->text().trimmed().toDouble();
P=P_line->text().trimmed().toDouble();
COUNT=N_line->text().trimmed().toInt();
V=new D[IN*HN];
W=new D[HN*ON];
x=new D[COUNT*IN];
y=new D[COUNT*ON];
}
}
void Ann::InitBp()
{
srand((unsigned)time(NULL));
for(int i=0;i<IN;i++)
{
for(int j=0;j<HN;j++)
{
V[i*HN+j] = rand()/(double)(RAND_MAX);
//qDebug()<<"V["<<i<<"]["<<j<<"]:"<<V[i*HN+j];
}
}
for(int i=0;i<HN;i++)
for(int j=0;j<ON;j++)
W[i*ON+j] = rand()/(double)(RAND_MAX);
}
D Ann::fnet(D net)
{ //Sigmoid函数,神经网络激活函数
return 1/(1+exp(-net));
}
void Ann::R_data()
{
openBtn->setEnabled(false);
QFile file(file_line->text());
if(!file.exists())
{
QMessageBox::warning(0,QStringLiteral("警告"),QStringLiteral("样本数据不存在!"),QMessageBox::Ok);
return;
}
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(0,QStringLiteral("警告"),QStringLiteral("读取样本数据失败!"),QMessageBox::Ok);
return;
}
QTextStream in(&file);
in.setIntegerBase(10);
for(int i=0;i<COUNT;i++)
{
int j = 0;
while(j!=(IN+ON)&&!in.atEnd())
{
if(j>(IN-1))
{
in >> y[i*ON+(j-IN)];
//qDebug()<<"y["<<i<<"]["<<(j-IN)<<"]:"<<y[i*ON+(j-IN)];
}
else
{
in >> x[i*IN+j];
//qDebug()<<"x["<<i<<"]["<<j<<"]:"<<x[i*IN+j];
}
j++;
}
}
InitBp();
TrainBp();
}
void Ann::F_test()
{
COUNT=F_line->text().trimmed().toInt();
F_Btn->setEnabled(false);
QFile file(F_openline->text());
if(!file.exists())
{
QMessageBox::warning(0,QStringLiteral("警告"),QStringLiteral("样本数据不存在!"),QMessageBox::Ok);
return;
}
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(0,QStringLiteral("警告")
Qt5下解决了中文乱码的程序,可以用来参考其中解决乱码的方法
5星 · 超过95%的资源 需积分: 50 27 浏览量
2012-11-16
14:45:36
上传
评论 1
收藏 13KB RAR 举报
- 1
- 2
- 3
- 4
- 5
- 6
前往页