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

📄 test_rose.cc

📁 COOOL:CWP面向对象最优化库(CWP Object Oriented Optimization Library) COOOL是C++类的一个集合
💻 CC
字号:
#include <iostream.h>#include <stdlib.h>#include <GetOpt.h>#include <C++.hh>#include <ObjFcns.hh>#include <Optim.hh>#include <BFGS.hh>static int verbose        = 0;static int modelsize      = 3;static int itMaxLine 	  = 10;static int itMaxCG 	  = 20;static double tol	  = 0.00001;static char *opt_string = "S:A:D:N:T:V:M:L:U:C:";int cstraints(Vector<double>& v){      for (int i=0; i< v.size(); i++)   {      if (fabs((float)v[i]) > 5)	 return -1;   }   return 1;}int main(int argc, char *argv[]){	int c; 		// used in data input 	int i;		// counters	char *  instring;	Vector<double> delta(1), upper(1), lower(1);        GetOpt getopt(argc, argv, opt_string);        while (( c = getopt()) != EOF)        switch(c) {	  case 'N':	    modelsize = atoi(getopt.optarg);	    break;	  case 'M':	    itMaxLine = atoi(getopt.optarg);	    break;	  case 'C':	    itMaxCG = atoi(getopt.optarg);	    break;	  case 'T':	    tol = atof(getopt.optarg);	    break;	  case 'D':	    instring = getopt.optarg;	    delta[0] = atof(instring);	    while((instring = strpbrk(instring, ",")) != NULL)	    {	       instring++;	       delta.addVal(atof(instring));	    }	    break;	  case 'L':	    instring = getopt.optarg;	    lower[0] = atof(instring);	    while((instring = strpbrk(instring, ",")) != NULL)	    {	       instring++;	       lower.addVal(atof(instring));	    }	    break;	  case 'U':	    instring = getopt.optarg;	    upper[0] = atof(instring);	    while((instring = strpbrk(instring, ",")) != NULL)	    {	       instring++;	       upper.addVal(atof(instring));	    }	    break;	  case 'V':	    i = atoi(getopt.optarg);	    if (i) verbose++;	    break;	  default:	    cerr << argv[0] << ": bad argument \n";	    exit(1);	}	if( argc == 1) {	    cerr << "Usage:  " << argv[0] << endl;	    cerr << " -N number of unknowns" << endl;	    cerr << " -D fraction of step size for numerical gradient" << endl;	    cerr << " -M maximum number of iterations in line search" << endl;	    cerr << " -C maximum number of iterations in conjugate gradient" << endl;	    cerr << " -L lower boundary of model parameters" << endl;	    cerr << " -U upper boundary of model parameters" << endl;	    cerr << " -T tolerance of the residue" << endl;	    cerr << " -V verbose" << endl;	    exit(-1);	}		// construct the model space and the initial guess	Vector<double> LOWER(modelsize, lower.size(), lower.toPointer());	Vector<double> UPPER(modelsize, upper.size(), upper.toPointer());	Vector<double> DELTA(modelsize, delta.size(), delta.toPointer());	if (verbose) cerr << "Upper Boundary: \n" << UPPER <<endl;	if (verbose) cerr << "Lower Boundary: \n" << LOWER <<endl;	if (verbose) cerr << "Interval: \n" << DELTA << endl;	//construct objective function, connecting with user executable        RosenBrock*  fp = new RosenBrock(modelsize);	//constrcut optimization objects	LineSearch*  ls = new CubicLineSearch(fp, itMaxLine);	//****************NEW: TO SPECIFY POINTER TO THE CONSTRAINTS FUNCTION	int (*tp)(Vector<double>&);	tp = cstraints;//	Model<double> m(modelsize, tp);//	Model<double> m(UPPER,LOWER,tp);	Model<double> m(modelsize);	// starting model is random	int seed=getgid();	srand(seed);	for (i=0; i<modelsize; i++)	   m[i] = (UPPER[i]-LOWER[i])*rand()/RAND_MAX+LOWER[i]; 	if (verbose) cerr<<"\n The initial guess model: "<<m<<endl;  		// construct conjugate gradient object	ConjugateGradient* opt = new ConjugateGradient(ls,itMaxCG,tol,verbose);//	BFGS* opt = new BFGS(ls,itMaxCG,tol,verbose);	double initialSp = fp->performance(m);	m = opt->optimizer(m);	double finalSp = fp->performance(m);	cerr <<"\n The number of iterations: "<< opt->numIterations() << endl;	cerr <<"Normalized residue: " << opt->allResidue() <<endl;	cerr <<"Initital RosenBrock value: " << initialSp << endl;	cerr <<"Final RosenBrock value: " << finalSp << endl;	cout<<"\n The final optimized model: "<<m<<endl;}

⌨️ 快捷键说明

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