public minlmreport(minlm.minlmreport obj) { _innerobj = obj; }
public minlmstate(minlm.minlmstate obj) { _innerobj = obj; }
/************************************************************************* Calculates FI/F/G/H for problem min(||Ax-b||) *************************************************************************/ private static void axmb(minlm.minlmstate state, double[,] a, double[] b, int n) { int i = 0; int j = 0; int k = 0; double v = 0; int i_ = 0; if( (state.needf | state.needfg) | state.needfgh ) { state.f = 0; } if( state.needfg | state.needfgh ) { for(i=0; i<=n-1; i++) { state.g[i] = 0; } } if( state.needfgh ) { for(i=0; i<=n-1; i++) { for(j=0; j<=n-1; j++) { state.h[i,j] = 0; } } } for(i=0; i<=n-1; i++) { v = 0.0; for(i_=0; i_<=n-1;i_++) { v += a[i,i_]*state.x[i_]; } if( (state.needf | state.needfg) | state.needfgh ) { state.f = state.f+math.sqr(v-b[i]); } if( state.needfg | state.needfgh ) { for(j=0; j<=n-1; j++) { state.g[j] = state.g[j]+2*(v-b[i])*a[i,j]; } } if( state.needfgh ) { for(j=0; j<=n-1; j++) { for(k=0; k<=n-1; k++) { state.h[j,k] = state.h[j,k]+2*a[i,j]*a[i,k]; } } } if( state.needfi ) { state.fi[i] = v-b[i]; } if( state.needfij ) { state.fi[i] = v-b[i]; for(i_=0; i_<=n-1;i_++) { state.j[i,i_] = a[i,i_]; } } } }
/************************************************************************* Asserts that State fields are consistent with RKind. Returns False otherwise. RKind is an algorithm selector: * -2 = V, AccType=1 * -1 = V, AccType=0 * 0 = FJ * 1 = FGJ * 2 = FGH * 3 = VJ, AccType=0 * 4 = VJ, AccType=1 * 5 = VJ, AccType=2 *************************************************************************/ private static bool rkindvsstatecheck(int rkind, minlm.minlmstate state) { bool result = new bool(); int nset = 0; nset = 0; if( state.needfi ) { nset = nset+1; } if( state.needf ) { nset = nset+1; } if( state.needfg ) { nset = nset+1; } if( state.needfij ) { nset = nset+1; } if( state.needfgh ) { nset = nset+1; } if( state.xupdated ) { nset = nset+1; } if( nset!=1 ) { result = false; return result; } if( rkind==-2 ) { result = state.needfi | state.xupdated; return result; } if( rkind==-1 ) { result = state.needfi | state.xupdated; return result; } if( rkind==0 ) { result = (state.needf | state.needfij) | state.xupdated; return result; } if( rkind==1 ) { result = ((state.needf | state.needfij) | state.needfg) | state.xupdated; return result; } if( rkind==2 ) { result = ((state.needf | state.needfg) | state.needfgh) | state.xupdated; return result; } if( rkind==3 ) { result = (state.needfi | state.needfij) | state.xupdated; return result; } if( rkind==4 ) { result = (state.needfi | state.needfij) | state.xupdated; return result; } if( rkind==5 ) { result = (state.needfi | state.needfij) | state.xupdated; return result; } result = false; return result; }
/************************************************************************* Internal 4PL/5PL fitting function. Accepts X, Y and already initialized and prepared MinLMState structure. On input P1 contains initial guess, on output it contains solution. FLast stores function value at P1. *************************************************************************/ private static void logisticfitinternal(double[] x, double[] y, int n, bool is4pl, double lambdav, minlm.minlmstate state, minlm.minlmreport replm, ref double[] p1, ref double flast) { int i = 0; int j = 0; double ta = 0; double tb = 0; double tc = 0; double td = 0; double tg = 0; double vp = 0; double vp0 = 0; double vp1 = 0; flast = 0; minlm.minlmrestartfrom(state, p1); while( minlm.minlmiteration(state) ) { ta = state.x[0]; tb = state.x[1]; tc = state.x[2]; td = state.x[3]; tg = state.x[4]; if( state.xupdated ) { // // Save best function value obtained so far. // flast = state.f; continue; } if( state.needfi ) { // // Function vector // for(i=0; i<=n-1; i++) { if( (double)(x[i])>(double)(0) ) { state.fi[i] = td+(ta-td)/Math.Pow(1.0+Math.Pow(x[i]/tc, tb), tg)-y[i]; } else { if( (double)(tb)>=(double)(0) ) { state.fi[i] = ta-y[i]; } else { state.fi[i] = td-y[i]; } } } for(i=0; i<=4; i++) { state.fi[n+i] = lambdav*state.x[i]; } continue; } if( state.needfij ) { // // Function vector and Jacobian // for(i=0; i<=n-1; i++) { if( (double)(x[i])>(double)(0) ) { if( is4pl ) { vp = Math.Pow(x[i]/tc, tb); state.fi[i] = td+(ta-td)/(1+vp)-y[i]; state.j[i,0] = 1/(1+vp); state.j[i,1] = -((ta-td)*vp*Math.Log(x[i]/tc)/math.sqr(1+vp)); state.j[i,2] = (ta-td)*(tb/tc)*vp/math.sqr(1+vp); state.j[i,3] = 1-1/(1+vp); state.j[i,4] = 0; } else { vp0 = Math.Pow(x[i]/tc, tb); vp1 = Math.Pow(1+vp0, tg); state.fi[i] = td+(ta-td)/vp1-y[i]; state.j[i,0] = 1/vp1; state.j[i,1] = (ta-td)*-tg*Math.Pow(1+vp0, -tg-1)*vp0*Math.Log(x[i]/tc); state.j[i,2] = (ta-td)*-tg*Math.Pow(1+vp0, -tg-1)*vp0*-(tb/tc); state.j[i,3] = 1-1/vp1; state.j[i,4] = -((ta-td)/vp1*Math.Log(1+vp0)); } } else { if( (double)(tb)>=(double)(0) ) { state.fi[i] = ta-y[i]; state.j[i,0] = 1; state.j[i,1] = 0; state.j[i,2] = 0; state.j[i,3] = 0; state.j[i,4] = 0; } else { state.fi[i] = td-y[i]; state.j[i,0] = 0; state.j[i,1] = 0; state.j[i,2] = 0; state.j[i,3] = 1; state.j[i,4] = 0; } } } for(i=0; i<=4; i++) { for(j=0; j<=4; j++) { state.j[n+i,j] = 0.0; } } for(i=0; i<=4; i++) { state.fi[n+i] = lambdav*state.x[i]; state.j[n+i,i] = lambdav; } continue; } alglib.ap.assert(false, "LogisticFitX: internal error"); } minlm.minlmresultsbuf(state, ref p1, replm); alglib.ap.assert(replm.terminationtype>0, "LogisticFitX: internal error"); }
private static bool rkindvsstatecheck(int rkind, minlm.minlmstate state) { bool result = new bool(); int nset = 0; nset = 0; if( state.needf ) { nset = nset+1; } if( state.needfg ) { nset = nset+1; } if( state.needfij ) { nset = nset+1; } if( state.needfgh ) { nset = nset+1; } if( state.xupdated ) { nset = nset+1; } if( nset!=1 ) { result = false; return result; } if( rkind==0 & (state.needfg | state.needfgh) ) { result = false; return result; } if( rkind==1 & state.needfgh ) { result = false; return result; } if( rkind==2 & state.needfij ) { result = false; return result; } result = true; return result; }