int get_steps(int s)
{
	if (s == 1 || s == 0)
		return 1;
	unsigned int count = get_steps(s - 1) + get_steps(s - 2);
	return count;
}
int get_steps(int s)
{
	if (s <= 2)
		return s;
	else 
		return get_steps(s - 1) + get_steps(s - 2);
}
int get_steps(int s)
{
	if (s < 0)
		return 0;
	else if (s == 0)
		return 1;
	else
		return get_steps(s - 1) + get_steps(s - 2);
}
int get_steps(int s)
{
	if (s <= 0)
		return 0;

	if (s == 1)
		return 1;
	if (s == 2)
		return 2;

	return get_steps(s - 1) + get_steps(s - 2);
}
Beispiel #5
0
/*
 * Starts running the game or continues a last run that was stopped
 *
 * quit_at_step: -1 to ignore step count
 */
void Game::run(GameObserver& observer, bool pause_at_end_of_input, int stop_at_step) {
    REQUIRE(initialised);

    bool stop_at_end_of_input = !pause_at_end_of_input;
    while (!observer.should_stop() && stop_at_step != get_steps() && !get_state().is_game_over()) {
        if (!observer.is_paused()) {
            auto input = get_input();

            if (input.empty()) {
                if (stop_at_end_of_input) {
                    break;
                }
                else {
                    stop_at_end_of_input = true;
                    observer.pause();
                }
            }
            else {
                if (act(input)) {
                    observer.finished_step(get_state());
                }
            }
        }
    }
}
ARMSIM_STATUS as_execute(ARMSIM_CTX *ctx, size_t cycles, size_t *finished) {
    if(!ctx->entry_set){
        return AS_BAD_ENTRY;
    }
    as_log(ctx, "Entering as_execute\n",0);

    int start_steps = get_steps(ctx);
    int current_steps = start_steps;
    if(cycles == 0){
        run(ctx);
    }
    else {
        for (; current_steps - start_steps < cycles && current_steps == get_steps(ctx); ++current_steps) {
            cpu_step(ctx);
        }
    }
    (*finished) = (unsigned int)(get_steps(ctx) - start_steps);
    as_log(ctx, "Leaving as_execute\n",0);
    return AS_OK;
}
void adapt(mcmc ** chains, const unsigned int n_beta, const unsigned int n_swap) {
	unsigned int i;

#ifdef RWM
	static double prob_old[100];
#endif

#ifdef RWM
	for (i = 0; i < n_beta; i++) {
		prob_old[i] = get_prob(chains[i]);
		markov_chain_step(chains[i], 0);
		rmw_adapt_stepwidth(chains[i], prob_old[i]);
	}
#endif
#ifdef ADAPT
	for (i = 0; i < n_beta; i++) {
		if (get_params_accepts_sum(chains[i]) + get_params_rejects_sum(
						chains[i]) < 20000) {
			continue;
		}
		if (get_params_accepts_sum(chains[i]) * 1.0
				/ get_params_rejects_sum(chains[i]) < TARGET_ACCEPTANCE_RATE - 0.05) {
			dump_i("too few accepts, scaling down", i);
			gsl_vector_scale(get_steps(chains[i]), 0.99);
		} else if (get_params_accepts_sum(chains[i]) * 1.0
				/ get_params_rejects_sum(chains[i]) > TARGET_ACCEPTANCE_RATE + 0.05) {
			dump_i("too many accepts, scaling up", i);
			gsl_vector_scale(get_steps(chains[i]), 1 / 0.99);
		}
		if (get_params_accepts_sum(chains[i]) + get_params_rejects_sum(
						chains[i]) > 100000) {
			reset_accept_rejects(chains[i]);
		}
	}
#endif
	/* avoid unused warnings if adapt is disabled */
	(void) chains;
	i = n_beta + n_swap;
}
int get_steps(int s)
{
	if (s == 0 ||s ==1) return 1;
	return (get_steps(s -1) + get_steps(s - 2));
}
Beispiel #9
0
// Draws one frame then returns
void run_one_frame() {
    frame_drawn = 0;

    while (!frame_drawn) {
        if (halted || stopped) {
            long current_cycles = cgb_speed ? 2 : 4;
            update_timers(current_cycles);
            sound_add_cycles(current_cycles);
            inc_serial_cycles(current_cycles);

            // If Key pressed in "stop" mode, then gameboy is "unstopped"
            if (stopped) {
                if(key_pressed()) {
                    stopped = 0;
                }
            }
            if (halted) {
                update_graphics(current_cycles);
            }
        }
        else if (!(halted || stopped)) {
            current_cycles = 0;
            current_cycles += exec_opcode(skip_bug);

        }

        cycles += current_cycles;
      
		#ifdef EFIAPI
        if (cycles > 3000) {
		#else
		if (cycles > 15000) {
		#endif
            quit |= update_keys();
            cycles = 0;
        }
        skip_bug = handle_interrupts();

        if (debug && step_count > 0 && --step_count == 0) {
            int flags = get_command();
            step_count = (flags & STEPS_SET) ? get_steps() : STEPS_OFF;
        }
    }

}

void setup_debug() {
    if (debug) {
        int flags = get_command();
        step_count = (flags & STEPS_SET) ?  get_steps() : STEPS_OFF;

        breakpoint =  (flags & BREAKPOINT_SET) ?
                      get_breakpoint() : BREAKPOINT_OFF;
    }


}

void run() {
    log_message(LOG_INFO, "About to setup debug\n");
    setup_debug();
    log_message(LOG_INFO, "About to run\n");
    while(!quit) {
        run_one_frame();
    }
}
/**
 * needs:
 * params file
 * BURN_IN_ITERATIONS
 * first line in calibration_result
 * BETA_ALIGNMENT
 * BETA_0
 * SKIP_CALIBRATE_ALLCHAINS
 **
 * does:
 * calibrate remaining chains (beta < 1)
 * writes all betas, stepwidths and start values in file calibration_result
 **
 * provides:
 * stepwidths of first chain (calibration_result)
 * new params file (params_suggest)
 * new start values (calibration_result)
 **/
void calibrate_rest() {
	int n_beta = N_BETA;
	const double desired_acceptance_rate = TARGET_ACCEPTANCE_RATE;
	const double max_ar_deviation = MAX_AR_DEVIATION;
	double beta_0 = BETA_0;
	const unsigned long burn_in_iterations = BURN_IN_ITERATIONS;
	const unsigned long iter_limit = ITER_LIMIT;
	const double mul = MUL;
	unsigned int n_par;
	int i;
	gsl_vector * stepwidth_factors;
	mcmc ** chains = setup_chains();

	read_calibration_file(chains, 1);

	printf("Calibrating chains\n");
	fflush(stdout);
	n_par = get_n_par(chains[0]);
	stepwidth_factors = gsl_vector_alloc(n_par);
	gsl_vector_set_all(stepwidth_factors, 1);

	i = 1;
	if (n_beta > 1) {
		if (beta_0 < 0)
			set_beta(chains[i], get_chain_beta(i, n_beta, calc_beta_0(
					chains[0], stepwidth_factors)));
		else
			set_beta(chains[i], get_chain_beta(i, n_beta, beta_0));
		gsl_vector_free(get_steps(chains[i]));
		chains[i]->params_step = dup_vector(get_steps(chains[0]));
		gsl_vector_scale(get_steps(chains[i]), pow(get_beta(chains[i]), -0.5));
		set_params(chains[i], dup_vector(get_params_best(chains[0])));
		calc_model(chains[i], NULL);
		mcmc_check(chains[i]);
		printf("Calibrating second chain to infer stepwidth factor\n");
		printf("\tChain %2d - ", i);
		printf("beta = %f\tsteps: ", get_beta(chains[i]));
		dump_vectorln(get_steps(chains[i]));
		fflush(stdout);
		markov_chain_calibrate(chains[i], burn_in_iterations,
				desired_acceptance_rate, max_ar_deviation, iter_limit, mul,
				DEFAULT_ADJUST_STEP);
		gsl_vector_scale(stepwidth_factors, pow(get_beta(chains[i]), -0.5));
		gsl_vector_mul(stepwidth_factors, get_steps(chains[0]));
		gsl_vector_div(stepwidth_factors, get_steps(chains[i]));
		mem_free(chains[i]->additional_data);
	}

	printf("stepwidth factors: ");
	dump_vectorln(stepwidth_factors);

	if (beta_0 < 0) {
		beta_0 = calc_beta_0(chains[0], stepwidth_factors);
		printf("automatic beta_0: %f\n", beta_0);
	}

	fflush(stdout);

#pragma omp parallel for
	for (i = 1; i < n_beta; i++) {
		printf("\tChain %2d - ", i);
		fflush(stdout);
		chains[i]->additional_data
				= mem_malloc(sizeof(parallel_tempering_mcmc));
		set_beta(chains[i], get_chain_beta(i, n_beta, beta_0));
		gsl_vector_free(get_steps(chains[i]));
		chains[i]->params_step = dup_vector(get_steps(chains[0]));
		gsl_vector_scale(get_steps(chains[i]), pow(get_beta(chains[i]), -0.5));
		gsl_vector_mul(get_steps(chains[i]), stepwidth_factors);
		set_params(chains[i], dup_vector(get_params_best(chains[0])));
		calc_model(chains[i], NULL);
		mcmc_check(chains[i]);
		printf("beta = %f\tsteps: ", get_beta(chains[i]));
		dump_vectorln(get_steps(chains[i]));
		fflush(stdout);
#ifndef SKIP_CALIBRATE_ALLCHAINS
		markov_chain_calibrate(chains[i], burn_in_iterations,
				desired_acceptance_rate, max_ar_deviation, iter_limit, mul,
				DEFAULT_ADJUST_STEP);
#else
		burn_in(chains[i], burn_in_iterations);
#endif
	}
	gsl_vector_free(stepwidth_factors);
	fflush(stdout);
	printf("all chains calibrated.\n");
	for (i = 0; i < n_beta; i++) {
		printf("\tChain %2d - beta = %f \tsteps: ", i, get_beta(chains[i]));
		dump_vectorln(get_steps(chains[i]));
	}
	write_calibration_summary(chains, n_beta);
	write_calibrations_file(chains, n_beta);
}