/* metropolis algorithm (with periodic checking) */ INLINE int cago_metro1(cago_t *go, real amp, real bet) { int i, j; rv3_t xi; real du; static int cc; i = (int) (go->n * rnd0()); for (j = 0; j < 3; j++) { xi[j] = go->x[i][j] + (rnd0() * 2.f - 1) * amp; } du = cago_depot(go, go->x, i, xi); if (du < 0 || rnd0() < exp(-bet * du)) { rv3_copy(go->x[i], xi); go->epot += du; if (++cc % 1000 == 0) { /* refresh energy */ real ep = cago_force(go, go->x, NULL); if (fabs(ep - go->epot) > 0.01) { fprintf(stderr, "energy corruptions %g vs. %g\n", go->epot, ep); } go->epot = ep; } return 1; } else { return 0; } }
/* mutate velocities by random rotation */ INLINE int md_rotv3d(rv3_t *v, int n) { int i; real mat[3][3], v1[3], axis[3], ang; rv3_normalize(rv3_rnd(axis, -.5f, 1.f)); ang = 2 * M_PI * rnd0(); /* amplitude has to be 2 * pi */ rm3_mkrot(mat, axis, ang); for (i = 0; i < n; i++) { rm3_mulvec(v1, mat, v[i]); rv3_copy(v[i], v1); } return 0; }
void randomizescrolls() { uchar numscrolls=sizeof(sname)/sizeof(sname[0]); char * tmp; uchar changeto=0; for(int i=0; i<numscrolls; i++) { changeto=rnd0(numscrolls-1); // because numscrolls is 1 based. // move destination to tmp tmp=sname[changeto]; // move current index to destination sname[changeto]=sname[i]; // move tmp to current index sname[i]=tmp; } }
objectclass *randomscroll() { struct sinfo { objectclass *(*fp)(); // Function that creates the object uchar prob; // probability of object being created. }; struct sinfo sinfo[]= { // Put all the class create() function is here that are // possible candidates for this random object generator. // The sum of all the probabilities in this list must be 1000. // Think of it as XX.X% chance of being generated. {scroll_of_enchant_armor::create,63}, {scroll_of_destroy_armor::create,45}, {scroll_of_confuse_monster::create,53}, {scroll_of_scare_monster::create,35}, {scroll_of_remove_curse::create,65}, {scroll_of_enchant_weapon::create,85}, {scroll_of_create_monster::create,45}, {scroll_of_taming::create,15}, {scroll_of_genocide::create,15}, {scroll_of_light::create,95}, {scroll_of_teleportation::create,55}, {scroll_of_gold_detection::create,33}, {scroll_of_food_detection::create,25}, {scroll_of_identify::create,185}, {scroll_of_magic_mapping::create,45}, {scroll_of_amnesia::create,35}, {scroll_of_fire::create,48}, {scroll_of_punishment::create,15}, {scroll_of_charging::create,15}, {scroll_of_blank_paper::create,28} // make sure the last one does not have a trailing comma }; sshort prob; uchar count=0; prob=rnd0(1000); while((prob -= sinfo[count].prob) > 0) count++; return (sinfo[count].fp)(); }
int runSimulator(char *ip, int port) { int bear; int knots; int mps=0; int myTime =0; int lapse=defaultLapse; long x, y, d; float lat, lon; frm_cmd_gps_t gps; char buff[TRANS_MAX_BUFF_SIZE]; size_t transLen; int seq =0; int sckt; struct sockaddr_in server; // INIT SOCKET printf("Incializando...\n"); memset(&server,0,sizeof(server)); sckt= socket(AF_INET,SOCK_DGRAM,0); if (sckt<0) { die("Error en la creacion del socket"); } server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr(ip); server.sin_port = htons(port); y = lat2y(42.22); x = lon2x(-8.65); printf("Empezando simulacion...(lapso=%ds)\n\n",lapse); while (true) { if (myTime<=0) { myTime = rnd1(30); bear = rnd0(359); mps = rnd1(MAX_MPS); } d = mps * lapse; // Ojo origen de rumbo PI y += round( d * cos(bear * DEG_RAD)); x += round( d * sin(bear * DEG_RAD)); lat = y2lat(y); lon = x2lon(x); knots = mps * MPS_TO_KNOTS; // gps.cmd = 0x11; // gps.len =0; // gps.seq_l= seq++; // gps.seq_s=0; // gps.bearing = GPS_ENCODE_BEARING(bear); // gps.knots = knots; // gps.lat_sign = lat<0?1:0; // gps.lat_deg = gps.lat_sign?-lat:lat; // gps.lat_min =htons(GPS_ENCODE_LOCMIN(lat)); // gps.lon_sign = lon<0?1:0; // gps.lon_deg = gps.lon_sign?-lon:lon; // gps.lon_min = htons(GPS_ENCODE_LOCMIN(lon)); // // gps.fix = 1; // gps.hdop = rnd1(20); // gps.time.epoch = htonl(time(NULL)); int offset=0; transLen = sizeof(buff); frame_encode_gps(seq++,bear,knots,lat,lon,1,rnd1(20),0,buff,&transLen); offset +=transLen; if (addCnxFrame) { transLen = sizeof(buff) - offset; frame_encode_cnx("1234567890",0,1,0,buff+offset,&transLen); offset +=transLen; } transLen = sizeof(buff); if ( frame_encode_transport(1,NULL,offset,buff,&transLen) ) { // if (frame_test_transport(buff,transLen)) { // printf("Trama de transporte verificada\n"); // } printf("Enviando: %d lat=%f lon=%f bear=%d knots=%d time=%d\n",seq, lat,lon,bear,knots,ntohl(gps.time.epoch)); sendto(sckt,buff,transLen,0,(struct sockaddr *)&server,sizeof(server)); } myTime -=lapse; sleep(lapse); } close(sckt); return 0; }
/* run md simulation */ static int run(void) { int t, i, id, etot = 0, eacc = 0; double r[DIM], r1[DIM], ep, ep1, dep; double x0 = 0, y0 = 0, x1 = 0, y1 = 0, dx, dy, alf = 0; ad2_t *al; hist2_t *hs; al = ad2_open(-M_PI, M_PI, M_PI/9.999, -M_PI, M_PI, M_PI/9.999, 0.001, 1.0, 0.0, 0.0); hs = hs2_opensqr1(-M_PI, M_PI, M_PI/9.999); for (i = 0; i < DIM; i++) { r[i] = (rnd0() * 2 - 1) * M_PI; } ep = force(r); id = ad2_getid(al, r[0], r[1]); for (t = 1; t <= nsteps; t++) { for (i = 0; i < DIM; i++) { r1[i] = WRAP( r[i] + (rnd0() - .5) * 1.0 ); } ep1 = force(r1); dep = ep1 - ep; etot++; if (dep <= 0 || rnd0() < exp(-dep)) { eacc++; for (i = 0; i < DIM; i++) { r[i] = r1[i]; } ep = ep1; } hs2_add1ez(hs, r[0], r[1], 0); /* register */ if (t % seglen == 0) { static const double fcmax = 1000000000.0; x1 = r[0]; y1 = r[1]; dx = DIFF(x1, x0); dy = DIFF(y1, y0); ad2_add(al, id, dx, dy); /* ad2_getcorr(al, id, &delx, &dely); dx += delx; dy += dely; */ alf = 0.1; al->fx[id] = dblconfine(al->fx[id] + dx * alf, -fcmax, fcmax); al->fy[id] = dblconfine(al->fy[id] + dy * alf, -fcmax, fcmax); /* set the new start point */ x0 = x1; y0 = y1; id = ad2_getid(al, x0, y0); if (t % nevery == 0) { printf("t %d, x %g, y %g\n", t, r[0], r[1]); } if (t % nreport == 0) { ad2_save(al, fnout); } } } printf("acc %g\n", 1.0*eacc/etot); ad2_close(al); hs2_save(hs, "xy.his", 0); return 0; }
double rnd1(double s) { return s*rnd0(); }
int runSimulator(char *ip, int port) { int bear; int knots; int mps=0; int myTime =0; int lapse=defaultLapse; long x, y, d; float lat, lon; frm_cmd_gps_t gps; char buff[TRANS_MAX_BUFF_SIZE]; size_t transLen; int seq =0; int sckt; struct sockaddr_in server; time_t timestamp; PROJ *p; PROJ_XY xy; PROJ_LP lp; // INIT SOCKET printf("Incializando...\n"); memset(&server,0,sizeof(server)); sckt= socket(AF_INET,SOCK_DGRAM,0); if (sckt<0) { die("Error en la creacion del socket"); } server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr(ip); server.sin_port = htons(port); //Projection initialization //WGS84 p = proj_initstr("proj=merc ellps=WGS84"); lp.phi = ORIG_LAT * D_TO_R; lp.lam = ORIG_LON * D_TO_R; xy = proj_fwd(lp,p); printf("Empezando simulacion...(lapso=%ds)\n\n",lapse); while (true) { if (myTime<=0) { myTime = rnd1(30); bear = rnd0(359); mps = rnd1(MAX_MPS); } d = mps * lapse; // Ojo origen de rumbo PI xy.y += round( d * cos(bear * D_TO_R)); xy.x += round( d * sin(bear * D_TO_R)); lp = proj_inv(xy,p); knots = mps * MPS_TO_KNOTS; int offset=0; transLen = sizeof(buff); lat = lp.phi* R_TO_D; lon = lp.lam * R_TO_D; timestamp = time(NULL); frame_encode_gps(seq++,bear,knots,lat,lon,1,rnd1(20),timestamp,buff,&transLen); offset +=transLen; if (addCnxFrame) { transLen = sizeof(buff) - offset; frame_encode_cnx("1234567890",0,1,0,buff+offset,&transLen); offset +=transLen; } transLen = sizeof(buff); if ( frame_encode_transport(1,NULL,offset,buff,&transLen) ) { // if (frame_test_transport(buff,transLen)) { // printf("Trama de transporte verificada\n"); // } printf("Enviando: %d lat=%f lon=%f bear=%d knots=%d time=%d\n",seq, lat,lon,bear,knots,timestamp); sendto(sckt,buff,transLen,0,(struct sockaddr *)&server,sizeof(server)); } myTime -=lapse; sleep(lapse); } proj_free(p); close(sckt); return 0; }