int main(int argc, char **argv) { Halide::Func cell("cell"); Halide::Var x, y; cell(x, y) = 0; cell(2, 1) = 1; cell(3, 2) = 1; cell(1, 3) = 1; cell(2, 3) = 1; cell(3, 3) = 1; Halide::Image<int32_t> output; output = cell.realize(NX,NY); Halide::ImageParam input(Halide::Int(32), 2); // int32_t 2D Halide::Func cell2; cell2(x,y)=input(x,y)+1; Halide::Func nbd("nbd"); Halide::Func bc("bc"); for (int t=0; t<10; ++t) { Halide::Image<int32_t> output = cell.realize(NX,NY); for (int j = 0; j < output.height(); j++) { for (int i = 0; i < output.width(); i++) { printf("%1d", output(i, j)); } printf("\n"); } printf("\n"); bc(x,y)=select((x>=0)&&(x<NX)&&(y>=0)&&(y<NY) ,output(min(NX-1,max(x,0)),min(NY-1,max(y,0))),0); nbd(x,y)=-bc(x,y); // staged programming! for(int dy=-1; dy<=1; ++dy) { for(int dx=-1; dx<=1; ++dx) { nbd(x,y)+=bc(x+dx,y+dy); } } cell(x,y)=select((bc(x,y)==1&&nbd(x,y)==3)||(bc(x,y)==1&&nbd(x,y)==2) || (bc(x,y)==0&&nbd(x,y)==3) ,1,0); } return 0; }
void Lbfgs::minimize(int maxiter) { int n = objToMinimize.ProblemSize(); std::cout << "number of free variables for the minimizer: " << n << std::endl; std::vector<double> l(n); std::vector<double> u(n); Vint nbd(n); x.resize(n); g.resize(n); for (int i=0;i<n; i++) { l[i]=0; u[i]=0; nbd[i]=0; x[i] = 0.0; g[i] = 0.0; } //unconstrained problem int rc; int m = 5; m_opt = lbfgsb_create(n, m, &l[0], &u[0], &nbd[0]); assert(m_opt); m_opt->iprint=-1; double f = DBL_MAX; m_opt->max_iter = maxiter; int last_iter = 0; /* opt->iprint = 0;*/ while (1) { rc = lbfgsb_run(m_opt, &x[0], &f, &g[0]); if (rc == 0) { break; } else if (rc < 0) { printf("lbfgsb stop with an error"); break; } else if (rc == 1) { /* //check analytical derivatives with surreal: { std::vector<dbl> vdblx; tocplx(x,vdblx); std::vector<dbl> vdblg; tocplx(g,vdblg); f = objToMinimize.Function(vdblx); objToMinimize.Derivatives(vdblx,vdblg); g=todbl(vdblg); for (uint i=0; i<x.size(); i++) { std::vector<surreal> svdblx = vdblx ; svdblx[i]+=surreal(0,1); std::cout <<"check: " << svdblx[i] << std::endl; std::cout << g[i] << "==" << imag(objToMinimize.Function(svdblx)) << " " ; } std::cout << std::endl; } //end check derivatives */ if (last_iter < m_opt->niter) { //this is a new iteration last_iter = m_opt->niter; //saves the minimizer variables for each iteration (can be useful for generating animations) m_vars_over_time.push_back(x); } std::vector<dbl> vdblx; tocplx(x,vdblx); std::vector<dbl> vdblg; tocplx(g,vdblg); f = objToMinimize.Function(vdblx); objToMinimize.Derivatives(vdblx,vdblg); g=todbl(vdblg); // std::cout << "analytical derivatives: \n"; // for(uint i=0; i<g.size(); i++) // { // std::cout << "deriv[" << i << "]: " << g[i] << std::endl; // } // objToMinimize.NumDerivatives(x,g,true); } else { assert(!"can not reach here"); } } m_opt->task[59]='\0'; //add a null terminating character to task[] std::cout << m_opt->task << " | " << m_opt->niter << " iterations\n"; }
int Engine::smoothSearch() { char task[60]; std::vector<int> nbd(xBuffer_.size()); double f, dsave[29], *wa; int tr = -1, iter = 0, *iwa, isave[44], lsave[4]; int xSize = xBuffer_.size(); int m = 5; bool canstop = 0; wa = (double*) malloc( ((2 * m + 4) * xSize + 11 * m * m + 8 * m) * sizeof(double)); iwa = (int *) R_alloc(3 * xSize, sizeof(int)); if (itSoftMax_ < 100) itSoftMax_ = 100; else if (itSoftMax_ > 1000) itSoftMax_ = 1000; strcpy(task, "START"); for (int i = 0; i < xSize; ++i) { nbd[i] = 2; } L111: if (iter >= itSoftMax_) { fValue_ = f; return 0; } if (isVerbose()) { Rprintf("iter: %d itSoftMax: %d \n", iter, itSoftMax_); } Utils::setulb(xSize, m, &xBuffer_[0], &lower_[0], &upper_[0], &nbd[0], &f, &g_[0], factr_, &pgTol_, wa, iwa, task, tr, lsave, isave, dsave); iter++; if (strncmp(task, "FG", 2) == 0) { f = fObjective(xBuffer_); // Check if we have reach the threshold if (knowRealEnergy_) { canstop = f <= realEnergyThreshold_; if (canstop) { if (isVerbose()) { Rprintf( "Have got accurate energy %.10g <= %.10g in smooth search\n", f, realEnergyThreshold_); } fValue_ = f; return 0; } } gradient(); //Rprintf("LS TRACE: fValue: %.10g gradient: %.10g\n", f, g_[0]); goto L111; } if (strncmp(task, "NEW_X", 5) == 0) { goto L111; } // We should stop here fValue_ = f; if (fValue_ >= BIG_VALUE) { return -1; } return 0; }
// This attempts to match the behavior of optim() defined in the documentation // https://stat.ethz.ch/R-manual/R-devel/library/stats/html/optim.html // and in the reference implementation // https://svn.r-project.org/R/trunk/src/library/stats/R/optim.R // https://svn.r-project.org/R/trunk/src/library/stats/src/optim.c // https://svn.r-project.org/R/trunk/src/include/R_ext/Applic.h nimSmartPtr<OptimResultNimbleList> NimOptimProblem::solve( NimArr<1, double>& par) { NIM_ASSERT1(!par.isMap(), "Internal error: failed to handle mapped NimArr"); const int n = par.dimSize(0); nimSmartPtr<OptimResultNimbleList> result = new OptimResultNimbleList; result->par = par; result->counts.initialize(NA_INTEGER, true, 2); if (hessian_) { result->hessian.initialize(NA_REAL, true, n, n); } // Set context-dependent default control_ values. if (control_->maxit == NA_INTEGER) { if (method_ == "Nelder-Mead") { control_->maxit = 500; } else { control_->maxit = 100; } } // Parameters common to all methods. double* dpar = par.getPtr(); double* X = result->par.getPtr(); double* Fmin = &(result->value); int* fail = &(result->convergence); void* ex = this; int* fncount = &(result->counts[0]); int* grcount = &(result->counts[1]); if (method_ == "Nelder-Mead") { nmmin(n, dpar, X, Fmin, NimOptimProblem::fn, fail, control_->abstol, control_->reltol, ex, control_->alpha, control_->beta, control_->gamma, control_->trace, fncount, control_->maxit); } else if (method_ == "BFGS") { std::vector<int> mask(n, 1); vmmin(n, dpar, Fmin, NimOptimProblem::fn, NimOptimProblem::gr, control_->maxit, control_->trace, mask.data(), control_->abstol, control_->reltol, control_->REPORT, ex, fncount, grcount, fail); result->par = par; } else if (method_ == "CG") { cgmin(n, dpar, X, Fmin, NimOptimProblem::fn, NimOptimProblem::gr, fail, control_->abstol, control_->reltol, ex, control_->type, control_->trace, fncount, grcount, control_->maxit); } else if (method_ == "L-BFGS-B") { if (lower_.dimSize(0) == 1) lower_.initialize(lower_[0], true, n); if (upper_.dimSize(0) == 1) upper_.initialize(upper_[0], true, n); NIM_ASSERT_SIZE(lower_, n); NIM_ASSERT_SIZE(upper_, n); std::vector<int> nbd(n, 0); for (int i = 0; i < n; ++i) { if (std::isfinite(lower_[i])) nbd[i] |= 1; if (std::isfinite(upper_[i])) nbd[i] |= 2; } char msg[60]; lbfgsb(n, control_->lmm, X, lower_.getPtr(), upper_.getPtr(), nbd.data(), Fmin, NimOptimProblem::fn, NimOptimProblem::gr, fail, ex, control_->factr, control_->pgtol, fncount, grcount, control_->maxit, msg, control_->trace, control_->REPORT); result->message = msg; } else { NIMERROR("Unknown method_: %s", method_.c_str()); } result->value *= control_->fnscale; // Compute Hessian. if (hessian_) { Rf_warning("Hessian computation is not implemented"); // TODO } return result; }
bool GoEyeUtil::NumberOfMoveToEye2(const GoBoard& board, SgBlackWhite color, SgPoint p, int& nummoves) { nummoves = 0; bool capturing = false; SgVector<SgPoint> usedpoints; usedpoints.PushBack(p); SgPointSet counted; // Can never turn own stone into an eye if (board.IsColor(p, color)) return false; // If opponent stone then it must be captured to make eye if (board.IsColor(p, SgOppBW(color))) { capturing = true; // If it is obviously safe then it can never be an eye if (SinglePointSafe2(board, p)) // Quick, naive safety test return false; for (GoBoard::LibertyIterator libit(board, p); libit; ++libit) counted.Include(*libit); } // Count immediate adjacencies for (SgNb4Iterator nb(p); nb; ++nb) { SgPoint adj = *nb; // Empty points must be filled if (board.IsColor(adj, SG_EMPTY)) { counted.Include(adj); } // If adjacent opponent then can never be an eye else if (board.IsColor(adj, SgOppBW(color))) { if (capturing) counted.Include(adj); // must capture and then fill else return false; } } // Up to one diagonal can be ignored: estimate most costly SgPoint toignore = SG_NULLPOINT; int maxcost = 0; int infcost = 1000; if (board.Line(p) > 1) { for (SgNb4DiagIterator nbd(p); nbd; ++nbd) { SgPoint diag = *nbd; int cost = 0; if ( board.IsColor(diag, SG_EMPTY) && ! IsSinglePointEye2(board, diag, color, usedpoints)) { cost = 1; } else if (board.IsColor(diag, SgOppBW(color))) { // quick safety test if (SinglePointSafe2(board, diag)) cost = infcost; else cost = board.NumLiberties(diag); } if (cost > maxcost) { maxcost = cost; toignore = diag; } } } // Now mark points that must be played to secure diagonals for (SgNb4DiagIterator nbd(p); nbd; ++nbd) { SgPoint diag = *nbd; if (diag == toignore) continue; // Empty points must be filled (unless they are eyes) if ( board.IsColor(diag, SG_EMPTY) && ! IsSinglePointEye2(board, diag, color, usedpoints)) { counted.Include(diag); } // Opponent stones on diagonals must be captured and filled else if (board.IsColor(diag, SgOppBW(color))) { if (SinglePointSafe2(board, diag)) return false; else { counted.Include(diag); for (GoBoard::LibertyIterator libit(board, diag); libit; ++libit) counted.Include(*libit); } } } nummoves = counted.Size(); return true; }