int main( ) { // Use Boost library to make a random number generator. boost::mt19937 randomNumbergenerator( time( 0 ) ); boost::random::uniform_real_distribution< > uniformDistribution( 0.0, 10.0 ); boost::variate_generator< boost::mt19937&, boost::random::uniform_real_distribution < > > generateRandomNumbers( randomNumbergenerator, uniformDistribution ); // Generate random altitude value between 0 and 10 km. const double altitudeKilometers = generateRandomNumbers( ); // Use the Tudat Core library to convert km to m. const double altitudeMeters = tudat::unit_conversions::convertKilometersToMeters( altitudeKilometers ); // Use the Eigen library to create position vectors. Eigen::Vector3d positionOfBodySubjectToAcceleration; positionOfBodySubjectToAcceleration << 1737.1e3 + altitudeMeters, 0.0, 0.0; const Eigen::Vector3d positionOfBodyExertingAcceleration = Eigen::Vector3d::Zero( ); // Use the Tudat library to compute the acceleration vector. const Eigen::Vector3d gravitationalAcceleration = tudat::gravitation::computeGravitationalAcceleration( positionOfBodySubjectToAcceleration, 4.9e12, positionOfBodyExertingAcceleration ); // Print the altitude and norm of the acceleration vector. std::cout << "Hello world!" << std::endl; std::cout << "I am floating " << altitudeKilometers << " km above the Moon's surface." << std::endl; std::cout << "The gravitational acceleration here is " << gravitationalAcceleration.norm() << " m/s^2." << std::endl; return 0; }
void PSOOptimiser::initSwarm() { // we want to start the swarm around initial_parameters //debug << "Initial Swarm: " << endl; for (int i=0; i<m_num_particles; i++) { m_swarm_best.push_back(m_initial_parameters); m_swarm_best_fitness.push_back(0); m_swarm_failures.push_back(0); vector<Parameter> particle = m_initial_parameters; vector<float> velocity = vector<float>(m_num_dimensions, 0); for (int j=0; j<m_num_dimensions; j++) { float min = m_initial_parameters[j].min(); float max = m_initial_parameters[j].max(); particle[j].set(uniformDistribution(min, max)); velocity[j] = normalDistribution(0, (max-min)/8); // initial velocity between +/- range } //debug << i << ": " << Parameter::getAsVector(particle) << endl; //debug << i << ": " << velocity << endl; m_swarm_position.push_back(particle); m_swarm_velocity.push_back(velocity); } m_best = m_initial_parameters; m_best_fitness = 0; }
/** * I'm fully aware that this code looks terrible. */ int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); int selection = 0; QTextStream io(stdin); do { qDebug() << "Select Task:\n1 - Task 1\n2 - Task 2"; selection = io.readLine().toInt(); } while (!(selection == 1 || selection == 2)); QVector<double>& sample = QVector<double>(); QVector<double> sample1; QVector<double> sample2; sample1 << 0.0 << 0.0 << 0.0 << 0.0 << 0.0 << 0.0 << 1.0 << 1.0 << 0.0 << 0.0 << 0.0 << 0.0 << 1.0 << 0.0 << 0.0 << 0.0 << 0.0 << 1.0 << 0.0 << 0.0 << 0.0 << 0.0 << 1.0 << 0.0 << 0.0; sample2 << 0.0 << 1.0 << 1.0 << 1.0 << 0.0 << 0.0 << 1.0 << 0.0 << 1.0 << 0.0 << 0.0 << 1.0 << 0.0 << 1.0 << 0.0 << 0.0 << 1.0 << 0.0 << 1.0 << 0.0 << 0.0 << 1.0 << 1.0 << 1.0 << 0.0; switch (selection) { case 1: sample = sample1; case 2: sample = sample2; default: sample = sample1; } //Correct way of using randoms in C++. Might be a bit overkill for {0; 1} set. std::random_device randomDevice; std::mt19937 randomGenerator(randomDevice()); std::uniform_int_distribution<int> uniformDistribution(0, 1); //initialization QVector<double> results; for (int i = 0; i < 25; i++) { results.push_back((double)uniformDistribution(randomGenerator)); } QVector<double> weights; QVector<double> theta; { QVector<double> C; for (int i = 0; i < 25; i++) for (int j = 0; j < 25; j++) { if (i == j) { C.push_back(0.0); } else { C.push_back((sample[i] - 0.5) * (sample[j] - 0.5)); } } QVector<double> D; for (int i = 0; i < 25; i++) for (int j = 0; j < 25; j++) { if (selection == 2) { if (i == j) { D.push_back(0.0); } else { D.push_back((sample2[i] - 0.5) * (sample2[j] - 0.5)); } } else { D.push_back(0.0); } } for (int i = 0; i < 25; i++) for (int j = 0; j < 25; j++) { weights.push_back(2.0 * (C[25 * i + j] + D[25 * i + j])); } for (int i = 0; i < 25; i++) { theta.push_back(0.0); for (int j = 0; j < 25; j++) { theta[i] += C[25 * i + j] + D[25 * i + j]; } } } QString line; std::system("cls"); qDebug() << "Initialized values"; for (int j = 1; j <= 25; j++) { if (results[j - 1] == 1.0) { line += "*"; } else { line += " "; } if (j % 5 == 0) { qDebug() << line; line.clear(); } } qDebug() << "Press Enter."; io.readLine(); //Learning for (int iteration = 1; iteration <= 25; iteration++) { QVector<double> resultsCopy = results; //Setting new values for (int j = 0; j < 25; j++) { double u = 0.0; for (int k = 0; k < 25; k++) { u += weights[k * 25 + j] * resultsCopy[k]; } u = u - theta[j]; if (u > 0.0) { results[j] = 1.0; } else { if (u < 0.0) { results[j] = 0.0; } } } //Printing effects on the screen std::system("cls"); //Qt equivalent does not work. qDebug() << "Iteration " + QString::number(iteration); for (int j = 1; j <= 25; j++) { if (results[j - 1] == 1.0) { line += "*"; } else { line += " "; } if (j % 5 == 0) { qDebug() << line; line.clear(); } } qDebug() << "Press Enter."; io.readLine(); } return a.exec(); }
Float64 Math::random(Float64 min, Float64 max) { std::uniform_real_distribution<Float64> uniformDistribution(min, max); return uniformDistribution(randomEngine); }
Int32 Math::random(Int32 min, Int32 max) { std::uniform_int_distribution<> uniformDistribution(min, max); return uniformDistribution(randomEngine); }
void CollapsedSampler::sampleZ(){//{{{ int_least32_t i,j,k; // Resize Z and initialize if not big enough. {{{ if((long)Z.size() != Nmap){ Z.assign(Nmap,0); // init Z&C for(i=0;i<Nmap;i++){ //choose random transcript; k = (int_least32_t) (m * uniformDistribution(rng_mt)); Z[i]=k; C[k]++; } }//}}} // TimeStats {{{ #ifdef DoSTATS nZ++; struct timeval start, end; gettimeofday(&start, NULL); #endif // }}} vector<double> phi(m,0); // phi of size M should be enough // because of summing the probabilities for each isoform when reading the data double probNorm,r,sum,const1a,const1b,const2a; int_least32_t readsAlignmentsN; const1a = beta->beta + Nunmap; const1b = m * dir->alpha + Nmap - 1; const2a = beta->alpha + Nmap - 1; // randomize order: ??? for(i=0;i<Nmap;i++){ probNorm=0; C[Z[i]]--; // use counts without the current one readsAlignmentsN = alignments->getReadsI(i+1) - alignments->getReadsI(i); for(j=0, k=alignments->getReadsI(i); j<readsAlignmentsN; j++, k++){ //message("%ld %lf ",(*alignments)[k].getTrId(),(*alignments)[k].getProb()); if(alignments->getTrId(k) == 0){ phi[j] = alignments->getProb(k) * (const1a + C[0]) * (const1b - C[0]); // this comes from division in "false part" }else{ phi[j] = alignments->getProb(k) * (const2a - C[0]) * (dir->alpha + C[ alignments->getTrId(k) ]); /* /(m * dir->alpha + Nmap - 1 - C[0]) ; this term was replaced by *(const1b - C[0]) and moved into "true part" as multiplication */ } probNorm += phi[j]; } r = uniformDistribution(rng_mt); // Apply Normalization constant: r *= probNorm; for(j = 0, sum = 0 ; (sum<r) && (j<readsAlignmentsN); j++){ sum += phi[j]; } if(j==0){ // e.g. if probNorm == 0 // assign to noise. Z[i] = 0; } else { Z[i] = alignments->getTrId(alignments->getReadsI(i) + j -1); } C[ Z[i] ]++; } // TimeStats {{{ #ifdef DoSTATS gettimeofday(&end, NULL); tZ += (end.tv_sec-start.tv_sec)*1000*1000+(end.tv_usec-start.tv_usec); #endif // }}} }//}}}