Esempio n. 1
0
void CState::randnVelocity(double mean, double sigma_v, long *seed)
{
    vec2 randomNormal, randomUniform;
    mat velocities(nAtoms, 3);
    rowvec3 momentum;
    double R;

    for (int i = 0; i < nAtoms; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            // Box-Muller transform
            randomUniform << ran2(seed) << ran2(seed);
            R = sqrt(-2*log(randomUniform(0)));
            randomNormal(0) = R*cos(2*pi*randomUniform(1));
            velocities(i, j) = randomNormal(0)*sigma_v + mean;

            // unused random number... should probably use this one for something
            // randomNormal(1) = R*sin(2*pi*randomUniform(1))*sigma_v + mean;
        }
    }

    momentum = sum(velocities)/nAtoms; // finding the linear momentum of the system
    for (int i = 0; i < nAtoms; i++)
    {
        velocities.row(i) -= momentum; // removing initial linear momentum from the system
    }

    // sending the generated velocities to the atoms
    for (int i = 0; i < nAtoms; i++)
    {
        atoms[i]->setVelocity(velocities.row(i).t());
    }
}
Esempio n. 2
0
void randomCorrelation( double (&R)[NS][NS], uint32_t &seed )
{
  double T[NS][NS];
  for ( size_t i=0; i<NS; ++i )
  {
    for ( size_t j=0; j<NS; ++j )
    {
      T[i][j] = randomNormal( seed );
    }
  }

  for ( size_t j=0; j<NS; ++j )
  {
    double sqSum = 0.0;
    for ( size_t i=0; i<NS; ++i )
    {
      sqSum += T[i][j] * T[i][j];
    }
    double norm = sqrt( sqSum );
    for ( size_t i=0; i<NS; ++i )
      T[i][j] /= norm;
  }

  double TTrans[NS][NS];
  trans( T, TTrans );

  multMatMat( TTrans, T, R );
}
/********** Cr� la population **********/ 
void itsHybridContinuousInteractingAntColony::createAnts()
{

  // r�ervations vecteurs

  antProbaMemory.reserve( antsNb );
  //antMoveRange.reserve( antsNb*SpaceDim() );
  antMoveRange.reserve( antsNb );
  antCurrentPoint.reserve( antsNb * this->getProblem()->getDimension() );
  antCurrentValue.reserve( antsNb );
  // trop gourmand : antIncomingPoints.reserve( antsNb*antsNb*SpaceDim() );
  antIncomingPoints.reserve( antsNb*antsNb );
  antIncomingValues.reserve( antsNb*antsNb );
  antState.reserve( antsNb );
  
  for( unsigned int i=0; i < antsNb; i++ ) {
    
    // tirage al�toire uniforme sur l'hypercube de l'espace de recherche
    
    vector<double> aPoint;
    /*
    aPoint.reserve( this->getProblem()->getDimension() );
    aPoint = randomUniform( this->getProblem()->boundsMinima(), this->getProblem()->boundsMaxima() );
    */
    aPoint = this->getSamplePointAddr(i)->getSolution();
    
    // tire suivant N l'amplitude max de mouvement
    // distribution normale sur la moyenne de l'amplitude de l'espace de recherche
    double aMoveRange;
    aMoveRange = abs( randomNormal( 
				  mean( substraction(this->getProblem()->boundsMaxima(),this->getProblem()->boundsMinima()) ) * moveRangeMeanRatio, 
				  1/2 * mean( substraction(this->getProblem()->boundsMaxima(),this->getProblem()->boundsMinima()) ) * moveRangeStdRatio 
				  ) );
    
    // tire sur N la probabilit�de d�art d'utiliser un type de m�oire
    double aProbaUseMemory;
    aProbaUseMemory = randomNormal( probaUseMemoryMean, probaUseMemoryStd );
    // corrige les d�assements
    if ( aProbaUseMemory < 0 ) { aProbaUseMemory = 0; }
    if ( aProbaUseMemory > 1 ) { aProbaUseMemory = 1; }
    
    // ajoute la fourmi
    addAnt( aPoint, aMoveRange, aProbaUseMemory );
  }
}
Esempio n. 4
0
vector< T > randomNormal( vector< T > & means, vector< T > & stds )
{
  vector< T > v;

  unsigned int i;
  for(i=0; i<means.size(); i++) {
    T x;
    x = randomNormal(means[i], stds[i]);
    v.push_back(x);
  }
    
  return v;
}
Esempio n. 5
0
T randomNormalMulti( T means, U varcovar )
{
/*  
  - Let $u$ a vector of $n$ number, following a centered/reducted
  normal distribution; 
  - let $L$ be the matrix resulting from the Cholesky decomposition
  of $V$; 
  - the vector $y=m+Lu$ follow the multi-normal distribution, 
  with a mean $m$ and a variance-covariance matrix $V$.
*/
  T finalPoint;
  
  // Cholesky decomposition of the variance-covariance matrix
  U popVarCholesky; // low triangular matrix
  popVarCholesky = cholesky( varcovar );

  // Vector with terms in a centered/reducted normal distribution
  T u;
  T mean(means.size(),0.0);
  T variance(means.size(),1.0);
  u = randomNormal( mean,variance );

  // temporary vector for multiplication
  U tempU;
  tempU.push_back(u);
  
  // post multiplication by the u vector
  U tempCompVar;
  tempCompVar = multiply( popVarCholesky, transpose(tempU) );

  // transposition
  T compVar = transpose(tempCompVar)[0];

  // addition to the mean
  finalPoint = addition( means, compVar );
  
  return finalPoint;
}
Esempio n. 6
0
void randomNormalVec( double (&vec)[NS], uint32_t &seed )
{
  for ( size_t i=0; i<NS; ++i )
    vec[i] = randomNormal( seed );
}
Esempio n. 7
0
void GenEcho::step() {
	// Implement a simple sine oscillator
  //float deltaTime = engineGetSampleTime();
  float amp_out = 0.0;

  // handle the 3 switches for accumlating and mirror toggle
  // and probability distrobution selection
  is_accumulating = (int) params[ACCM_PARAM].value;
  is_mirroring = (int) params[MIRR_PARAM].value;
  dt = (DistType) params[PDST_PARAM].value;

  // read in cv vals for astp, dstp and bpts
  bpts_sig = 5.f * quadraticBipolar((inputs[BPTS_INPUT].value / 5.f) * params[BPTSCV_PARAM].value);
  astp_sig = quadraticBipolar((inputs[ASTP_INPUT].value / 5.f) * params[ASTPCV_PARAM].value);
  dstp_sig = quadraticBipolar((inputs[DSTP_INPUT].value / 5.f) * params[DSTPCV_PARAM].value);

  max_amp_step = rescale(params[ASTP_PARAM].value + (astp_sig / 4.f), 0.0, 1.0, 0.05, 0.3);
  max_dur_step = rescale(params[DSTP_PARAM].value + (dstp_sig / 4.f), 0.0, 1.0, 0.01, 0.3);

  sample_length = (int) (clamp(params[SLEN_PARAM].value, 0.1, 1.f) * MAX_SAMPLE_SIZE);

  bpt_spc = (unsigned int) params[BPTS_PARAM].value + 800;
  bpt_spc += (unsigned int) rescale(bpts_sig, -1.f, 1.f, 1.f, 200.f);
  num_bpts = sample_length / bpt_spc + 1;
 
  env_dur = bpt_spc / 2;

  // snap knob for selecting envelope for the grain
  int env_num = (int) clamp(roundf(params[ENVS_PARAM].value), 1.0f, 4.0f);

  if (env.et != (EnvType) env_num) {
    env.switchEnvType((EnvType) env_num);
  }

  // handle sample reset
  if (smpTrigger.process(params[TRIG_PARAM].value)
      || resetTrigger.process(inputs[RSET_INPUT].value / 2.f)) {
    for (unsigned int i=0; i<MAX_SAMPLE_SIZE; i++) sample[i] = _sample[i];
    for (unsigned int i=0; i<MAX_BPTS; i++) {
      mAmps[i] = 0.f;
      mDurs[i] = 1.f; 
    }
  }

  // handle sample trigger through gate 
  if (g2Trigger.process(inputs[GATE_INPUT].value / 2.f)) {

    // reset accumulated breakpoint vals
    for (unsigned int i=0; i<MAX_BPTS; i++) {
      mAmps[i] = 0.f;
      mDurs[i] = 1.f;
    }

    num_bpts = sample_length / bpt_spc;
    sampling = true;
    idx = 0;
    s_i = 0;
  }

  if (sampling) {
    if (s_i >= MAX_SAMPLE_SIZE - 50) {
      float x,y,p;
      x = sample[s_i-1];
      y = sample[0];
      p = 0.f;
      while (s_i < MAX_SAMPLE_SIZE) {
        sample[s_i] = (x * (1-p)) + (y * p);
        p += 1.f / 50.f;
        s_i++;
      }
      debug("Finished sampling");
      sampling = false;
    } else {
      sample[s_i] = inputs[WAV0_INPUT].value; 
      _sample[s_i] = sample[s_i];
      s_i++;
    } 
  }

  if (phase >= 1.0) {
    phase -= 1.0;

    amp = amp_next;
    index = (index + 1) % num_bpts;
    
    // adjust vals
    astp = max_amp_step * rg.my_rand(dt, randomNormal());
    dstp = max_dur_step * rg.my_rand(dt, randomNormal());

    if (is_mirroring) {
      mAmps[index] = mirror((is_accumulating ? mAmps[index] : 0.f) + astp, -1.0f, 1.0f); 
      mDurs[index] = mirror(mDurs[index] + (dstp), 0.5, 1.5);
    }
    else {
      mAmps[index] = wrap((is_accumulating ? mAmps[index] : 0.f) + astp, -1.0f, 1.0f); 
      mDurs[index] = wrap(mDurs[index] + dstp, 0.5, 1.5);
    }
  
    amp_next = mAmps[index];
    
    // step/adjust grain sample offsets 
    g_idx = g_idx_next;
    g_idx_next = 0.0;
  }

  // change amp in sample buffer
  sample[idx] = wrap(sample[idx] + (amp * env.get(g_idx)), -5.f, 5.f);
  amp_out = sample[idx];

  idx = (idx + 1) % sample_length;
  g_idx = fmod(g_idx + (1.f / (4.f * env_dur)), 1.f);
  g_idx_next = fmod(g_idx_next + (1.f / (4.f * env_dur)), 1.f);
  
  phase += 1.f / (mDurs[index] * bpt_spc);

  // get that amp OUT
  outputs[SINE_OUTPUT].value = amp_out;
}
Esempio n. 8
0
File: view.cpp Progetto: nobbk/copo
/// creates a random noise texture
void view::create_noise_texture(int width /* = 512 */, int height /* = 512 */, int seed /* = 1 */ )
{
		Pixel* data = new Pixel[width * height];
		if(data == 0) {
			std::cout << "shit, memory full:(\n";
			exit(1);
		}

		Utilities::Random r(seed);

		for(int i = 0; i < 200; ++i) {
			int x = r.randomFloat() * width;
			int y = r.randomFloat() * height;

			int pos = x + y * width;
			// get random normal
			float theta = r.randomFloat(0.0, PI);
			float phi = r.randomFloat(0.0, PI_2);
			point3f randomNormal(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
			data[pos].r = (unsigned char)255.0f * randomNormal.x();
			data[pos].g = (unsigned char)255.0f * randomNormal.y();
			data[pos].b = (unsigned char)255.0f * randomNormal.z();
			data[pos].a = 255;
		}

//		for(int i = 0; i < width; ++i) {
//			for(int j = 0; j < height; ++j) {
//				int pos = i + j * width;
//				data[pos].r = 255;
//				data[pos].g = 0;
//				data[pos].b = 0;
//			}
//		}

		unsigned int texId;
		glGenTextures(1, &texId);
		glBindTexture(GL_TEXTURE_2D, texId);

		m_noise_tex.level = 0;
		m_noise_tex.internal_format = GL_RGBA8;
		m_noise_tex.width = width;
		m_noise_tex.height = height;
		m_noise_tex.border = 0;
		m_noise_tex.format = GL_RGBA;
		m_noise_tex.type = GL_UNSIGNED_BYTE;
		m_noise_tex.pixels = NULL;
		m_noise_tex.bound = true;
		m_noise_tex.texname = texId;

		glTexImage2D(	GL_TEXTURE_2D, // target
						m_noise_tex.level,
						m_noise_tex.internal_format,
						m_noise_tex.width,
						m_noise_tex.height,
						m_noise_tex.border,
						m_noise_tex.format,
						m_noise_tex.type,
						data);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

		delete[] data;
}