public mincgstate(mincg.mincgstate obj) { _innerobj = obj; }
public mincgreport(mincg.mincgreport obj) { _innerobj = obj; }
/************************************************************************* Calculate test function f(x) = 0.5*(x-x0)'*A*(x-x0), A = D+V'*Vd*V *************************************************************************/ private static void calclowrank(mincg.mincgstate state, int n, int vcnt, double[] d, double[,] v, double[] vd, double[] x0) { int i = 0; int j = 0; double dx = 0; double t = 0; double t2 = 0; int i_ = 0; state.f = 0; for(i=0; i<=n-1; i++) { state.g[i] = 0; } for(i=0; i<=n-1; i++) { dx = state.x[i]-x0[i]; state.f = state.f+0.5*dx*d[i]*dx; state.g[i] = state.g[i]+d[i]*dx; } for(i=0; i<=vcnt-1; i++) { t = 0; for(j=0; j<=n-1; j++) { t = t+v[i,j]*(state.x[j]-x0[j]); } state.f = state.f+0.5*t*vd[i]*t; t2 = t*vd[i]; for(i_=0; i_<=n-1;i_++) { state.g[i_] = state.g[i_] + t2*v[i,i_]; } } }
/************************************************************************* Calculate test function IIP2 f(x) = sum( ((i*i+1)*x[i])^2, i=0..N-1) It has high condition number which makes fast convergence unlikely without good preconditioner. *************************************************************************/ private static void calciip2(mincg.mincgstate state, int n) { int i = 0; if( state.needf | state.needfg ) { state.f = 0; } for(i=0; i<=n-1; i++) { if( state.needf | state.needfg ) { state.f = state.f+math.sqr(i*i+1)*math.sqr(state.x[i]); } if( state.needfg ) { state.g[i] = math.sqr(i*i+1)*2*state.x[i]; } } }
/************************************************************************* Calculate test function #3 Simple variation of #1, much more nonlinear, with non-zero value at minimum. It achieve two goals: * makes unlikely premature convergence of algorithm . * solves some issues with EpsF stopping condition which arise when F(minimum) is zero *************************************************************************/ private static void testfunc3(mincg.mincgstate state) { double s = 0; s = 0.001; if( (double)(state.x[0])<(double)(100) ) { if( state.needf | state.needfg ) { state.f = math.sqr(Math.Exp(state.x[0])-2)+math.sqr(math.sqr(state.x[1])+s)+math.sqr(state.x[2]-state.x[0]); } if( state.needfg ) { state.g[0] = 2*(Math.Exp(state.x[0])-2)*Math.Exp(state.x[0])+2*(state.x[0]-state.x[2]); state.g[1] = 2*(math.sqr(state.x[1])+s)*2*state.x[1]; state.g[2] = 2*(state.x[2]-state.x[0]); } } else { if( state.needf | state.needfg ) { state.f = Math.Sqrt(math.maxrealnumber); } if( state.needfg ) { state.g[0] = Math.Sqrt(math.maxrealnumber); state.g[1] = 0; state.g[2] = 0; } } }
/************************************************************************* Calculate test function #2 Simple variation of #1, much more nonlinear, which makes unlikely premature convergence of algorithm . *************************************************************************/ private static void testfunc2(mincg.mincgstate state) { if( (double)(state.x[0])<(double)(100) ) { state.f = math.sqr(Math.Exp(state.x[0])-2)+math.sqr(math.sqr(state.x[1]))+math.sqr(state.x[2]-state.x[0]); state.g[0] = 2*(Math.Exp(state.x[0])-2)*Math.Exp(state.x[0])+2*(state.x[0]-state.x[2]); state.g[1] = 4*state.x[1]*math.sqr(state.x[1]); state.g[2] = 2*(state.x[2]-state.x[0]); } else { state.f = Math.Sqrt(math.maxrealnumber); state.g[0] = Math.Sqrt(math.maxrealnumber); state.g[1] = 0; state.g[2] = 0; } }