Example #1
0
int stopcheck_mt(double fx, int N, double *xc, double *xf, double *jac, double *dx, double fsval, double gtol, double stol, int retval) {
	int rcode, i;
	double num, den;
	double stop0;
	double *scheck;

	rcode = 0;

	if (retval == 3) {
		rcode = 4;
		return rcode;
	}
	if (retval == 15) {
		rcode = 15;
		return rcode;
	}
	
	scheck = (double*)malloc(sizeof(double)*N);

	if (fabs(fx) > fabs(fsval)) {
		den = fabs(fx);
	}
	else {
		den = fabs(fsval);
	}
	for (i = 0; i < N; ++i) {
		if (fabs(xf[i]) > 1.0 / fabs(dx[i])) {
			num = fabs(xf[i]);
		}
		else {
			num = 1.0 / fabs(dx[i]);
		}
		scheck[i] = fabs(jac[i]) * num / den;
	}

	stop0 = array_max_abs(scheck, N);

	if (stop0 <= gtol) {
		rcode = 1;
	}
	else {
		for (i = 0; i < N; ++i) {
			if (fabs(xf[i]) > 1.0 / fabs(dx[i])) {
				den = fabs(xf[i]);
			}
			else {
				den = 1.0 / fabs(dx[i]);
			}
			num = fabs(xf[i] - xc[i]);
			scheck[i] = num / den;
		}
		stop0 = array_max_abs(scheck, N);
		if (stop0 <= stol) {
			rcode = 2;
		}
	}

	free(scheck);
	return rcode;
}
Example #2
0
int stopcheck3_mt(double *xi,double *xf,double fx, int N, double fo, double *jac, double *dx, double eps,
		double stoptol, double functol, int retval) {
	int rcode,i;
	double nrm,nrmnx,relfit,num,den,stop0;
	double *scheck;
	rcode = 0;

	scheck = (double*)malloc(sizeof(double)*N);

	if (retval == 3) {
		rcode = 4;
		return rcode;
	}
	if (retval == 15) {
		rcode = 15;
		return rcode;
	}

	nrm = l2norm(jac, N);
	nrmnx = nrm / (double) N;

	if (fabs(fo) < eps) {
		relfit = fabs(fx - fo);
	}
	else {
		relfit = fabs((fx - fo)/fo);
	}

	if (nrmnx < stoptol) {
		rcode = 1; // Successful Convergence
	} else if (relfit < functol) {
		rcode = 6; // Relative fit less than function tolerance
	} else {
		for (i = 0; i < N; ++i) {
			den = 1.0+fabs(xf[i]);
			num = fabs(xf[i] - xi[i]);
			scheck[i] = num / den;
		}
		stop0 = array_max_abs(scheck, N);
		if (stop0 <= stoptol) {
			rcode = 2;
		}
	}

	free(scheck);
	return rcode;
}
Example #3
0
void newton_fd(double (*funcpt)(double *,int),double *xi,int N,double *x) {
	int it,i;
	double *jac,*hess,*L,*xf;
	double stop,result;
	
	jac = (double*) malloc(sizeof(double) *N);
	xf = (double*) malloc(sizeof(double) *N);
	hess = (double*) malloc(sizeof(double) *N * N);
	L = (double*) malloc(sizeof(double) *N * N);
	
	it = 0;
	stop = 1.0;
	
	while (stop > 1e-05 && it < 100) {
		//result = funcpt(xi,N,jac,hess);
		jacobian_fd(funcpt,xi,N,jac);
		hessian_fd(funcpt,xi,N,hess);
		//mdisplay(jac,1,N);
		//mdisplay(hess,N,N);
		modelhess2(hess,N,L);
		//mdisplay(L,N,N);
		scale(jac,1,N,-1.0);
		linsolve_lower(L,N,jac,x);
		madd(x,xi,x,1,N);
		msub(x,xi,xf,1,N);
		stop = array_max_abs(xf,N);
		for(i=0;i < N;++i) {
			xi[i] = x[i];
		}
		it++;
		//printf("result %lf \n",result);
		printf("Values obtained %lf , %lf after %d Iterations \n",x[0],x[1],it);
	}
	
	free(jac);
	free(hess);
	free(xf);
	free(L);
}
Example #4
0
int cvsrch(custom_function *funcpt, custom_gradient *funcgrad, double *x, double *f, double *g, double *stp, double *s, int N, double *dx, double maxstep,
	int MAXITER,double eps2,double ftol, double gtol, double xtol) {
	int info,i,siter,nfev;
	int infoc, j, brackt, stage1;
	double dg, dgm, dginit, dgtest, dgx, dgxm, dgy, dgym, finit, ftest1, fm, fx, fxm, fy, fym, p5, p66, stx, sty,
		stmin, stmax, width, width1, xtrapf;
	double nlen,den,rell,stepmin;
	double *rcheck,*wa;

	rcheck = (double*)malloc(sizeof(double)*N);
	wa = (double*)malloc(sizeof(double)*N);

	nlen = 0.0;
	p5 = 0.5;
	p66 = 0.66;
	xtrapf = 4.0;
	info = 0;
	infoc = 1;
	siter = MAXITER;


	if (N <= 0 || *stp <= 0.0 || ftol < 0.0 || gtol < 0.0 || xtol < 0.0) {
		return info;
	}


	for (i = 0; i < N; ++i) {
		nlen += dx[i] * s[i] * dx[i] * s[i];
	}
	nlen = sqrt(nlen);

	for (i = 0; i < N; ++i) {
		if (fabs(x[i]) > 1.0 / fabs(dx[i])) {
			den = fabs(x[i]);
		}
		else {
			den = 1.0 / fabs(dx[i]);
		}
		rcheck[i] = s[i] / den;
	}

	rell = array_max_abs(rcheck, N);

	stepmin = ftol / rell;

	dginit = 0.0;

	for (j = 0; j < N; ++j) {
		dginit += g[j] * s[j];
	}

	if (dginit >= 0.0) {
		return info;
	}

	brackt = 0;
	stage1 = 1;
	finit = *f;
	nfev = 0;
	dgtest = ftol*dginit;
	width = maxstep - stepmin;
	width1 = width / 0.5;

	for (j = 0; j < N; ++j) {
		wa[j] = x[j];
	}

	/*     The variables stx, fx, dgx contain the values of the step, 
     function, and directional derivative at the best step.
     The variables sty, fy, dgy contain the value of the step,
     function, and derivative at the other endpoint of
     the interval of uncertainty.
     The variables stp, f, dg contain the values of the step,
     function, and derivative at the current step.
	*/

	stx = 0.0;
	fx = finit;
	dgx = dginit;
	sty = 0.0;
	fy = finit;
	dgy = dginit;

	// Iteration

	while (info == 0) {

		if (brackt == 1) {
			stmin = pmin(stx, sty);
			stmax = pmax(stx, sty);
		} else {
			stmin = stx;
			stmax = *stp + xtrapf*(*stp - stx);
		}

		*stp = pmax(*stp, stepmin);
		*stp = pmin(*stp, maxstep);

		if ((brackt == 1 && (*stp <= stmin || *stp >= stmax)) || nfev >= siter - 1 || infoc == 0 || (brackt == 1 && (stmax - stmin) <= xtol*stmax)) {
			*stp = stx;
		}

		for (j = 0; j < N; ++j) {
			x[j] = wa[j] + *stp * s[j];
		}

		*f = FUNCPT_EVAL(funcpt,x, N);
		if (*f >= DBL_MAX || *f <= -DBL_MAX) {
			printf("Program Exiting as the function value exceeds the maximum double value");
			free(rcheck);
			free(wa);
			return 15;
		}
		if (*f != *f) {
			printf("Program Exiting as the function returns NaN");
			free(rcheck);
			free(wa);
			return 15;
		}
		grad_cd(funcpt,funcgrad, x, N, dx, eps2,g);
		nfev++;


		dg = 0.0;
		for (j = 0; j < N; ++j) {
			dg = dg + g[j]*s[j];
		}	
		ftest1 = finit + *stp * dgtest;

		//       Test for convergence.

		if ((brackt == 1 && (*stp <= stmin || *stp >= stmax)) || infoc == 0) {
			info = 6;
		}

		if (*stp == maxstep && *f <= ftest1 && dg <= dgtest) {
			info = 5;
		}

		if (*stp == stepmin && (*f > ftest1 || dg >= dgtest)) {
			info = 4;
		}

		if (nfev >= siter) {
			info = 3;
		}

		if (brackt == 1 && ((stmax - stmin) <= xtol*stmax)) {
			info = 2;
		}

		if (*f <= ftest1 && fabs(dg) <= gtol*(-dginit)) {
			info = 1;
		}

		if (stage1 == 1 && *f <= ftest1 && dg >= pmin(ftol, gtol)*dginit) {
			stage1 = 0;
		}


		/*
		A modified function is used to predict the step only if
        we have not obtained a step for which the modified
        function has a nonpositive function value and nonnegative
        derivative, and if a lower function value has been
        obtained but the decrease is not sufficient.
		*/
		if (stage1 == 1 && *f <= fx && *f > ftest1) {

			fm = *f - *stp*dgtest;
			fxm = fx - stx*dgtest;
			fym = fy - sty*dgtest;
			dgm = dg - dgtest;
			dgxm = dgx - dgtest;
			dgym = dgy - dgtest;

			infoc = cstep(&stx, &fxm, &dgxm, &sty, &fym, &dgym, stp, &fm, &dgm, &brackt, stmin, stmax);

			fx = fxm + stx*dgtest;
			fy = fym + sty*dgtest;
			dgx = dgxm + dgtest;
			dgy = dgym + dgtest;
		} else {
			
			infoc = cstep(&stx, &fx, &dgx, &sty, &fy, &dgy, stp, f, &dg, &brackt, stmin, stmax);
		}

		if (brackt == 1) {
			if (fabs(sty - stx) >= p66*width1) {
				*stp = stx + p5*(sty - stx);
			}
			width1 = width;
			width = fabs(sty - stx);
		}

	}

	free(rcheck);
	free(wa);

	return info;
}
Example #5
0
double cholmod(double *A, int N, double *L, double maxinp) {
	/*
	 * Algorithm 5.5.2 Dennis, Schnabel, Numerical Methods for Unconstrained Optimization and 
	 * Non-Linear Equations
	 */ 
	int i,j,k,step,step2;
	double beta,d1,ls;
	double *U22;
	double maxadd,maxdiag,minl,minl2,minljj;
	
	U22 = (double*) malloc(sizeof(double) * N * N);
	
	minl = sqrt(sqrt((double) FDVAL)) * maxinp;
	maxadd = 0.0;
	
	for(i = 0; i < N;++i) {
		U22[i] = A[i+i*N];
	}
	
	for(i = 0; i < N * N;++i) {
		L[i] = 0.0;
	}
	
	maxdiag = array_max_abs(U22,N);
	
	if ( maxinp == 0.0) {
		maxinp = sqrt(maxdiag);
	}
	
	minl2 = sqrt((double) FDVAL) * maxinp;
	
	for(j = 0; j < N;++j) {
		step = j * N;
		ls = 0.0;
		for(i = 0; i < j;++i) {
			ls += L[step+i] * L[step+i];
		}
		
		L[step+j] = A[step+j] - ls;
		
		for(i = j+1; i < N;++i) {
			ls = 0.0;
			step2 = i * N;
			for(k = 0;k < j;++k) {
				ls += L[step+k] * L[step2+k];
			}
			L[step2+j] = A[step+i] - ls;
			
			if (fabs(L[step2+j]) > minljj) {
				minljj = fabs(L[step2+j]);
			}
		}
		
		if (minljj/maxinp > minl) {
			minljj = minljj/maxinp;
		} else {
			minljj = minl;
		}
		
		if (L[step+j] > minljj*minljj) {
				L[step+j] = sqrt(L[step+j]);
		} else {
			if (minljj < minl2) {
				minljj = minl2;
			}
			
			if (maxadd < (minljj*minljj - L[step+j])) {
				maxadd = minljj*minljj - L[step+j];
			}
			
			L[step+j] = minljj;
		}
		
		for(i = j+1; i < N;++i) {
			L[i*N+j] /= L[step+j];
		}
		
		
	}
	
	free(U22);
	
	return maxadd;
}
Example #6
0
int newton_min_func(double (*funcpt)(double *,int),double *xi,int N,double *dx,double fsval,double *xf) {
	int rcode,iter;
	int i,siter,retval;
	double gtol,stol;
	double fx,num,den,stop0,maxstep,fxf;
	double *jac,*hess,*scheck,*xc,*L,*step;
	
	jac = (double*) malloc(sizeof(double) *N);
	scheck = (double*) malloc(sizeof(double) *N);
	xc = (double*) malloc(sizeof(double) *N);
	step = (double*) malloc(sizeof(double) *N);
	hess = (double*) malloc(sizeof(double) *N * N);
	L = (double*) malloc(sizeof(double) *N * N);
	
	rcode = 0;
	iter = 0;
	siter = (int) SETITER;
	gtol = pow((double) FDVAL,1.0/3.0);
	stol = gtol * gtol;
	fx = funcpt(xi,N);
	jacobian_fd(funcpt,xi,N,jac);
	
	//set values
	maxstep = 1.0;
	
	for(i = 0; i < N;++i) {
		dx[i] = 1.0 / dx[i];
	}
	
	//Check Stop0
	if (fabs(fx) > fabs(fsval)) {
			den = fabs(fx);
	} else {
			den = fabs(fsval);
	}
	for(i = 0; i < N;++i) {
		if (fabs(xi[i]) > 1.0 / fabs(dx[i])) {
			num = fabs(xi[i]);
		} else {
			num = 1.0 / fabs(dx[i]);
		}
		scheck[i] = fabs(jac[i]) * num / den;
	}
	
	stop0 = array_max_abs(scheck,N);
	
	if (stop0 <= gtol * 1e-03) {
		rcode = 1;
		for(i = 0; i < N;++i) {
			xf[i] = xi[i];
		}
		return rcode;
	}
	
	hessian_fd(funcpt,xi,N,hess);
	
	for(i = 0; i < N;++i) {
		xc[i] = xi[i];
	}
	
	while (rcode == 0 && iter < siter) {
		modelhess(hess,N,dx,L);
		scale(jac,1,N,-1.0);
		linsolve_lower(L,N,jac,step);
		scale(jac,1,N,-1.0);
		retval = lnsrch(funcpt,xc,jac,step,N,maxstep,dx,stol,xf); 
		fxf = funcpt(xf,N);
		jacobian_fd(funcpt,xf,N,jac);
		rcode = stopcheck(fxf,N,xc,xf,jac,dx,fsval,gtol,stol,retval);
		hessian_fd(funcpt,xf,N,hess);
		for(i = 0; i < N;++i) {
			xc[i] = xf[i];
		}
	}
	
	free(jac);
	free(hess);
	free(scheck);
	free(xc);
	free(L);
	free(step);
	return rcode;
}
Example #7
0
int lnsrch(double (*funcpt)(double *,int),double *xi,double *jac,double *p,int N,double maxstep,double * dx,double stol,double *x) {
	int retval,i,j;
	double alpha,lambda,lambdamin,funcf,funci,lambdaprev,lambdatemp,funcprev;
	double lambda2,lambdaprev2,ll,den,rell;
	double *slopei,*temp1,*temp2,*ab,*rcheck;
	
	slopei = (double*) malloc(sizeof(double) *1);
	temp1 = (double*) malloc(sizeof(double) *4);
	temp2 = (double*) malloc(sizeof(double) *2);
	ab = (double*) malloc(sizeof(double) *2);
	rcheck = (double*) malloc(sizeof(double) *N);
	retval = 100;
	alpha = 1e-04;
	lambda = 1.0;
	
	mmult(jac,p,slopei,1,N,1);
	
	for(i = 0; i < N;++i) {
		if (fabs(xi[i]) > 1.0 /fabs(dx[i])) {
			den = fabs(xi[i]);
		} else {
			den = 1.0 /fabs(dx[i]);
		}
		rcheck[i] = p[i]/den;
	}
	
	rell = array_max_abs(rcheck,N);
	
	lambdamin = stol/rell;
	
	while (retval > 1) {
		scale(p,1,N,lambda);
		madd(xi,p,x,1,N);
		funcf = funcpt(x,N);
		funci = funcpt(xi,N);
		
		if (funcf <= funci + alpha *lambda *slopei[0]) {
			retval = 0;
		} else if (lambda < lambdamin) {
			retval = 1;
			for (i = 0; i < N;++i) {
				x[i] = xi[i]; // Check
			}
		} else {
			if (lambda == 1.0) {
				lambdatemp = - slopei[0] / (funcf - funci - slopei[0]); 
			} else {
				lambda2 = lambda * lambda;
				lambdaprev2 = lambdaprev * lambdaprev;
				ll = lambda - lambdaprev;
				temp1[0] = 1.0 / lambda2; temp1[1] = -1.0 /lambdaprev2;
				temp1[2] = - lambdaprev / lambda2; temp1[3] = lambda /lambdaprev2;
				temp2[0] = funcf - funci - lambda * slopei[0];
				temp2[1] = funcprev - funci - lambdaprev * slopei[0];
				mmult(temp1,temp2,ab,2,2,1);
				scale(ab,1,2,ll);
				if (ab[0] == 0.0) {
					lambdatemp = - slopei[0] / (2.0 * ab[1]);
				} else {
					lambdatemp = (-ab[1] + sqrt( ab[1] * ab[1] - 3.0 * ab[0] *slopei[0]))/ (3.0 * ab[0]);
				}
				
				if (lambdatemp > 0.5 * lambda) {
					lambdatemp = 0.5 * lambda;
				}
			}
			lambdaprev = lambda;
			funcprev = funcf;
			if (lambdatemp <= 0.1 * lambda) {
				lambda = 0.1 * lambda;
			} else {
				lambda = lambdatemp;
			}
		}
	
	}
	
	free(slopei);
	free(temp1);
	free(temp2);
	free(ab);
	free(rcheck);
	return retval;
}
Example #8
0
double modelhess(double *A,int N,double *dx,double *L) {
	/*
	 * Algorithm 5.5.1 Dennis, Schnabel, Numerical Methods for Unconstrained Optimization and 
	 * Non-Linear Equations
	 */ 
	 double *U22;
	 double sqrteps,maxdiag,mindiag,maxposdiag,u;
	 double maxoffdiag,maxinp,maxadd;
	 double maxev,minev,offrow,sdd;
	 int step,i,j,k;
	 
	 sqrteps = sqrt((double) FDVAL);
	 
	 U22 = (double*) malloc(sizeof(double) * N * N);
	 
	//scale
	
	for(i = 0;i < N;++i) {
		step = i*N;
		for(j = 0;j < N;++j) {
			A[step+j] /= (dx[i] * dx[j]);
		}
	}
	
	for(i = 0; i < N;++i) {
		U22[i] = A[i+i*N];
	}
	
	maxdiag = array_max(U22,N);
	mindiag = array_min(U22,N);
	
	maxposdiag = 0.0;
	
	if (maxdiag > maxposdiag) {
		maxposdiag = maxdiag;
	}
	
	for(i = 0; i < N;++i) {
		U22[i] = 0.0;
	}
	u = 0.0;
	if (mindiag <= sqrteps*maxposdiag) {
		u = 2 * (maxposdiag - mindiag) * sqrteps - mindiag;
		maxdiag += u;
	}
	k = 0;
	for (i = 0; i < N;++i) {
		for(j = 0;j < N;++j) {
			if (j > i) {
				U22[k] = A[i*N+j];
				k++;
			}
		}
	}
	
	maxoffdiag = array_max_abs(U22,k);
	
	if ( maxoffdiag*(1+2*sqrteps) > maxdiag) {
		u += (maxoffdiag - maxdiag) + 2*sqrteps*maxoffdiag;
		maxdiag = maxoffdiag*(1+2*sqrteps);
	}
	
	if (maxdiag == 0) {
		u = 1;
		maxdiag = 1;
	}
	
	if (u > 0) {
		for(i=0;i < N;++i) {
			A[i*N+i] += u;
		}
	}
	
	if (maxdiag > maxoffdiag / N) {
		maxinp = sqrt(maxdiag);
	} else {
		maxinp = sqrt(maxoffdiag / N);
	}
	
	maxadd = cholmod(A,N,L,maxinp);
	
	if (maxadd > 0) {
		maxev = minev = A[0];
		for(i = 0; i < N;++i) {
			offrow = 0.0;
			step = i*N;
			
			for(j = 0; j < i;++j) {
				offrow += fabs(A[step+j]);
			}
			
			for(j = i+1; j < N;++j) {
				offrow += fabs(A[step+j]);
			}
			
			if (maxev < A[step+i] + offrow) {
				maxev = A[step+i] + offrow;
			}
			if (minev > A[step+i] - offrow) {
				minev = A[step+i] - offrow;
			}
			
		}
		sdd = (maxev - minev) * sqrteps - minev;
		if (sdd < 0) {
			sdd = 0;
		}
		if (maxadd > sdd) {
			u = sdd;
		} else {
			u = maxadd;
		}
		
		for(i = 0; i < N;++i) {
			A[i*N+i] += u; 
		}
	}
	
	maxadd = cholmod(A,N,L,0.0);
	
	//unscale
	
	for(i = 0;i < N;++i) {
		step = i*N;
		for(j = 0;j < N;++j) {
			A[step+j] *= (dx[i] * dx[j]);
		}
	}
	
	for(i = 0;i < N;++i) {
		step = i*N;
		for(j = 0;j < i;++j) {
			L[step+j] *= dx[i];
		}
	}
	
	free(U22);
	
	return maxadd;
	 
}