示例#1
0
 double get_fitness_effect(Random & random, Clone const & individual) const {
     double x;
     
     do{ x = eps/sample_uniform(random); }  // draw pareto variable
     while( sample_uniform(random) > std::exp(x-eps) ); // rejection sample
     
     return std::pow(W,x);
 };
示例#2
0
 double get_fitness_effect(Random & random, Clone const & individual){
     if( sample_uniform(random) < dfe1.get_mutation_rate(individual)/(dfe1.get_mutation_rate(individual)+dfe2.get_mutation_rate(individual)) ){
         return dfe1.get_fitness_effect(random,individual);
     }
     else{
         return dfe2.get_fitness_effect(random,individual);
     }
 }
示例#3
0
文件: tlib.c 项目: pillhead/ml-lib
void sample_chain_with_prior (int N, int W, int T, int *w, int *d, int *z, double **Nwt, double **Ndt, double *Nt, int *order, double **prior_Nwt) //
{
	int ii, i, t;
	double totprob, U, cumprob;
	double *prob = dvec(T);
	int wid, did;
	double *word_vec;
	double *doc_vec;
	double *prior_word_vec;

	for (ii = 0; ii < N; ii++) {

		i = order[ ii ];

		wid = w[i];
		did = d[i];

		word_vec = Nwt[wid];
		doc_vec  = Ndt[did];
		prior_word_vec = prior_Nwt[wid];

		t = z[i];
		Nt[t]--;
		word_vec[t]--;
		doc_vec[t]--;
		totprob = 0;

		for (t = 0; t < T; t++) {
			prob[t] = doc_vec[t] * (word_vec[t] + prior_word_vec[t]) / Nt[t];
			totprob += prob[t];
		}

		// U = drand48()*totprob;
		U = sample_uniform() * totprob;
		cumprob = prob[0];
		t = 0;
		while (U>cumprob) {
			t++;
			cumprob += prob[t];
		}

		z[i] = t;
		word_vec[t]++;
		doc_vec[t]++;
		Nt[t]++;
	}

	free(prob);
}
示例#4
0
文件: splines.cpp 项目: cutun/kazoo
Spline::Spline (
    size_t size_in,
    size_t size_out)

  : m_size_in(size_in),
    m_size_out(size_out),

    m_i0(size_in),
    m_w0(size_in),
    m_w1(size_in),

    m_scale_bwd(size_out),
    m_scaled_e_rng(size_out),

    m_tolerance(1e-10)
{
  ASSERTW(size_in >= size_out,
          "spline is being used : small -> large; consider reversing");
  Vector<float> vect(m_size_in);
  sample_uniform(vect);
  setup(vect);
}
示例#5
0
 double get_fitness_effect(Random & random, Clone const & individual) const {
     return std::pow(W,-log(1.0-sample_uniform(random)*p_thresh));
 };
示例#6
0
 double get_fitness_effect(Random & random, Clone const & individual) const {
     return std::pow(W,sample_uniform(random));
 };
示例#7
0
void sampleSARD_ego(const SARDInputModel* p_in_model,
                    SARDSimParams* p_sim_params,
                    SARDMCMCDiagnostics* p_out_data,
                    acq_maximizer_fn_t acq_maximizer_fn,
                    gp_add_data_fn_t add_data_to_gp_fn,
                    gp_maximizer_fn_t maximize_gp_fn,
                    gp_posterior_mean_fn_t gp_posterior_mean_fn,
		    gp_maximizer_local_fn_t gp_maximizer_local_fn,
		    sample_gp_posterior_mean_fn_t sample_gp_posterior_mean_fn,
		    return_sample_fn_t return_sample_fn, size_t return_samples,
                    size_t strategy, double *policy, size_t policy_size)
{
    SAWData* p_saw_data = (SAWData*)malloc(sizeof(SAWData));
    SARDState* p_sard_state = (SARDState*)malloc(sizeof(SARDState));

    initSARDState(p_sard_state, p_in_model, p_sim_params);
    // Set up the heaps, etc. 
    initSAWData(p_saw_data, p_in_model, p_sim_params);

    int nruns_until_next_param_update = p_sim_params->nruns_per_param_update;
    int first_run_with_curr_param_idx = 0;

    double *bounds, *amcmc_params;
    const size_t n_amcmc_dims = 8;
    {
        // Set up the bounds for EGO. The bounds are made up of ndim*2 numbers:
        // [bounds[2i], bounds[2i+1]] are the [lb, ub] of the ith dimension.
        // There are eight parameters, iters_per_move, SAW_length_[min, max],
        // P_LL, P_LH, P_HL, gamma_H and gamma_L, so there are eight
        // dimensions. Each dimension has two bounds. The total size is 16.
        bounds = malloc(sizeof(double) * n_amcmc_dims * 2);
        bounds[0] = (double)p_sim_params->iters_per_move_min;
        bounds[1] = (double)p_sim_params->iters_per_move_max;
        bounds[2] = (double)p_sim_params->min_SAW_length_bound_min;
        bounds[3] = (double)p_sim_params->min_SAW_length_bound_max;
        bounds[4] = (double)p_sim_params->max_SAW_length_bound_min;
        bounds[5] = (double)p_sim_params->max_SAW_length_bound_max;
        bounds[6] = 0.0; // P_LL
        bounds[7] = 1.0; // P_LL
        bounds[8] = 0.0; // P_LH
        bounds[9] = 1.0; // P_LH
        bounds[10] = 0.0; // P_HL
        bounds[11] = 1.0; // P_HL
        bounds[12] = p_sim_params->gamma_hi_min;
        bounds[13] = p_sim_params->gamma_hi_max;
        bounds[14] = p_sim_params->gamma_lo_min;
        bounds[15] = p_sim_params->gamma_lo_max;

        // Select initial parameters - this must be done without the help of EGO
        // because the Gaussian process contains no information at all. The initial
        // parameters can either be picked randomly or set to something reasonable.
        // acq_maximizer_fn(bounds, amcmc_params, n_amcmc_dims);
        amcmc_params = malloc(sizeof(double) * n_amcmc_dims);
        for (int i = 0; i < n_amcmc_dims; ++i)
        {
            int idx = i * 2;
            amcmc_params[i] = (bounds[idx + 1] + bounds[idx]) / 2;
        }

        assign_sard_params(p_sard_state, p_saw_data, p_in_model,
                           p_sim_params, amcmc_params, 1);

        int constraints_violated =
            check_violated_sard_constraints(amcmc_params, n_amcmc_dims);
        if (constraints_violated)
        {
            printf("Initial SARD_ego parameters violate constraints.\n");
        }
    }

    objective obj_fn = ACF;

    // the first adaptation is already done (and is hardcoded, since the GP has
    // no information at the start of the adaptation stage).
    int n_adaptations = p_sim_params->n_adaptations - 1;
    int n_adaptations_done = 0;
    int adaptation_finished = 0;

    for (int curr_move=0; curr_move<p_sim_params->n_moves; curr_move++)
    {
        if (!adaptation_finished &&
            nruns_until_next_param_update == 0 && curr_move > 0)
        {
            double reward = -1.0;

            if (obj_fn == ACF)
            {
                reward =
                    compute_robust_avg_abs_acf_est
                        (p_out_data->E_samples,
                         p_sim_params->nruns_per_param_update,
                         first_run_with_curr_param_idx);
            }
            else
            {
                printf("Invalid objective function specified\n");
            }

	    printf("Reward: %f\n", reward);
            // Update the Gaussian process
            add_data_to_gp_fn(amcmc_params, n_amcmc_dims, reward);

            // IMPL: Check whether the best possible parameter settings have
            // improved much and if they haven't, then stop adapting. I'm not
            // sure exactly how to do this..

            for (int i = 0; i < p_sim_params->nruns_per_param_update; ++i)
            {
                p_out_data->rewards[i+first_run_with_curr_param_idx] =
                    reward;
            }
                
            first_run_with_curr_param_idx += p_sim_params->nruns_per_param_update;

            if (n_adaptations > 0)
            {
                // Select next set of parameters
                int num_times_violated = 0;

                while (true)
                {
                    acq_maximizer_fn(bounds, amcmc_params, n_amcmc_dims);

                    int constraints_violated =
                        check_violated_sard_constraints(amcmc_params, n_amcmc_dims);
                    if (!constraints_violated)
                    {
                        break;
                    }

                    ++num_times_violated;
                    printf("Constraints violated %d times\n",
                            num_times_violated);

                    add_data_to_gp_fn(amcmc_params, n_amcmc_dims, 0.0);
                }

                assign_sard_params(p_sard_state, p_saw_data, p_in_model,
                                   p_sim_params, amcmc_params, 1);

                --n_adaptations;
                ++n_adaptations_done;
            }
            else if (n_adaptations == 0)
            {
                // adaptation is done - construct probability distribution over
                // parameters and draw parameters from it
                printf("START: Building SIR policy\n");
                build_sir_policy(policy, policy_size, bounds, n_amcmc_dims, strategy,
                                 gp_posterior_mean_fn, acq_maximizer_fn, gp_maximizer_local_fn,
				 sample_gp_posterior_mean_fn);
                printf("STOP: Building SIR policy\n");
                size_t param_idx = sample_uniform(policy_size);
                
                assign_sard_params
                    (p_sard_state, p_saw_data, p_in_model, p_sim_params,
                     policy+(n_amcmc_dims*param_idx), 1);  

                
		int reset = 0;
		if (reset) {
		    // adaptation is done - reset SARD state
		    printf("START: Resetting SARD state\n");
		    setSARDState(p_sard_state, p_in_model,
				(const int*)p_sim_params->S_reset,
				(const double*)p_sim_params->h_effective_reset);
		    printf("STOP: Resetting SARD state\n");
		}
                adaptation_finished = 1;
            }
            else
            {
                printf("Invalid value for n_adaptations\n");
            }

            nruns_until_next_param_update = p_sim_params->nruns_per_param_update;
        }

        if (!adaptation_finished)
        {
            --nruns_until_next_param_update;
        }
        else
        {
            size_t param_idx = sample_uniform(policy_size);
            assign_sard_params(p_sard_state, p_saw_data, 
			       p_in_model, p_sim_params, 
			       policy + (n_amcmc_dims*param_idx), 1);
        }

        // Select move type:
        {
            double R=genrand_real2();
            if (R <= p_sim_params->P_LL)
            {
                p_saw_data->move_type=LL;
            }
            else if (R <= p_sim_params->P_LL+p_sim_params->P_LH)
            {
                p_saw_data->move_type=LH;
            }
            else
            {
                p_saw_data->move_type=HL;
            }
        }

        double E_initial= p_sard_state->E;
        proposeSAWMove(p_saw_data, p_sard_state, p_in_model, p_sim_params);

        double log_f_fwd, log_f_rev;
        evalLogFs(&log_f_fwd, &log_f_rev, p_saw_data, p_sim_params);

        double MH_ratio = p_sim_params->beta_true*(E_initial - p_sard_state->E);
        MH_ratio += (log_f_rev - log_f_fwd);
        MH_ratio = exp(MH_ratio);
        MH_ratio = MH_ratio >1.0 ? 1.0 : MH_ratio;

        double reward = 0.0;

	if (adaptation_finished && return_samples == 1) {
	    // return_samples specifies whether samples are to be returned
	    // 0: do not return
	    // 1: return samples after adaptation is finished
	    // 2: return all samples	    
	    return_sample_fn(p_sard_state->S, p_in_model->num_nodes);
	}
	
        storeDiagnosticsData(p_out_data, p_sim_params, curr_move,
                             E_initial, p_sard_state->E,
                             log_f_fwd, log_f_rev, MH_ratio,
                             (int)p_saw_data->move_type,
                             p_saw_data->sigma_lengths,
                             p_saw_data->rho_lengths,
                             p_sim_params->iters_per_move,
                             p_sim_params->gamma_hi, p_sim_params->gamma_lo,
                             p_sim_params->P_LL, p_sim_params->P_LH,
                             p_sim_params->P_HL, reward);

        // If rejection, restore state:
        if (genrand_real2() > MH_ratio)
        { 
            undoSAWMove(p_saw_data, p_sard_state, p_in_model, p_sim_params);
        }
        
        if ((curr_move+1)%1000 == 0) {
	    printf("Iteration %d Finished.\n", curr_move+1);
	}
    } // end moves loop

    printf("X\n");

    storeEndState(p_out_data, p_sard_state->S, p_in_model->num_nodes);

    printf("Y\n");

    freeSARDState(p_sard_state, p_in_model, p_sim_params);
    freeSAWData(p_saw_data, p_in_model, p_sim_params);  
    free(p_sard_state);
    free(p_saw_data);
    free(amcmc_params);
    free(bounds);
    printf("Z\n");
}