inline void perturb(double scale){ double perturbance_pos = random_sample(simulation_noise_mean.position*scale, simulation_noise_variance.position*scale); double perturbance_spd = random_sample(simulation_noise_mean.speed*scale, simulation_noise_variance.speed*scale); double perturbance_eff = random_sample(simulation_noise_mean.effort*scale, simulation_noise_variance.effort*scale); //LOG_INFO("Perturbing joint %n by pos: %f, spd: %f, eff: %f", mh, // perturbance_pos, perturbance_spd, perturbance_eff); j_state.position += perturbance_pos; j_state.speed += perturbance_spd; j_state.effort += perturbance_eff; }
/// Pick a random sample /// \param sizeSample The size of the sample. /// \param vec_index The possible data indices. /// \param sample The random sample of sizeSample indices (output). static void UniformSample(int sizeSample, const std::vector<int> &vec_index, std::vector<int> *sample) { sample->resize(sizeSample); random_sample(*sample, sizeSample, static_cast<int>(vec_index.size())); for(int i = 0; i < sizeSample; ++i) (*sample)[i] = vec_index[ (*sample)[i] ]; }
void rrt_t::step() { random_sample(); nearest_vertex(); if(propagate()) { add_to_tree(); } }
int main(int argc, char **argv) { util::time::time_utility::update(); tquerystring_sample(); hash_sample(); random_sample(); log_sample(); cmd_option_sample(); return 0; }
double comet_permutation_test(int k, int num_permutation, int num_patients, int *gene_set, int *freq, int *tbl){ // Parameters int i, p2g_index, l; // Helper variables int *ex_cells = get_ex_cells(k); int Tobs = sum_cells(tbl, ex_cells, k); free(ex_cells); int count = 0; int count_mid_p = 0; for (i=0; i<num_permutation; i++){ // initialization int Trand = 0; int permute_mut_data[num_patients]; for(l=0; l<num_patients; l++){ permute_mut_data[l] = 0; } // randomize mutation data in permute_mut_data for(p2g_index=0; p2g_index<k; p2g_index++){ // random sampling from a list with freq samples? int *new_samples = random_sample(num_patients-1, freq[p2g_index]); for (l=0; l<freq[p2g_index]; l++){ permute_mut_data[new_samples[l]]++; } free(new_samples); } for(l=0; l<num_patients; l++){ if (permute_mut_data[l] == 1) Trand++; } if (Tobs <= Trand){ count++; } if (Tobs < Trand){ count_mid_p++; } } return (double)(count+count_mid_p)/2; }
int main(int argc, char** argv) { std::random_device rd; std::mt19937 rng(rd()); std::array<int, 7> x = { 2, 3, 5, 7, 11, 13, 17}; // choose and print a random element from x 5 times for (int i = 0; i < 5; ++i) std::cout << *random_choice(x.begin(), x.end(), rng) << "\n"; // choose a random sample of 3 elements from x and print them 5 times for (int i = 0; i < 5; ++i) { std::array<int, 3> out; random_sample(x.begin(), x.end(), out.begin(), 3, rng); for (auto& e : out) std::cout << e << " "; std::cout << "\n"; } return 0; }
int main(int argc, char** argv) { int ndims = 6; int nsamples = 1000; int niter = 1000; for(int i = 1; i<argc; i++) { if(!strcmp(argv[i],"-i")) { if(i+1 < argc) sscanf(argv[i+1],"%d",&niter); } if(!strcmp(argv[i],"-s")) { if(i+1 < argc) sscanf(argv[i+1],"%d",&nsamples); } } int dims[] = {2,3,4,7,10,100}; char fname[20]; //cubes with warm start for(int i = 0; i<ndims; i++) { sprintf(fname,"cube%d_warm",dims[i]); printf("Working on cube of dimension %d with a warm start\n",dims[i]); point **sample; sample = new point*[nsamples]; for(int j = 0; j<nsamples; j++) { sample[j] = new point(dims[i]); (*sample[j]) = random_sample(*sample[j],cube_separation_oracle,niter); } FILE *fout = fopen(fname,"w"); for(int j = 0; j<nsamples; j++) { fprintf(fout,"%lf",sample[j]->x[0]); for(int k = 1; k<dims[i]; k++) { fprintf(fout," %lf",sample[j]->x[k]); } fprintf(fout,"\n"); } fclose(fout); } //cubes with cold start for(int i = 0; i<ndims; i++) { sprintf(fname,"cube%d_cold",dims[i]); printf("Working on cube of dimension %d with a cold start\n",dims[i]); point **sample; sample = new point*[nsamples]; for(int j = 0; j<nsamples; j++) { sample[j] = new point(dims[i]); for(int k = 0; k<dims[i]; k++) { sample[j]->x[k] = 1-1e-4; } (*sample[j]) = random_sample(*sample[j],cube_separation_oracle,niter); } FILE *fout = fopen(fname,"w"); for(int j = 0; j<nsamples; j++) { fprintf(fout,"%lf",sample[j]->x[0]); for(int k = 1; k<dims[i]; k++) { fprintf(fout," %lf",sample[j]->x[k]); } fprintf(fout,"\n"); } fclose(fout); } //spheres with warm start for(int i = 0; i<ndims; i++) { sprintf(fname,"sphere%d_warm",dims[i]); printf("Working on sphere of dimension %d with a warm start\n",dims[i]); point **sample; sample = new point*[nsamples]; for(int j = 0; j<nsamples; j++) { sample[j] = new point(dims[i]); (*sample[j]) = random_sample(*sample[j],sphere_separation_oracle,niter); } FILE *fout = fopen(fname,"w"); for(int j = 0; j<nsamples; j++) { fprintf(fout,"%lf",sample[j]->x[0]); for(int k = 1; k<dims[i]; k++) { fprintf(fout," %lf",sample[j]->x[k]); } fprintf(fout,"\n"); } fclose(fout); } //spheres with cold start for(int i = 0; i<ndims; i++) { sprintf(fname,"sphere%d_cold",dims[i]); printf("Working on sphere of dimension %d with a cold start\n",dims[i]); point **sample; sample = new point*[nsamples]; for(int j = 0; j<nsamples; j++) { sample[j] = new point(dims[i]); sample[j]->x[0] = 1-1e-4; (*sample[j]) = random_sample(*sample[j],sphere_separation_oracle,niter); } FILE *fout = fopen(fname,"w"); for(int j = 0; j<nsamples; j++) { fprintf(fout,"%lf",sample[j]->x[0]); for(int k = 1; k<dims[i]; k++) { fprintf(fout," %lf",sample[j]->x[k]); } fprintf(fout,"\n"); } fclose(fout); } //simplex with warm start for(int i = 0; i<ndims; i++) { sprintf(fname,"simplex%d_warm",dims[i]); printf("Working on simplex of dimension %d with a warm start\n",dims[i]); point **sample; sample = new point*[nsamples]; for(int j = 0; j<nsamples; j++) { sample[j] = new point(dims[i]); (*sample[j]) = random_sample(*sample[j],simplex_separation_oracle,niter); } FILE *fout = fopen(fname,"w"); for(int j = 0; j<nsamples; j++) { fprintf(fout,"%lf",sample[j]->x[0]); for(int k = 1; k<dims[i]; k++) { fprintf(fout," %lf",sample[j]->x[k]); } fprintf(fout,"\n"); } fclose(fout); } //simplex with cold start for(int i = 0; i<ndims; i++) { sprintf(fname,"simplex%d_cold",dims[i]); printf("Working on simplex of dimension %d with a cold start\n",dims[i]); point **sample; sample = new point*[nsamples]; for(int j = 0; j<nsamples; j++) { sample[j] = new point(dims[i]); sample[j]->x[0] = 1-1e-4; (*sample[j]) = random_sample(*sample[j],simplex_separation_oracle,niter); } FILE *fout = fopen(fname,"w"); for(int j = 0; j<nsamples; j++) { fprintf(fout,"%lf",sample[j]->x[0]); for(int k = 1; k<dims[i]; k++) { fprintf(fout," %lf",sample[j]->x[k]); } fprintf(fout,"\n"); } fclose(fout); } return 0; }
//random version of bestSplittingInDimenstion //x = 0 1 2 3, randomly pick up 4 number in (0, 3) as candidate splitting value static bool bestSplittingInDimenstionWithRandom(const vector< vector< nl_vector > > & inputs, const vector< double > & outputs, const vector< unsigned int > & indices, const unsigned int scaleIndex, const unsigned int dim, const unsigned int minSize, const rf_rgrsn_tree_parameter & para, const ff_tree_cost_function & costFunction, //output double & loss, double & threshold, vector< unsigned int >& leftIndices, vector< unsigned int >& rightIndices) { assert(indices.size() >= minSize); assert(scaleIndex < inputs[0][scaleIndex].size()); //collect data in nDim of inputs vector<double > data_x_nDim; for (int i = 0; i<indices.size(); i++) { int idx = indices[i]; data_x_nDim.push_back(inputs[idx][scaleIndex][dim]); } random_sample(data_x_nDim, std::max((unsigned int)inputs.front().size(), para.max_sample_num_)); loss = INT_MAX; //for each possible threshold bool isDivided = false; for (int i = 0; i < data_x_nDim.size(); i++) { vector<unsigned int> curLeftIndices; vector<unsigned int> curRightIndices; //try every possible threshold double curThreshold = data_x_nDim[i]; // loop current data (in one dimension) for (int j = 0; j<indices.size(); j++) { int idx = indices[j]; if (inputs[idx][scaleIndex][dim] < curThreshold) { curLeftIndices.push_back(idx); } else { curRightIndices.push_back(idx); } } if (curLeftIndices.size() * 2 < minSize || curRightIndices.size() * 2 < minSize) { continue; } double left_err = 0; nl_vector wtDump; if (curLeftIndices.size() > 0) { left_err = costFunction.cost(inputs, outputs, curLeftIndices, scaleIndex, wtDump); assert(left_err >= 0); } double right_err = 0; if (curRightIndices.size() > 0) { right_err = costFunction.cost(inputs, outputs, curRightIndices, scaleIndex, wtDump); assert(right_err >= 0); } if (left_err + right_err < loss) { loss = left_err + right_err; leftIndices = curLeftIndices; rightIndices = curRightIndices; threshold = curThreshold; isDivided = true; } } return isDivided; }
/** Train the unsupervised SOM network. * * The network is initialized with random (non-graduate) values. It is then * trained with the image pixels (RGB). Once the network is done training the * centroids of the resulting clusters is returned. * * @note The length of the clusters depends on the parameter 'n'. The greatest * n, the more colors in the clusters, the less posterized the image. * * @param[out] res The resulting R, G and B clusters centroids. * @param[in] imgPixels The original image pixels. * @param[in] nbPixels The number of pixels of th image (height * width). * @param[in] nbNeurons The posterization leveldefined by its number of * neurons. * @param[in] noEpoch Number of training passes (a too small number of epochs * won't be enough for the network to correctly know the image but a too high * value will force the network to modify the wieghts so much that the image * can actually looks "ugly"). * @param[in] thresh The threshold value. The SOM delta parameter and the * training rate are dicreasing from one epoch to the next. The treshold value * set a stop condifition in case the value has fallen under this minimum * value. * * @return SOM_OK if everything goes right or SOM_NO_MEMORY if a memory * allocation (malloc) fail. */ int som_train(float **res, float **imgPixels, unsigned int nbPixels, int nbNeurons, int noEpoch, float thresh){ int mapWidth = // Map width. This can be calculated from its (int)sqrt(nbNeurons); // number of neurons as the map is square. int mapHeight = // Map height. This can be calculated from its (int)sqrt(nbNeurons); // number of neurons as the map is square. float delta = INT_MAX; // Start with an almost impossible value int it = 0; // Count the network iterations unsigned int pick; // The choosen input pixel float rad; // Neighbooring radius (keep dicreasing) float eta; // Learning rate (keep dicreasing) float pickRGB[3] = {0}; // Contains the choosen input RGB vector size_t choosen; // Best Matching Unit (BMU) int choosen_x; // BMU abscissa int choosen_y; // BMU ordinate float *neigh = // Neighbooring mask malloc(sizeof(float) * nbNeurons); if(neigh == NULL){ return SOM_NO_MEMORY; } memset(neigh, 0, nbNeurons); float *WR = // RED part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(WR == NULL){ return SOM_NO_MEMORY; } memset(WR, 0, nbNeurons); float *WG = // GREEN part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(WG == NULL){ return SOM_NO_MEMORY; } memset(WG, 0, nbNeurons); float *WB = // BLUE part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(WB == NULL){ return SOM_NO_MEMORY; } memset(WB, 0, nbNeurons); float *deltaR = // New RED part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(deltaR == NULL){ return SOM_NO_MEMORY; } memset(deltaR, 0, nbNeurons); float *deltaG = // New GREEN part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(deltaG == NULL){ return SOM_NO_MEMORY; } memset(deltaG, 0, nbNeurons); float *deltaB = // New BLUE part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(deltaB == NULL){ return SOM_NO_MEMORY; } memset(deltaB, 0, nbNeurons); float *absDeltaR = // Absolute value of deltaR malloc(sizeof(float) * nbNeurons); if(absDeltaR == NULL){ return SOM_NO_MEMORY; } memset(absDeltaR, 0, nbNeurons); float *absDeltaG = // Absolute value of deltaG malloc(sizeof(float) * nbNeurons); if(absDeltaG == NULL){ return SOM_NO_MEMORY; } memset(absDeltaG, 0, nbNeurons); float *absDeltaB = // Absolute value of deltaB malloc(sizeof(float) * nbNeurons); if(absDeltaB == NULL){ return SOM_NO_MEMORY; } memset(absDeltaB, 0, nbNeurons); float *dists = // Vectors euclidian distances from input form malloc(sizeof(float) * nbNeurons); if(dists == NULL){ return SOM_NO_MEMORY; } memset(dists, 0, nbNeurons); /* Randomly initialize weight vectors */ srand(time(NULL)); random_sample(WR, nbNeurons); random_sample(WG, nbNeurons); random_sample(WB, nbNeurons); while(it < noEpoch && delta >= thresh){ /* Randomly choose an input form */ pick = random_uint(nbPixels); pickRGB[0] = imgPixels[pick][0]; pickRGB[1] = imgPixels[pick][1]; pickRGB[2] = imgPixels[pick][2]; /* Compute every vectors euclidian distance from the input form */ euclidian(dists, nbNeurons, WR, WG, WB, pickRGB); /* Determine the BMU */ choosen = arr_min_idx(dists, nbNeurons); choosen_x = (int)choosen % mapWidth; choosen_y = choosen / mapHeight; /* Compute the new neighbooring radius */ rad = som_radius(it, noEpoch, mapWidth, mapHeight); /* Find the BMU neighboors */ som_neighbourhood(neigh, choosen_x, choosen_y, rad, mapWidth, mapHeight); /* Compute the new learning rate */ eta = som_learning_rate(it, noEpoch); /* Compute new value of the network weight vectors */ compute_delta(deltaR, eta, neigh, nbNeurons, pickRGB[0], WR); compute_delta(deltaG, eta, neigh, nbNeurons, pickRGB[1], WG); compute_delta(deltaB, eta, neigh, nbNeurons, pickRGB[2], WB); /* Update the network weight vectors values */ arr_add(WR, deltaR, nbNeurons); arr_add(WG, deltaG, nbNeurons); arr_add(WB, deltaB, nbNeurons); arr_abs(absDeltaR, deltaR, nbNeurons); arr_abs(absDeltaG, deltaG, nbNeurons); arr_abs(absDeltaB, deltaB, nbNeurons); delta = arr_sum(absDeltaR, nbNeurons) + arr_sum(absDeltaG, nbNeurons) + arr_sum(absDeltaB, nbNeurons); it++; } res[0] = WR; res[1] = WG; res[2] = WB; free(dists); free(deltaR); free(deltaG); free(deltaB); free(absDeltaR); free(absDeltaG); free(absDeltaB); free(neigh); return SOM_OK; }
static void bic_seq_resample(double *tumor, int n_tumor, double *normal, int n_nml, SRM_binning args) { SEG_PERMUTE segs = NULL; int *tumor_bin, *normal_bin, nbins; int n_tumor_sample, n_normal_sample,i,k, total,start,end, kmin; double tmp, freq, N_tumor, N_normal; struct timeval tv; int seed; gettimeofday(&tv, NULL); seed = tv.tv_sec * 1000000 + tv.tv_usec; seed_set(seed); srand48(seed); segs = SEG_PERMUTE_create(args.B); tmp = tumor[n_tumor-1] > normal[n_nml-1] ? tumor[n_tumor-1]:normal[n_nml-1]; nbins = floor(tmp/args.bin_size)+10; nbins = nbins>10?nbins:10; tumor_bin = (int *) malloc(sizeof(int)*nbins); normal_bin = (int *)malloc(sizeof(int)*nbins); if(tumor_bin==NULL||normal_bin==NULL){ fprintf(stderr,"Error in bic_seq_resample: memory allocation failed\n"); exit(1); } tmp = tumor[0] < normal[0] ? tumor[0]:normal[0]; kmin = (int) floor(tmp/args.bin_size)-1; kmin = (kmin>0? kmin:0); for(i=0;i<segs->size;i++){ n_tumor_sample = rbinom(args.tumor_freq,n_tumor+n_nml); n_normal_sample = rbinom(1-args.tumor_freq,n_tumor+n_nml); random_sample(tumor, n_tumor, normal, n_nml, n_tumor_sample, args.bin_size ,tumor_bin, nbins, args.paired, args.insert, args.sd); random_sample(tumor, n_tumor, normal, n_nml, n_normal_sample, args.bin_size ,normal_bin,nbins, args.paired, args.insert, args.sd); N_tumor=0.0; N_normal = 0.0; for(k=kmin;k<nbins;k++){ start = k*args.bin_size+1; end = start+args.bin_size; total = tumor_bin[k] + normal_bin[k]; freq = ((double) tumor_bin[k])/((double) total); if(total>0) ll_append(segs->bins_perm[i], bin_new(tumor_bin[k], total, freq, start, end)); N_tumor += tumor_bin[k]; N_normal += normal_bin[k]; } set_BinList(segs->bins_perm[i]); set_totalreadcount(N_tumor,N_normal); if(args.autoselect_lambda!=1){ bic_seq(args.paired); //bic_seq(0); }else{ bic_seq_auto(ll_length(segs->bins_perm[i]),args.FP,args.paired); //bic_seq_auto(ll_length(segs->bins_perm[i]),args.FP,0); } segs->bins_perm[i] = get_BinList(); } print_SEG_PERMUTE(segs,args.output); SEG_PERMUTE_destroy(segs); segs = NULL; free(tumor_bin); tumor_bin = NULL; free(normal_bin);normal_bin = NULL; return; }
void train (void) { // extract mini_batch from buffer and backpropagate if (0 == (n_train_called_ % params_.train_interval_)) { if (experiences_.size() < params_.mini_batch_size_) return; std::vector<ExpBatch> samples = random_sample(); // batch data process std::vector<PybindT> states; // <ninput, batchsize> std::vector<PybindT> new_states; // <ninput, batchsize> std::vector<PybindT> action_mask; // <noutput, batchsize> std::vector<PybindT> new_states_mask; // <batchsize> std::vector<PybindT> rewards; // <batchsize> for (size_t i = 0, n = samples.size(); i < n; i++) { ExpBatch batch = samples[i]; states.insert(states.end(), batch.observation_.begin(), batch.observation_.end()); { std::vector<PybindT> local_act_mask( source_qnet_->get_noutput(), 0); local_act_mask[batch.action_idx_] = 1.0; action_mask.insert(action_mask.end(), local_act_mask.begin(), local_act_mask.end()); } rewards.push_back(batch.reward_); if (batch.new_observation_.empty()) { new_states.insert(new_states.end(), source_qnet_->get_ninput(), 0); new_states_mask.push_back(0); } else { new_states.insert(new_states.end(), batch.new_observation_.begin(), batch.new_observation_.end()); new_states_mask.push_back(1); } } // enter processed batch data train_input_->assign(states.data(), train_input_->shape()); output_mask_->assign(action_mask.data(), output_mask_->shape()); next_input_->assign(new_states.data(), next_input_->shape()); next_output_mask_->assign(new_states_mask.data(), next_output_mask_->shape()); reward_->assign(rewards.data(), reward_->shape()); sess_->update({ train_input_->get_tensor().get(), output_mask_->get_tensor().get(), next_input_->get_tensor().get(), next_output_mask_->get_tensor().get(), reward_->get_tensor().get(), }); assign_groups(updates_, [this](std::unordered_set<ade::iTensor*>& updated) { this->sess_->update(updated); }); iteration_++; } n_train_called_++; }