00001 // ods_subs.cpp 00002 //--------------------------------------------------------------- 00003 // 00004 //(c) Copyright 2000, Ncompass Research, Inc. 00005 // 00006 // This file contains two routines for line searching. The first calculates the 00007 // line search function. The second performs the line search. 00008 00009 //============================================================================= 00010 // Revision Record: 00011 // 2/2/2000 2:35 pm 00012 //============================================================================= 00013 //============================================================================= 00014 00015 double ods_func(double P,double *C,double *mu,int nconstr,int neqconstr, 00016 int exponent); 00017 void ods(int n,double *X,double *F,double *dx,double epsc,double minalf, 00018 double maxalf,double *alpha,double *Xn,double *fact,int *oflag, 00019 int *nfc,int mfc); 00020 00021 00022 00023 //============================================================================= 00024 //============================================================================= 00025 // FOR USAGE, SEE COMMENTS BELOW 00026 00027 /*----------------------------------------------------------------------------- 00028 double ods_func(double P,double *C,double *mu,int nconstr,int neqconstr, 00029 int exponent); 00030 00031 This function calculates a value of the function minimized by the one 00032 dimensional search routine "ods". It basically appends constraints 00033 to a performance criterion. 00034 _______________________________________________________________________________ 00035 00036 Input: 00037 P = performance 00038 C = vector of constraints 00039 mu = constraint weight factors 00040 nconstr = total number of constraints 00041 neqconstr = number of equality constraints 00042 exponent = this determines how the constraints values will be applied 00043 ____________________________________________________________________________ */ 00044 00045 00046 //_____________________________________________________________________________ 00047 /* 00048 void ods(int n,double *X,double *F,double *dx,double epsc,double minalf, 00049 double maxalf,double *alpha,double *Xn,double *fact,int *oflag, 00050 int *nfc,int mfc); 00051 00052 This function performs a one dimensional search on the function "F" by curve 00053 fitting it with a quadratic polynomial, and then minimizing this quadratic. 00054 _______________________________________________________________________________ 00055 00056 Input ----- 00057 n = number of independent variables 00058 X = vector of independent variables at the initial point 00059 dx = direction of descent at the initial point 00060 epsc = convergence value 00061 minalf = minimum allowed size of the step length (alpha) 00062 maxalf = maximum allowed size of the step length (alpha) 00063 fact[0] = multiplication factor used to adjust the size of the interval. 00064 Try fact[0] = 2.0 00065 fact[1] = multiplication factor used to cut the size of alpha[1] when needed. 00066 Try fact[1] = 0.1 00067 fact[2] = multiplication factor used to determine the step length to the right 00068 most point of the interval. Try fact[2] = 2.0 00069 mfc = maximum number of function calls (nfc) 00070 00071 Output ----- 00072 Xn = vector of independent variables at the current alpha 00073 nfc = number of function calls in this line search 00074 00075 Input and Output ----- 00076 alpha = vector of step lengths (alpha[3] = minimum) 00077 An initial value for alpha[1] must be supplied. Try alpha[1] = 0.2 00078 00079 F = vector of function values at each alpha step 00080 F[0] must be calculated before calling this routine 00081 F[3] = minimum 00082 00083 oflag = a flag which indicates the currect status of the ods algorithm. 00084 -3 => error condition (alpha is less than minalf) 00085 -2 => error condition (alpha is greater than maxalf) 00086 -1 => error condition (too many function calls) 00087 0 => line search completed (this must also be the initial input) 00088 1 => calculate first alpha step 00089 2 => calculate second alpha step 00090 3 => calculate third alpha step 00091 4 => calculate the minimum value 00092 00093 Other ----- 00094 F[4] = value of the quadratic function at its minimum 00095 00096 00097 Example ----- 00098 00099 int n,m,meq,nfc,mfc,oflag; 00100 double X[25],Xn[25],dx[25],P,C[9],mu[9],F[5],fact[3] 00101 double epsc,minalf,maxalf,alpha[4]; 00102 . 00103 . 00104 . 00105 00106 n = 25; 00107 m = 9; 00108 meq = 3; 00109 00110 epsc = 1.0e-06; 00111 minalf = 1.0e-06; 00112 maxalf = 1.0e+06; 00113 fact[0] = 2.0; 00114 fact[1] = 0.2; 00115 fact[2] = 5.0; 00116 mfc = 30; 00117 00118 . 00119 . 00120 . 00121 00122 00123 alpha[1] = 0.2; 00124 00125 performance(X,&P,C); 00126 F[0] = ods_func(P,C,mu,m,meq,2); 00127 00128 oflag = 0; // Initialize the line search function. 00129 00130 do 00131 { 00132 ods(n,X,F,dx,epsc,minalf,maxalf,alpha,Xn,fact,&oflag,&nfc,mfc); 00133 00134 switch(oflag) 00135 { 00136 case 0: 00137 printf("Minimum value = %f\n",F[3]); 00138 printf("The function is minimized at:\n"); 00139 for (i=0;i<n;i++) printf("Xn[%d] = %f\n",i,Xn[i]); 00140 printf("with alpha = %f",alpha[3]); 00141 break; 00142 00143 case -1: 00144 printf("\nToo many functions calls."); 00145 return -1; 00146 00147 case -2: 00148 printf("\nalpha too large."); 00149 return -2; 00150 00151 case -3: 00152 printf("\nalpha too small."); 00153 return -3; 00154 00155 default; 00156 performance(Xn,&P,C); 00157 F[oflag-1] = ods_func(P,C,mu,m,meq,2); 00158 break; 00159 00160 } // End of switch ----- 00161 00162 } 00163 while(oflag); 00164 . 00165 . 00166 . 00167 ____________________________________________________________________________ */
1.3