/* Driver program to test above function */ int main() { int arr[] = {80, 2, 6, 3, 100}; int size = sizeof(arr)/sizeof(arr[0]); printf("Maximum difference is %d", maxDiff(arr, size)); getch(); return 0; }
int main() { int arr[] = {34,8,10,3,2,80,30,33,1}; printf("max diff: %d\n",maxDiff(arr,sizeof(arr)/sizeof(arr[0]))); return 0; }
//----- the rprop wrapped with stopping criteria uint Rprop::loop(arr& _x, ScalarFunction& f, double *fmin_return, double stoppingTolerance, double initialStepSize, uint maxEvals, uint verbose) { if(!s->stepSize.N) init(initialStepSize); arr x, J(_x.N), x_min, J_min; double fx, fx_min=0; uint rejects=0, small_steps=0; x=_x; if(verbose>1) cout <<"*** optRprop: starting point x=" <<x <<endl; ofstream fil; if(verbose>0) fil.open("z.opt"); uint evals=0; double diff=0.; for(;;) { //checkGradient(p, x, stoppingTolerance); //compute value and gradient at x fx = f.fs(J, NoArr, x); evals++; if(verbose>0) fil <<evals <<' ' <<eval_cost <<' ' << fx <<' ' <<diff <<' ' <<x <<endl; if(verbose>1) cout <<"optRprop " <<evals <<' ' <<eval_cost <<" \tf(x)=" <<fx <<" \tdiff=" <<diff <<" \tx=" <<x <<endl; //infeasible point! undo the previous step if(fx!=fx) { //is NAN if(!evals) HALT("can't start Rprop with unfeasible point"); s->stepSize*=(double).1; s->lastGrad=(double)0.; x=x_min; fx=fx_min; J=J_min; rejects=0; } //update best-so-far if(evals<=1) { fx_min= fx; x_min=x; } if(fx<=fx_min) { x_min=x; fx_min=fx; J_min=J; rejects=0; } else { rejects++; if(rejects>10) { s->stepSize*=(double).1; s->lastGrad=(double)0.; x=x_min; fx=fx_min; J=J_min; rejects=0; } } //update x s->step(x, J, NULL); //check stopping criterion based on step-length in x diff=maxDiff(x, x_min); if(diff<stoppingTolerance) { small_steps++; } else { small_steps=0; } if(small_steps>3) break; if(evals>maxEvals) break; } if(verbose>0) fil.close(); if(verbose>1) gnuplot("plot 'z.opt' us 1:3 w l", NULL, true); if(fmin_return) *fmin_return= fx_min; _x=x_min; return evals; }