📄 winsvmdlg.cpp
字号:
limitstep=limitstep/3;
b=coef0temp-bstep+4*bstep/(k+4),bend=coef0temp+bstep-4*bstep/(k+4),bstep=4*bstep/(k+4);//可以为负数
c=nutemp-cstep+4*cstep/(l+4);
if(c<=0)
c=0.00000001,cend=nutemp+cstep-2*cstep/(l+2),cstep=2*cstep/(l+2);
else
{
cend=nutemp+cstep-4*cstep/(l+4),cstep=4*cstep/(l+4);
if(cend>1)
cend=1;
}
yichuan1(++i, b, bend, bstep, c, cend, cstep);
i--;
}
if(i==0)
{
yichuanend();
}
}
void CwinsvmDlg::yichuan3(int i,int a,int aend,double b,double bend,double bstep,double c,double cend,double cstep,double d,double dend,double dstep)
{
//int j=aend-a;//度的步数
int k=(bend-b)/bstep;//coef0步数
int l=(cend-c)/cstep;//C步数
int n=(dend-d)/dstep;//p步数
double zy;//预测效率
int m=0;//递归参数
//临时变量
int degreetemp(0);
double coef0temp(0);
double Ctemp(0);
double ptemp(0);
for(int ii=a ;ii<aend;ii++)
{
param.degree=ii;
for( double jj=b;jj<bend;jj+=bstep)
{
param.coef0=jj;
for( double kk=c;kk<cend;kk+=cstep)
{
param.C=kk;
for( double ll=d;ll<dend;ll+=dstep)
{
param.p=ll;
info.Empty();
zy = do_cross_validationy();
if( zy > (limit+limitstep) )
{
m++;
limit=zy;
degreetemp=param.degree;
coef0temp=param.coef0;
Ctemp=param.C;
ptemp=param.p;
infoy=info;
info.Format("度是 %d,coef0是 %f,惩罚因子C是 %f,损失函数ε是 %f\n",degreetemp,coef0temp,Ctemp,ptemp);
infoy+=info;
}
}
}
}
}
if(m)
{
limitstep=limitstep/3;
a=degreetemp;aend=degreetemp+1;
b=coef0temp-bstep+4*bstep/(k+4),bend=coef0temp+bstep-4*bstep/(k+4),bstep=4*bstep/(k+4);//可以为负数
c=Ctemp-cstep+4*cstep/(l+4);
d=ptemp-dstep+4*dstep/(n+4);
if(c<=0)
c=0.00000001,cend=Ctemp+cstep-2*cstep/(l+2),cstep=2*cstep/(l+2);
else
cend=Ctemp+cstep-4*cstep/(l+4),cstep=4*cstep/(l+4);
if(d<0)
d=0.00000001,dend=ptemp+dstep-2*dstep/(n+2),dstep=2*dstep/(n+2);
else
dend=ptemp+dstep-4*dstep/(n+4),dstep=4*dstep/(n+4);
yichuan3(++i,a,aend, b, bend, bstep, c, cend, cstep, d, dend, dstep);
i--;
}
if(i==0)
{
yichuanend();
}
}
void CwinsvmDlg::yichuan3(int i,double c,double cend,double cstep,double d,double dend,double dstep)
{
int l=(cend-c)/cstep;//C步数
int n=(dend-d)/dstep;//p步数
double zy;//预测效率
int m=0;//递归参数
//临时变量
double Ctemp(0);
double ptemp(0);
for( double ii=c;ii<cend;ii+=cstep)
{
param.C=ii;
for( double jj=d;jj<dend;jj+=dstep)
{
param.p=jj;
info.Empty();
zy = do_cross_validationy();
if( zy > (limit+limitstep) )
{
m++;
limit=zy;
Ctemp=param.C;
ptemp=param.p;
infoy=info;
info.Format("惩罚因子C是 %f,损失函数ε是 %f\n",Ctemp,ptemp);
infoy+=info;
}
}
}
if(m)
{
limitstep=limitstep/3;
c=Ctemp-cstep+4*cstep/(l+4);
d=ptemp-dstep+4*dstep/(n+4);
if(c<=0)
c=0.00000001,cend=Ctemp+cstep-2*cstep/(l+2),cstep=2*cstep/(l+2);
else
cend=Ctemp+cstep-4*cstep/(l+4),cstep=4*cstep/(l+4);
if(d<0)
d=0.00000001,dend=ptemp+dstep-2*dstep/(n+2),dstep=2*dstep/(n+2);
else
dend=ptemp+dstep-4*dstep/(n+4),dstep=4*dstep/(n+4);
yichuan3(++i, c, cend, cstep, d, dend, dstep);
i--;
}
if(i==0)
{
yichuanend();
}
}
void CwinsvmDlg::yichuan3(int i,double b,double bend,double bstep,double c,double cend,double cstep,double d,double dend,double dstep)
{
//int j=aend-a;//度的步数
int k=(bend-b)/bstep;//coef0步数
int l=(cend-c)/cstep;//C步数
int n=(dend-d)/dstep;//p步数
double zy;//预测效率
int m=0;//递归参数
//临时变量
double coef0temp(0);
double Ctemp(0);
double ptemp(0);
for( double ii=b;ii<bend;ii+=bstep)
{
param.coef0=ii;
for( double jj=c;jj<cend;jj+=cstep)
{
param.C=jj;
for( double kk=d;kk<dend;kk+=dstep)
{
param.p=kk;
info.Empty();
zy = do_cross_validationy();
if( zy > (limit+limitstep) )
{
m++;
limit=zy;
coef0temp=param.coef0;
Ctemp=param.C;
ptemp=param.p;
infoy=info;
info.Format("coef0是 %f,惩罚因子C是 %f,损失函数ε是 %f\n",coef0temp,Ctemp,ptemp);
infoy+=info;
}
}
}
}
if(m)
{
limitstep=limitstep/3;
b=coef0temp-bstep+4*bstep/(k+4),bend=coef0temp+bstep-4*bstep/(k+4),bstep=4*bstep/(k+4);//可以为负数
c=Ctemp-cstep+4*cstep/(l+4);
d=ptemp-dstep+4*dstep/(n+4);
if(c<=0)
c=0.00000001,cend=Ctemp+cstep-2*cstep/(l+2),cstep=2*cstep/(l+2);
else
cend=Ctemp+cstep-4*cstep/(l+4),cstep=4*cstep/(l+4);
if(d<0)
d=0.00000001,dend=ptemp+dstep-2*dstep/(n+2),dstep=2*dstep/(n+2);
else
dend=ptemp+dstep-4*dstep/(n+4),dstep=4*dstep/(n+4);
yichuan3(++i, b, bend, bstep, c, cend, cstep, d, dend, dstep);
i--;
}
if(i==0)
{
yichuanend();
}
}
void CwinsvmDlg::yichuan4(int i,int a,int aend,double b,double bend,double bstep,double c,double cend,double cstep,double d,double dend,double dstep)
{
//int j=aend-a;//度的步数
int k=(bend-b)/bstep;//coef0步数
int l=(cend-c)/cstep;//C步数
int n=(dend-d)/dstep;//p步数
double zy;//预测效率
int m=0;//递归参数
//临时变量
int degreetemp(0);
double coef0temp(0);
double Ctemp(0);
double nutemp(0);
for( int ii=a;ii<aend;ii++)
{
param.degree=ii;
for( double jj=b;jj<bend;jj+=bstep)
{
param.coef0=jj;
for( double kk=c;kk<cend;kk+=cstep)
{
param.C=kk;
for( double ll=d;ll<dend;ll+=dstep)
{
param.nu=ll;
info.Empty();
zy = do_cross_validationy();
if( zy > (limit+limitstep) )
{
m++;
limit=zy;
degreetemp=param.degree;
coef0temp=param.coef0;
Ctemp=param.C;
nutemp=param.nu;
infoy=info;
info.Format("度是 %d,coef0是 %f,惩罚因子C是 %f,参数v(nu)是 %f\n",degreetemp,coef0temp,Ctemp,nutemp);
infoy+=info;
}
}
}
}
}
if(m)
{
limitstep=limitstep/3;
a=degreetemp;aend=degreetemp+1;
b=coef0temp-bstep+4*bstep/(k+4),bend=coef0temp+bstep-4*bstep/(k+4),bstep=4*bstep/(k+4);//可以为负数
c=Ctemp-cstep+4*cstep/(l+4);
d=nutemp-dstep+4*dstep/(n+4);
//if(b<0)
// b=0,bend=coef0temp+bstep-2*bstep/(k+2),bstep=2*bstep/(k+2);
//else
// bend=coef0temp+bstep-4*bstep/(k+4),bstep=4*bstep/(k+4);
if(c<=0)
c=0.00000001,cend=Ctemp+cstep-2*cstep/(l+2),cstep=2*cstep/(l+2);
else
cend=Ctemp+cstep-4*cstep/(l+4),cstep=4*cstep/(l+4);
if(d<=0)
d=0.00000001,dend=nutemp+dstep-2*dstep/(n+2),dstep=2*dstep/(n+2);
else
{
dend=nutemp+dstep-4*dstep/(n+4),dstep=4*dstep/(n+4);
if(dend>1)
dend=1;
}
yichuan4(++i,a,aend, b, bend, bstep, c, cend, cstep, d, dend, dstep);
i--;
}
if(i==0)
{
yichuanend();
}
}
void CwinsvmDlg::yichuan4(int i,double c,double cend,double cstep,double d,double dend,double dstep)
{
int l=(cend-c)/cstep;//C步数
int n=(dend-d)/dstep;//p步数
double zy;//预测效率
int m=0;//递归参数
//临时变量
double Ctemp(0);
double nutemp(0);
for( double ii=c;ii<cend;ii+=cstep)
{
param.C=ii;
for(double jj=d ;jj<dend;jj+=dstep)
{
param.nu=jj;
info.Empty();
zy = do_cross_validationy();
if( zy > (limit+limitstep) )
{
m++;
limit=zy;
Ctemp=param.C;
nutemp=param.nu;
infoy=info;
info.Format("惩罚因子C是 %f,参数v(nu)是 %f\n",Ctemp,nutemp);
infoy+=info;
}
}
}
if(m)
{
limitstep=limitstep/3;
c=Ctemp-cstep+4*cstep/(l+4);
d=nutemp-dstep+4*dstep/(n+4);
if(c<=0)
c=0.00000001,cend=Ctemp+cstep-2*cstep/(l+2),cstep=2*cstep/(l+2);
else
cend=Ctemp+cstep-4*cstep/(l+4),cstep=4*cstep/(l+4);
if(d<=0)
d=0.00000001,dend=nutemp+dstep-2*dstep/(n+2),dstep=2*dstep/(n+2);
else
{
dend=nutemp+dstep-4*dstep/(n+4),dstep=4*dstep/(n+4);
if(dend>1)
dend=1;
}
yichuan4(++i, c, cend, cstep, d, dend, dstep);
i--;
}
if(i==0)
{
yichuanend();
}
}
void CwinsvmDlg::yichuan4(int i,double b,double bend,double bstep,double c,double cend,double cstep,double d,double dend,double dstep)
{
//int j=aend-a;//度的步数
int k=(bend-b)/bstep;//coef0步数
int l=(cend-c)/cstep;//C步数
int n=(dend-d)/dstep;//p步数
double zy;//预测效率
int m=0;//递归参数
//临时变量
double coef0temp(0);
double Ctemp(0);
double nutemp(0);
for( double ii=b;ii<bend;ii+=bstep)
{
param.coef0=ii;
for( double jj=c;jj<cend;jj+=cstep)
{
param.C=jj;
for( double kk=d;kk<dend;kk+=dstep)
{
param.nu=kk;
info.Empty();
// modely = svm_train(&prob,¶m);
zy = do_cross_validationy();
if( zy > (limit+limitstep) )
{
m++;
limit=zy;
coef0temp=param.coef0;
Ctemp=param.C;
nutemp=param.nu;
// trainmodel=modely;
infoy=info;
info.Format("coef0是 %f,惩罚因子C是 %f,参数v(nu)是 %f\n",coef0temp,Ctemp,nutemp);
infoy+=info;
}
}
}
}
if(m)
{
limitstep=limitstep/3;
b=coef0temp-bstep+4*bstep/(k+4),bend=coef0temp+bstep-4*bstep/(k+4),bstep=4*bstep/(k+4);//可以为负数
c=Ctemp-cstep+4*cstep/(l+4);
d=nutemp-dstep+4*dstep/(n+4);
if(c<=0)
c=0.00000001,cend=Ctemp+cstep-2*cstep/(l+2),cstep=2*cstep/(l+2);
else
cend=Ctemp+cstep-4*cstep/(l+4),cstep=4*cstep/(l+4);
if(d<=0)
d=0.00000001,dend=nutemp+dstep-2*dstep/(n+2),dstep=2*dstep/(n+2);
else
{
dend=nutemp+dstep-4*dstep/(n+4),dstep=4*dstep/(n+4);
if(dend>1)
dend=1;
}
yichuan4(++i, b, bend, bstep, c, cend, cstep, d, dend, dstep);
i--;
}
if(i==0)
{
yichuanend();
// yichuanyend();
}
}
int CwinsvmDlg::yichuanbegin()
{
//const char *error_msg;
if (read_problem(input_file_name)==0)
{
svm_destroy_param(¶m);
return 0;//应该打不开返回推出。
}
if (param.svm_type == 1)
{
double xy = param.nu;
param.nu=nuy;
double opq = svm_check_parameter(&prob,¶m);
if(opq)
{
info.Format("规定的ν-SVC的参数nu不能大于 %f",opq);
MessageBox(info);
svm_destroy_param(¶m);
free(prob.y);
free(prob.x);
free(x_space);
info.Empty();
return 0;
}
param.nu=xy;
}
return 1;
}
void CwinsvmDlg::yichuanend()
{
if(!infoy.IsEmpty())
{
info.Format("svm类型是 %d,核函数是 %d,参数γ是 %f\n",param.svm_type,param.kernel_type,param.gamma);
infoy+=info;
CFile fg;
CFileException e;
if( !fg.Open( model_file_name, CFile::modeCreate | CFile::modeWrite, &e ) )
{
infoy+="参数优化输出不能备件!";
MessageBox(infoy);
svm_destroy_param(¶m);
free(prob.y);
free(prob.x);
free(x_space);
info.Empty();infoy.Empty();
return;
}
fg.Write(infoy ,infoy.GetLength());//写文本
fg.Close();
ShellExecute(NULL , NULL ,model_file_name,NULL ,NULL , SW_NORMAL);//打开文本
}
else
{
MessageBox("唉!找不到优化结果");
}
svm_destroy_param(¶m);
free(prob.y);
free(prob.x);
free(x_space);
info.Empty();infoy.Empty();
}
double CwinsvmDlg::do_cross_validationy(void)
{
int i;
int total_correct = 0;
double total_error = 0;
//double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
double *target = Malloc(double,prob.l);
svm_cross_validation(&prob,¶m,nr_fold,target);
if(param.svm_type == EPSILON_SVR ||
param.svm_type == NU_SVR)
{
for(i=0;i<prob.l;i++)
{
double y = prob.y[i];
double v = target[i];
total_error += (v-y)*(v-y);
//sumv += v;
//sumy += y;
//sumvv += v*v;
//sumyy += y*y;
//sumvy += v*y;
}
CString str;
str.Format("交叉验证的方差是 %g\n",total_error/prob.l);
info+=str;
//str.Format("交叉验证的平方相关系数(相似性)是 %g\n",
// ((prob.l*sumvy-sumv*sumy)*(prob.l*sumvy-sumv*sumy))/
// ((prob.l*sumvv-sumv*sumv)*(prob.l*sumyy-sumy*sumy))
// );
//info+=str;
return 1-total_error/prob.l;
}
else
{
for(i=0;i<prob.l;i++)
{
if(target[i] == prob.y[i])
{
++total_correct;
}
}
CString str;
str.Format("交叉验证正确率 = %g%%\n",100.0*total_correct/prob.l);
info+=str;
return (double)total_correct/prob.l;
}
free(target);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -