void ai_eval_hexes( int x, int y, int range, int(*eval_func)(int,int,void*), void *ctx ) { List *list = list_create( LIST_NO_AUTO_DELETE, LIST_NO_CALLBACK ); AI_Pos *pos; int i, nx, ny; /* gather and handle all positions by breitensuche. use AUX mask to mark visited positions */ map_clear_mask( F_AUX ); list_add( list, ai_create_pos( x, y ) ); mask[x][y].aux = 1; list_reset( list ); while ( list->count > 0 ) { pos = list_dequeue( list ); if ( !eval_func( pos->x, pos->y, ctx ) ) { free( pos ); break; } for ( i = 0; i < 6; i++ ) if ( get_close_hex_pos( pos->x, pos->y, i, &nx, &ny ) ) if ( !mask[nx][ny].aux && get_dist( x, y, nx, ny ) <= range ) { list_add( list, ai_create_pos( nx, ny ) ); mask[nx][ny].aux = 1; } free( pos ); } list_delete( list ); }
RGBColor ImplicitSurface::trace_ray(const Ray& ray) const{ //float range = 10.83; float range = 3.79; range *= range; float vres = (*world_ptr).vp.vres; float hres = (*world_ptr).vp.hres; const float x = (ray.o.x + hres/2) / hres * range; const float y = (ray.o.y + vres/2) / vres * range; float eval = eval_func(x, y); return RGBColor(eval, eval, eval); //return white; }
// min int min_node(int depth, int side, XY *move) { int best = INFINITY, value; int nmoves; XY moves[MOVENUM], opp[MOVENUM]; int pre_board[8][8]; int i; if (depth == DEPTH) return eval_func(); // 手を生成 nmoves = generate_moves(side, moves); // 手がないとき if (nmoves == 0) { if (generate_moves(-side, opp) == 0) // 相手もおけないときは終了 return get_terminal_value(); else // 違うときはパス moves[nmoves++] = PASSMOVE; } // たどっていく for (i = 0; i < nmoves; i++) { memcpy(pre_board, board, sizeof(board)); // 盤面の保存 place_disk(side, moves[i]); // 一手進める // 再帰(recursive) value = max_node(depth + 1, -side, move); memcpy(board, pre_board, sizeof(board)); // 戻す // 値の更新 if (value <= best) { best = value; if (depth == 0) *move = moves[i]; } } return best; }
Value eval(const Tree& tree, const Params& params, void* p, Cost* limit) { switch (tree.expr_id().type) { case Expr::Term: { return eval_term(tree.expr_id().index, params, p, limit); } case Expr::Func: { Args args; for (const Tree& child : tree.children()) args.push_back(eval(child, params, p, limit)); return eval_func(tree.expr_id().index, args, p, limit); } case Expr::Select: { unsigned branch = eval_select(tree.expr_id().index, params, p, limit); assert(branch < tree.children().size()); return eval(tree.children()[branch], params, p, limit); } case Expr::Invalid: throw std::logic_error("Invalid expression"); } assert(false); }
namespace GiNaC { ////////// // Hermite polynomials H_n(x) ////////// static ex hermite_evalf(const ex& n, const ex& x, PyObject* parent) { if (not is_exactly_a<numeric>(x) or not is_exactly_a<numeric>(n)) return hermite(n,x).hold(); // see http://dlmf.nist.gov/18.5.E13 numeric numn = ex_to<numeric>(n); numeric numx = ex_to<numeric>(x); std::vector<numeric> numveca, numvecb; numveca.push_back(numn / *_num_2_p); numveca.push_back(*_num1_2_p + (numn / *_num_2_p)); return pow(numx * (*_num2_p), numn) * hypergeometric_pFq(numveca, numvecb, -pow(numx, *_num2_p).inverse(), parent); } static ex hermite_eval(const ex& n, const ex& x) { if (is_exactly_a<numeric>(x)) { numeric numx = ex_to<numeric>(x); if (numx.is_zero()) if (n.info(info_flags::integer) and n.info(info_flags::odd)) return _ex0; if (is_exactly_a<numeric>(n) and numx.info(info_flags::inexact)) return hermite_evalf(n, x, nullptr); } if (not is_exactly_a<numeric>(n)) return hermite(n, x).hold(); numeric numn = ex_to<numeric>(n); if (not numn.info(info_flags::integer) or numn < 0) throw std::runtime_error("hermite_eval: The index n must be a nonnegative integer"); if (numn.is_zero()) return _ex1; // apply the formula from http://oeis.org/A060821 // T(n, k) = ((-1)^((n-k)/2))*(2^k)*n!/(k!*((n-k)/2)!) if n-k is even and >=0, else 0. // sequentially for all viable k. Effectively there is the recurrence // T(n, k) = -(k+2)*(k+1)/(2*(n-k)) * T(n, k+2), with T(n, n) = 2^n numeric coeff = _num2_p->power(numn); ex sum = _ex0; int fac = 1; while (numn >= 0) { sum = sum + power(x, numn) * coeff; coeff /= *_num_4_p; coeff *= numn; --numn; coeff *= numn; --numn; coeff /= fac++; } return sum; } static ex hermite_deriv(const ex& n, const ex & x, unsigned deriv_param) { if (deriv_param == 0) throw std::runtime_error("derivative w.r.t. to the index is not supported yet"); return _ex2 * n * hermite(n-1, x).hold(); } REGISTER_FUNCTION(hermite, eval_func(hermite_eval). evalf_func(hermite_evalf). derivative_func(hermite_deriv). latex_name("H")); ////////// // Gegenbauer (ultraspherical) polynomials C^a_n(x) ////////// static ex gegenb_evalf(const ex& n, const ex &a, const ex& x, PyObject* parent) { if (not is_exactly_a<numeric>(x) or not is_exactly_a<numeric>(a) or not is_exactly_a<numeric>(n)) return gegenbauer(n,a,x).hold(); // see http://dlmf.nist.gov/18.5.E9 numeric numn = ex_to<numeric>(n); numeric numx = ex_to<numeric>(x); numeric numa = ex_to<numeric>(a); numeric num2a = numa * (*_num2_p); std::vector<numeric> numveca, numvecb; numveca.push_back(-numn); numveca.push_back(numn + num2a); numvecb.push_back(numa + (*_num1_2_p)); numeric factor = numn + num2a - (*_num1_p); factor = factor.binomial(numn); return factor * hypergeometric_pFq(numveca, numvecb, ((*_num1_p)-numx)/(*_num2_p), parent); } static ex gegenb_eval(const ex& n, const ex &a, const ex& x) { numeric numa = 1; if (is_exactly_a<numeric>(x)) { numeric numx = ex_to<numeric>(x); if (is_exactly_a<numeric>(n) and is_exactly_a<numeric>(a) and numx.info(info_flags::inexact)) return gegenb_evalf(n, a, x, nullptr); } if (not is_exactly_a<numeric>(n)) return gegenbauer(n, a, x).hold(); numeric numn = ex_to<numeric>(n); if (not numn.info(info_flags::integer) or numn < 0) throw std::runtime_error("gegenb_eval: The index n must be a nonnegative integer"); if (numn.is_zero()) return _ex1; if (numn.is_equal(*_num1_p)) return _ex2 * a * x; if (is_exactly_a<numeric>(a)) { numa = ex_to<numeric>(a); if (numa.is_zero()) return _ex0; // C_n^0(x) = 0 } if (not is_exactly_a<numeric>(a) or numa < 0) { ex p = _ex1; ex sum = _ex0; ex sign = _ex1; ex aa = _ex1; unsigned long nn = numn.to_long(); sign = - 1; for (unsigned long k=0; k<=nn/2; k++) { sign *= - 1; aa = a; p = 1; // rising factorial (a)_{n-k} for (unsigned long i=0; i<nn-k; i++) { p *= aa; aa = aa + 1; } p /= numeric(k).factorial(); p /= numeric(nn-2*k).factorial(); sum += pow(2*x, nn-2*k) * p * sign; } return sum; } // from here on see flint2/fmpq_poly/gegenbauer_c.c numeric numer = numa.numer(); numeric denom = numa.denom(); numeric t = numn.factorial(); numeric overall_denom = pow(denom, numn) * t; unsigned long nn = numn.to_long(); numeric p = t / (numeric(nn/2).factorial()); if ((nn%2) != 0u) p *= *_num2_p; if ((nn&2) != 0u) p = -p; for (unsigned long k=0; k < nn-nn/2; k++) { p *= numer; numer += denom; } p *= denom.power(nn/2); ex sum = _ex0; if ((nn%2) != 0u) sum += x*p; else sum += p; for (long k=nn/2-1; k>=0; --k) { p *= numer; p *= 4*(k+1); p *= *_num_1_p; p /= denom; p /= nn-2*k-1; p /= nn-2*k; sum += pow(x, nn-2*k) * p; numer += denom; } return sum / overall_denom; } static ex gegenb_deriv(const ex& n, const ex & a, const ex & x, unsigned deriv_param) { if (deriv_param == 0) throw std::runtime_error("derivative w.r.t. to the index is not supported yet"); if (deriv_param == 1) throw std::runtime_error("derivative w.r.t. to the second index is not supported yet"); return _ex2 * a * gegenbauer(n-1, a+1, x).hold(); } REGISTER_FUNCTION(gegenbauer, eval_func(gegenb_eval). evalf_func(gegenb_evalf). derivative_func(gegenb_deriv). latex_name("C")); } // namespace GiNaC
static ex exp_imag_part(const ex & x) { return exp(GiNaC::real_part(x))*sin(GiNaC::imag_part(x)); } static ex exp_conjugate(const ex & x) { // conjugate(exp(x))==exp(conjugate(x)) return exp(x.conjugate()); } REGISTER_FUNCTION(exp, eval_func(exp_eval). evalf_func(exp_evalf). expand_func(exp_expand). derivative_func(exp_deriv). real_part_func(exp_real_part). imag_part_func(exp_imag_part). conjugate_func(exp_conjugate). latex_name("\\exp")) ////////// // natural logarithm ////////// static ex log_evalf(const ex & x) { if (is_exactly_a<numeric>(x)) return log(ex_to<numeric>(x)); return log(x).hold(); }
case info_flags::negint: case info_flags::nonnegint: case info_flags::has_indices: return arg.info(inf); } return false; } static bool conjugate_info(const ex & arg, unsigned inf) { return func_arg_info(arg, inf); } REGISTER_FUNCTION(conjugate_function, eval_func(conjugate_eval). evalf_func(conjugate_evalf). expl_derivative_func(conjugate_expl_derivative). info_func(conjugate_info). print_func<print_latex>(conjugate_print_latex). conjugate_func(conjugate_conjugate). real_part_func(conjugate_real_part). imag_part_func(conjugate_imag_part). set_name("conjugate","conjugate")) ////////// // real part ////////// static ex real_part_evalf(const ex & arg) { if (is_exactly_a<numeric>(arg)) { return ex_to<numeric>(arg).real();
/* ** ** void setup_spop(EVOLDATA ev, MATRIX fSearchPop, VECTOR fRefProbDist, ** MATRIX fRefPop, MATRIX fLC, MATRIX fDC) ** ** Sets up the search population by performing the initial evaluations. ** ** Assumes that ref. population is already setup and SORTED ** */ void setup_spop(EVOLDATA ev, MATRIX fSearchPop, VECTOR fRefProbDist, MATRIX fRefPop, MATRIX fLC, MATRIX fDC) { int i, j, k, iSelIndex; float fRandVal; float fTempVec[MAXVECTOR]; int iFlag; for (i=1; i<=ev.iSearchPopSize; i++) { eval_func(fSearchPop[i], ev.iNumTestCase, ev); /* simple evaluation */ /* now, pick a reference vector at RANDOM (0) or ORDERED (1) */ if (ev.iSelRefPoint == RANDOM) iSelIndex = (int)randgen(1.0, (float)ev.iRefPopSize); else { fRandVal = randgen(0.0, 1.0); k=1; while (fRandVal >= fRefProbDist[k]) k++; if (k >= ev.iRefPopSize) k = 1; iSelIndex = k; } /* move from search to reference */ k = 0; iFlag = FALSE; fRandVal = 0.0; if (ev.iRepairMethod == RANDOM) do { fRandVal = randgen(0.0, 1.0); for (j=1; j<=ev.iNumVars; j++) fTempVec[j] = fRandVal*fSearchPop[i][j] + (1-fRandVal)*fRefPop[iSelIndex][j]; iFlag = check_all(ev, fTempVec, fLC, fDC); k++; } while ((iFlag == FALSE) && (k < 100)); else if (ev.iRepairMethod == DETERMINISTIC) do { fRandVal = 1.0/power(2.0, k); for (j=1; j<=ev.iNumVars; j++) fTempVec[j] = fRandVal*fSearchPop[i][j] + (1-fRandVal)*fRefPop[iSelIndex][j]; iFlag = check_all(ev, fTempVec, fLC, fDC); k++; } while ((iFlag == FALSE) && (k < 20)); /* if no feasible Z, Z = Y */ if (iFlag == FALSE) for (j=1; j<=ev.iNumVars; j++) fTempVec[j] = fRefPop[iSelIndex][j]; /* now, evaluate Z */ eval_func(fTempVec, ev.iNumTestCase, ev); /* if Z better than Y, replace Y by Z */ switch(ev.iObjFnType) { case MAX: if (fTempVec[0] > fRefPop[iSelIndex][0]) for (j=0; j<=ev.iNumVars; j++) fRefPop[iSelIndex][j] = fTempVec[j]; break; case MIN: if (fTempVec[0] < fRefPop[iSelIndex][0]) for (j=0; j<=ev.iNumVars; j++) fRefPop[iSelIndex][j] = fTempVec[j]; break; default: break; } /* eval(X) = eval(Z) */ fSearchPop[i][0] = fTempVec[0]; /* if probability allows, replace X by Z as well */ fRandVal = randgen(0.0, 1.0); if (fRandVal < ev.fReplRatio) for (j=0; j<=ev.iNumVars; j++) fSearchPop[i][j] = fTempVec[j]; } }