Exemplo n.º 1
0
std::vector<Creature> Creature::Crossover(Creature mate){
	std::vector<Creature> children;
	std::vector<Brain> childrens_brain;
	children.resize(2);
	childrens_brain.resize(2);

	std::uniform_real_distribution<float> int_dist(0.0f,1.0f);

	double should_crossover = int_dist(rng_.mt_rng_);
	SettingsManager::Instance()->GetCrossover();

	if(SettingsManager::Instance()->GetCrossover() >= should_crossover) {
	// TODO: if crossover should be done, else return the incomming creatures
		childrens_brain = brain_.Crossover(mate.GetBrain());
		children[0].brain_ = childrens_brain[0];
		children[1].brain_ = childrens_brain[1];
	}
	else {
		children[0].brain_ = brain_;
		children[1].brain_ = mate.GetBrain();
	}

	return children;

}
Exemplo n.º 2
0
// An HSA version of C++AMP program
int main ()
{
  // define inputs and output
  const int vecSize = 2048;

  int table_a[vecSize];
  int table_b[vecSize];
  int table_c[vecSize];
  int *p_a = &table_a[0];
  int *p_b = &table_b[0];
  int *p_c = &table_c[0];

  // initialize test data
  std::random_device rd;
  std::uniform_int_distribution<int32_t> int_dist;
  for (int i = 0; i < vecSize; ++i) {
    table_a[i] = int_dist(rd);
    table_b[i] = int_dist(rd);
  }

  // launch kernel
  std::cout << "async launch the 1st kernel\n";
  Concurrency::extent<1> e(vecSize);
  Concurrency::completion_future fut = Concurrency::async_parallel_for_each(
    e,
    [=](Concurrency::index<1> idx) restrict(amp) {
      p_c[idx[0]] = p_a[idx[0]] + p_b[idx[0]];

  });
Exemplo n.º 3
0
// An example which shows how to use accelerator_view::create_marker()
// and how to use tick function
bool test() {
  bool ret = true;

  // define inputs and output
  const int vecSize = 2048;

  hc::array_view<int, 1> table_a(vecSize);
  hc::array_view<int, 1> table_b(vecSize);
  hc::array_view<int, 1> table_c(vecSize);

  // initialize test data
  std::random_device rd;
  std::uniform_int_distribution<int32_t> int_dist;
  for (int i = 0; i < vecSize; ++i) {
    table_a[i] = int_dist(rd);
    table_b[i] = int_dist(rd);
  }

  // launch kernel
  hc::extent<1> e(vecSize);
  hc::completion_future fut = hc::parallel_for_each(
    e,
    [=](hc::index<1> idx) __HC__ {
      for (int i = 0; i < LOOP_COUNT; ++i) 
        table_c(idx) = table_a(idx) + table_b(idx);

  });
Exemplo n.º 4
0
// An HSA version of C++AMP program
int main ()
{
  // define inputs and output
  const int vecSize = 2048;

  int table_a[vecSize];
  int table_b[vecSize];
  int table_c[vecSize];
  int *p_a = &table_a[0];
  int *p_b = &table_b[0];
  int *p_c = &table_c[0];

  // initialize test data
  std::random_device rd;
  std::uniform_int_distribution<int32_t> int_dist;
  for (int i = 0; i < vecSize; ++i) {
    table_a[i] = int_dist(rd);
    table_b[i] = int_dist(rd);
  }

  // launch kernel
  Concurrency::extent<1> e(vecSize);
  Concurrency::completion_future fut = Concurrency::async_parallel_for_each(
    e.tile<256>(),
    [=](Concurrency::tiled_index<256> idx) restrict(amp) {
      int fidx = idx.global[0];
      for (int i = 0; i < 1024 * 1024; ++i) 
        p_c[fidx] = p_a[fidx] + p_b[fidx];

  });
uint LogDiscreteSampler::sample(mt19937 & prng) {

	if (has_finite) {

		double random01_log_sample_scaled = log(generate_canonical<double,std::numeric_limits<double>::digits>(prng)) + cum_probs.back();
		return search(random01_log_sample_scaled);

	} else {

		std::uniform_int_distribution<int> int_dist(0, cum_probs.size() - 1);
		return int_dist(prng);
	}
}
SchemeObject* random(SchemeObject* args, SchemeObjectCreator* creator) {
    SchemeObject* top = args->car();

    std::default_random_engine generator;

    if (top->is_flonum()) {
        double value = top->flonum_value();
        std::uniform_real_distribution<double> float_dist(0.0, value);
        double result = float_dist(generator);

        return creator->make_flonum(result);

    } else if (top->is_fixnum()) {
        long value = top->fixnum_value();
        std::uniform_int_distribution<long> int_dist(0, value);
        long result = int_dist(generator);

        return creator->make_fixnum(result);
    }
}
Exemplo n.º 7
0
 int gen_int() { return int_dist(gen); }
Exemplo n.º 8
0
void generate (SNPMatrix& A,
               std::vector<double>& y,
               const snp_int_t& num_rhs,
               SNPSet& true_snps) {
  typedef typename SNPSet::value_type snp_type;
  /* psuedo-random generator engine for various phases in this function */
  boost::random::mt19937 prng;
  prng.seed (int_params[RAND_SEED_INDEX]);

  /* parameters that we will refer to over and over again */
  snp_int_t M = int_params[M_INDEX]; /* not making it const for BLAS */
  snp_int_t N = int_params[N_INDEX]; /* not making it const for BLAS */
  const double cutoff_for_0 = dbl_params[CUTOFF_FOR_0_INDEX];
  const double cutoff_for_1 = dbl_params[CUTOFF_FOR_1_INDEX];
  double signal_strength = dbl_params[SIGNAL_STRENGTH_INDEX];

  /*************************************************************************/
  /*                generate epistatic and marginal SNPs                   */
  const snp_int_t num_mar_snps = floor (dbl_params [PERCENT_MAR_INDEX] * 
           static_cast<double>(int_params [NUM_TRUE_SNPS_INDEX]));
  const snp_int_t num_epi_snps = 
                          int_params [NUM_TRUE_SNPS_INDEX]-num_mar_snps;

  /* initialize a uniform random number generator in the range [0,N) */
  boost::uniform_int<snp_int_t> int_dist(0, (N-1));

  for (snp_int_t i=0;i<num_mar_snps;++i)
    true_snps.insert(snp_type(int_dist(prng)));

  /* generate some epistatic snp pairs */
  generate_epistatic_t<snp_type,
                       boost::uniform_int<snp_int_t>, 
                       boost::random::mt19937> gen_epistatic (int_dist,prng);
  for (snp_int_t i=0; i<num_epi_snps; ++i) {
    snp_type epi_snp = gen_epistatic();
    while (true_snps.end()!=true_snps.find(epi_snp)) epi_snp=gen_epistatic();
    true_snps.insert(epi_snp);
  }

  /*************************************************************************/
  /*                                generate A                             */
  boost::uniform_real<double> real_dist (0.0 /*lb*/, 1.0 /*ub*/);

  for (snp_int_t col=0; col<N; ++col) for (snp_int_t row=0; row<M; ++row) {
    const double next_rnd = real_dist(prng);
    const unsigned char next_snp = ((cutoff_for_0>next_rnd)? 0:
      ((cutoff_for_0<=next_rnd && cutoff_for_1>next_rnd)) ? 1:2);
    A.set (row,col,next_snp);
  }

  /*************************************************************************/
  /*                                generate y                             */
  boost::normal_distribution<double> nrml_dist (0.0/*mean*/, 1.0/*sigma*/);
  std::vector<double> multiplier (M);
  int truncated_M=static_cast<int> (M);

  /* Initialize y to zero for good measure */
  y.resize (M*num_rhs, 0.0);

  /* Marginal: Set the right positions of x to be signal strength */
  typename SNPSet::iterator first = true_snps.begin ();
  while (first != true_snps.end()) {

    /* Generate the column based on a linear model */
    if (first->marginal()) A.interact (first->first(), multiplier.begin());
    else A.interact (first->first(), first->second(), multiplier.begin());

    daxpy_ (&truncated_M,
            &signal_strength,
            &(multiplier[0]),
            &ONE_STEP,
            &(y[0]),
            &ONE_STEP);

    ++first;
  }

  /* Copy this "y" into all the other "y"'s */
  double* y_src = &(y[0]);
  for (snp_int_t i=1; i<num_rhs; ++i) {
    double* y_dst = y_src + (i*truncated_M);
    dcopy_ (&truncated_M, y_src, &ONE_STEP, y_dst, &ONE_STEP);
  }

  /* Now, for each "y", add a different gaussian noise */
  for (size_t i=0; i<y.size(); ++i) y[i]+=nrml_dist(prng);
}
Exemplo n.º 9
0
 IntegralType operator()(const IntegralType& ULIMIT) {
   Distribution int_dist (0, ULIMIT-1);
   return int_dist (prng);
 }