QDate PollenOnsetDateFromCurve::calculate() { typedef boost::uniform_01<double> Distribution; typedef boost::variate_generator<UniSim::Random::Generator&, Distribution> Variate; Distribution distribution; Variate variate(*randomGenerator(), distribution); double fractile = variate(); Q_ASSERT(fractile >= 0. && fractile < 1.); Model *calendar = seekOne<Model*>("calendar"); Model *weather = seekOne<Model*>("weather"); calendar->deepReset(); weather->deepCleanup(); weather->deepReset(); model->deepReset(); const double *curveTotal = model->pullValuePtr<double>("total"); int day = 0; while (*curveTotal < fractile && day < 365) { calendar->update(); weather->update(); model->update(); ++day; } Q_ASSERT(day < 365); calendar->deepReset(); weather->deepCleanup(); weather->deepReset(); model->deepReset(); QDate firstDate = calendar->pullValue<QDate>("initialDate"); return firstDate.addDays(day); }
typename graph_traits<Graph>::edge_descriptor random_out_edge(Graph& g, typename graph_traits<Graph>::vertex_descriptor src, RandomNumGen& gen) { typedef typename graph_traits<Graph>::degree_size_type degree_size_type; typedef boost::uniform_int<degree_size_type> ui_type; ui_type ui(0, out_degree(src, g) - 1); boost::variate_generator<RandomNumGen&, ui_type> variate(gen, ui); typename graph_traits<Graph>::out_edge_iterator it = out_edges(src, g).first; std::advance(it, variate()); return *it; }
void variate(unsigned i) { unsigned j; if (i>=k) { print(); return; } for (j=0; j<n; j++) { mp[i]=j; variate(i+1); } }
void RandomProbabilities::assign_random_value(RandomNumberEngine& rne) { /// @todo Creating the variate every time from scratch is slow. typedef uniform_real<float> Distribution; Distribution distribution(0.0f, 1.0f); variate_generator<RandomNumberEngine&, Distribution> variate(rne, distribution); float sum = 0.0; for (iterator i = pt_.begin(); i != pt_.end(); ++i) { i->second = variate(); sum += i->second; } for (iterator i = pt_.begin(); i != pt_.end(); ++i) i->second /= sum; }
/* рекурсия */ void variate(unsigned i) { unsigned j; /* без if (i>=k) и return; тук (а само print(i); ако искаме всички * генерирания с дължина 1,2, …, k, а не само вариациите с дължина k */ if (i >= k) { print(i); return; } for (j = 0; j < n; j++) { /* if (allowed(k)) { */ taken[i] = j; variate(i + 1); } }
QDate PollenOnsetDateFromFile::calculate() { Model *calendar = seekOne<Model*>("calendar"); QDate date = calendar->pullValue<QDate>("date"); int year = date.year(); if (date.month() == 12) ++year; int ixMin = yearsFirstLine.value(year); int ixMax = (year == lastYear) ? (data.rowNumber() - 1) : (yearsFirstLine.value(year+1) - 1); Q_ASSERT(ixMin < ixMax); typedef boost::random::uniform_int_distribution<> Distribution; typedef boost::variate_generator<UniSim::Random::Generator&, Distribution> Variate; Distribution distribution(ixMin, ixMax); Variate variate(*randomGenerator(), distribution); int index = variate(); Q_ASSERT(index>=0 && index<data.rowNumber()); QDate onset = stringToValue<QDate>(data.row(index).at(1)); Q_ASSERT(onset.year() == year); return onset; }
int main() { variate(0); return 0; }
return *it; } template <typename Graph, typename WeightMap, typename RandomNumGen> typename graph_traits<Graph>::edge_descriptor weighted_random_out_edge(Graph& g, typename graph_traits<Graph>::vertex_descriptor src, WeightMap weight, RandomNumGen& gen) { typedef graph_traits<Graph> gt; typedef typename gt::vertex_descriptor vertex_descriptor; typedef typename property_traits<WeightMap>::value_type weight_type; weight_type weight_sum(0); BGL_FORALL_OUTEDGES_T(src, e, g, Graph) {weight_sum += get(weight, e);} typedef boost::uniform_real<> ur_type; ur_type ur(0, weight_sum); boost::variate_generator<RandomNumGen&, ur_type> variate(gen, ur); weight_type chosen_weight = variate(); BGL_FORALL_OUTEDGES_T(src, e, g, Graph) { weight_type w = get(weight, e); if (chosen_weight < w) { return e; } else { chosen_weight -= w; } } assert (false); // Should not get here } namespace detail { class dummy_property_copier { public: template<class V1, class V2>
int state2() /* Do what needs to be done in state 2. pre: The global variable 'mu' contains the number of indiviuals you need to read using 'read_sel()'. The global variable 'lambda' contains the number of individuals you need to create by variation of the individuals specified the 'sel' file. post: Optionally call read_arc() in order to delete old uncessary individuals from the global population. read_sel() called 'lambda' children generated from the 'mu' parents Children added to the global population using add_individual(). Information about children written to the 'var' file using write_var(). Return value == 0 if successful, == 1 if unspecified errors happened, == 2 if file reading failed. */ { int *parent_identities, *offspring_identities; /* array of identities */ int result; /* stores return values of called functions */ parent_identities = (int *) malloc(mu * sizeof(int)); if (parent_identities == NULL) { log_to_file(log_file, __FILE__, __LINE__, "variator out of memory"); return (1); } offspring_identities = (int *) malloc(lambda * sizeof(int)); if (offspring_identities == NULL) { log_to_file(log_file, __FILE__, __LINE__, "variator out of memory"); return (1); } result = read_sel(parent_identities); if (result != 0) /* if some file reading error occurs, return 2 */ return (2); result = read_arc(); if (result != 0) /* if some file reading error occurs, return 2 */ return (2); /**********| added for DTLZ |**************/ result = variate(parent_identities, offspring_identities); if (result != 0) return (1); gen++; /**********| addition for DTLZ end |*******/ result = write_var(offspring_identities); if (result != 0) { log_to_file(log_file, __FILE__, __LINE__, "couldn't write var"); free(offspring_identities); free(parent_identities); return (1); } free(offspring_identities); free(parent_identities); return (0); }
int main(void) { variate(0); return 0; }