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

📄 testlp.cpp

📁 基于线性规划的回归支持向量机源程序
💻 CPP
字号:
#include <time.h>
#include "matlib.h"
#include "fstream.h"
#include "conio.h"
#include "SVR.h"
void main()
{
	int m,n;
	char buf[256];
	cout<<"input data file:";
	cin>>buf;
	ifstream ifile(buf);
	ifile>>m;
	ifile>>n;
	cout<<"m="<<m<<", n="<<n<<endl;
	int m1=m/2;
	int m2=m-m1;
	matrix x1(m1,n-1),x2(m2,n-1);
	vector y1(m1),y2(m2);
	for(int i=0;i<m1;i++)
	{
		for(int j=0;j<n-1;j++)
			ifile>>x1(i,j);
		ifile>>y1[i];
	}
cout<<"x1="<<x1<<"y1="<<y1<<endl;
	for(i=0;i<m2;i++)
	{
		for(int j=0;j<n-1;j++)
			ifile>>x2(i,j);
		ifile>>y2[i];
	}
	CSVR svr;
	cout<<"input sigma:";
	cin>>svr.m_sigma;
	double C;
	cout<<"input Punished factor C=";
	cin>>C;
	time_t btime,etime;
	cout.precision(8);

try
{
	ofstream ofile("result.txt");
	cout<<"input type of svm:(0 - linear;\n1 - Gauss\n";
	cin>>i;
	double eps;
	cout<<"input eps=";
	cin>>eps;
	switch(i)
	{
		case 0:
		svr.m_type=LINEAR;
			break;
		case 1:
		svr.m_type=GAUSS;
		break;
	}
/*
	btime=time(NULL);
	svr.SolveByNorm1(x1,y1,10);
    etime=time(NULL);
	if(svr.m_type==LINEAR)
	{
		double data=svr.m_alpha[1];
		svr.m_alpha/=data;
		svr.m_b/=data;
	}
	ofile.precision(8);
	cout<<"1-norm:\n";
	cout<<"number of sv="<<svr.m_alpha()<<",alpha=\n"<<svr.m_alpha;
	cout<<"b="<<svr.m_b<<endl;
	cout<<"time="<<etime-btime<<endl;
	cout<<"Averr="<<svr.ERR(x1,y1)<<", TrueRatio="<<svr.TrueRatio(x1,y1)<<endl;
	cout<<"for test, Averr="<<svr.ERR(x2,y2)<<", TrueRatio="<<svr.TrueRatio(x2,y2)<<endl;
	ofile<<"1-norm:\n";
	ofile<<"number of sv="<<svr.m_alpha()<<",alpha=\n"<<svr.m_alpha;
	ofile<<"b="<<svr.m_b<<endl;
	ofile<<"time="<<etime-btime<<endl;
	ofile<<"Averr="<<svr.ERR(x1,y1)<<", TrueRatio="<<svr.TrueRatio(x1,y1)<<endl;
	ofile<<"for test, Averr="<<svr.ERR(x2,y2)<<", TrueRatio="<<svr.TrueRatio(x2,y2)<<endl;
*/
	btime=time(NULL);
	double werr=svr.SolveByNorm8(x1,y1,eps,C);
    etime=time(NULL);
	cout<<"8-norm: objvalue="<<werr<<"\n";
	cout<<"number of sv="<<svr.m_alpha()<<",alpha=\n"<<svr.m_alpha;
	cout<<"b="<<svr.m_b<<endl;
	cout<<"time="<<etime-btime<<endl;
	cout<<"Averr="<<svr.ERR(x1,y1)<<endl;
	cout<<"for test, Averr="<<svr.ERR(x2,y2)<<endl;
	ofile<<"8-norm:\n";
	ofile<<"number of sv="<<svr.m_alpha()<<",alpha=\n"<<svr.m_alpha;
	ofile<<"b="<<svr.m_b<<endl;
	ofile<<"time="<<etime-btime<<endl;
	ofile<<"Averr="<<svr.ERR(x1,y1)<<endl;
	ofile<<"for test, Averr="<<svr.ERR(x2,y2)<<endl;

	/*

	matrix a(2,4);
	vector b(2),c(4),x;
	a=0;
	a(0,0)=1;a(0,1)=-1;a(0,2)=1;
	a(1,1)=1;a(1,3)=1;
	b=15;
	c=0;
	c[0]=-2;c[1]=1;
	lp_affineinner(a,b,c,x);
	cout<<x;
	getch();
*/
}
catch(char *str)
{
	cout<<str<<endl;
}
catch(...)
{
	cout<<"exception"<<endl;
}
}

⌨️ 快捷键说明

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