Exemple #1
0
void CS(int lb[], int ub[], int maxI, int nnest, int dim, Nest *Nests, double (*fobj)(int, double x[], int)){
	srand(time(0));

	unsigned long germe[6] = { rand(), rand(), rand(), rand(), rand(), rand() };
    RngStream_SetPackageSeed (germe);
    RngStream g1 = RngStream_CreateStream ("Laplace");

	double pa = 0.25;

	Nest new_Nest[nnest], best;

	init(nnest,dim,lb,ub,Nests,g1);
	memcpy(new_Nest, Nests, sizeof(new_Nest));
	get_best_nest(nnest,dim, &best, new_Nest, Nests,fobj);
	double fxmin = best.fx;
	
	int t;
	for (t = 0; t < 1; t++){

		get_cuckoos(nnest,lb, ub, dim, &best, new_Nest, Nests,g1);
		get_best_nest(nnest,dim, &best, new_Nest, Nests, fobj);
		empty_nests(nnest,dim, lb, ub, new_Nest, Nests,pa,g1);
		get_best_nest(nnest,dim, &best, new_Nest, Nests, fobj);

		if(best.fx < fxmin){
			fxmin = best.fx;
		}

		stRank(Nests, nnest, dim, g1);
	}
	
	show_nest(nnest,dim, Nests, new_Nest, &best, fxmin);
}
Exemple #2
0
/**
 * Bittorrent example launcher
 */
int main(int argc, char *argv[])
{
  xbt_dynar_t host_list;
  msg_host_t host;
  unsigned i;

  MSG_init(&argc, argv);

  /* Check the arguments */
  if (argc < 3) {
    printf("Usage: %s platform_file deployment_file \n", argv[0]);
    return -1;
  }

  const char *platform_file = argv[1];
  const char *deployment_file = argv[2];

  MSG_create_environment(platform_file);

  host_list = MSG_hosts_as_dynar();
  xbt_dynar_foreach(host_list, i, host) {
    char descr[512];
    RngStream stream;
    snprintf(descr, sizeof descr, "RngSream<%s>", MSG_host_get_name(host));
    stream = RngStream_CreateStream(descr);
    MSG_host_set_data(host, stream);
  }
Exemple #3
0
int main(int argc, char const *argv[]){

	srand(time(0));

	unsigned long germe[6] = { rand(), rand(), rand(), rand(), rand(), rand() };
    RngStream_SetPackageSeed (germe);
    RngStream g1 = RngStream_CreateStream ("Laplace");

    double (*fObj)(int, int, double[]);

    printf("Funcion 1\n");
    fObj=function;
    GWO(lb, ub, fObj, g1);
    showPack();
    
    printf("Funcion 6\n");
    fObj=function6;
    GWO(lb, ub, fObj, g1);
    showPack();

    printf("Funcion 7\n");
    fObj=function7;
    GWO(-1.28, 1.28, fObj, g1);
    showPack();

    printf("Funcion 8\n");
    fObj = function8;
    GWO(-500, 500, fObj, g1);
    showPack();

    RngStream_DeleteStream (&g1); 
    return 0;
}
JNIEXPORT void JNICALL Java_org_simgrid_msg_RngStream_create(JNIEnv *env, jobject jrngstream, jstring jname) {
  const char *name = env->GetStringUTFChars(jname, 0);
  RngStream rngstream = RngStream_CreateStream(name);
  //Bind the RngStream object
  env->SetLongField(jrngstream, jrngstream_bind, (intptr_t)rngstream);

  env->ReleaseStringUTFChars(jname, name);
}
Exemple #5
0
TEST_F(ArsGammaTest, GenerateSample) {
    double gamma_args[] = {2.0, 2.0};
    RngStream rng = RngStream_CreateStream("");
    double samp;
    samp = gamma_sampler.sample(rng, gamma_args);
    EXPECT_GT(samp, 0.0);
    EXPECT_GT(gamma_sampler.x0, 0.0);
    EXPECT_GT(gamma_sampler.x1, gamma_sampler.x0);
    EXPECT_EQ(gamma_sampler.hull.num_hull_segments, 2);
    EXPECT_EQ(gamma_sampler.hull.upper_hull_max, -INFINITY);
}
Exemple #6
0
int main (int argc, char const *argv[])
{
    srand(time(0));
    for (int i = 0; i < 30; ++i){
        unsigned long germe[6] = { rand(), rand(), rand(), rand(), rand(), rand() };
        RngStream_SetPackageSeed (germe);
        g1 = RngStream_CreateStream ("Laplace");
        GWO();
        showPack();
        RngStream_DeleteStream (&g1);   
    }
    return 0;
}
Exemple #7
0
TEST_F(ArsGammaTest, GammaMomentTest) {
    double gamma_args[] = {2.0, 2.0};
    RngStream rng = RngStream_CreateStream("");
    double samp, mean = 0.0;
    int n = 1, nsamp = 100000;
    while(n <= nsamp) {
        samp = gamma_sampler.sample(rng, gamma_args);
        mean = mean * (n - 1.0) / n + samp / n;
        n++;
    }

    double true_mean = gamma_args[0] / gamma_args[1];
    double true_se = sqrt(true_mean / gamma_args[1] / nsamp);
    EXPECT_GT(mean, true_mean - 2.6 * true_se);
    EXPECT_LT(mean, true_mean + 2.6 * true_se);

}
Exemple #8
0
TEST_F(ArsWeibullTest, WeibullMomentTest) {
    double weibull_args[] = {2.0, 2.0};
    double k = 2.0;
    double ell = 2.0;
    RngStream rng = RngStream_CreateStream("");
    double samp, mean = 0.0;
    int n = 1, nsamp = 100000;
    while(n <= nsamp) {
        samp = weibull_sampler.sample(rng, weibull_args);
        ASSERT_GT(samp, 0.0);
        mean = mean * (n - 1.0) / n + samp / n;
        n++;
    }
    double true_mean = ell * exp(lgamma(1.0 + 1.0 / k));
    double true_se = sqrt((ell * ell * exp(lgamma(1.0 + 2.0 / k)) - true_mean * true_mean) / nsamp);
    EXPECT_GT(mean, true_mean - 2.6 * true_se);
    EXPECT_LT(mean, true_mean + 2.6 * true_se);
}
Exemple #9
0
/** Bittorrent example launcher */
int main(int argc, char* argv[])
{
  msg_host_t host;
  unsigned i;

  MSG_init(&argc, argv);

  /* Check the arguments */
  xbt_assert(argc > 2, "Usage: %s platform_file deployment_file", argv[0]);

  MSG_create_environment(argv[1]);

  xbt_dynar_t host_list = MSG_hosts_as_dynar();
  xbt_dynar_foreach (host_list, i, host) {
    char descr[512];
    snprintf(descr, sizeof descr, "RngSream<%s>", MSG_host_get_name(host));
    RngStream stream = RngStream_CreateStream(descr);
    MSG_host_set_data(host, stream);
  }
Exemple #10
0
SEXP R_RngStreams_Init (SEXP R_obj, SEXP R_name)
     /*----------------------------------------------------------------------*/
     /* Create and initialize Stream generator object.                       */
     /*                                                                      */
     /* parameters:                                                          */
     /*   obj    ... (S4 class) ... rstream object                           */ 
     /*   R_name ... (string)   ... name of the Stream                       */
     /*                                                                      */
     /* return:                                                              */
     /*   pointer to Stream object                                           */
     /*----------------------------------------------------------------------*/
{
  SEXP R_newstream;
  RngStream newstream;   /* Notice: RngStream is a pointer to a structure */
  const char *name;

  /* check argument */
  if (!R_name || TYPEOF(R_name) != STRSXP)
    error("bad string\n");

  /* get pointer to argument string */
  name = CHAR(STRING_ELT(R_name,0));

  /* create Stream generator object */
  newstream = RngStream_CreateStream(name);

  /* this must not be a NULL pointer */
  if (newstream == NULL) 
    error("cannot create Stream object\n");

  /* make R external pointer and store pointer to Stream generator */
  PROTECT(R_newstream = R_MakeExternalPtr(newstream, RngStreams_tag(), R_obj));
  UNPROTECT(1);
  
  /* register destructor as C finalizer */
  R_RegisterCFinalizer(R_newstream, R_RngStreams_free);

  /* return pointer to R */
  return R_newstream;

} /* end of R_RngStreams_init() */
Exemple #11
0
primal_t *primal_create (instance_t *inst)
{
	primal_t *primal = (primal_t*) malloc(sizeof(primal_t));

	primal->inst = instance_copy(inst);
	primal->state = primal_state_undef;

	primal->guiding_x = (int*) calloc(inst->n, sizeof(int));
	primal->improving_partial_x = (int*) calloc(inst->n, sizeof(int));
	primal->best_x = (int*) calloc(inst->n, sizeof(int));
	primal->best_z = INFINITY;

	primal->sol = solution_alloc(inst);
	primal->tabu = (double*) calloc(inst->n, sizeof(double));
	primal->n_moves = 0;
	primal->n_moves_at_last_improvement = 0;

	primal->rng = RngStream_CreateStream("");

	return primal;
}
Exemple #12
0
SEXP r_create_stream (SEXP sexp_name)
{
    RngStream newstream;
    SEXP sexp_newstream;
    int i;

    const char *name = CHAR(STRING_ELT(sexp_name,0));
    newstream =  RngStream_CreateStream(name);

    PROTECT(sexp_newstream = allocVector(REALSXP, 20));

    for (i = 0; i < 6; ++i) {
        REAL(sexp_newstream)[i] = (double) newstream->Cg[i];
        REAL(sexp_newstream)[i+6] = (double) newstream->Bg[i];
        REAL(sexp_newstream)[i+12] = (double) newstream->Ig[i];
    }
    REAL(sexp_newstream)[18] = (int) newstream->Anti;
    REAL(sexp_newstream)[19] = (int) newstream->IncPrec;
    UNPROTECT(1);

    RngStream_DeleteStream(&newstream);
    return(sexp_newstream);
}
Exemple #13
0
void pepbayes_v3(double *Y, double *hyper_param, int *pstart,
		int *pnum, int *n_position, int *n_peptide, int *n_indiv, int *nP,
		int *cen_ind, int *cen_pep, int *cen_num, int *cen_pos,
		int *n_iter, int *n_sweep, int *n_burn,
		double *OutProbs, int *write)
{
	R_CStackLimit = (uintptr_t)-1;
	// miscellaneous useful quantities
	int p, i, j, k = 0, c, d;
	int pep, pos, th_id;
	int *pos_ind_by_pep;
	double *ProbSum;
	int total_iterations = *n_burn+(*n_sweep)*(*n_iter);
	int percent_complete = 10;

	adpt zeta_adpt;
	adpt m_adpt;

	zeta_adpt.total_count = 0;
	zeta_adpt.count = 0;
	zeta_adpt.tune = 2.5;
	m_adpt.total_count = 0;
	m_adpt.count = 0;
	m_adpt.tune = 5.0;

	RngStream rng[*nP];

	//statically allocated struct for Adaptive Rejection Sampling
	ARS_workspace workspace;

	// missing data variables
	double *Exprs;			// censor-completed expressions
	int *Gamma;			// cluster membership indicator
	double *W;				// t-distribution weights
	double dof = 4.0;				// t-distribution degrees of freedom
	double *D;				// imputed censored tails

	// component membership probability parameters
	double *Omega_logit;	// membership probabilities, on logit scale
	int *Omega_ind;			// indicator for whether omega_{cp} > 0
	double *A, *B, *U; 		// prior parameters for omega probabilities
	double lambda_a, lambda_b; //hyperparameters for A, B
	double a_0, b_0;		// hyperparameters for U
	double **xA, **xB;		// vectors required for ARS

	// location-related parameters
	double *Mu;				// peptide specific means
	double kappa = 10.0;	// prior mean precision
	double *Alpha_pep;		// peptide effects
	double m = 2.0, zeta = 1.0;			// prior mean and variance of peptide effects
	double m_0, v_0;		// hyperparameters for m

	// variance parameters
	double *Sig2;			// peptide response variance
	double alpha = 10.0;	// prior parameter
	double beta = 10.0; 	// prior parameter
	double alpha_0, beta_0; // rates on alpha, beta (hyperparameters)
	double *xAlpha;			// vector needed for ARS

	// file pointers
	FILE *AFILE, *BFILE, *PFILE, *VARFILE, *Sig2FILE, *MUFILE, *DFILE, *OFILE;
	FILE *ALPHAFILE;

	// retreive and initialize hyperparameter values
	a_0 = hyper_param[0];
	b_0 = hyper_param[1];
	lambda_a = hyper_param[2];
	lambda_b = hyper_param[3];
	alpha_0 = hyper_param[4];
	beta_0 = hyper_param[5];
	m_0 = hyper_param[6];
	v_0 = hyper_param[7];

	// begin memory allocation
	Gamma = (int*) R_alloc(*n_peptide*(*n_indiv), sizeof(int));
	Exprs = (double*) R_alloc(*n_peptide*(*n_indiv), sizeof(double));
	W = (double *) R_alloc(*n_peptide*(*n_indiv), sizeof(double));
	double *RB = (double *) R_alloc(*n_peptide*(*n_indiv), sizeof(double));

	// xA and xB hold starting values for ARS of a_p, b_p, and alpha.
	xA = (double**) R_alloc(*n_position, sizeof(double*));
	xB = (double**) R_alloc(*n_position, sizeof(double*));
	xAlpha = (double*) R_alloc(NMAX, sizeof(double));
	// initial values for hull quantiles.
	xAlpha[0] = 1.0;
	xAlpha[1] = 2.0;

	Omega_ind = (int*) R_alloc(*n_peptide, sizeof(int));
	Omega_logit = (double*) R_alloc(*n_peptide, sizeof(double));
	pos_ind_by_pep = (int*) R_alloc(*n_peptide, sizeof(int));
	ProbSum = (double*) R_alloc(*n_peptide*(*n_indiv), sizeof(double));

	for(p = 0; p < *n_position; p++)
	{
		xA[p] = (double*) R_alloc(NMAX, sizeof(double));
		xB[p] = (double*) R_alloc(NMAX, sizeof(double));
		for(c = 0; c < pnum[p]; c++)
		{
			pos_ind_by_pep[pstart[p] + c] = k;
		}
		k++;
	}

	Alpha_pep = (double*) R_alloc(*n_peptide, sizeof(double));
	Sig2 = (double*) R_alloc(*n_peptide, sizeof(double));
	A = (double*) R_alloc(*n_position, sizeof(double));
	B = (double*) R_alloc(*n_position, sizeof(double));
	U = (double*) R_alloc(*n_position, sizeof(double));
	Mu = (double*) R_alloc(*n_peptide, sizeof(double));
	double* likworkspace = (double *) R_alloc((*n_peptide)*(*n_indiv), sizeof(double));

	// check whether our data is censored at all,
	// if so prepare for augmentation.

	if(*cen_num > 0)
	{
		D = (double*) R_alloc(*cen_num, sizeof(double));
		for(i = 0; i < *cen_num; i++)
		{
			D[i] = 0.0;
		}
	}

	if(*write == 1)
	{
		AFILE = fopen("afile.txt", "w");
		BFILE = fopen("bfile.txt", "w");
		PFILE = fopen("pfile.txt", "w");
		OFILE = fopen("ofile.txt", "w");
		ALPHAFILE = fopen("alphafile.txt", "w");
		VARFILE = fopen("varfile.txt", "w");
		Sig2FILE = fopen("sig2file.txt", "w");
		MUFILE = fopen("mufile.txt", "w");
		if(*cen_num > 0)
		{
			DFILE = fopen("dfile.txt", "w");
		}
	}
	for(i = 0; i < *nP; i++)
	{
		rng[i] = RngStream_CreateStream("");
	}

	GetRNGstate();
	initialize_chain(ProbSum, Exprs, Y, W, Omega_ind, Omega_logit, Alpha_pep,
			Gamma, Sig2, Mu, A, B, U,
			n_position, pstart, pnum, n_peptide, n_indiv,
			xA, xB, RB);
	PutRNGstate();


	Rprintf("parameters initialized \n");
	for(i = 1; i <= total_iterations; i++)
	{
		R_CheckUserInterrupt();
		update_dof_integrated(&dof, Exprs, W, Alpha_pep, Gamma,
				Sig2, Mu, likworkspace, *n_indiv, *n_peptide, rng[0]);

#pragma omp parallel private(th_id, workspace, pos) num_threads(*nP)
		{
			th_id = omp_get_thread_num();
#pragma omp for
			for(pep = 0; pep < *n_peptide; pep++)
			{
				pos = pos_ind_by_pep[pep];
				update_peptide(Exprs, Mu + pep, Alpha_pep + pep,
						W, Omega_ind + pep, Omega_logit + pep, Gamma,
						Sig2 + pep, alpha, beta,
						U[pos], A[pos], B[pos],
						m, zeta, dof, kappa, *n_indiv, *n_peptide, pep, rng[th_id], RB);
			}
#pragma omp for
			for(p = 0; p < *n_position; p++)
			{
				update_position_p(Omega_ind, Omega_logit,
						A + p, B + p, U + p,
						a_0, b_0, lambda_a, lambda_b,
						p, *n_indiv, pnum, pstart, rng[th_id],
						xA[p], xB[p], &workspace);
			}
			// update gammas, alphas, omegas

			if((i > *n_burn) && ((i - *n_burn) % (*n_sweep) == 0 ))
			{
#pragma omp for
				for(d = 0; d < (*n_indiv)*(*n_peptide); d++)
				{
					ProbSum[d] += RB[d];
				}
			}
		}

		update_global_params(&m, &zeta, &alpha, &beta, &kappa,
				Mu, Sig2, Alpha_pep, Omega_ind,
				m_0, v_0, alpha_0, beta_0,
				*n_indiv, *n_peptide, rng[0], &m_adpt, &zeta_adpt,
				&workspace, xAlpha, *nP);

		if(i % TEST_INT_LENGTH == 0)
		{
			update_tuning(&m_adpt, i, *n_burn);
			update_tuning(&zeta_adpt, i, *n_burn);
		}

		// check whether we need to update complete data
		if((*cen_num > 0) & (i > *n_burn/2))
		{
			update_censoring(W, D, *cen_num, cen_ind, cen_pep, Y, Exprs,
					Gamma, Alpha_pep, Mu, *n_indiv, Sig2, rng[0]);
		}

		if((i > *n_burn) && ((i - *n_burn) % (*n_sweep) == 0 ) && *write == 1)
		{
			store_mcmc_output(Alpha_pep, Mu, A, B, U, Sig2, D, Omega_logit,
					Omega_ind, kappa, alpha, beta, m, zeta,
					dof, *n_peptide, *n_indiv, *n_position, cen_num,
					AFILE, BFILE, PFILE, VARFILE, Sig2FILE, MUFILE, DFILE,
					OFILE, ALPHAFILE);
		}

		if( 100*((double) i)/total_iterations >= percent_complete)
		{
			Rprintf("MCMC %d percent complete\n", percent_complete);
			percent_complete += 10;
		}
	}

	// finalize marginal PPA
	for(d = 0; d < (*n_indiv)*(*n_peptide); d++)
	{
		OutProbs[d] = ProbSum[d]/(*n_iter);
	}

	Rprintf("M acceptance rate: %.3lf, Zeta acceptance rate: %.3lf\n", (double)(m_adpt.total_count)/(*n_iter*(*n_sweep)),
			(double)(zeta_adpt.total_count)/(*n_iter*(*n_sweep)));

	Rprintf("closing files\n");
	if(*write == 1)
	{
		fclose(AFILE);
		fclose(BFILE);
		fclose(PFILE);
		fclose(OFILE);
		fclose(VARFILE);
		fclose(ALPHAFILE);
		fclose(Sig2FILE);
		fclose(MUFILE);
		if(*cen_num > 0)
		{
			fclose(DFILE);
		}
	}
	return;
}
Exemple #14
0
VALUE rng_initialize(VALUE self) {
  RngStream stream = RngStream_CreateStream(NULL);
  VALUE stream_ptr = Data_Wrap_Struct(rb_cObject, 0, rng_free, (void*) stream);
  rb_iv_set(self, "@stream_prt", stream_ptr);
  return self;
}
Exemple #15
0
	RNG_lecuyer(int seed1, int seed2) {
		_stream = RngStream_CreateStream("RNG_lecuyer");
		set_seed(seed1, seed2);
	}