?? driver.bak
字號:
/* We illustrate the operation of the code using the problem min \sum_{i=1}^n exp (x [i]) - sqrt (i)*x [i] ; subject to 0 <= x <= 1 We solve the problem 3 times using different parameter setting. On a linux workstation, the final statistics generated by driver1 were as follows:Final convergence status = 0Convergence tolerance for gradient satisfiedabsolute largest component of projected gradient: 5.642870e-09function value: -4.0110349e+02Total cg iterations: 8Total cg function evaluations: 13Total cg gradient evaluations: 13Total cbb iterations: 5Total cbb function evaluations: 6Total cbb gradient evaluations: 6------------------------------------------Total function evaluations: 19Total gradient evaluations: 19==========================================See the drivers in the distribution of cg_descent Version 3.0for more examples */#include <math.h> #include "asa_user.h" /* needed by the program which calls asa_cg *//* prototypes for the function and gradient evaluation routines */double myvalue(double *x, INT n) ;void mygrad(double *g, double *x, INT n) ;double myvalgrad(double *g, double *x, INT n) ;int main (void){ double *x, *lo, *hi ; double grad_tol; INT i, n ; /* if you want to change parameter value, you need the following: */ asacg_parm cgParm ; asa_parm asaParm ; /* tolerance */ grad_tol = 1.e-8; /* allocate arrays for problem solution and bounds */ n = 100 ; /* problem dimension */ x = (double *) malloc (n*sizeof (double)) ; lo = (double *) malloc (n*sizeof (double)) ; hi = (double *) malloc (n*sizeof (double)) ; for (i = 0; i < n; i++) lo [i] = (double) 0 ; for (i = 0; i < n; i++) hi [i] = (double) 1 ; /* if you want to change parameter value, initialize strucs with default */ asa_cg_default (&cgParm) ; asa_default (&asaParm) ; /* if you want to change parameters, change them here: */ cgParm.PrintParms = TRUE ; cgParm.PrintLevel = 1 ; asaParm.PrintParms = TRUE ; asaParm.PrintLevel = 1 ; ///* starting guess */ //for (i = 0; i < n; i++) x[i] = 1 ; ///* run the code */ //asa_cg (x, lo, hi, n, NULL, &cgParm, &asaParm, grad_tol, myvalue, mygrad, myvalgrad, NULL) ; /* if no change in parameters, you could replace Parm arguments by NULL*/ /* starting guess */ //for (i = 0; i < n; i++) x [i] = 1 ; //asa_cg (x, lo, hi, n, NULL, NULL, NULL, grad_tol, myvalue, mygrad, myvalgrad, NULL) ; // /* with some loss of efficiency, you could omit the valgrad routine */ /* starting guess */ for (i = 0; i < n; i++) x [i] = 1 ; asa_cg (x, lo, hi, n, NULL, NULL, NULL, grad_tol, myvalue, mygrad, NULL, NULL); free (x) ; free (lo) ; free (hi) ;}/* evaluate the objective function */double myvalue(double *x, INT n){ double f, t ; INT i ; f = 0. ; for (i = 0; i < n; i++) { t = i + 1 ; t = sqrt (t) ; f += exp (x [i]) - t*x [i] ; } return (f) ;}/* evaluate the gradient of the objective function */void mygrad(double *g, double *x, INT n){ double t ; INT i ; for (i = 0; i < n; i++) { t = i + 1 ; t = sqrt (t) ; g [i] = exp (x [i]) - t ; } return ;}/* value and gradient of the objective function */double myvalgrad (double *g, double *x, INT n){ double f, xi, t ; INT i ; f = 0 ; for (i = 0; i < n; i++) { t = i + 1 ; t = sqrt (t) ; xi = x [i] ; f += exp (xi) - t*xi ; g [i] = exp (xi) - t ; } return (f) ;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -