/* * Perform polynomial mutation of x with probability p. */ SEXP do_pm(SEXP s_x, SEXP s_lb, SEXP s_ub, SEXP s_eta, SEXP s_p) { SEXP s_res; double deltaq, rnd; #if FALSE /* Only mutate in half of all calls: */ if (flip_coin()) return s_x; #endif /* Unpack arguments: */ UNPACK_REAL_VECTOR(s_x, x, d); UNPACK_REAL_VECTOR(s_lb, lb, dlb); UNPACK_REAL_VECTOR(s_ub, ub, dub); UNPACK_REAL(s_eta, eta); UNPACK_REAL(s_p, p); const double mpow = 1.0 / (eta + 1.0); /* Sanity checks: */ if (dlb != d || dub != d) error("do_pm: Dimension of individual and bounds differ."); /* Allocate results: */ PROTECT(s_res = allocVector(REALSXP, d)); double *res = REAL(s_res); GetRNGstate(); for (int i = 0; i < d; ++i) { /* Mutate with probability p: */ if (unif_rand() < p) { const double delta = ub[i] - lb[i]; rnd = unif_rand(); if (rnd <= 0.5) { const double xy = 1.0 - (x[i] - lb[i]) / delta; deltaq = pow(2.0 * rnd + (1.0 - 2.0 * rnd) * pow(xy, eta + 1.0), mpow) - 1.0; } else { const double xy = 1.0 - (ub[i] - x[i]) / delta; deltaq = 1.0 - pow(2.0 * (1.0 - rnd) + 2.0 * (rnd - 0.5) * pow(xy, eta + 1.0), mpow); } res[i] = clip_to_limits(x[i] + deltaq * delta, lb[i], ub[i]); } else { res[i] = x[i]; } } PutRNGstate(); UNPROTECT(1); /* s_res */ return (s_res); }
void scorer(mqd_t score_q) { int rc; long msg[10000]; int exit_called = FALSE; int match_over = FALSE; player_to_serve = flip_coin(); start_play(player_to_serve); playerA_gamescore = 0; playerB_gamescore = 0; playerA_setscore = 0; playerB_setscore = 0; playerA_matchscore = 0; playerB_matchscore = 0; printf("\n\nMATCH IS BEGINNING\n\n"); fflush(stdout); fflush(stdout); sleep(sleep_time_seconds); for (;;) { rc = mq_receive(score_q, (char *) &msg, sizeof(msg), NULL); if (rc == -1) { perror("error: scorer"); exit(EXIT_FAILURE); } switch(msg[0]) { case PLAYER_A: printf("POINT player %s\n", PLAYER_B_STR); fflush(stdout); playerB_gamescore++; volley_count = 0; match_over = eval_score( PLAYER_B_STR , &playerB_gamescore , &playerB_setscore , &playerB_matchscore ); break; case PLAYER_B: printf("POINT player %s\n", PLAYER_A_STR); fflush(stdout); playerA_gamescore++; volley_count = 0; match_over = eval_score( PLAYER_A_STR , &playerA_gamescore , &playerA_setscore , &playerA_matchscore ); break; case EXIT: printf(" scorer exit received\n"); exit_called = TRUE; match_over = TRUE; break; default: break; } if (exit_called || match_over) { break; // break from the for(;;) } start_play(player_to_serve); } kill(getpid(), SIGINT); }