void creature_t::step_toward(int px, int py, creature_t* exclude) { TCODMap* map = new TCODMap(current_dungeon->width, current_dungeon->height); for (int y = 0; y < current_dungeon->height; y++) { for (int x = 0; x < current_dungeon->width; x++) { tile_t* tile = get_tile_at(current_dungeon, x, y); bool walkable = tile_is_walkable_by(*tile, this); if (flag & CF_INTELLIGENT) { // Intelligent monsters know how to open doors. if (tile->id == TILE_DOOR_CLOSED) { walkable = true; } } map->setProperties(x, y, tile->properties & TILE_PROP_TRANSPARENT, walkable); } } for (size_t i = 0; i < current_dungeon->creatures.size(); i++) { creature_t* c = current_dungeon->creatures.at(i); if (c == this || c == exclude) continue; if (sees(c)) { // If this creature can see another creature, then consider that // square as unwalkable. map->setProperties(c->pos.x, c->pos.y, 1, 0); } } TCODPath* path = new TCODPath(map, 1.0f); if (path->compute(pos.x, pos.y, px, py)) { if (path->size() > 0) { int move_x, move_y; path->get(0, &move_x, &move_y); try_move(move_x, move_y); } else { // No path found, walk around confused. random_walk(); } } else { // No path found, walk around confused. random_walk(); } delete map; delete path; }
// Random walking of matrix of position created in populate // Step 1: Move particles via 6D random walk // Step 2: Destroy and create particles as need based on Anderson // Step 3: check energy, end if needed. void diffuse(h3plus& state, std::ostream& output, std::ostream& output2){ // For now, I am going to set a definite number of timesteps // This will be replaced by a while loop in the future. // double diff = 1.0; // while (diff > 0.01) { for (size_t t = 0; t < 200; t++){ double v_last = state.v_ref; random_walk(state); branch(state); state.t += state.dt; // diff = sqrt((v_last - state.v_ref) * (v_last - state.v_ref)); // Debug information for the current timestep std::cout << std::fixed << state.energy << '\t' << state.particles.size() << '\t' << state.dt << '\n'; if (t % 1 == 0) { print_visualization_data(output, state, output2); } state.dt = TIMESTEP * exp(-(t / 200.0)) + 0.001; } }
void random_walk_all( float *turtles, int num_turtles, float magnitude, float half_w, float half_h, float degrees ) { int i; for (i = 0; i < num_turtles; ++i) { random_walk(turtles + (i * TURTLE_DATA_SIZE), magnitude, half_w, half_h, degrees); } }
void handler_ctrl(void) { // runs at 1KHz static uint16 t = 0; t++; // if (t < 512) { // pwmWrite(X_OUT_P,tri(t)); // } else { // pwmWrite(Y_OUT_P,tri(t)); // } //square(t); random_walk(); static uint16 t2 = 0; if ((t%32 == 0)) { t = 0; t2++;; random_im(); } draw_point(); // pwmWrite(LASER,t/256); }
// [[register]] SEXP mcmcbas(SEXP Y, SEXP X, SEXP Rweights, SEXP Rprobinit, SEXP Rmodeldim, SEXP incint, SEXP Ralpha,SEXP method, SEXP modelprior, SEXP Rupdate, SEXP Rbestmodel, SEXP plocal, SEXP BURNIN_Iterations, SEXP MCMC_Iterations, SEXP LAMBDA, SEXP DELTA, SEXP Rparents) { SEXP RXwork = PROTECT(duplicate(X)), RYwork = PROTECT(duplicate(Y)); int nProtected = 2, nUnique=0, newmodel=0; int nModels=LENGTH(Rmodeldim); // Rprintf("Allocating Space for %d Models\n", nModels) ; SEXP ANS = PROTECT(allocVector(VECSXP, 15)); ++nProtected; SEXP ANS_names = PROTECT(allocVector(STRSXP, 15)); ++nProtected; SEXP Rprobs = PROTECT(duplicate(Rprobinit)); ++nProtected; SEXP MCMCprobs= PROTECT(duplicate(Rprobinit)); ++nProtected; SEXP R2 = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP shrinkage = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP modelspace = PROTECT(allocVector(VECSXP, nModels)); ++nProtected; SEXP modeldim = PROTECT(duplicate(Rmodeldim)); ++nProtected; SEXP counts = PROTECT(duplicate(Rmodeldim)); ++nProtected; SEXP beta = PROTECT(allocVector(VECSXP, nModels)); ++nProtected; SEXP se = PROTECT(allocVector(VECSXP, nModels)); ++nProtected; SEXP mse = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP modelprobs = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP priorprobs = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP logmarg = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP sampleprobs = PROTECT(allocVector(REALSXP, nModels)); ++nProtected; SEXP NumUnique = PROTECT(allocVector(INTSXP, 1)); ++nProtected; SEXP Rse_m = NULL, Rcoef_m = NULL, Rmodel_m; double *Xwork, *Ywork, *wts, *coefficients,*probs, shrinkage_m, *MCMC_probs, SSY, yty, mse_m, *se_m, MH=0.0, prior_m=1.0, *real_model, R2_m, RSquareFull, alpha, prone, denom, logmargy, postold, postnew; int nobs, p, k, i, j, m, n, l, pmodel, pmodel_old, *xdims, *model_m, *bestmodel, *varin, *varout; int mcurrent, update, n_sure; double mod, rem, problocal, *pigamma, eps, *hyper_parameters; double *XtX, *XtY, *XtXwork, *XtYwork, *SSgam, *Cov, *priorCov, *marg_probs; double lambda, delta, one=1.0; int inc=1; int *model, *modelold, bit, *modelwork, old_loc, new_loc; // char uplo[] = "U", trans[]="T"; struct Var *vars; /* Info about the model variables. */ NODEPTR tree, branch; /* get dimsensions of all variables */ nobs = LENGTH(Y); xdims = INTEGER(getAttrib(X,R_DimSymbol)); p = xdims[1]; k = LENGTH(modelprobs); update = INTEGER(Rupdate)[0]; lambda=REAL(LAMBDA)[0]; delta = REAL(DELTA)[0]; // Rprintf("delta %f lambda %f", delta, lambda); eps = DBL_EPSILON; problocal = REAL(plocal)[0]; // Rprintf("Update %i and prob.switch %f\n", update, problocal); /* Extract prior on models */ hyper_parameters = REAL(getListElement(modelprior,"hyper.parameters")); /* Rprintf("n %d p %d \n", nobs, p); */ Ywork = REAL(RYwork); Xwork = REAL(RXwork); wts = REAL(Rweights); /* Allocate other variables. */ PrecomputeData(Xwork, Ywork, wts, &XtXwork, &XtYwork, &XtX, &XtY, &yty, &SSY, p, nobs); alpha = REAL(Ralpha)[0]; vars = (struct Var *) R_alloc(p, sizeof(struct Var)); probs = REAL(Rprobs); n = sortvars(vars, probs, p); for (i =n; i <p; i++) REAL(MCMCprobs)[vars[i].index] = probs[vars[i].index]; for (i =0; i <n; i++) REAL(MCMCprobs)[vars[i].index] = 0.0; MCMC_probs = REAL(MCMCprobs); pigamma = vecalloc(p); real_model = vecalloc(n); marg_probs = vecalloc(n); modelold = ivecalloc(p); model = ivecalloc(p); modelwork= ivecalloc(p); varin= ivecalloc(p); varout= ivecalloc(p); /* create gamma gamma' matrix */ SSgam = (double *) R_alloc(n * n, sizeof(double)); Cov = (double *) R_alloc(n * n, sizeof(double)); priorCov = (double *) R_alloc(n * n, sizeof(double)); for (j=0; j < n; j++) { for (i = 0; i < n; i++) { SSgam[j*n + i] = 0.0; Cov[j*n + i] = 0.0; priorCov[j*n + i] = 0.0; if (j == i) priorCov[j*n + i] = lambda; } marg_probs[i] = 0.0; } RSquareFull = CalculateRSquareFull(XtY, XtX, XtXwork, XtYwork, Rcoef_m, Rse_m, p, nobs, yty, SSY); /* fill in the sure things */ for (i = n, n_sure = 0; i < p; i++) { model[vars[i].index] = (int) vars[i].prob; if (model[vars[i].index] == 1) ++n_sure; } GetRNGstate(); tree = make_node(-1.0); /* Rprintf("For m=0, Initialize Tree with initial Model\n"); */ m = 0; bestmodel = INTEGER(Rbestmodel); INTEGER(modeldim)[m] = n_sure; /* Rprintf("Create Tree\n"); */ branch = tree; for (i = 0; i< n; i++) { bit = bestmodel[vars[i].index]; if (bit == 1) { if (i < n-1 && branch->one == NULL) branch->one = make_node(-1.0); if (i == n-1 && branch->one == NULL) branch->one = make_node(0.0); branch = branch->one; } else { if (i < n-1 && branch->zero == NULL) branch->zero = make_node(-1.0); if (i == n-1 && branch->zero == NULL) branch->zero = make_node(0.0); branch = branch->zero; } model[vars[i].index] = bit; INTEGER(modeldim)[m] += bit; branch->where = 0; } /* Rprintf("Now get model specific calculations \n"); */ pmodel = INTEGER(modeldim)[m]; PROTECT(Rmodel_m = allocVector(INTSXP,pmodel)); model_m = INTEGER(Rmodel_m); for (j = 0, l=0; j < p; j++) { if (model[j] == 1) { model_m[l] = j; l +=1;} } SET_ELEMENT(modelspace, m, Rmodel_m); Rcoef_m = NEW_NUMERIC(pmodel); PROTECT(Rcoef_m); Rse_m = NEW_NUMERIC(pmodel); PROTECT(Rse_m); coefficients = REAL(Rcoef_m); se_m = REAL(Rse_m); for (j=0, l=0; j < pmodel; j++) { XtYwork[j] = XtY[model_m[j]]; for ( i = 0; i < pmodel; i++) { XtXwork[j*pmodel + i] = XtX[model_m[j]*p + model_m[i]]; } } R2_m = 0.0; mse_m = yty; memcpy(coefficients, XtYwork, sizeof(double)*pmodel); cholreg(XtYwork, XtXwork, coefficients, se_m, &mse_m, pmodel, nobs); if (pmodel > 1) R2_m = 1.0 - (mse_m * (double) ( nobs - pmodel))/SSY; SET_ELEMENT(beta, m, Rcoef_m); SET_ELEMENT(se, m, Rse_m); REAL(R2)[m] = R2_m; REAL(mse)[m] = mse_m; gexpectations(p, pmodel, nobs, R2_m, alpha, INTEGER(method)[0], RSquareFull, SSY, &logmargy, &shrinkage_m); REAL(sampleprobs)[m] = 1.0; REAL(logmarg)[m] = logmargy; REAL(shrinkage)[m] = shrinkage_m; prior_m = compute_prior_probs(model,pmodel,p, modelprior); REAL(priorprobs)[m] = prior_m; UNPROTECT(3); old_loc = 0; pmodel_old = pmodel; nUnique=1; INTEGER(counts)[0] = 0; postold = REAL(logmarg)[m] + log(REAL(priorprobs)[m]); memcpy(modelold, model, sizeof(int)*p); /* Rprintf("model %d max logmarg %lf\n", m, REAL(logmarg)[m]); */ /* Rprintf("Now Sample the Rest of the Models \n"); */ m = 0; while (nUnique < k && m < INTEGER(BURNIN_Iterations)[0]) { memcpy(model, modelold, sizeof(int)*p); pmodel = n_sure; MH = 1.0; if (pmodel_old == n_sure || pmodel_old == n_sure + n){ MH = random_walk(model, vars, n); MH = 1.0 - problocal; } else { if (unif_rand() < problocal) { // random MH = random_switch(model, vars, n, pmodel_old, varin, varout ); } else { // Randomw walk proposal flip bit// MH = random_walk(model, vars, n); } } branch = tree; newmodel= 0; for (i = 0; i< n; i++) { bit = model[vars[i].index]; if (bit == 1) { if (branch->one != NULL) branch = branch->one; else newmodel = 1; } else { if (branch->zero != NULL) branch = branch->zero; else newmodel = 1.0; } pmodel += bit; } if (pmodel == n_sure || pmodel == n + n_sure) MH = 1.0/(1.0 - problocal); if (newmodel == 1) { new_loc = nUnique; PROTECT(Rmodel_m = allocVector(INTSXP,pmodel)); model_m = INTEGER(Rmodel_m); for (j = 0, l=0; j < p; j++) { if (model[j] == 1) { model_m[l] = j; l +=1;} } Rcoef_m = NEW_NUMERIC(pmodel); PROTECT(Rcoef_m); Rse_m = NEW_NUMERIC(pmodel); PROTECT(Rse_m); coefficients = REAL(Rcoef_m); se_m = REAL(Rse_m); for (j=0, l=0; j < pmodel; j++) { XtYwork[j] = XtY[model_m[j]]; for ( i = 0; i < pmodel; i++) { XtXwork[j*pmodel + i] = XtX[model_m[j]*p + model_m[i]]; } } R2_m = 0.0; mse_m = yty; memcpy(coefficients, XtYwork, sizeof(double)*pmodel); cholreg(XtYwork, XtXwork, coefficients, se_m, &mse_m, pmodel, nobs); if (pmodel > 1) R2_m = 1.0 - (mse_m * (double) ( nobs - pmodel))/SSY; prior_m = compute_prior_probs(model,pmodel,p, modelprior); gexpectations(p, pmodel, nobs, R2_m, alpha, INTEGER(method)[0], RSquareFull, SSY, &logmargy, &shrinkage_m); postnew = logmargy + log(prior_m); } else { new_loc = branch->where; postnew = REAL(logmarg)[new_loc] + log(REAL(priorprobs)[new_loc]); } MH *= exp(postnew - postold); // Rprintf("MH new %lf old %lf\n", postnew, postold); if (unif_rand() < MH) { if (newmodel == 1) { new_loc = nUnique; insert_model_tree(tree, vars, n, model, nUnique); INTEGER(modeldim)[nUnique] = pmodel; SET_ELEMENT(modelspace, nUnique, Rmodel_m); SET_ELEMENT(beta, nUnique, Rcoef_m); SET_ELEMENT(se, nUnique, Rse_m); REAL(R2)[nUnique] = R2_m; REAL(mse)[nUnique] = mse_m; REAL(sampleprobs)[nUnique] = 1.0; REAL(logmarg)[nUnique] = logmargy; REAL(shrinkage)[nUnique] = shrinkage_m; REAL(priorprobs)[nUnique] = prior_m; UNPROTECT(3); ++nUnique; } old_loc = new_loc; postold = postnew; pmodel_old = pmodel; memcpy(modelold, model, sizeof(int)*p); } else { if (newmodel == 1) UNPROTECT(3); } INTEGER(counts)[old_loc] += 1; for (i = 0; i < n; i++) { /* store in opposite order so nth variable is first */ real_model[n-1-i] = (double) modelold[vars[i].index]; REAL(MCMCprobs)[vars[i].index] += (double) modelold[vars[i].index]; } // Update SSgam = gamma gamma^T + SSgam F77_NAME(dsyr)("U", &n, &one, &real_model[0], &inc, &SSgam[0], &n); m++; } for (i = 0; i < n; i++) { REAL(MCMCprobs)[vars[i].index] /= (double) m; } // Rprintf("\n%d \n", nUnique); // Compute marginal probabilities mcurrent = nUnique; compute_modelprobs(modelprobs, logmarg, priorprobs,mcurrent); compute_margprobs(modelspace, modeldim, modelprobs, probs, mcurrent, p); // Now sample W/O Replacement // Rprintf("NumUnique Models Accepted %d \n", nUnique); INTEGER(NumUnique)[0] = nUnique; if (nUnique < k) { update_probs(probs, vars, mcurrent, k, p); update_tree(modelspace, tree, modeldim, vars, k,p,n,mcurrent, modelwork); for (m = nUnique; m < k; m++) { for (i = n; i < p; i++) { INTEGER(modeldim)[m] += model[vars[i].index]; } branch = tree; for (i = 0; i< n; i++) { pigamma[i] = 1.0; bit = withprob(branch->prob); /* branch->done += 1; */ if (bit == 1) { for (j=0; j<=i; j++) pigamma[j] *= branch->prob; if (i < n-1 && branch->one == NULL) branch->one = make_node(vars[i+1].prob); if (i == n-1 && branch->one == NULL) branch->one = make_node(0.0); branch = branch->one; } else { for (j=0; j<=i; j++) pigamma[j] *= (1.0 - branch->prob); if (i < n-1 && branch->zero == NULL) branch->zero = make_node(vars[i+1].prob); if (i == n-1 && branch->zero == NULL) branch->zero = make_node(0.0); branch = branch->zero; } model[vars[i].index] = bit; INTEGER(modeldim)[m] += bit; } REAL(sampleprobs)[m] = pigamma[0]; pmodel = INTEGER(modeldim)[m]; /* Now subtract off the visited probability mass. */ branch=tree; for (i = 0; i < n; i++) { bit = model[vars[i].index]; prone = branch->prob; if (bit == 1) prone -= pigamma[i]; denom = 1.0 - pigamma[i]; if (denom <= 0.0) { if (denom < 0.0) { warning("neg denominator %le %le %le !!!\n", pigamma, denom, prone); if (branch->prob < 0.0 && branch->prob < 1.0) warning("non extreme %le\n", branch->prob);} denom = 0.0;} else { if (prone <= 0) prone = 0.0; if (prone > denom) { if (prone <= eps) prone = 0.0; else prone = 1.0; /* Rprintf("prone > 1 %le %le %le %le !!!\n", pigamma, denom, prone, eps);*/ } else prone = prone/denom; } if (prone > 1.0 || prone < 0.0) Rprintf("%d %d Probability > 1!!! %le %le %le %le \n", m, i, prone, branch->prob, denom, pigamma); /* if (bit == 1) pigamma /= (branch->prob); else pigamma /= (1.0 - branch->prob); if (pigamma > 1.0) pigamma = 1.0; */ branch->prob = prone; if (bit == 1) branch = branch->one; else branch = branch->zero; /* Rprintf("%d %d \n", branch->done, n - i); */ /* if (log((double) branch->done) < (n - i)*log(2.0)) { if (bit == 1) branch = branch->one; else branch = branch->zero; } else { branch->one = NULL; branch->zero = NULL; break; } */ } /* Now get model specific calculations */ PROTECT(Rmodel_m = allocVector(INTSXP, pmodel)); model_m = INTEGER(Rmodel_m); for (j = 0, l=0; j < p; j++) { if (model[j] == 1) { model_m[l] = j; l +=1;} } SET_ELEMENT(modelspace, m, Rmodel_m); for (j=0, l=0; j < pmodel; j++) { XtYwork[j] = XtY[model_m[j]]; for ( i = 0; i < pmodel; i++) { XtXwork[j*pmodel + i] = XtX[model_m[j]*p + model_m[i]]; } } PROTECT(Rcoef_m = allocVector(REALSXP,pmodel)); PROTECT(Rse_m = allocVector(REALSXP,pmodel)); coefficients = REAL(Rcoef_m); se_m = REAL(Rse_m); mse_m = yty; memcpy(coefficients, XtYwork, sizeof(double)*pmodel); cholreg(XtYwork, XtXwork, coefficients, se_m, &mse_m, pmodel, nobs); /* olsreg(Ywork, Xwork, coefficients, se_m, &mse_m, &pmodel, &nobs, pivot,qraux,work,residuals,effects,v,betaols); */ if (pmodel > 1) R2_m = 1.0 - (mse_m * (double) ( nobs - pmodel))/SSY; SET_ELEMENT(beta, m, Rcoef_m); SET_ELEMENT(se, m, Rse_m); REAL(R2)[m] = R2_m; REAL(mse)[m] = mse_m; gexpectations(p, pmodel, nobs, R2_m, alpha, INTEGER(method)[0], RSquareFull, SSY, &logmargy, &shrinkage_m); REAL(logmarg)[m] = logmargy; REAL(shrinkage)[m] = shrinkage_m; REAL(priorprobs)[m] = compute_prior_probs(model,pmodel,p, modelprior); if (m > 1) { rem = modf((double) m/(double) update, &mod); if (rem == 0.0) { mcurrent = m; compute_modelprobs(modelprobs, logmarg, priorprobs,mcurrent); compute_margprobs(modelspace, modeldim, modelprobs, probs, mcurrent, p); if (update_probs(probs, vars, mcurrent, k, p) == 1) { // Rprintf("Updating Model Tree %d \n", m); update_tree(modelspace, tree, modeldim, vars, k,p,n,mcurrent, modelwork); } }} UNPROTECT(3); } } compute_modelprobs(modelprobs, logmarg, priorprobs,k); compute_margprobs(modelspace, modeldim, modelprobs, probs, k, p); SET_VECTOR_ELT(ANS, 0, Rprobs); SET_STRING_ELT(ANS_names, 0, mkChar("probne0")); SET_VECTOR_ELT(ANS, 1, modelspace); SET_STRING_ELT(ANS_names, 1, mkChar("which")); SET_VECTOR_ELT(ANS, 2, logmarg); SET_STRING_ELT(ANS_names, 2, mkChar("logmarg")); SET_VECTOR_ELT(ANS, 3, modelprobs); SET_STRING_ELT(ANS_names, 3, mkChar("postprobs")); SET_VECTOR_ELT(ANS, 4, priorprobs); SET_STRING_ELT(ANS_names, 4, mkChar("priorprobs")); SET_VECTOR_ELT(ANS, 5,sampleprobs); SET_STRING_ELT(ANS_names, 5, mkChar("sampleprobs")); SET_VECTOR_ELT(ANS, 6, mse); SET_STRING_ELT(ANS_names, 6, mkChar("mse")); SET_VECTOR_ELT(ANS, 7, beta); SET_STRING_ELT(ANS_names, 7, mkChar("mle")); SET_VECTOR_ELT(ANS, 8, se); SET_STRING_ELT(ANS_names, 8, mkChar("mle.se")); SET_VECTOR_ELT(ANS, 9, shrinkage); SET_STRING_ELT(ANS_names, 9, mkChar("shrinkage")); SET_VECTOR_ELT(ANS, 10, modeldim); SET_STRING_ELT(ANS_names, 10, mkChar("size")); SET_VECTOR_ELT(ANS, 11, R2); SET_STRING_ELT(ANS_names, 11, mkChar("R2")); SET_VECTOR_ELT(ANS, 12, counts); SET_STRING_ELT(ANS_names, 12, mkChar("freq")); SET_VECTOR_ELT(ANS, 13, MCMCprobs); SET_STRING_ELT(ANS_names, 13, mkChar("probs.MCMC")); SET_VECTOR_ELT(ANS, 14, NumUnique); SET_STRING_ELT(ANS_names, 14, mkChar("n.Unique")); setAttrib(ANS, R_NamesSymbol, ANS_names); UNPROTECT(nProtected); // Rprintf("Return\n"); PutRNGstate(); return(ANS); }
/*-------------------------------------------------------------------*/ void loop() { random_walk(); broadcast(); color_code(); }
/* Launches one of those fancy effects. */ void launch_effect(int effect) { switch (effect) { case 0: /* Lights all the layers one by one. */ load_bar(1000); break; case 1: /* A pixel bouncing randomly around. */ boing_boing(150, 500, 0x03, 0x01); break; case 2: /* Randomly fill and empty the cube. */ fill(0x00); random_filler(100, 1, 500, 1); random_filler(100, 1, 500, 0); break; case 3: /* Send voxels randomly back and forth the z-axis. */ send_voxels_rand_z(150, 500, 2000); break; case 4: /* Spinning spiral */ spiral(1, 75, 1000); break; case 5: /* A coordinate bounces randomly around the cube. For every position * the status of that voxel is toggled. */ boing_boing(150, 500, 0x03, 0x02); break; case 6: /* Random raindrops */ rain(40, 1000, 500, 500); break; case 7: /* Snake: a snake randomly bounce around the cube. */ boing_boing(150, 500, 0x03, 0x03); break; case 8: /* Spinning plane */ spinning_plane(1, 50, 1000); break; case 9: /* Set x number of random voxels, delay, unset them. x increases * from 1 to 32 and back to 1. */ random_2(48); break; case 10: /* Set then unset all 64 voxels in a random order. */ random_filler2(200, 1); delay_ms(2000); random_filler2(200, 0); delay_ms(1000); break; case 11: /* Bounce a plane up and down all the directions. */ fly_plane('z', 1, 1000); delay_ms(2000); fly_plane('y', 1, 1000); delay_ms(2000); fly_plane('x', 1, 1000); delay_ms(2000); fly_plane('z', 0, 1000); delay_ms(2000); fly_plane('y', 0, 1000); delay_ms(2000); fly_plane('x', 0, 1000); delay_ms(2000); break; case 12: /* Fade in and out at low framerate. */ blinky2(); break; case 13: /* Random walk */ random_walk(4, 250, 1000); break; case 14: /* Spinning square */ spinning_square(1, 50, 1000); break; } }
std::pair<ptrobot2D_test_world::point_type, bool> ptrobot2D_test_world::random_back_walk(const ptrobot2D_test_world::point_type& p_u) const { return random_walk(p_u); };
int main() { GraphTraits::out_edge_iterator out_i, out_end; GraphTraits::edge_descriptor e; boost::graph_traits<Graph>::adjacency_iterator ai; boost::graph_traits<Graph>::adjacency_iterator ai_end; boost::minstd_rand gen; std::map<uint64_t, uint32_t> m; std::map<int_type, int_type> id_map; size_t count = 0; uint64_t *v_array = (uint64_t *)malloc(sizeof(uint64_t)*(n_count + n_count*3)); // Create graph with 100 nodes and edges with probability 0.05 double prob = n_count*n_count; prob = n_count*3/prob; Graph g(ERGen(gen, n_count, prob), ERGen(), n_count); std::cout << "done generating\n\n"; IndexMap index = get(boost::vertex_index, g); cow_trie_p my_map = cow_trie_alloc(0, 0); my_map->ref_count = 1; my_map->child_bitmap = 0; my_map->value_bitmap = 0; uint32_t id = 0; std::cout << "beginning vertex iteration\n\n"; std::pair<vertex_iter, vertex_iter> vp; for (vp = vertices(g); vp.first != vp.second; ++vp.first) { Vertex v = *vp.first; boost::tie(out_i, out_end) = out_edges(v, g); uint32_t deg = out_degree(v, g); uint32_t *succs = new uint32_t[deg]; val_t my_val = cow_trie_create_val(0, deg, succs, 0, 0); int_type my_id = cow_trie_insert(my_map, my_val, &my_map); id_map[id] = my_id; ++id; } std::cout << "vertex iteration done, beginning edge iteration\n\n"; boost::graph_traits<Graph>::edge_iterator ei, ei_end; boost::tie(ei, ei_end) = edges(g); std::cout << "id before is" << id << "\n\n"; for (ei; ei != ei_end; ++ei) { val_t my_val = cow_trie_create_val(1, 0, 0, id_map[index[source(*ei, g)]], id_map[index[target(*ei, g)]]); int_type my_id = cow_trie_insert(my_map, my_val, &my_map); m[index[source(*ei, g)] << 32 | index[target(*ei, g)]] = id; ++id; } std::cout << "id is\n\n" << id << "\n\n"; std::cout << "done edge iterating\n\n"; std::cout << "putting in successors\n\n"; int p = 0; for (vp = vertices(g); vp.first != vp.second; ++vp.first) { Vertex v = *vp.first; val_t my_val; cow_trie_lookup(my_map, p, &my_val); uint32_t *succs = edge_list_values(my_val._.node.succs); boost::tie(out_i, out_end) = out_edges(v, g); int eindex = 0; for (out_i; out_i != out_end; ++out_i) { e = *out_i; Vertex src = source(e, g), targ = target(e, g); uint32_t id = m[index[src] << 32 | index[targ]]; succs[eindex] = id_map[id]; ++eindex; } ++p; } val_t v; cow_trie_lookup(my_map, 0, &v); Vertex vtest = *vertices(g).first; std::cout << "beginning random traversal\n\n"; float t1 = random_walk(vtest, index, g); printf("time for theirs %f", t1); float t2 = random_walk_c(v, my_map); printf("time for ours %f", t2); std::cout << "end random traversal\n\n"; }
void creature_t::update() { if (flag & CF_SPELLCASTER) { if (spell_timer > 0) spell_timer--; } if (get_active_effects_for(this) & EF_CONFUSE) { random_walk(); } else if (identity == IDENT_SHOPKEEPER) { update_shopkeeper(this); } else if (flag & CF_NEUTRAL) { random_walk(); } else if ((flag & CF_HIDES) && (!(get_active_effects_for(this) & EF_HIDDEN)) && !player.sees(this)) { hide_creature(this); } else if (flag & CF_PLAYER_ALLY) { creature_t* closest_enemy = get_closest_enemy(); if (closest_enemy && sees(closest_enemy)) { if ((flag & CF_SPELLCASTER) && spell_timer == 0) { creature_cast_spell(this, closest_enemy); } else { step_toward(closest_enemy->pos.x, closest_enemy->pos.y, closest_enemy); } } else { _regular_walk_around(this); } } else if (sees(&player)) { if (seen_player == 0) { // Shout should depend on if the creature is intelligent etc. if ((flag & CF_SPEAKS)&& !(flag & CF_SPELLCASTER)) { shout(""); } seen_player = 1; } forget_player_timer = FORGETFULNESS; if ((flag & CF_SPELLCASTER) && spell_timer == 0) { creature_cast_spell(this, &player); } else { bool move_towards = true; if (flag & CF_INTELLIGENT) { if (_try_zap_wand(this)) { move_towards = false; } } if (move_towards) { step_toward(player.pos.x, player.pos.y, nullptr); } } // Stop being interested in whatever noise it heard. heard_noise = 0; } else { // If the spell timer has reached 0 while the creature is outside player view, reset // it for a random value to make things more varied when a caster enters view. if (spell_timer == 0) { spell_timer = random(0, spell_timer_duration); } if (seen_player && forget_player_timer > 0) { forget_player_timer--; step_toward(player.pos.x, player.pos.y, nullptr); } else { seen_player = 0; if (heard_noise) { step_toward(noise_location.x, noise_location.y, nullptr); if (pos.x == noise_location.x && pos.y == noise_location.y) { // Found the position of the noise. heard_noise = 0; } } else { // If the player has allies and I can see one, attack it. creature_t* closest_enemy = get_closest_enemy(); if (closest_enemy && sees(closest_enemy)) { step_toward(closest_enemy->pos.x, closest_enemy->pos.y, closest_enemy); } else { _regular_walk_around(this); } } } } ap -= speed; }
void DropletCustomOne::DropletMainLoop() { switch ( state ) { case COLLABORATING: if ( get_32bit_time() - collab_time > COLLABORATE_DURATION ) { change_state ( LEAVING ); } break; case LEAVING: if ( !is_moving(NULL) ) { change_state ( SAFE ); } break; case SAFE: if ( check_safe () ) { change_state ( SEARCHING ); } // If you start in the red region then try to get out before you die else { random_walk (); } break; case SEARCHING: random_walk (); if ( !check_safe() ) { change_state ( WAITING ); } break; case START_DELAY: { uint32_t curr_time = get_32bit_time (); if ( curr_time - start_delay_time > START_DELAY_TIME ) change_state ( SAFE ); } break; case WAITING: if ( get_32bit_time() - heartbeat_time > HEART_RATE ) { heartbeat_time = get_32bit_time (); send_heartbeat (); } // Checks incoming messages and updates group size. // There is a chance the state can be changed to COLLABORATING in // this function if the droplet sees a GO message. update_group_size (); if ( get_32bit_time() - voting_time > HEART_RATE && state == WAITING ) { voting_time = get_32bit_time (); check_votes (); } break; default: break; } }