📄 optima.cc
字号:
#include <iostream.h>#include <stdlib.h>#include <GetOpt.h>#include <C++.hh>#include <ObjFcns.hh>#include <Optim.hh>#include <cwp.h>static int verbose = 0;static AString SlaveP = "";static AString SlavePArgs = "";static int modelsize = 3;static int itMaxLine = 10;static int itMaxCG = 20;static double tol = 0.00001;static double DELTA = 0.1;static double lower = -5.;static double upper = 5.;static double numBits = 8;static char *opt_string = "S:A:D:N:T:V:M:L:U:C:";extern "C" { int getpid(void); }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 'S': SlaveP = getopt.optarg; break; case 'A': SlavePArgs = getopt.optarg; break; case 'N': modelsize = atoi(getopt.optarg); SlavePArgs += " -N "; SlavePArgs += toAS(modelsize); break; case 'M': itMaxLine = atoi(getopt.optarg); break; case 'C': itMaxCG = atoi(getopt.optarg); break; case 'T': tol = atof(getopt.optarg); 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)); } case 'V': i = atoi(getopt.optarg); if (i==1) verbose = 1; else verbose = 0; SlavePArgs += " -V "; SlavePArgs += toAS(verbose); break; default: cerr << argv[0] << ": bad argument \n"; exit(1); } if( argc == 1) { cerr << "Usage: " << argv[0] << endl; cerr << " -S name of the slave file" << endl; cerr << " -N number of unknowns" << endl; cerr << " -A arguments for the slave file" << 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()); if (verbose) cerr << "Upper Boundary: \n" << UPPER <<endl; if (verbose) cerr << "Lower Boundary: \n" << LOWER <<endl; Model<double> m(UPPER, LOWER); //construct objective function, connecting with user executable SlaveForward* my_slave = new SlaveForward(modelsize, 1, SlaveP, SlavePArgs, 0); DirectObjFcn* fp = new DirectObjFcn(modelsize, my_slave, 1); //constrcut optimization objects CubicLineSearch* ls = new CubicLineSearch(fp,itMaxLine); // starting model is random statics (-20, 20) ms int seed=getpid(); sranuni(seed); for (i=0; i<modelsize; i++) m[i] = (UPPER[i]-LOWER[i])*franuni()+LOWER[i]; cerr << "Initial 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); cerr << "Final Model: " << m << endl; cout << m << endl; double finalSp = fp->performance(m); cerr <<"The number of iterations: "<< opt->numIterations() << endl; cerr <<"Original stacking power: " << -initialSp << endl; cerr <<"Final stacking power: " << -finalSp << endl; cerr <<"Residue history: " << opt->allResidue() <<endl;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -