⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linearregressionfrmv.cpp

📁 vc编程实现bp神经网络
💻 CPP
字号:
// LinearRegressionFRMV.cpp : implementation file
//
#include "stdlib.h"
#include "stdio.h"

#include "stdafx.h"
#include "optsoftware.h"
#include "LinearRegressionFRMV.h"
#include "UserDataDlg.h"
#include "ForecastDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLinearRegressionFRMV

IMPLEMENT_DYNCREATE(CLinearRegressionFRMV, CFormView)

CLinearRegressionFRMV::CLinearRegressionFRMV()
	: CFormView(CLinearRegressionFRMV::IDD)
{
	//{{AFX_DATA_INIT(CLinearRegressionFRMV)
	m_MDescription = _T("");
	m_MFunction = _T("");
	m_MName = _T("");
	m_UsName = _T("");
	nModelMethod=1;
	//}}AFX_DATA_INIT
	
	//	CModelInfoSet *rs_minfo=new CModelInfoSet(&m_database); 
/*   	CString strSQL;	
//	strSQL.Format ("select * from tab_ModelInfo where Model_Name='%s'",m_ModelName);
//  rs_minfo->Open ();
	if(rs_minfo->GetRecordCount()!=0)
	{
		rs_minfo->MoveLast();
		m_MName=rs_minfo->m_Model_Name;
		m_MFunction=rs_minfo->m_Model_Function;
	}*/

	
}

CLinearRegressionFRMV::~CLinearRegressionFRMV()
{
//	delete []LineCo;
}

void CLinearRegressionFRMV::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLinearRegressionFRMV)
	DDX_Control(pDX, IDC_LIST2, m_List2);
	DDX_Control(pDX, IDC_LIST1, m_List1);
	DDX_Text(pDX, IDC_EDT_MDESCRIPTION, m_MDescription);
	DDX_Text(pDX, IDC_EDT_MFUNCTION, m_MFunction);
	DDX_Text(pDX, IDC_MNAME, m_MName);
	DDX_Text(pDX, IDC_USNAME, m_UsName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLinearRegressionFRMV, CFormView)
	//{{AFX_MSG_MAP(CLinearRegressionFRMV)
	ON_BN_CLICKED(IDC_BTN_DEFVARI, OnBtnDefvari)
	ON_BN_CLICKED(IDC_BUILD, OnBuild)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_FORECAST, OnForecast)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLinearRegressionFRMV diagnostics

#ifdef _DEBUG
void CLinearRegressionFRMV::AssertValid() const
{
	CFormView::AssertValid();
}

void CLinearRegressionFRMV::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLinearRegressionFRMV message handlers

void CLinearRegressionFRMV::OnBtnDefvari() 
{
	// TODO: Add your control notification handler code here
	CUserDataDlg UsDataDlg;
	UsDataDlg.DoModal();
}

void CLinearRegressionFRMV::OnBuild() 
{
	// TODO: Add your control notification handler code here
  
	if(VariInCnt==0||VariOutCnt==0||tn.IsEmpty())
	{
		AfxMessageBox("请先进行模型变量定义!");
		return;
	}
	
	UpdateData(true);
	if(m_MDescription==_T(""))
	{
		AfxMessageBox("请输入模型描述!");
		return;
	}
	m_MName=ModelName;
	CString strSQL;
	if(m_recordset->IsOpen())
		m_recordset->Close();
	strSQL.Format ("select * from tab_ModelInfo where Us_Name='%s' and Model_Name='%s'\
	and Model_Method='线性回归'",username,ModelName);
    m_recordset->Open (AFX_DAO_USE_DEFAULT_TYPE,strSQL);
    if(!m_recordset->IsEOF())
	{
		AfxMessageBox("此模型已存在!");	
		return;
	}
	m_recordset->Close();
	strSQL.Format ("select * from tab_VariableInfo where Us_Name='%s' and Model_Name='%s'and Variable_In=true",username,ModelName);
    m_recordset->Open(AFX_DAO_USE_DEFAULT_TYPE,strSQL);

	dblvector a(VariInCnt+1);
	dblvector v(VariInCnt);
	
	double *PartCo;
	PartCo=new double[VariInCnt];
	LineCo=new double[VariInCnt+1];
	
	m_recordset->MoveFirst();

	CDaoTableDef td(&m_Db);
	CDaoFieldInfo fi;
	COleVariant var;

	strvector VariInName(VariInCnt),VariOutName(1);
		
	td.Open(tn);
	if(m_set.IsOpen())
		m_set.Close();
	m_set.Open(&td);
	int ItemCnt;
	ItemCnt=SampleEnd-SampleStart+1;

	dblvector y(ItemCnt);

	dblvector x(ItemCnt*VariInCnt);
	m_set.MoveFirst();
	if (SampleStart>1)
		m_set.Move(SampleStart-1);

	int i;
	int j=0,k=0;
	while(!m_recordset->IsEOF())
	{
		for(i=0;i<m_set.GetFieldCount();i++)	
		{
	    	m_set.GetFieldInfo(i,fi);
			m_recordset->GetFieldValue(5,var);
	    	if(fi.m_strName==(LPCTSTR)var.bstrVal)
	    	while(!m_set.IsEOF() && ItemCnt>0)
			{
		    	m_set.GetFieldValue(i,var);
    	        x[j]=var.dblVal;
	        	m_set.MoveNext();
		        j++;
				ItemCnt--;
			}
		}

		m_recordset->GetFieldValue(2,var);
		VariInName[k]=(LPCTSTR)var.bstrVal;
		m_set.MoveFirst();
		if (SampleStart>1)
			m_set.Move(SampleStart-1);
		ItemCnt=SampleEnd-SampleStart+1;
        m_recordset->MoveNext();
		k++;
	}
	if(m_recordset->IsOpen()) m_recordset->Close();
	strSQL.Format ("select * from tab_VariableInfo where US_Name='%s' and Model_Name='%s'and Variable_In=false",username,ModelName);
    m_recordset->Open(AFX_DAO_USE_DEFAULT_TYPE,strSQL);

	m_set.MoveFirst();
	if (SampleStart>1)
		m_set.Move(SampleStart-1);
	ItemCnt=SampleEnd-SampleStart+1;
	j=0;
	for(i=0;i<m_set.GetFieldCount();i++)
	{
		m_set.GetFieldInfo(i,fi);
		m_recordset->GetFieldValue(5,var);
	   	if(fi.m_strName==(LPCTSTR)var.bstrVal)
		while(!m_set.IsEOF() && ItemCnt>0)
		{
			m_set.GetFieldValue(i,var);
			y[j]=var.dblVal;
			m_set.MoveNext();
			j++;
			ItemCnt--;
		}
	}
	m_recordset->GetFieldValue(2,var);
	VariOutName[0]=(LPCTSTR)var.bstrVal;
	ItemCnt=SampleEnd-SampleStart+1;

    double LineEva[4];
	a.swap(jcsqt(x,y,VariInCnt,ItemCnt,a,LineEva,v,PartCo));
	for (i=0;i<=VariInCnt;i++)
		LineCo[i]=a[i];

	CString strFunc,str;
	strFunc.Format("表达式:%s=(%e)*%s",VariOutName[0],a[0],VariInName[0]);
	for(i=1;i<VariInCnt;i++)
	{
		strFunc+=_T("+");
		str.Format("(%e)",a[i]);
	    strFunc+=str;
		strFunc+=_T("*");
		strFunc+=VariInName[i];
	}
	strFunc+=_T("+");
	str.Format("(%e)",a[i]);
	strFunc+=str;	

    strSQL.Format("insert into tab_ModelInfo(Us_Name,Model_Name,\
					Model_Method,Model_Function,Model_Description)\
					Values('%s','%s','线性回归','%s','%s')",\
					username,m_MName,strFunc,m_MDescription);
			try
			{
				if(m_database->CanUpdate())
					m_database->Execute(strSQL);
			}
			catch(CDaoException* e)
			{
				e->ReportError();
				e->Delete();
				return;
			}
	        
    m_List1.SetExtendedStyle(LVS_EX_GRIDLINES);//为List添加表格
	m_List1.InsertColumn(0,"",LVCFMT_LEFT,120);
	m_List1.InsertColumn(1,"偏相关系数",LVCFMT_LEFT,150);
	
	m_List2.SetExtendedStyle(LVS_EX_GRIDLINES);//为List添加表格
	m_List2.InsertColumn(0,"",LVCFMT_LEFT,120);
	m_List2.InsertColumn(1,"评价指标",LVCFMT_LEFT,150);

	m_MFunction=strFunc;
    for(i=0;i<VariInCnt;i++)
		{
			m_List1.InsertItem(i,"");
			str.Format("PartialCo[%d]",i);
		    m_List1.SetItemText(i,0,str);
			str.Format("%e",PartCo[i]);
		    m_List1.SetItemText(i,1,str);
		}
	for(i=0;i<=3;i++)
		{
			m_List2.InsertItem(i,"");
		    m_List2.SetItemText(i,0,"");
			str.Format("%e",LineEva[i]);
		    m_List2.SetItemText(i,1,str);
		}
	m_List2.SetItemText(0,0,"偏差平方和");
	m_List2.SetItemText(1,0,"平均标准偏差");
	m_List2.SetItemText(2,0,"复相关系数");
	m_List2.SetItemText(3,0,"回归平方和");

	UpdateData(false);
    m_recordset->Close();
	delete []PartCo;
    
//    UpdateData(false);
}

void CLinearRegressionFRMV::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CFormView::OnShowWindow(bShow, nStatus);
	
	// TODO: Add your message handler code here
	
	if(!ModelName.IsEmpty())
		m_MName=ModelName;
    if(!username.IsEmpty())
		m_UsName=username;
   UpdateData(false);
}

void CLinearRegressionFRMV::OnSave() 
{
	// TODO: Add your control notification handler code here
	static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
	static char BASED_CODE lpszDefExt[] = TEXT("txt");
    CString m_strSaveResult;
	//Create the dialog to select the demo data file to save the result
	CFileDialog dlg(FALSE,
		            lpszDefExt,
					NULL,
					OFN_HIDEREADONLY    | 
					OFN_CREATEPROMPT	|
					OFN_OVERWRITEPROMPT,
					szFilter,
					this);

	if(dlg.DoModal ()==IDOK)
	{
		m_strSaveResult = dlg.GetPathName ();
		SaveResultToFile(m_strSaveResult);
//		HWND hWnd = ::GetDlgItem (this->m_hWnd , IDC_SAVE);
//		::SetWindowText (hWnd, m_strSaveResult);
		
	}
	else
		return;
}

bool CLinearRegressionFRMV::SaveResultToFile(CString &strFileName)
{
	// Convert CString to LPCTSTR 
	LPCTSTR	lpszFileName = "";
			
	strFileName.TrimLeft ();
	strFileName.TrimRight ();
	lpszFileName = (LPCTSTR)strFileName;

	CStdioFile dataFile;

	if(!dataFile.Open (lpszFileName, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::typeText))
	{
		::MessageBox(this->m_hWnd, _T("不能创建存储网络参数的文件!"), _T("错误!"), MB_OK | MB_ICONERROR);
		dataFile.Close ();
		return FALSE;
	}

	dataFile.SeekToEnd ();
	char ReturnNewline[]="\n";
//	dataFile.Write(ReturnNewline,(sizeof(ReturnNewline)-1)/sizeof(char));
		// Write data into the file
	CString str;
	str.Format("#用户名:  %s",username);
	char *pBuffer = new char[str.GetLength ()];
	memcpy(pBuffer,str,str.GetLength ());
	dataFile.Write (pBuffer,str.GetLength ());
//	char ReturnNewline[]="\n";
	dataFile.Write(ReturnNewline,(sizeof(ReturnNewline)-1)/sizeof(char));

	str.Format("#模型名:  %s",ModelName);
	pBuffer = new char[str.GetLength ()];
	memcpy(pBuffer,str,str.GetLength ());
	dataFile.Write (pBuffer,str.GetLength ());
//	ReturnNewline[]="\n";
	dataFile.Write(ReturnNewline,(sizeof(ReturnNewline)-1)/sizeof(char));

	str.Format("#模型描述:%s",m_MDescription);
	pBuffer = new char[str.GetLength ()];
	memcpy(pBuffer,str,str.GetLength ());
	dataFile.Write (pBuffer,str.GetLength ());
//	ReturnNewline[]="\n";
	dataFile.Write(ReturnNewline,(sizeof(ReturnNewline)-1)/sizeof(char));

    str.Format("#自变量数目:");
	pBuffer = new char[str.GetLength ()];
	memcpy(pBuffer,str,str.GetLength ());
	dataFile.Write (pBuffer,str.GetLength ());
//	ReturnNewline[]="\n";
	dataFile.Write(ReturnNewline,(sizeof(ReturnNewline)-1)/sizeof(char));

	str.Format("%d",VariInCnt);
	pBuffer = new char[str.GetLength ()];
	memcpy(pBuffer,str,str.GetLength ());
	dataFile.Write (pBuffer,str.GetLength ());
//	ReturnNewline[]="\n";
	dataFile.Write(ReturnNewline,(sizeof(ReturnNewline)-1)/sizeof(char));

	str.Format("#线性回归系数:");
	pBuffer = new char[str.GetLength ()];
	memcpy(pBuffer,str,str.GetLength ());
	dataFile.Write (pBuffer,str.GetLength ());
//	ReturnNewline[]="\n";
	dataFile.Write(ReturnNewline,(sizeof(ReturnNewline)-1)/sizeof(char));

	for(int i=0;i<=VariInCnt;i++)
	{
		str.Format("%e",LineCo[i]);
		pBuffer = new char[str.GetLength ()];
    	memcpy(pBuffer,str,str.GetLength ());
    	dataFile.Write (pBuffer,str.GetLength ());
    	dataFile.Write(ReturnNewline,(sizeof(ReturnNewline)-1)/sizeof(char));
	}

	dataFile.Close ();
	return TRUE;

}

void CLinearRegressionFRMV::OnForecast() 
{
	// TODO: Add your control notification handler code here
	CForecastDlg ForecastDlg;
	ForecastDlg.DoModal();
}

dblvector CLinearRegressionFRMV::chchol(dblvector a, int n, int m, dblvector d)
{
	int i,j,k,u,v;
	if((a[0]+1.0==1.0)||(a[0]<0.0))
	{
		AfxMessageBox("fail!");
		return(-1);
	}
	a[0]=sqrt(a[0]);
	for(j=1;j<=n-1;j++)
		a[j]=a[j]/a[0];
	for(i=1;i<=n-1;i++)
	{
		u=i*n+i;
		for(j=1;j<=i;j++)
		{
			v=(j-1)*n+i;
			a[u]=a[u]-a[v]*a[v];
		}
		if((a[u]+1.0==1.0)||(a[u]<0.0))
		{
			AfxMessageBox("fail!");
		    return(-1);
		}
		a[u]=sqrt(a[u]);

		if(i!=(n-1))
		{
			for(j=i+1;j<=n-1;j++)
			{
				v=i*n+j;
				for(k=1;k<=i;k++)
					a[v]=a[v]-a[(k-1)*n+i]*a[(k-1)*n+j];
				a[v]=a[v]/a[u];
			}
		}
	}
	for(j=0;j<=m-1;j++)
	{
		d[j]=d[j]/a[0];
		for(i=1;i<=n-1;i++)
		{
			u=i*n+i;
			v=i*m+j;
			for(k=1;k<=i;k++)
				d[v]=d[v]-a[(k-1)*n+i]*d[(k-1)*m+j];
			d[v]=d[v]/a[u];
		}
	}
	for(j=0;j<=m-1;j++)
	{
		u=(n-1)*m+j;
		d[u]=d[u]/a[n*n-1];
		for(k=n-1;k>=1;k--)
		{
			u=(k-1)*m+j;
			for(i=k;i<=n-1;i++)
			{
				v=(k-1)*n+i;
				d[u]=d[u]-a[v]*d[i*m+j];
			}
			v=(k-1)*n+k-1;
			d[u]=d[u]/a[v];
		}
	}
	return(d);
}

dblvector CLinearRegressionFRMV::jcsqt(dblvector x, dblvector y, int m, int n, dblvector a, double dt[], dblvector v, double *PartCo)
{
	int i,j,k,mm;
	double q,e,u,p,yy,s,r,pp;
	mm=(m+1)*(m+1);
	dblvector b(mm);
	mm=m+1;
	b[mm*mm-1]=n;

	for(j=0;j<=m-1;j++)
	{
		p=0.0;
		for(i=0;i<=n-1;i++)
			p=p+x[j*n+i];
		b[m*mm+j]=p;
		b[j*mm+m]=p;
	}
	for(i=0;i<=m-1;i++)
		for(j=i;j<=m-1;j++)
		{
			p=0.0;
			for(k=0;k<=n-1;k++)
				p=p+x[i*n+k]*x[j*n+k];
			b[j*mm+i]=p;
			b[i*mm+j]=p;
		}
	a[m]=0.0;
	for(i=0;i<=n-1;i++)
		a[m]=a[m]+y[i];
	for(i=0;i<=m-1;i++)
	{
		a[i]=0.0;
		for(j=0;j<=n-1;j++)
			a[i]=a[i]+x[i*n+j]*y[j];
	}
	a.swap(chchol(b,mm,1,a));
	yy=0.0;
	for (i=0;i<=n-1;i++)
		yy=yy+y[i]/n;
	q=0.0;
	e=0.0;
	u=0.0;
	for (i=0;i<=n-1;i++)
	{
  		p=a[m];
		for(j=0;j<=m-1;j++)
			p=p+a[j]*x[j*n+i];
	    q=q+(y[i]-p)*(y[i]-p);
		e=e+(y[i]-yy)*(y[i]-yy);
		u=u+(yy-p)*(yy-p);
	}
	s=sqrt(q/n);
	r=sqrt(1.0-q/e);
	for(j=0;j<=m-1;j++)
	{
		p=0.0;
		for(i=0;i<=n-1;i++)
		{
			pp=a[m];
			for(k=0;k<=m-1;k++)
				if(k!=j)
					pp=pp+a[k]*x[k*n+i];
					p=p+(y[i]-pp)*(y[i]-pp);
		}
		v[j]=sqrt(1.0-q/p);
		PartCo[j]=v[j];
	}

    

	dt[0]=q;
	dt[1]=s;
	dt[2]=r;
	dt[3]=u;

	return(a);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -