/* Solving LU=b for general matrix */ Vec EqSolver::LU_resolve(Vec c){ Vec dd=c; double u=0; /* Changes c to compensate swaping rows during LU decomposition */ for(int i=0;i<c.Size();++i){ if(b[i]!=i){ u=c[i]; c.SetEntrie(i,c[b[i]]); c.SetEntrie(b[i],u); } b[(int) b[i]]=b[i]; } /* Ly=b */ for(int i=0;i<c.Size();++i){ u=0; for(int j=0;j<i;++j) u+=m[i][j]*dd[j]; dd.SetEntrie(i,c[i]-u); } /* Ux=y */ for(int i=c.Size()-1;i>-.5;--i){ u=0; for(int j=c.Size()-1;j>i;--j) u+=m[i][j]*dd[j]; dd.SetEntrie(i,(dd[i]-u)/m[i][i]); } return dd; }
double ME_Model::constrained_line_search(double C, const Vec & x0, const Vec & grad0, const double f0, const Vec & dx, Vec & x, Vec & grad1) { // compute the orthant to explore Vec orthant = x0; for (size_t i = 0; i < orthant.Size(); i++) { if (orthant[i] == 0) orthant[i] = -grad0[i]; } double t = 1.0 / LINE_SEARCH_BETA; double f; do { t *= LINE_SEARCH_BETA; x = x0 + t * dx; x.Project(orthant); // for (size_t i = 0; i < x.Size(); i++) { // if (x0[i] != 0 && sign(x[i]) != sign(x0[i])) x[i] = 0; // } f = regularized_func_grad(C, x, grad1); // cout << "*"; } while (f > f0 + LINE_SEARCH_ALPHA * dot_product(x - x0, grad0)); return f; }
double ME_Model::regularized_func_grad(const double C, const Vec & x, Vec & grad) { double f = FunctionGradient(x.STLVec(), grad.STLVec()); for (size_t i = 0; i < x.Size(); i++) { f += C * fabs(x[i]); } return f; }
// transfers the data to a thread that does the actual sending static void QueueMessageForSending(Vec<byte>& msg) { size_t len = msg.Size(); if (0 == len) return; MemBlock *block = GetBlock(len); if (block) { block->Append(msg.LendData(), len); SetEvent(gSendThreadEvent); } else { lf("memtrace.dll: QueueMessageForSending() couldn't queu %d bytes", (int)len); } }
static Vec pseudo_gradient(const Vec & x, const Vec & grad0, const double C) { Vec grad = grad0; for (size_t i = 0; i < x.Size(); i++) { if (x[i] != 0) { grad[i] += C * sign(x[i]); continue; } const double gm = grad0[i] - C; if (gm > 0) { grad[i] = gm; continue; } const double gp = grad0[i] + C; if (gp < 0) { grad[i] = gp; continue; } grad[i] = 0; } return grad; }